og386.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. SmartFilesCount,
  53. SmartHeaderCount : longint;
  54. objsmart : boolean;
  55. writer : pobjectwriter;
  56. path : pathstr;
  57. ObjFile : string;
  58. place : tcutplace;
  59. currsec : tsection;
  60. constructor init(smart:boolean);
  61. destructor done;virtual;
  62. { Writing }
  63. procedure NextSmartName;
  64. procedure initwriting(Aplace:tcutplace);virtual;
  65. procedure donewriting;virtual;
  66. function sectionsize(s:tsection):longint;virtual;
  67. procedure setsectionsizes(var s:tsecsize);virtual;
  68. procedure writebytes(var data;len:longint);virtual;
  69. procedure writealloc(len:longint);virtual;
  70. procedure writealign(len:longint);virtual;
  71. procedure writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);virtual;
  72. procedure writesymbol(p:pasmsymbol);virtual;
  73. procedure writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);virtual;
  74. procedure writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  75. nidx,nother,line:longint;reloc:boolean);virtual;
  76. procedure defaultsection(sec:tsection);
  77. end;
  78. var
  79. objectalloc : pobjectalloc;
  80. objectoutput : pobjectoutput;
  81. implementation
  82. uses
  83. comphook,
  84. cutils,globtype,globals,verbose,fmodule;
  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. SmartFilesCount:=0;
  127. SmartHeaderCount:=0;
  128. objsmart:=smart;
  129. objfile:=current_module^.objfilename^;
  130. { Which path will be used ? }
  131. if objsmart and
  132. (cs_asm_leave in aktglobalswitches) then
  133. begin
  134. path:=current_module^.path^+FixFileName(current_module^.modulename^)+target_info.smartext;
  135. {$I-}
  136. mkdir(path);
  137. {$I+}
  138. if ioresult<>0 then;
  139. path:=FixPath(path,false);
  140. end
  141. else
  142. path:=current_module^.path^;
  143. { init writer }
  144. if objsmart and
  145. not(cs_asm_leave in aktglobalswitches) then
  146. writer:=New(parobjectwriter,Init(current_module^.staticlibfilename^))
  147. else
  148. writer:=New(pobjectwriter,Init);
  149. end;
  150. destructor tobjectoutput.done;
  151. begin
  152. Dispose(writer,done);
  153. end;
  154. procedure tobjectoutput.NextSmartName;
  155. var
  156. s : string;
  157. begin
  158. inc(SmartFilesCount);
  159. if SmartFilesCount>999999 then
  160. Message(asmw_f_too_many_asm_files);
  161. if (cs_asm_leave in aktglobalswitches) then
  162. s:=current_module^.asmprefix^
  163. else
  164. s:=current_module^.modulename^;
  165. case place of
  166. cut_begin :
  167. begin
  168. inc(SmartHeaderCount);
  169. s:=s+tostr(SmartHeaderCount)+'h';
  170. end;
  171. cut_normal :
  172. s:=s+tostr(SmartHeaderCount)+'s';
  173. cut_end :
  174. s:=s+tostr(SmartHeaderCount)+'t';
  175. end;
  176. ObjFile:=FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  177. end;
  178. procedure tobjectoutput.initwriting(Aplace:tcutplace);
  179. begin
  180. place:=Aplace;
  181. if objsmart then
  182. NextSmartName;
  183. writer^.create(objfile);
  184. end;
  185. procedure tobjectoutput.donewriting;
  186. begin
  187. writer^.close;
  188. end;
  189. function tobjectoutput.sectionsize(s:tsection):longint;
  190. begin
  191. sectionsize:=0;
  192. end;
  193. procedure tobjectoutput.setsectionsizes(var s:tsecsize);
  194. begin
  195. end;
  196. procedure tobjectoutput.defaultsection(sec:tsection);
  197. begin
  198. currsec:=sec;
  199. end;
  200. procedure tobjectoutput.writesymbol(p:pasmsymbol);
  201. begin
  202. Do_halt(211);
  203. end;
  204. procedure tobjectoutput.writereloc(data,len:longint;p:pasmsymbol;relative:relative_type);
  205. begin
  206. Do_halt(211);
  207. end;
  208. procedure tobjectoutput.writebytes(var data;len:longint);
  209. begin
  210. Do_halt(211);
  211. end;
  212. procedure tobjectoutput.writealloc(len:longint);
  213. begin
  214. Do_halt(211);
  215. end;
  216. procedure tobjectoutput.writealign(len:longint);
  217. begin
  218. Do_halt(211);
  219. end;
  220. procedure tobjectoutput.writestabs(section:tsection;offset:longint;p:pchar;nidx,nother,line:longint;reloc:boolean);
  221. begin
  222. Do_halt(211);
  223. end;
  224. procedure tobjectoutput.writesymstabs(section:tsection;offset:longint;p:pchar;ps:pasmsymbol;
  225. nidx,nother,line:longint;reloc:boolean);
  226. begin
  227. Do_halt(211);
  228. end;
  229. end.
  230. {
  231. $Log$
  232. Revision 1.7 2000-10-01 19:48:25 peter
  233. * lot of compile updates for cg11
  234. Revision 1.6 2000/09/24 15:06:19 peter
  235. * use defines.inc
  236. Revision 1.5 2000/08/27 16:11:51 peter
  237. * moved some util functions from globals,cobjects to cutils
  238. * splitted files into finput,fmodule
  239. Revision 1.4 2000/08/06 10:42:29 peter
  240. * merged patches name generation in lib and asm constant eval
  241. Revision 1.3 2000/07/13 12:08:26 michael
  242. + patched to 1.1.0 with former 1.09patch from peter
  243. Revision 1.2 2000/07/13 11:32:43 michael
  244. + removed logs
  245. }