og386.pas 8.1 KB

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