og386.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. function sectionsize(s:tsection):longint;virtual;
  63. procedure setsectionsizes(var s:tsecsize);virtual;
  64. procedure writebytes(var data;len:longint);virtual;
  65. procedure writealloc(len:longint);virtual;
  66. procedure writealign(len:longint);virtual;
  67. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  68. procedure writesymbol(p:pasmsymbol);virtual;
  69. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  70. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  71. nidx,nother,line:longint;reloc:boolean);virtual;
  72. procedure defaultsection(sec:tsection);
  73. end;
  74. var
  75. objectalloc : pobjectalloc;
  76. objectoutput : pobjectoutput;
  77. implementation
  78. uses
  79. strings,comphook,
  80. cutils,globtype,globals,verbose,fmodule,
  81. assemble;
  82. {****************************************************************************
  83. tobjectoutput
  84. ****************************************************************************}
  85. constructor tobjectalloc.init;
  86. begin
  87. end;
  88. destructor tobjectalloc.done;
  89. begin
  90. end;
  91. procedure tobjectalloc.setsection(sec:tsection);
  92. begin
  93. currsec:=sec;
  94. end;
  95. procedure tobjectalloc.resetsections;
  96. begin
  97. FillChar(secsize,sizeof(secsize),0);
  98. end;
  99. procedure tobjectalloc.sectionalloc(l:longint);
  100. begin
  101. inc(secsize[currsec],l);
  102. end;
  103. procedure tobjectalloc.sectionalign(l:longint);
  104. begin
  105. if (secsize[currsec] mod l)<>0 then
  106. inc(secsize[currsec],l-(secsize[currsec] mod l));
  107. end;
  108. procedure tobjectalloc.staballoc(p:pchar);
  109. begin
  110. inc(secsize[sec_stab]);
  111. if assigned(p) and (p[0]<>#0) then
  112. inc(secsize[sec_stabstr],strlen(p)+1);
  113. end;
  114. function tobjectalloc.sectionsize:longint;
  115. begin
  116. sectionsize:=secsize[currsec];
  117. end;
  118. {****************************************************************************
  119. tobjectoutput
  120. ****************************************************************************}
  121. constructor tobjectoutput.init(smart:boolean);
  122. begin
  123. smarthcount:=0;
  124. objsmart:=smart;
  125. objfile:=current_module^.objfilename^;
  126. { Which path will be used ? }
  127. if objsmart and
  128. (cs_asm_leave in aktglobalswitches) then
  129. begin
  130. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  131. {$I-}
  132. mkdir(path);
  133. {$I+}
  134. if ioresult<>0 then;
  135. path:=FixPath(path,false);
  136. end
  137. else
  138. path:=current_module^.path^;
  139. { init writer }
  140. if objsmart and
  141. not(cs_asm_leave in aktglobalswitches) then
  142. writer:=New(parobjectwriter,Init(current_module^.staticlibfilename^))
  143. else
  144. writer:=New(pobjectwriter,Init);
  145. end;
  146. destructor tobjectoutput.done;
  147. begin
  148. Dispose(writer,done);
  149. end;
  150. procedure tobjectoutput.NextSmartName;
  151. var
  152. s : string;
  153. begin
  154. inc(SmartLinkFilesCnt);
  155. if SmartLinkFilesCnt>999999 then
  156. Message(asmw_f_too_many_asm_files);
  157. if (cs_asm_leave in aktglobalswitches) then
  158. s:=current_module^.asmprefix^
  159. else
  160. s:=current_module^.modulename^;
  161. case place of
  162. cut_begin :
  163. begin
  164. inc(smarthcount);
  165. s:=s+tostr(smarthcount)+'h';
  166. end;
  167. cut_normal :
  168. s:=s+tostr(smarthcount)+'s';
  169. cut_end :
  170. s:=s+tostr(smarthcount)+'t';
  171. end;
  172. ObjFile:=FixFileName(s+tostr(SmartLinkFilesCnt)+target_info.objext);
  173. end;
  174. procedure tobjectoutput.initwriting(Aplace:tcutplace);
  175. begin
  176. place:=Aplace;
  177. if objsmart then
  178. NextSmartName;
  179. writer^.create(objfile);
  180. end;
  181. procedure tobjectoutput.donewriting;
  182. begin
  183. writer^.close;
  184. end;
  185. function tobjectoutput.sectionsize(s:tsection):longint;
  186. begin
  187. sectionsize:=0;
  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. Do_halt(211);
  199. end;
  200. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  201. begin
  202. Do_halt(211);
  203. end;
  204. procedure tobjectoutput.writebytes(var data;len:longint);
  205. begin
  206. Do_halt(211);
  207. end;
  208. procedure tobjectoutput.writealloc(len:longint);
  209. begin
  210. Do_halt(211);
  211. end;
  212. procedure tobjectoutput.writealign(len:longint);
  213. begin
  214. Do_halt(211);
  215. end;
  216. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  217. begin
  218. Do_halt(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. Do_halt(211);
  224. end;
  225. end.
  226. {
  227. $Log$
  228. Revision 1.5 2000-08-27 16:11:51 peter
  229. * moved some util functions from globals,cobjects to cutils
  230. * splitted files into finput,fmodule
  231. Revision 1.4 2000/08/06 10:42:29 peter
  232. * merged patches name generation in lib and asm constant eval
  233. Revision 1.3 2000/07/13 12:08:26 michael
  234. + patched to 1.1.0 with former 1.09patch from peter
  235. Revision 1.2 2000/07/13 11:32:43 michael
  236. + removed logs
  237. }