og386.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. Contains the base stuff for 386 binary object file writers
  5. * This code was inspired by the NASM sources
  6. The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. unit og386;
  22. interface
  23. uses
  24. {$ifdef Delphi}
  25. dmisc,
  26. {$else Delphi}
  27. dos,
  28. {$endif Delphi}
  29. owbase,owar,
  30. systems,cpubase,aasm;
  31. type
  32. tsecsize = array[tsection] of longint;
  33. relative_type = (relative_false,relative_true,relative_rva);
  34. pobjectalloc = ^tobjectalloc;
  35. tobjectalloc = object
  36. currsec : tsection;
  37. secsize : tsecsize;
  38. constructor init;
  39. destructor done;
  40. procedure setsection(sec:tsection);
  41. function sectionsize:longint;
  42. procedure sectionalloc(l:longint);
  43. procedure sectionalign(l:longint);
  44. procedure staballoc(p:pchar);
  45. procedure resetsections;
  46. end;
  47. pobjectoutput = ^tobjectoutput;
  48. tobjectoutput = object
  49. smarthcount : longint;
  50. objsmart : boolean;
  51. writer : pobjectwriter;
  52. path : pathstr;
  53. ObjFile : string;
  54. place : tcutplace;
  55. currsec : tsection;
  56. constructor init(smart:boolean);
  57. destructor done;virtual;
  58. { Writing }
  59. procedure NextSmartName;
  60. procedure initwriting(Aplace:tcutplace);virtual;
  61. procedure donewriting;virtual;
  62. procedure setsectionsizes(var s:tsecsize);virtual;
  63. procedure writebytes(var data;len:longint);virtual;
  64. procedure writealloc(len:longint);virtual;
  65. procedure writealign(len:longint);virtual;
  66. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  67. procedure writesymbol(p:pasmsymbol);virtual;
  68. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  69. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  70. nidx,nother,line:longint;reloc:boolean);virtual;
  71. procedure defaultsection(sec:tsection);
  72. end;
  73. var
  74. objectalloc : pobjectalloc;
  75. objectoutput : pobjectoutput;
  76. implementation
  77. uses
  78. strings,comphook,
  79. globtype,globals,verbose,files,
  80. assemble;
  81. {****************************************************************************
  82. tobjectoutput
  83. ****************************************************************************}
  84. constructor tobjectalloc.init;
  85. begin
  86. end;
  87. destructor tobjectalloc.done;
  88. begin
  89. end;
  90. procedure tobjectalloc.setsection(sec:tsection);
  91. begin
  92. currsec:=sec;
  93. end;
  94. procedure tobjectalloc.resetsections;
  95. begin
  96. FillChar(secsize,sizeof(secsize),0);
  97. end;
  98. procedure tobjectalloc.sectionalloc(l:longint);
  99. begin
  100. inc(secsize[currsec],l);
  101. end;
  102. procedure tobjectalloc.sectionalign(l:longint);
  103. begin
  104. if (secsize[currsec] mod l)<>0 then
  105. inc(secsize[currsec],l-(secsize[currsec] mod l));
  106. end;
  107. procedure tobjectalloc.staballoc(p:pchar);
  108. begin
  109. inc(secsize[sec_stab]);
  110. if assigned(p) and (p[0]<>#0) then
  111. inc(secsize[sec_stabstr],strlen(p)+1);
  112. end;
  113. function tobjectalloc.sectionsize:longint;
  114. begin
  115. sectionsize:=secsize[currsec];
  116. end;
  117. {****************************************************************************
  118. tobjectoutput
  119. ****************************************************************************}
  120. constructor tobjectoutput.init(smart:boolean);
  121. begin
  122. smarthcount:=0;
  123. objsmart:=smart;
  124. objfile:=current_module^.objfilename^;
  125. { Which path will be used ? }
  126. if objsmart and
  127. (cs_asm_leave in aktglobalswitches) then
  128. begin
  129. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  130. {$I-}
  131. mkdir(path);
  132. {$I+}
  133. if ioresult<>0 then;
  134. path:=FixPath(path,false);
  135. end
  136. else
  137. path:=current_module^.path^;
  138. { init writer }
  139. if objsmart and
  140. not(cs_asm_leave in aktglobalswitches) then
  141. writer:=New(parobjectwriter,Init(current_module^.staticlibfilename^))
  142. else
  143. writer:=New(pobjectwriter,Init);
  144. end;
  145. destructor tobjectoutput.done;
  146. begin
  147. Dispose(writer,done);
  148. end;
  149. procedure tobjectoutput.NextSmartName;
  150. var
  151. s : string;
  152. begin
  153. inc(SmartLinkFilesCnt);
  154. if SmartLinkFilesCnt>999999 then
  155. Message(asmw_f_too_many_asm_files);
  156. if (cs_asm_leave in aktglobalswitches) then
  157. s:=current_module^.asmprefix^
  158. else
  159. s:=current_module^.modulename^;
  160. case place of
  161. cut_begin :
  162. begin
  163. inc(smarthcount);
  164. s:=s+tostr(smarthcount)+'h';
  165. end;
  166. cut_normal :
  167. s:=s+tostr(smarthcount)+'s';
  168. cut_end :
  169. s:=s+tostr(smarthcount)+'t';
  170. end;
  171. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext)
  172. end;
  173. procedure tobjectoutput.initwriting(Aplace:tcutplace);
  174. begin
  175. place:=Aplace;
  176. if objsmart then
  177. NextSmartName;
  178. writer^.create(objfile);
  179. end;
  180. procedure tobjectoutput.donewriting;
  181. begin
  182. writer^.close;
  183. end;
  184. procedure tobjectoutput.setsectionsizes(var s:tsecsize);
  185. begin
  186. end;
  187. procedure tobjectoutput.defaultsection(sec:tsection);
  188. begin
  189. currsec:=sec;
  190. end;
  191. procedure tobjectoutput.writesymbol(p:pasmsymbol);
  192. begin
  193. Do_halt(211);
  194. end;
  195. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  196. begin
  197. Do_halt(211);
  198. end;
  199. procedure tobjectoutput.writebytes(var data;len:longint);
  200. begin
  201. Do_halt(211);
  202. end;
  203. procedure tobjectoutput.writealloc(len:longint);
  204. begin
  205. Do_halt(211);
  206. end;
  207. procedure tobjectoutput.writealign(len:longint);
  208. begin
  209. Do_halt(211);
  210. end;
  211. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  212. begin
  213. Do_halt(211);
  214. end;
  215. procedure tobjectoutput.writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  216. nidx,nother,line:longint;reloc:boolean);
  217. begin
  218. Do_halt(211);
  219. end;
  220. end.
  221. {
  222. $Log$
  223. Revision 1.2 2000-07-13 11:32:43 michael
  224. + removed logs
  225. }