t_go32v2.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) Go32v2 target
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit t_go32v2;
  20. {$i fpcdefs.inc}
  21. interface
  22. implementation
  23. uses
  24. link,
  25. cutils,cclasses,
  26. globtype,globals,systems,verbose,script,fmodule,i_go32v2,ogcoff;
  27. type
  28. tlinkergo32v2=class(texternallinker)
  29. private
  30. Function WriteResponseFile(isdll:boolean) : Boolean;
  31. Function WriteScript(isdll:boolean) : Boolean;
  32. public
  33. constructor Create;override;
  34. procedure SetDefaultInfo;override;
  35. function MakeExecutable:boolean;override;
  36. end;
  37. {****************************************************************************
  38. TLinkerGo32v2
  39. ****************************************************************************}
  40. Constructor TLinkerGo32v2.Create;
  41. begin
  42. Inherited Create;
  43. { allow duplicated libs (PM) }
  44. SharedLibFiles.doubles:=true;
  45. StaticLibFiles.doubles:=true;
  46. end;
  47. procedure TLinkerGo32v2.SetDefaultInfo;
  48. begin
  49. with Info do
  50. begin
  51. ExeCmd[1]:='ld $SCRIPT $OPT $STRIP -o $EXE @$RES';
  52. end;
  53. end;
  54. Function TLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  55. Var
  56. linkres : TLinkRes;
  57. i : longint;
  58. s : string;
  59. linklibc : boolean;
  60. begin
  61. WriteResponseFile:=False;
  62. { Open link.res file }
  63. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  64. { Write staticlibraries }
  65. if not StaticLibFiles.Empty then
  66. begin
  67. LinkRes.Add('-(');
  68. While not StaticLibFiles.Empty do
  69. begin
  70. S:=StaticLibFiles.GetFirst;
  71. LinkRes.AddFileName(GetShortName(s))
  72. end;
  73. LinkRes.Add('-)');
  74. end;
  75. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  76. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  77. linklibc:=false;
  78. While not SharedLibFiles.Empty do
  79. begin
  80. S:=SharedLibFiles.GetFirst;
  81. if s<>'c' then
  82. begin
  83. i:=Pos(target_info.sharedlibext,S);
  84. if i>0 then
  85. Delete(S,i,255);
  86. LinkRes.Add('-l'+s);
  87. end
  88. else
  89. begin
  90. LinkRes.Add('-l'+s);
  91. linklibc:=true;
  92. end;
  93. end;
  94. { be sure that libc&libgcc is the last lib }
  95. if linklibc then
  96. begin
  97. LinkRes.Add('-lc');
  98. LinkRes.Add('-lgcc');
  99. end;
  100. { Write and Close response }
  101. linkres.writetodisk;
  102. LinkRes.Free;
  103. WriteResponseFile:=True;
  104. end;
  105. Function TLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  106. Var
  107. scriptres : TLinkRes;
  108. HPath : TStringListItem;
  109. s : string;
  110. begin
  111. WriteScript:=False;
  112. { Open link.res file }
  113. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName);
  114. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  115. ScriptRes.Add('ENTRY(start)');
  116. ScriptRes.Add('SECTIONS');
  117. ScriptRes.Add('{');
  118. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  119. ScriptRes.Add(' . = ALIGN(16);');
  120. { add objectfiles, start with prt0 always }
  121. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  122. while not ObjectFiles.Empty do
  123. begin
  124. s:=ObjectFiles.GetFirst;
  125. if s<>'' then
  126. begin
  127. ScriptRes.Add(' . = ALIGN(16);');
  128. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  129. end;
  130. end;
  131. ScriptRes.Add(' *(.text)');
  132. ScriptRes.Add(' etext = . ; _etext = .;');
  133. ScriptRes.Add(' . = ALIGN(0x200);');
  134. ScriptRes.Add(' }');
  135. ScriptRes.Add(' .data ALIGN(0x200) : {');
  136. ScriptRes.Add(' djgpp_first_ctor = . ;');
  137. ScriptRes.Add(' *(.ctor)');
  138. ScriptRes.Add(' djgpp_last_ctor = . ;');
  139. ScriptRes.Add(' djgpp_first_dtor = . ;');
  140. ScriptRes.Add(' *(.dtor)');
  141. ScriptRes.Add(' djgpp_last_dtor = . ;');
  142. ScriptRes.Add(' *(.data)');
  143. ScriptRes.Add(' *(.gcc_exc)');
  144. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  145. ScriptRes.Add(' *(.eh_fram)');
  146. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  147. ScriptRes.Add(' LONG(0)');
  148. ScriptRes.Add(' edata = . ; _edata = .;');
  149. ScriptRes.Add(' . = ALIGN(0x200);');
  150. ScriptRes.Add(' }');
  151. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  152. ScriptRes.Add(' {');
  153. ScriptRes.Add(' _object.2 = . ;');
  154. ScriptRes.Add(' . += 24 ;');
  155. ScriptRes.Add(' *(.bss)');
  156. ScriptRes.Add(' *(COMMON)');
  157. ScriptRes.Add(' end = . ; _end = .;');
  158. ScriptRes.Add(' . = ALIGN(0x200);');
  159. ScriptRes.Add(' }');
  160. ScriptRes.Add(' }');
  161. { Write path to search libraries }
  162. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  163. while assigned(HPath) do
  164. begin
  165. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  166. HPath:=TStringListItem(HPath.Next);
  167. end;
  168. HPath:=TStringListItem(LibrarySearchPath.First);
  169. while assigned(HPath) do
  170. begin
  171. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  172. HPath:=TStringListItem(HPath.Next);
  173. end;
  174. { Write and Close response }
  175. ScriptRes.WriteToDisk;
  176. ScriptRes.Free;
  177. WriteScript:=True;
  178. end;
  179. function TLinkerGo32v2.MakeExecutable:boolean;
  180. var
  181. binstr,
  182. cmdstr : string;
  183. success : boolean;
  184. StripStr : string[40];
  185. begin
  186. if not(cs_link_extern in aktglobalswitches) then
  187. Message1(exec_i_linking,current_module.exefilename^);
  188. { Create some replacements }
  189. StripStr:='';
  190. if (cs_link_strip in aktglobalswitches) then
  191. StripStr:='-s';
  192. { Write used files and libraries and our own ld script }
  193. WriteScript(false);
  194. WriteResponsefile(false);
  195. { Call linker }
  196. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  197. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  198. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  199. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  200. Replace(cmdstr,'$STRIP',StripStr);
  201. Replace(cmdstr,'$SCRIPT','--script='+maybequoted(outputexedir+Info.ScriptName));
  202. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  203. { Remove ReponseFile }
  204. if (success) and not(cs_link_extern in aktglobalswitches) then
  205. begin
  206. RemoveFile(outputexedir+Info.ResName);
  207. RemoveFile(outputexedir+Info.ScriptName);
  208. end;
  209. MakeExecutable:=success; { otherwise a recursive call to link method }
  210. end;
  211. {$ifdef notnecessary}
  212. procedure tlinkergo32v2.postprocessexecutable(const n : string);
  213. type
  214. tcoffheader=packed record
  215. mach : word;
  216. nsects : word;
  217. time : longint;
  218. sympos : longint;
  219. syms : longint;
  220. opthdr : word;
  221. flag : word;
  222. end;
  223. tcoffsechdr=packed record
  224. name : array[0..7] of char;
  225. vsize : longint;
  226. rvaofs : longint;
  227. datalen : longint;
  228. datapos : longint;
  229. relocpos : longint;
  230. lineno1 : longint;
  231. nrelocs : word;
  232. lineno2 : word;
  233. flags : longint;
  234. end;
  235. psecfill=^TSecfill;
  236. TSecfill=record
  237. fillpos,
  238. fillsize : longint;
  239. next : psecfill;
  240. end;
  241. var
  242. f : file;
  243. coffheader : tcoffheader;
  244. firstsecpos,
  245. maxfillsize,
  246. l : longint;
  247. coffsec : tcoffsechdr;
  248. secroot,hsecroot : psecfill;
  249. zerobuf : pointer;
  250. begin
  251. { when -s is used quit, because there is no .exe }
  252. if cs_link_extern in aktglobalswitches then
  253. exit;
  254. { open file }
  255. assign(f,n);
  256. {$I-}
  257. reset(f,1);
  258. if ioresult<>0 then
  259. Message1(execinfo_f_cant_open_executable,n);
  260. { read headers }
  261. seek(f,2048);
  262. blockread(f,coffheader,sizeof(tcoffheader));
  263. { read section info }
  264. maxfillsize:=0;
  265. firstsecpos:=0;
  266. secroot:=nil;
  267. for l:=1to coffheader.nSects do
  268. begin
  269. blockread(f,coffsec,sizeof(tcoffsechdr));
  270. if coffsec.datapos>0 then
  271. begin
  272. if secroot=nil then
  273. firstsecpos:=coffsec.datapos;
  274. new(hsecroot);
  275. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  276. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  277. hsecroot^.next:=secroot;
  278. secroot:=hsecroot;
  279. if secroot^.fillsize>maxfillsize then
  280. maxfillsize:=secroot^.fillsize;
  281. end;
  282. end;
  283. if firstsecpos>0 then
  284. begin
  285. l:=firstsecpos-filepos(f);
  286. if l>maxfillsize then
  287. maxfillsize:=l;
  288. end
  289. else
  290. l:=0;
  291. { get zero buffer }
  292. getmem(zerobuf,maxfillsize);
  293. fillchar(zerobuf^,maxfillsize,0);
  294. { zero from sectioninfo until first section }
  295. blockwrite(f,zerobuf^,l);
  296. { zero section alignments }
  297. while assigned(secroot) do
  298. begin
  299. seek(f,secroot^.fillpos);
  300. blockwrite(f,zerobuf^,secroot^.fillsize);
  301. hsecroot:=secroot;
  302. secroot:=secroot^.next;
  303. dispose(hsecroot);
  304. end;
  305. freemem(zerobuf,maxfillsize);
  306. close(f);
  307. {$I+}
  308. i:=ioresult;
  309. postprocessexecutable:=true;
  310. end;
  311. {$endif}
  312. {*****************************************************************************
  313. Initialize
  314. *****************************************************************************}
  315. initialization
  316. RegisterExternalLinker(system_i386_go32v2_info,TLinkerGo32v2);
  317. RegisterInternalLinker(system_i386_go32v2_info,TCoffLinker);
  318. RegisterTarget(system_i386_go32v2_info);
  319. end.
  320. {
  321. $Log$
  322. Revision 1.3 2003-10-03 14:16:48 marco
  323. * -XP<prefix> support
  324. Revision 1.2 2003/04/26 09:16:08 peter
  325. * .o files belonging to the unit are first searched in the same dir
  326. as the .ppu
  327. Revision 1.1 2002/09/06 15:03:51 carl
  328. * moved files to systems directory
  329. Revision 1.25 2002/08/12 15:08:44 carl
  330. + stab register indexes for powerpc (moved from gdb to cpubase)
  331. + tprocessor enumeration moved to cpuinfo
  332. + linker in target_info is now a class
  333. * many many updates for m68k (will soon start to compile)
  334. - removed some ifdef or correct them for correct cpu
  335. Revision 1.24 2002/07/26 21:15:46 florian
  336. * rewrote the system handling
  337. Revision 1.23 2002/07/01 18:46:35 peter
  338. * internal linker
  339. * reorganized aasm layer
  340. Revision 1.22 2002/05/18 13:34:26 peter
  341. * readded missing revisions
  342. Revision 1.21 2002/05/16 19:46:53 carl
  343. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  344. + try to fix temp allocation (still in ifdef)
  345. + generic constructor calls
  346. + start of tassembler / tmodulebase class cleanup
  347. Revision 1.19 2002/04/22 18:19:22 carl
  348. - remove use_bound_instruction field
  349. Revision 1.18 2002/04/20 21:43:18 carl
  350. * fix stack size for some targets
  351. + add offset to parameters from frame pointer info.
  352. - remove some unused stuff
  353. Revision 1.17 2002/04/15 19:44:23 peter
  354. * fixed stackcheck that would be called recursively when a stack
  355. error was found
  356. * generic changeregsize(reg,size) for i386 register resizing
  357. * removed some more routines from cga unit
  358. * fixed returnvalue handling
  359. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  360. Revision 1.16 2002/04/15 19:16:57 carl
  361. - remove size_of_pointer field
  362. Revision 1.15 2002/01/29 21:27:34 peter
  363. * default alignment changed to 4 bytes for locals and static const,var
  364. }