og386.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. {
  2. $Id$
  3. Copyright (c) 1999 by Florian Klaempfl
  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. objsmart : boolean;
  50. writer : pobjectwriter;
  51. path : pathstr;
  52. ObjFile : string;
  53. place : tcutplace;
  54. currsec : tsection;
  55. constructor init(smart:boolean);
  56. destructor done;virtual;
  57. { Writing }
  58. procedure NextSmartName;
  59. procedure initwriting(Aplace:tcutplace);virtual;
  60. procedure donewriting;virtual;
  61. procedure setsectionsizes(var s:tsecsize);virtual;
  62. procedure writebytes(var data;len:longint);virtual;
  63. procedure writealloc(len:longint);virtual;
  64. procedure writealign(len:longint);virtual;
  65. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  66. procedure writesymbol(p:pasmsymbol);virtual;
  67. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  68. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  69. nidx,nother,line:longint;reloc:boolean);virtual;
  70. procedure defaultsection(sec:tsection);
  71. end;
  72. var
  73. objectalloc : pobjectalloc;
  74. objectoutput : pobjectoutput;
  75. implementation
  76. uses
  77. strings,comphook,
  78. globtype,globals,verbose,files,
  79. assemble;
  80. {****************************************************************************
  81. tobjectoutput
  82. ****************************************************************************}
  83. constructor tobjectalloc.init;
  84. begin
  85. end;
  86. destructor tobjectalloc.done;
  87. begin
  88. end;
  89. procedure tobjectalloc.setsection(sec:tsection);
  90. begin
  91. currsec:=sec;
  92. end;
  93. procedure tobjectalloc.resetsections;
  94. begin
  95. FillChar(secsize,sizeof(secsize),0);
  96. end;
  97. procedure tobjectalloc.sectionalloc(l:longint);
  98. begin
  99. inc(secsize[currsec],l);
  100. end;
  101. procedure tobjectalloc.sectionalign(l:longint);
  102. begin
  103. if (secsize[currsec] mod l)<>0 then
  104. inc(secsize[currsec],l-(secsize[currsec] mod l));
  105. end;
  106. procedure tobjectalloc.staballoc(p:pchar);
  107. begin
  108. inc(secsize[sec_stab]);
  109. if assigned(p) and (p[0]<>#0) then
  110. inc(secsize[sec_stabstr],strlen(p)+1);
  111. end;
  112. function tobjectalloc.sectionsize:longint;
  113. begin
  114. sectionsize:=secsize[currsec];
  115. end;
  116. {****************************************************************************
  117. tobjectoutput
  118. ****************************************************************************}
  119. constructor tobjectoutput.init(smart:boolean);
  120. var
  121. i : longint;
  122. begin
  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. i:=ioresult;
  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. s:=s+'h';
  163. cut_normal :
  164. s:=s+'s';
  165. cut_end :
  166. s:=s+'t';
  167. end;
  168. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext)
  169. end;
  170. procedure tobjectoutput.initwriting(Aplace:tcutplace);
  171. begin
  172. place:=Aplace;
  173. if objsmart then
  174. NextSmartName;
  175. writer^.create(objfile);
  176. end;
  177. procedure tobjectoutput.donewriting;
  178. begin
  179. writer^.close;
  180. end;
  181. procedure tobjectoutput.setsectionsizes(var s:tsecsize);
  182. begin
  183. end;
  184. procedure tobjectoutput.defaultsection(sec:tsection);
  185. begin
  186. currsec:=sec;
  187. end;
  188. procedure tobjectoutput.writesymbol(p:pasmsymbol);
  189. begin
  190. Do_halt(211);
  191. end;
  192. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  193. begin
  194. Do_halt(211);
  195. end;
  196. procedure tobjectoutput.writebytes(var data;len:longint);
  197. begin
  198. Do_halt(211);
  199. end;
  200. procedure tobjectoutput.writealloc(len:longint);
  201. begin
  202. Do_halt(211);
  203. end;
  204. procedure tobjectoutput.writealign(len:longint);
  205. begin
  206. Do_halt(211);
  207. end;
  208. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  209. begin
  210. Do_halt(211);
  211. end;
  212. procedure tobjectoutput.writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  213. nidx,nother,line:longint;reloc:boolean);
  214. begin
  215. Do_halt(211);
  216. end;
  217. end.
  218. {
  219. $Log$
  220. Revision 1.14 1999-11-06 14:34:21 peter
  221. * truncated log to 20 revs
  222. Revision 1.13 1999/11/02 15:06:57 peter
  223. * import library fixes for win32
  224. * alignment works again
  225. Revision 1.12 1999/09/07 15:22:20 pierre
  226. * runerror => do_halt
  227. Revision 1.11 1999/08/04 00:23:04 florian
  228. * renamed i386asm and i386base to cpuasm and cpubase
  229. Revision 1.10 1999/07/18 10:19:56 florian
  230. * made it compilable with Dlephi 4 again
  231. + fixed problem with large stack allocations on win32
  232. Revision 1.9 1999/07/03 00:27:03 peter
  233. * better smartlinking support
  234. Revision 1.8 1999/05/19 12:41:48 florian
  235. * made source compilable with TP (too long line)
  236. * default values for set properties fixed
  237. Revision 1.7 1999/05/19 11:54:18 pierre
  238. + experimental code for externalbss and stabs problem
  239. Revision 1.6 1999/05/07 00:36:56 pierre
  240. * added alignment code for .bss
  241. * stabs correct but externalbss disabled
  242. would need a special treatment in writestabs
  243. Revision 1.5 1999/05/05 22:21:57 peter
  244. * updated messages
  245. Revision 1.4 1999/05/05 17:34:30 peter
  246. * output is more like as 2.9.1
  247. * stabs really working for go32v2
  248. Revision 1.3 1999/05/04 21:44:50 florian
  249. * changes to compile it with Delphi 4.0
  250. Revision 1.2 1999/05/02 22:41:54 peter
  251. * moved section names to systems
  252. * fixed nasm,intel writer
  253. Revision 1.1 1999/05/01 13:24:23 peter
  254. * merged nasm compiler
  255. * old asm moved to oldasm/
  256. Revision 1.8 1999/03/18 20:30:48 peter
  257. + .a writer
  258. Revision 1.7 1999/03/10 13:41:09 pierre
  259. + partial implementation for win32 !
  260. winhello works but pp still does not !
  261. Revision 1.6 1999/03/08 14:51:08 peter
  262. + smartlinking for ag386bin
  263. Revision 1.5 1999/03/05 13:09:51 peter
  264. * first things for tai_cut support for ag386bin
  265. Revision 1.4 1999/03/03 01:36:45 pierre
  266. + stabs output working (though not really tested)
  267. for a simple file the only difference to GAS output is due
  268. to the VMA of the different sections
  269. Revision 1.3 1999/03/02 02:56:26 peter
  270. + stabs support for binary writers
  271. * more fixes and missing updates from the previous commit :(
  272. Revision 1.2 1999/02/25 21:03:09 peter
  273. * ag386bin updates
  274. + coff writer
  275. }