og386.pas 9.3 KB

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