og386.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. var
  122. i : longint;
  123. begin
  124. smarthcount:=0;
  125. objsmart:=smart;
  126. objfile:=current_module^.objfilename^;
  127. { Which path will be used ? }
  128. if objsmart and
  129. (cs_asm_leave in aktglobalswitches) then
  130. begin
  131. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  132. {$I-}
  133. mkdir(path);
  134. {$I+}
  135. i:=ioresult;
  136. path:=FixPath(path,false);
  137. end
  138. else
  139. path:=current_module^.path^;
  140. { init writer }
  141. if objsmart and
  142. not(cs_asm_leave in aktglobalswitches) then
  143. writer:=New(parobjectwriter,Init(current_module^.staticlibfilename^))
  144. else
  145. writer:=New(pobjectwriter,Init);
  146. end;
  147. destructor tobjectoutput.done;
  148. begin
  149. Dispose(writer,done);
  150. end;
  151. procedure tobjectoutput.NextSmartName;
  152. var
  153. s : string;
  154. begin
  155. inc(SmartLinkFilesCnt);
  156. if SmartLinkFilesCnt>999999 then
  157. Message(asmw_f_too_many_asm_files);
  158. if (cs_asm_leave in aktglobalswitches) then
  159. s:=current_module^.asmprefix^
  160. else
  161. s:=current_module^.modulename^;
  162. case place of
  163. cut_begin :
  164. begin
  165. inc(smarthcount);
  166. s:=s+tostr(smarthcount)+'h';
  167. end;
  168. cut_normal :
  169. s:=s+tostr(smarthcount)+'s';
  170. cut_end :
  171. s:=s+tostr(smarthcount)+'t';
  172. end;
  173. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext)
  174. end;
  175. procedure tobjectoutput.initwriting(Aplace:tcutplace);
  176. begin
  177. place:=Aplace;
  178. if objsmart then
  179. NextSmartName;
  180. writer^.create(objfile);
  181. end;
  182. procedure tobjectoutput.donewriting;
  183. begin
  184. writer^.close;
  185. end;
  186. procedure tobjectoutput.setsectionsizes(var s:tsecsize);
  187. begin
  188. end;
  189. procedure tobjectoutput.defaultsection(sec:tsection);
  190. begin
  191. currsec:=sec;
  192. end;
  193. procedure tobjectoutput.writesymbol(p:pasmsymbol);
  194. begin
  195. Do_halt(211);
  196. end;
  197. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  198. begin
  199. Do_halt(211);
  200. end;
  201. procedure tobjectoutput.writebytes(var data;len:longint);
  202. begin
  203. Do_halt(211);
  204. end;
  205. procedure tobjectoutput.writealloc(len:longint);
  206. begin
  207. Do_halt(211);
  208. end;
  209. procedure tobjectoutput.writealign(len:longint);
  210. begin
  211. Do_halt(211);
  212. end;
  213. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  214. begin
  215. Do_halt(211);
  216. end;
  217. procedure tobjectoutput.writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  218. nidx,nother,line:longint;reloc:boolean);
  219. begin
  220. Do_halt(211);
  221. end;
  222. end.
  223. {
  224. $Log$
  225. Revision 1.16 2000-01-07 01:14:27 peter
  226. * updated copyright to 2000
  227. Revision 1.15 1999/11/08 10:37:12 peter
  228. * filename fixes for win32 imports for units with multiple needed dll's
  229. Revision 1.14 1999/11/06 14:34:21 peter
  230. * truncated log to 20 revs
  231. Revision 1.13 1999/11/02 15:06:57 peter
  232. * import library fixes for win32
  233. * alignment works again
  234. Revision 1.12 1999/09/07 15:22:20 pierre
  235. * runerror => do_halt
  236. Revision 1.11 1999/08/04 00:23:04 florian
  237. * renamed i386asm and i386base to cpuasm and cpubase
  238. Revision 1.10 1999/07/18 10:19:56 florian
  239. * made it compilable with Dlephi 4 again
  240. + fixed problem with large stack allocations on win32
  241. Revision 1.9 1999/07/03 00:27:03 peter
  242. * better smartlinking support
  243. Revision 1.8 1999/05/19 12:41:48 florian
  244. * made source compilable with TP (too long line)
  245. * default values for set properties fixed
  246. Revision 1.7 1999/05/19 11:54:18 pierre
  247. + experimental code for externalbss and stabs problem
  248. Revision 1.6 1999/05/07 00:36:56 pierre
  249. * added alignment code for .bss
  250. * stabs correct but externalbss disabled
  251. would need a special treatment in writestabs
  252. Revision 1.5 1999/05/05 22:21:57 peter
  253. * updated messages
  254. Revision 1.4 1999/05/05 17:34:30 peter
  255. * output is more like as 2.9.1
  256. * stabs really working for go32v2
  257. Revision 1.3 1999/05/04 21:44:50 florian
  258. * changes to compile it with Delphi 4.0
  259. Revision 1.2 1999/05/02 22:41:54 peter
  260. * moved section names to systems
  261. * fixed nasm,intel writer
  262. Revision 1.1 1999/05/01 13:24:23 peter
  263. * merged nasm compiler
  264. * old asm moved to oldasm/
  265. Revision 1.8 1999/03/18 20:30:48 peter
  266. + .a writer
  267. Revision 1.7 1999/03/10 13:41:09 pierre
  268. + partial implementation for win32 !
  269. winhello works but pp still does not !
  270. Revision 1.6 1999/03/08 14:51:08 peter
  271. + smartlinking for ag386bin
  272. Revision 1.5 1999/03/05 13:09:51 peter
  273. * first things for tai_cut support for ag386bin
  274. Revision 1.4 1999/03/03 01:36:45 pierre
  275. + stabs output working (though not really tested)
  276. for a simple file the only difference to GAS output is due
  277. to the VMA of the different sections
  278. Revision 1.3 1999/03/02 02:56:26 peter
  279. + stabs support for binary writers
  280. * more fixes and missing updates from the previous commit :(
  281. Revision 1.2 1999/02/25 21:03:09 peter
  282. * ag386bin updates
  283. + coff writer
  284. }