og386.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. {$endif Delphi}
  27. dos,
  28. owbase,owar,
  29. systems,i386base,aasm;
  30. type
  31. tsecsize = array[tsection] of longint;
  32. relative_type = (relative_false,relative_true,relative_rva);
  33. pobjectalloc = ^tobjectalloc;
  34. tobjectalloc = object
  35. currsec : tsection;
  36. secsize : tsecsize;
  37. constructor init;
  38. destructor done;
  39. procedure setsection(sec:tsection);
  40. function sectionsize:longint;
  41. procedure sectionalloc(l:longint);
  42. procedure sectionalign(l:longint);
  43. procedure staballoc(p:pchar);
  44. procedure resetsections;
  45. end;
  46. pobjectoutput = ^tobjectoutput;
  47. tobjectoutput = object
  48. writer : pobjectwriter;
  49. path : pathstr;
  50. ObjFile : string;
  51. IsEndFile : boolean; { special 'end' file for import dir ? }
  52. currsec : tsection;
  53. constructor init;
  54. destructor done;virtual;
  55. { Writing }
  56. procedure NextSmartName;
  57. procedure initwriting;virtual;
  58. procedure donewriting;virtual;
  59. procedure setsectionsizes(var s:tsecsize);virtual;
  60. procedure writebytes(var data;len:longint);virtual;
  61. procedure writealloc(len:longint);virtual;
  62. procedure writealign(len:longint);virtual;
  63. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  64. procedure writesymbol(p:pasmsymbol);virtual;
  65. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  66. procedure defaultsection(sec:tsection);
  67. end;
  68. var
  69. objectalloc : pobjectalloc;
  70. objectoutput : pobjectoutput;
  71. implementation
  72. uses
  73. strings,
  74. globtype,globals,verbose,files,
  75. assemble;
  76. {****************************************************************************
  77. tobjectoutput
  78. ****************************************************************************}
  79. constructor tobjectalloc.init;
  80. begin
  81. end;
  82. destructor tobjectalloc.done;
  83. begin
  84. end;
  85. procedure tobjectalloc.setsection(sec:tsection);
  86. begin
  87. currsec:=sec;
  88. end;
  89. procedure tobjectalloc.resetsections;
  90. begin
  91. FillChar(secsize,sizeof(secsize),0);
  92. end;
  93. procedure tobjectalloc.sectionalloc(l:longint);
  94. begin
  95. inc(secsize[currsec],l);
  96. end;
  97. procedure tobjectalloc.sectionalign(l:longint);
  98. begin
  99. if (secsize[currsec] mod l)<>0 then
  100. inc(secsize[currsec],l-(secsize[currsec] mod l));
  101. end;
  102. procedure tobjectalloc.staballoc(p:pchar);
  103. begin
  104. inc(secsize[sec_stab]);
  105. if assigned(p) and (p[0]<>#0) then
  106. inc(secsize[sec_stabstr],strlen(p)+1);
  107. end;
  108. function tobjectalloc.sectionsize:longint;
  109. begin
  110. sectionsize:=secsize[currsec];
  111. end;
  112. {****************************************************************************
  113. tobjectoutput
  114. ****************************************************************************}
  115. constructor tobjectoutput.init;
  116. var
  117. i : longint;
  118. begin
  119. objfile:=current_module^.objfilename^;
  120. { Which path will be used ? }
  121. if (cs_smartlink in aktmoduleswitches) and
  122. (cs_asm_leave in aktglobalswitches) then
  123. begin
  124. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  125. {$I-}
  126. mkdir(path);
  127. {$I+}
  128. i:=ioresult;
  129. path:=FixPath(path,false);
  130. end
  131. else
  132. path:=current_module^.path^;
  133. { init writer }
  134. if (cs_smartlink in aktmoduleswitches) and
  135. not(cs_asm_leave in aktglobalswitches) then
  136. writer:=New(parobjectwriter,Init(current_module^.staticlibfilename^))
  137. else
  138. writer:=New(pobjectwriter,Init);
  139. end;
  140. destructor tobjectoutput.done;
  141. begin
  142. Dispose(writer,done);
  143. end;
  144. procedure tobjectoutput.NextSmartName;
  145. var
  146. s : string;
  147. begin
  148. inc(SmartLinkFilesCnt);
  149. if SmartLinkFilesCnt>999999 then
  150. Message(asmw_f_too_many_asm_files);
  151. if (cs_asm_leave in aktglobalswitches) then
  152. begin
  153. if IsEndFile then
  154. begin
  155. s:=current_module^.asmprefix^+'e';
  156. IsEndFile:=false;
  157. end
  158. else
  159. s:=current_module^.asmprefix^;
  160. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext)
  161. end
  162. else
  163. begin
  164. if IsEndFile then
  165. begin
  166. s:=current_module^.modulename^+'_e';
  167. IsEndFile:=false;
  168. end
  169. else
  170. s:=current_module^.modulename^+'_';
  171. ObjFile:=FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext);
  172. end;
  173. end;
  174. procedure tobjectoutput.initwriting;
  175. begin
  176. if (cs_smartlink in aktmoduleswitches) 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. RunError(211);
  194. end;
  195. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  196. begin
  197. RunError(211);
  198. end;
  199. procedure tobjectoutput.writebytes(var data;len:longint);
  200. begin
  201. RunError(211);
  202. end;
  203. procedure tobjectoutput.writealloc(len:longint);
  204. begin
  205. RunError(211);
  206. end;
  207. procedure tobjectoutput.writealign(len:longint);
  208. begin
  209. RunError(211);
  210. end;
  211. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  212. begin
  213. RunError(211);
  214. end;
  215. end.
  216. {
  217. $Log$
  218. Revision 1.6 1999-05-07 00:36:56 pierre
  219. * added alignment code for .bss
  220. * stabs correct but externalbss disabled
  221. would need a special treatment in writestabs
  222. Revision 1.5 1999/05/05 22:21:57 peter
  223. * updated messages
  224. Revision 1.4 1999/05/05 17:34:30 peter
  225. * output is more like as 2.9.1
  226. * stabs really working for go32v2
  227. Revision 1.3 1999/05/04 21:44:50 florian
  228. * changes to compile it with Delphi 4.0
  229. Revision 1.2 1999/05/02 22:41:54 peter
  230. * moved section names to systems
  231. * fixed nasm,intel writer
  232. Revision 1.1 1999/05/01 13:24:23 peter
  233. * merged nasm compiler
  234. * old asm moved to oldasm/
  235. Revision 1.8 1999/03/18 20:30:48 peter
  236. + .a writer
  237. Revision 1.7 1999/03/10 13:41:09 pierre
  238. + partial implementation for win32 !
  239. winhello works but pp still does not !
  240. Revision 1.6 1999/03/08 14:51:08 peter
  241. + smartlinking for ag386bin
  242. Revision 1.5 1999/03/05 13:09:51 peter
  243. * first things for tai_cut support for ag386bin
  244. Revision 1.4 1999/03/03 01:36:45 pierre
  245. + stabs output working (though not really tested)
  246. for a simple file the only difference to GAS output is due
  247. to the VMA of the different sections
  248. Revision 1.3 1999/03/02 02:56:26 peter
  249. + stabs support for binary writers
  250. * more fixes and missing updates from the previous commit :(
  251. Revision 1.2 1999/02/25 21:03:09 peter
  252. * ag386bin updates
  253. + coff writer
  254. Revision 1.1 1999/02/16 17:59:39 peter
  255. + initial files
  256. }