t_go32v2.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Go32v2 target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_go32v2;
  19. {$i fpcdefs.inc}
  20. interface
  21. implementation
  22. uses
  23. SysUtils,
  24. cutils,cfileutl,cclasses,
  25. globtype,globals,systems,verbose,script,
  26. fmodule,i_go32v2,
  27. link,ogcoff;
  28. type
  29. TInternalLinkerGo32v2=class(TInternallinker)
  30. constructor create;override;
  31. procedure DefaultLinkScript;override;
  32. end;
  33. TExternalLinkerGo32v2=class(texternallinker)
  34. private
  35. Function WriteResponseFile(isdll:boolean) : Boolean;
  36. Function WriteScript(isdll:boolean) : Boolean;
  37. public
  38. constructor Create;override;
  39. procedure SetDefaultInfo;override;
  40. function MakeExecutable:boolean;override;
  41. end;
  42. {****************************************************************************
  43. TCoffLinker
  44. ****************************************************************************}
  45. constructor TInternalLinkerGo32v2.Create;
  46. begin
  47. inherited Create;
  48. CExeoutput:=TDJCoffexeoutput;
  49. CObjInput:=TDJCoffObjInput;
  50. end;
  51. procedure TInternalLinkerGo32v2.DefaultLinkScript;
  52. begin
  53. end;
  54. {****************************************************************************
  55. TExternalLinkerGo32v2
  56. ****************************************************************************}
  57. Constructor TExternalLinkerGo32v2.Create;
  58. begin
  59. Inherited Create;
  60. { allow duplicated libs (PM) }
  61. SharedLibFiles.doubles:=true;
  62. StaticLibFiles.doubles:=true;
  63. end;
  64. procedure TExternalLinkerGo32v2.SetDefaultInfo;
  65. begin
  66. with Info do
  67. begin
  68. ExeCmd[1]:='ld $RES';
  69. end;
  70. end;
  71. Function TExternalLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  72. Var
  73. linkres : TLinkRes;
  74. i : longint;
  75. s : string;
  76. linklibc : boolean;
  77. begin
  78. WriteResponseFile:=False;
  79. { Open link.res file }
  80. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  81. { Add all options to link.res instead of passing them via command line:
  82. DOS command line is limited to 126 characters! }
  83. LinkRes.Add('--script='+maybequoted(outputexedir+Info.ScriptName));
  84. if info.ExtraOptions<>'' then
  85. LinkRes.Add(Info.ExtraOptions);
  86. (* Potential issues with older ld version??? *)
  87. if (cs_link_strip in current_settings.globalswitches) then
  88. LinkRes.Add('-s');
  89. LinkRes.Add('-o '+maybequoted(current_module.exefilename^));
  90. { Write staticlibraries }
  91. if not StaticLibFiles.Empty then
  92. begin
  93. LinkRes.Add('-(');
  94. While not StaticLibFiles.Empty do
  95. begin
  96. S:=StaticLibFiles.GetFirst;
  97. LinkRes.AddFileName(GetShortName(s))
  98. end;
  99. LinkRes.Add('-)');
  100. end;
  101. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  102. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  103. linklibc:=false;
  104. While not SharedLibFiles.Empty do
  105. begin
  106. S:=SharedLibFiles.GetFirst;
  107. if s<>'c' then
  108. begin
  109. i:=Pos(target_info.sharedlibext,S);
  110. if i>0 then
  111. Delete(S,i,255);
  112. LinkRes.Add('-l'+s);
  113. end
  114. else
  115. begin
  116. LinkRes.Add('-l'+s);
  117. linklibc:=true;
  118. end;
  119. end;
  120. { be sure that libc&libgcc is the last lib }
  121. if linklibc then
  122. begin
  123. LinkRes.Add('-lc');
  124. LinkRes.Add('-lgcc');
  125. end;
  126. { Write and Close response }
  127. linkres.writetodisk;
  128. LinkRes.Free;
  129. WriteResponseFile:=True;
  130. end;
  131. Function TExternalLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  132. Var
  133. scriptres : TLinkRes;
  134. HPath : TCmdStrListItem;
  135. s : string;
  136. begin
  137. WriteScript:=False;
  138. { Open link.res file }
  139. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName);
  140. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  141. ScriptRes.Add('ENTRY(start)');
  142. ScriptRes.Add('SECTIONS');
  143. ScriptRes.Add('{');
  144. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  145. ScriptRes.Add(' . = ALIGN(16);');
  146. { add objectfiles, start with prt0 always }
  147. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  148. while not ObjectFiles.Empty do
  149. begin
  150. s:=ObjectFiles.GetFirst;
  151. if s<>'' then
  152. begin
  153. ScriptRes.Add(' . = ALIGN(16);');
  154. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  155. end;
  156. end;
  157. ScriptRes.Add(' *(.text)');
  158. ScriptRes.Add(' etext = . ; _etext = .;');
  159. ScriptRes.Add(' . = ALIGN(0x200);');
  160. ScriptRes.Add(' }');
  161. ScriptRes.Add(' .data ALIGN(0x200) : {');
  162. ScriptRes.Add(' djgpp_first_ctor = . ;');
  163. ScriptRes.Add(' *(SORT(.ctors.*))');
  164. ScriptRes.Add(' *(.ctor)');
  165. ScriptRes.Add(' *(.ctors)');
  166. ScriptRes.Add(' djgpp_last_ctor = . ;');
  167. ScriptRes.Add(' djgpp_first_dtor = . ;');
  168. ScriptRes.Add(' *(SORT(.dtors.*))');
  169. ScriptRes.Add(' *(.dtor)');
  170. ScriptRes.Add(' *(.dtors)');
  171. ScriptRes.Add(' djgpp_last_dtor = . ;');
  172. ScriptRes.Add(' *(.data)');
  173. ScriptRes.Add(' *(.fpc*)');
  174. ScriptRes.Add(' *(.gcc_exc)');
  175. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  176. ScriptRes.Add(' *(.eh_fram*)');
  177. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  178. ScriptRes.Add(' LONG(0)');
  179. ScriptRes.Add(' edata = . ; _edata = .;');
  180. ScriptRes.Add(' . = ALIGN(0x200);');
  181. ScriptRes.Add(' }');
  182. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  183. ScriptRes.Add(' {');
  184. ScriptRes.Add(' _object.2 = . ;');
  185. ScriptRes.Add(' . += 24 ;');
  186. ScriptRes.Add(' *(.bss)');
  187. ScriptRes.Add(' *(COMMON)');
  188. ScriptRes.Add(' end = . ; _end = .;');
  189. ScriptRes.Add(' . = ALIGN(0x200);');
  190. ScriptRes.Add(' }');
  191. ScriptRes.Add(' /* Stabs debugging sections. */');
  192. ScriptRes.Add(' .stab 0 : { *(.stab) }');
  193. ScriptRes.Add(' .stabstr 0 : { *(.stabstr) }');
  194. ScriptRes.Add(' /* DWARF 2 */');
  195. ScriptRes.Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  196. ScriptRes.Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  197. ScriptRes.Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  198. ScriptRes.Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  199. ScriptRes.Add(' .debug_line 0 : { *(.debug_line) }');
  200. ScriptRes.Add(' .debug_frame 0 : { *(.debug_frame) }');
  201. ScriptRes.Add(' .debug_str 0 : { *(.debug_str) }');
  202. ScriptRes.Add(' .debug_loc 0 : { *(.debug_loc) }');
  203. ScriptRes.Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  204. ScriptRes.Add(' }');
  205. { Write path to search libraries }
  206. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  207. while assigned(HPath) do
  208. begin
  209. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  210. HPath:=TCmdStrListItem(HPath.Next);
  211. end;
  212. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  213. while assigned(HPath) do
  214. begin
  215. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  216. HPath:=TCmdStrListItem(HPath.Next);
  217. end;
  218. { Write and Close response }
  219. ScriptRes.WriteToDisk;
  220. ScriptRes.Free;
  221. WriteScript:=True;
  222. end;
  223. function TExternalLinkerGo32v2.MakeExecutable:boolean;
  224. var
  225. binstr,
  226. cmdstr : TCmdStr;
  227. success : boolean;
  228. begin
  229. if not(cs_link_nolink in current_settings.globalswitches) then
  230. Message1(exec_i_linking,current_module.exefilename^);
  231. { Write used files and libraries and our own ld script }
  232. WriteScript(false);
  233. WriteResponsefile(false);
  234. { Call linker }
  235. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  236. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  237. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  238. { Remove ReponseFile }
  239. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  240. begin
  241. DeleteFile(outputexedir+Info.ResName);
  242. DeleteFile(outputexedir+Info.ScriptName);
  243. end;
  244. MakeExecutable:=success; { otherwise a recursive call to link method }
  245. end;
  246. {$ifdef notnecessary}
  247. procedure TExternalLinkerGo32v2.postprocessexecutable(const n : string);
  248. type
  249. tcoffheader=packed record
  250. mach : word;
  251. nsects : word;
  252. time : longint;
  253. sympos : longint;
  254. syms : longint;
  255. opthdr : word;
  256. flag : word;
  257. end;
  258. tcoffsechdr=packed record
  259. name : array[0..7] of char;
  260. vsize : longint;
  261. rvaofs : longint;
  262. datalen : longint;
  263. datapos : longint;
  264. relocpos : longint;
  265. lineno1 : longint;
  266. nrelocs : word;
  267. lineno2 : word;
  268. flags : longint;
  269. end;
  270. psecfill=^TSecfill;
  271. TSecfill=record
  272. fillpos,
  273. fillsize : longint;
  274. next : psecfill;
  275. end;
  276. var
  277. f : file;
  278. coffheader : tcoffheader;
  279. firstsecpos,
  280. maxfillsize,
  281. l : longint;
  282. coffsec : tcoffsechdr;
  283. secroot,hsecroot : psecfill;
  284. zerobuf : pointer;
  285. begin
  286. { when -s is used quit, because there is no .exe }
  287. if cs_link_nolink in current_settings.globalswitches then
  288. exit;
  289. { open file }
  290. assign(f,n);
  291. {$I-}
  292. reset(f,1);
  293. if ioresult<>0 then
  294. Message1(execinfo_f_cant_open_executable,n);
  295. { read headers }
  296. seek(f,2048);
  297. blockread(f,coffheader,sizeof(tcoffheader));
  298. { read section info }
  299. maxfillsize:=0;
  300. firstsecpos:=0;
  301. secroot:=nil;
  302. for l:=1to coffheader.nSects do
  303. begin
  304. blockread(f,coffsec,sizeof(tcoffsechdr));
  305. if coffsec.datapos>0 then
  306. begin
  307. if secroot=nil then
  308. firstsecpos:=coffsec.datapos;
  309. new(hsecroot);
  310. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  311. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  312. hsecroot^.next:=secroot;
  313. secroot:=hsecroot;
  314. if secroot^.fillsize>maxfillsize then
  315. maxfillsize:=secroot^.fillsize;
  316. end;
  317. end;
  318. if firstsecpos>0 then
  319. begin
  320. l:=firstsecpos-filepos(f);
  321. if l>maxfillsize then
  322. maxfillsize:=l;
  323. end
  324. else
  325. l:=0;
  326. { get zero buffer }
  327. getmem(zerobuf,maxfillsize);
  328. fillchar(zerobuf^,maxfillsize,0);
  329. { zero from sectioninfo until first section }
  330. blockwrite(f,zerobuf^,l);
  331. { zero section alignments }
  332. while assigned(secroot) do
  333. begin
  334. seek(f,secroot^.fillpos);
  335. blockwrite(f,zerobuf^,secroot^.fillsize);
  336. hsecroot:=secroot;
  337. secroot:=secroot^.next;
  338. dispose(hsecroot);
  339. end;
  340. freemem(zerobuf,maxfillsize);
  341. close(f);
  342. {$I+}
  343. i:=ioresult;
  344. postprocessexecutable:=true;
  345. end;
  346. {$endif}
  347. {*****************************************************************************
  348. Initialize
  349. *****************************************************************************}
  350. initialization
  351. RegisterExternalLinker(system_i386_go32v2_info,TExternalLinkerGo32v2);
  352. // RegisterInternalLinker(system_i386_go32v2_info,TInternalLinkerGo32v2);
  353. RegisterTarget(system_i386_go32v2_info);
  354. end.