og386.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  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. smarthcount : longint;
  50. objsmart : boolean;
  51. writer : pobjectwriter;
  52. path : pathstr;
  53. ObjFile : string;
  54. place : tcutplace;
  55. currsec : tsection;
  56. constructor init(smart:boolean);
  57. destructor done;virtual;
  58. { Writing }
  59. procedure NextSmartName;
  60. procedure initwriting(Aplace:tcutplace);virtual;
  61. procedure donewriting;virtual;
  62. procedure setsectionsizes(var s:tsecsize);virtual;
  63. procedure writebytes(var data;len:longint);virtual;
  64. procedure writealloc(len:longint);virtual;
  65. procedure writealign(len:longint);virtual;
  66. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  67. procedure writesymbol(p:pasmsymbol);virtual;
  68. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  69. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  70. nidx,nother,line:longint;reloc:boolean);virtual;
  71. procedure defaultsection(sec:tsection);
  72. end;
  73. var
  74. objectalloc : pobjectalloc;
  75. objectoutput : pobjectoutput;
  76. implementation
  77. uses
  78. strings,comphook,
  79. globtype,globals,verbose,files,
  80. assemble;
  81. {****************************************************************************
  82. tobjectoutput
  83. ****************************************************************************}
  84. constructor tobjectalloc.init;
  85. begin
  86. end;
  87. destructor tobjectalloc.done;
  88. begin
  89. end;
  90. procedure tobjectalloc.setsection(sec:tsection);
  91. begin
  92. currsec:=sec;
  93. end;
  94. procedure tobjectalloc.resetsections;
  95. begin
  96. FillChar(secsize,sizeof(secsize),0);
  97. end;
  98. procedure tobjectalloc.sectionalloc(l:longint);
  99. begin
  100. inc(secsize[currsec],l);
  101. end;
  102. procedure tobjectalloc.sectionalign(l:longint);
  103. begin
  104. if (secsize[currsec] mod l)<>0 then
  105. inc(secsize[currsec],l-(secsize[currsec] mod l));
  106. end;
  107. procedure tobjectalloc.staballoc(p:pchar);
  108. begin
  109. inc(secsize[sec_stab]);
  110. if assigned(p) and (p[0]<>#0) then
  111. inc(secsize[sec_stabstr],strlen(p)+1);
  112. end;
  113. function tobjectalloc.sectionsize:longint;
  114. begin
  115. sectionsize:=secsize[currsec];
  116. end;
  117. {****************************************************************************
  118. tobjectoutput
  119. ****************************************************************************}
  120. constructor tobjectoutput.init(smart:boolean);
  121. var
  122. i : longint;
  123. begin
  124. smarthcount:=0;
  125. objsmart:=smart;
  126. objfile:=current_module^.objfilename^;
  127. { Which path will be used ? }
  128. if objsmart and
  129. (cs_asm_leave in aktglobalswitches) then
  130. begin
  131. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  132. {$I-}
  133. mkdir(path);
  134. {$I+}
  135. i:=ioresult;
  136. path:=FixPath(path,false);
  137. end
  138. else
  139. path:=current_module^.path^;
  140. { init writer }
  141. if objsmart and
  142. not(cs_asm_leave in aktglobalswitches) then
  143. writer:=New(parobjectwriter,Init(current_module^.staticlibfilename^))
  144. else
  145. writer:=New(pobjectwriter,Init);
  146. end;
  147. destructor tobjectoutput.done;
  148. begin
  149. Dispose(writer,done);
  150. end;
  151. procedure tobjectoutput.NextSmartName;
  152. var
  153. s : string;
  154. begin
  155. inc(SmartLinkFilesCnt);
  156. if SmartLinkFilesCnt>999999 then
  157. Message(asmw_f_too_many_asm_files);
  158. if (cs_asm_leave in aktglobalswitches) then
  159. s:=current_module^.asmprefix^
  160. else
  161. s:=current_module^.modulename^;
  162. case place of
  163. cut_begin :
  164. begin
  165. inc(smarthcount);
  166. s:=s+tostr(smarthcount)+'h';
  167. end;
  168. cut_normal :
  169. s:=s+tostr(smarthcount)+'s';
  170. cut_end :
  171. s:=s+tostr(smarthcount)+'t';
  172. end;
  173. ObjFile:=Path+FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext)
  174. end;
  175. procedure tobjectoutput.initwriting(Aplace:tcutplace);
  176. begin
  177. place:=Aplace;
  178. if objsmart 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. Do_halt(211);
  196. end;
  197. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  198. begin
  199. Do_halt(211);
  200. end;
  201. procedure tobjectoutput.writebytes(var data;len:longint);
  202. begin
  203. Do_halt(211);
  204. end;
  205. procedure tobjectoutput.writealloc(len:longint);
  206. begin
  207. Do_halt(211);
  208. end;
  209. procedure tobjectoutput.writealign(len:longint);
  210. begin
  211. Do_halt(211);
  212. end;
  213. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  214. begin
  215. Do_halt(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. Do_halt(211);
  221. end;
  222. end.
  223. {
  224. $Log$
  225. Revision 1.17 2000-02-09 13:22:54 peter
  226. * log truncated
  227. Revision 1.16 2000/01/07 01:14:27 peter
  228. * updated copyright to 2000
  229. Revision 1.15 1999/11/08 10:37:12 peter
  230. * filename fixes for win32 imports for units with multiple needed dll's
  231. Revision 1.14 1999/11/06 14:34:21 peter
  232. * truncated log to 20 revs
  233. Revision 1.13 1999/11/02 15:06:57 peter
  234. * import library fixes for win32
  235. * alignment works again
  236. Revision 1.12 1999/09/07 15:22:20 pierre
  237. * runerror => do_halt
  238. Revision 1.11 1999/08/04 00:23:04 florian
  239. * renamed i386asm and i386base to cpuasm and cpubase
  240. }