og386.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. IsEndFile : boolean; { special 'end' file for import dir ? }
  54. currsec : tsection;
  55. constructor init(smart:boolean);
  56. destructor done;virtual;
  57. { Writing }
  58. procedure NextSmartName;
  59. procedure initwriting;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,
  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. begin
  158. if IsEndFile then
  159. begin
  160. s:=current_module^.asmprefix^+'e';
  161. IsEndFile:=false;
  162. end
  163. else
  164. s:=current_module^.asmprefix^;
  165. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext)
  166. end
  167. else
  168. begin
  169. if IsEndFile then
  170. begin
  171. s:=current_module^.modulename^+'_e';
  172. IsEndFile:=false;
  173. end
  174. else
  175. s:=current_module^.modulename^+'_';
  176. ObjFile:=FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext);
  177. end;
  178. end;
  179. procedure tobjectoutput.initwriting;
  180. begin
  181. if objsmart then
  182. NextSmartName;
  183. writer^.create(objfile);
  184. end;
  185. procedure tobjectoutput.donewriting;
  186. begin
  187. writer^.close;
  188. end;
  189. procedure tobjectoutput.setsectionsizes(var s:tsecsize);
  190. begin
  191. end;
  192. procedure tobjectoutput.defaultsection(sec:tsection);
  193. begin
  194. currsec:=sec;
  195. end;
  196. procedure tobjectoutput.writesymbol(p:pasmsymbol);
  197. begin
  198. RunError(211);
  199. end;
  200. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  201. begin
  202. RunError(211);
  203. end;
  204. procedure tobjectoutput.writebytes(var data;len:longint);
  205. begin
  206. RunError(211);
  207. end;
  208. procedure tobjectoutput.writealloc(len:longint);
  209. begin
  210. RunError(211);
  211. end;
  212. procedure tobjectoutput.writealign(len:longint);
  213. begin
  214. RunError(211);
  215. end;
  216. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  217. begin
  218. RunError(211);
  219. end;
  220. procedure tobjectoutput.writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  221. nidx,nother,line:longint;reloc:boolean);
  222. begin
  223. RunError(211);
  224. end;
  225. end.
  226. {
  227. $Log$
  228. Revision 1.11 1999-08-04 00:23:04 florian
  229. * renamed i386asm and i386base to cpuasm and cpubase
  230. Revision 1.10 1999/07/18 10:19:56 florian
  231. * made it compilable with Dlephi 4 again
  232. + fixed problem with large stack allocations on win32
  233. Revision 1.9 1999/07/03 00:27:03 peter
  234. * better smartlinking support
  235. Revision 1.8 1999/05/19 12:41:48 florian
  236. * made source compilable with TP (too long line)
  237. * default values for set properties fixed
  238. Revision 1.7 1999/05/19 11:54:18 pierre
  239. + experimental code for externalbss and stabs problem
  240. Revision 1.6 1999/05/07 00:36:56 pierre
  241. * added alignment code for .bss
  242. * stabs correct but externalbss disabled
  243. would need a special treatment in writestabs
  244. Revision 1.5 1999/05/05 22:21:57 peter
  245. * updated messages
  246. Revision 1.4 1999/05/05 17:34:30 peter
  247. * output is more like as 2.9.1
  248. * stabs really working for go32v2
  249. Revision 1.3 1999/05/04 21:44:50 florian
  250. * changes to compile it with Delphi 4.0
  251. Revision 1.2 1999/05/02 22:41:54 peter
  252. * moved section names to systems
  253. * fixed nasm,intel writer
  254. Revision 1.1 1999/05/01 13:24:23 peter
  255. * merged nasm compiler
  256. * old asm moved to oldasm/
  257. Revision 1.8 1999/03/18 20:30:48 peter
  258. + .a writer
  259. Revision 1.7 1999/03/10 13:41:09 pierre
  260. + partial implementation for win32 !
  261. winhello works but pp still does not !
  262. Revision 1.6 1999/03/08 14:51:08 peter
  263. + smartlinking for ag386bin
  264. Revision 1.5 1999/03/05 13:09:51 peter
  265. * first things for tai_cut support for ag386bin
  266. Revision 1.4 1999/03/03 01:36:45 pierre
  267. + stabs output working (though not really tested)
  268. for a simple file the only difference to GAS output is due
  269. to the VMA of the different sections
  270. Revision 1.3 1999/03/02 02:56:26 peter
  271. + stabs support for binary writers
  272. * more fixes and missing updates from the previous commit :(
  273. Revision 1.2 1999/02/25 21:03:09 peter
  274. * ag386bin updates
  275. + coff writer
  276. Revision 1.1 1999/02/16 17:59:39 peter
  277. + initial files
  278. }