og386.pas 9.2 KB

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