t_go32v2.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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,cscript,
  26. fmodule,i_go32v2,
  27. link,ogcoff,owar,aasmbase;
  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. CArObjectReader:=TArObjectReader;
  49. CExeoutput:=TDJCoffexeoutput;
  50. CObjInput:=TDJCoffObjInput;
  51. end;
  52. procedure TInternalLinkerGo32v2.DefaultLinkScript;
  53. var
  54. s: TCmdStr;
  55. linklibc: Boolean;
  56. i: longint;
  57. procedure AddLib(const name: TCmdStr);
  58. var
  59. s2: TCmdStr;
  60. begin
  61. if FindLibraryFile(name,target_info.staticClibprefix,target_info.staticClibext,s2) then
  62. LinkScript.Concat('READSTATICLIBRARY '+MaybeQuoted(s2))
  63. else
  64. Comment(V_Error,'Import library not found for '+name);
  65. end;
  66. begin
  67. with LinkScript do
  68. begin
  69. Concat('READOBJECT '+GetShortName(FindObjectFile('prt0','',false)));
  70. while not ObjectFiles.Empty do
  71. begin
  72. s:=ObjectFiles.GetFirst;
  73. if s<>'' then
  74. Concat('READOBJECT '+MaybeQuoted(s));
  75. end;
  76. while not StaticLibFiles.Empty do
  77. begin
  78. s:=StaticLibFiles.GetFirst;
  79. if s<>'' then
  80. Concat('READSTATICLIBRARY '+MaybeQuoted(s));
  81. end;
  82. linklibc:=False;
  83. while not SharedLibFiles.Empty do
  84. begin
  85. S:=SharedLibFiles.GetFirst;
  86. if S<>'c' then
  87. begin
  88. i:=Pos(target_info.sharedlibext,S);
  89. if i>0 then
  90. Delete(S,i,255);
  91. AddLib(s);
  92. end
  93. else
  94. linklibc:=true;
  95. end;
  96. { be sure that to add libc and libgcc at the end }
  97. if linklibc then
  98. begin
  99. AddLib('c');
  100. AddLib('gcc');
  101. end;
  102. Concat('ENTRYNAME start');
  103. Concat('HEADER');
  104. Concat('EXESECTION .text');
  105. Concat(' OBJSECTION .text*');
  106. Concat(' SYMBOL etext');
  107. Concat(' PROVIDE _etext');
  108. Concat('ENDEXESECTION');
  109. Concat('EXESECTION .data');
  110. Concat(' SYMBOL djgpp_first_ctor');
  111. Concat(' OBJSECTION .ctors.*');
  112. Concat(' OBJSECTION .ctor');
  113. Concat(' OBJSECTION .ctors');
  114. Concat(' SYMBOL djgpp_last_ctor');
  115. Concat(' SYMBOL djgpp_first_dtor');
  116. Concat(' OBJSECTION .dtors.*');
  117. Concat(' OBJSECTION .dtor');
  118. Concat(' OBJSECTION .dtors');
  119. Concat(' SYMBOL djgpp_last_dtor');
  120. Concat(' SYMBOL __environ');
  121. Concat(' SYMBOL _environ');
  122. Concat(' LONG 0');
  123. Concat(' OBJSECTION .data*');
  124. Concat(' OBJSECTION .fpc*');
  125. Concat(' OBJSECTION .gcc_exc*');
  126. Concat(' SYMBOL ___EH_FRAME_BEGIN__');
  127. Concat(' OBJSECTION .eh_fram*');
  128. Concat(' SYMBOL ___EH_FRAME_END__');
  129. Concat(' LONG 0');
  130. Concat(' SYMBOL edata');
  131. Concat(' SYMBOL _edata');
  132. Concat('ENDEXESECTION');
  133. Concat('EXESECTION .bss');
  134. //ScriptRes.Add(' _object.2 = . ;');
  135. //ScriptRes.Add(' . += 32 ;');
  136. Concat(' OBJSECTION .bss*');
  137. Concat(' SYMBOL end');
  138. Concat(' SYMBOL _end');
  139. Concat('ENDEXESECTION');
  140. { Stabs debugging sections }
  141. Concat('EXESECTION .stab');
  142. Concat(' OBJSECTION .stab');
  143. Concat('ENDEXESECTION');
  144. Concat('EXESECTION .stabstr');
  145. Concat(' OBJSECTION .stabstr');
  146. Concat('ENDEXESECTION');
  147. { DWARF 2 }
  148. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,.debug_abbrev,'+
  149. '.debug_line,.debug_frame,.debug_str,.debug_loc,.debug_macinfo');
  150. Concat('STABS');
  151. Concat('SYMBOLS');
  152. end;
  153. end;
  154. {****************************************************************************
  155. TExternalLinkerGo32v2
  156. ****************************************************************************}
  157. Constructor TExternalLinkerGo32v2.Create;
  158. begin
  159. Inherited Create;
  160. { allow duplicated libs (PM) }
  161. SharedLibFiles.doubles:=true;
  162. StaticLibFiles.doubles:=true;
  163. end;
  164. procedure TExternalLinkerGo32v2.SetDefaultInfo;
  165. begin
  166. with Info do
  167. begin
  168. ExeCmd[1]:='ld $OPT $RES';
  169. end;
  170. end;
  171. Function TExternalLinkerGo32v2.WriteResponseFile(isdll:boolean) : Boolean;
  172. Var
  173. linkres : TLinkRes;
  174. i : longint;
  175. s : string;
  176. linklibc : boolean;
  177. begin
  178. WriteResponseFile:=False;
  179. { Open link.res file }
  180. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  181. { Add all options to link.res instead of passing them via command line:
  182. DOS command line is limited to 126 characters! }
  183. { Newer or cross GNU ld do not like \ in path names,
  184. so we use bstoslash }
  185. LinkRes.Add('--script='+maybequoted(bstoslash(outputexedir+Info.ScriptName)));
  186. if (cs_link_map in current_settings.globalswitches) then
  187. LinkRes.Add('-Map '+maybequoted(bstoslash(ChangeFileExt(current_module.exefilename,'.map'))));
  188. if create_smartlink_sections then
  189. LinkRes.Add('--gc-sections');
  190. if info.ExtraOptions<>'' then
  191. LinkRes.Add(Info.ExtraOptions);
  192. (* Potential issues with older ld version??? *)
  193. if (cs_link_strip in current_settings.globalswitches) then
  194. LinkRes.Add('-s');
  195. LinkRes.Add('-o '+maybequoted(bstoslash(current_module.exefilename)));
  196. { Write staticlibraries }
  197. if not StaticLibFiles.Empty then
  198. begin
  199. LinkRes.Add('-(');
  200. While not StaticLibFiles.Empty do
  201. begin
  202. S:=StaticLibFiles.GetFirst;
  203. LinkRes.AddFileName(bstoslash(GetShortName(s)))
  204. end;
  205. LinkRes.Add('-)');
  206. end;
  207. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  208. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  209. linklibc:=false;
  210. While not SharedLibFiles.Empty do
  211. begin
  212. S:=SharedLibFiles.GetFirst;
  213. if s<>'c' then
  214. begin
  215. i:=Pos(target_info.sharedlibext,S);
  216. if i>0 then
  217. Delete(S,i,255);
  218. LinkRes.Add('-l'+s);
  219. end
  220. else
  221. linklibc:=true;
  222. end;
  223. { be sure that libc&libgcc is the last lib }
  224. if linklibc then
  225. begin
  226. LinkRes.Add('-lc');
  227. LinkRes.Add('-lgcc');
  228. end;
  229. { Write and Close response }
  230. linkres.writetodisk;
  231. LinkRes.Free;
  232. WriteResponseFile:=True;
  233. end;
  234. Function TExternalLinkerGo32v2.WriteScript(isdll:boolean) : Boolean;
  235. Var
  236. scriptres : TLinkRes;
  237. HPath : TCmdStrListItem;
  238. s : string;
  239. begin
  240. WriteScript:=False;
  241. { Open link.res file }
  242. ScriptRes:=TLinkRes.Create(outputexedir+Info.ScriptName,true);
  243. ScriptRes.Add('OUTPUT_FORMAT("coff-go32-exe")');
  244. ScriptRes.Add('ENTRY(start)');
  245. ScriptRes.Add('SECTIONS');
  246. ScriptRes.Add('{');
  247. ScriptRes.Add(' .text 0x1000+SIZEOF_HEADERS : {');
  248. ScriptRes.Add(' . = ALIGN(16);');
  249. { add objectfiles, start with prt0 always }
  250. ScriptRes.Add(' '+GetShortName(FindObjectFile('prt0','',false))+'(.text)');
  251. while not ObjectFiles.Empty do
  252. begin
  253. s:=ObjectFiles.GetFirst;
  254. if s<>'' then
  255. begin
  256. ScriptRes.Add(' . = ALIGN(16);');
  257. ScriptRes.Add(' '+GetShortName(s)+'(.text)');
  258. end;
  259. end;
  260. ScriptRes.Add(' *(.text)');
  261. ScriptRes.Add(' *(.text.*)');
  262. ScriptRes.Add(' etext = . ;');
  263. ScriptRes.Add(' PROVIDE(_etext = .);');
  264. ScriptRes.Add(' . = ALIGN(0x200);');
  265. ScriptRes.Add(' }');
  266. ScriptRes.Add(' .data ALIGN(0x200) : {');
  267. ScriptRes.Add(' djgpp_first_ctor = . ;');
  268. ScriptRes.Add(' *(SORT(.ctors.*))');
  269. ScriptRes.Add(' *(.ctor)');
  270. ScriptRes.Add(' *(.ctors)');
  271. ScriptRes.Add(' djgpp_last_ctor = . ;');
  272. ScriptRes.Add(' djgpp_first_dtor = . ;');
  273. ScriptRes.Add(' *(SORT(.dtors.*))');
  274. ScriptRes.Add(' *(.dtor)');
  275. ScriptRes.Add(' *(.dtors)');
  276. ScriptRes.Add(' djgpp_last_dtor = . ;');
  277. ScriptRes.Add(' __environ = . ;');
  278. ScriptRes.Add(' _environ = .;');
  279. ScriptRes.Add(' LONG(0)');
  280. ScriptRes.Add(' . = ALIGN(0x20);');
  281. ScriptRes.Add(' *(.data)');
  282. ScriptRes.Add(' *(.data.*)');
  283. ScriptRes.Add(' . = ALIGN(0x20);');
  284. ScriptRes.Add(' *(.fpc*)');
  285. ScriptRes.Add(' . = ALIGN(0x20);');
  286. ScriptRes.Add(' *(.gcc_exc)');
  287. ScriptRes.Add(' ___EH_FRAME_BEGIN__ = . ;');
  288. ScriptRes.Add(' *(.eh_fram*)');
  289. ScriptRes.Add(' ___EH_FRAME_END__ = . ;');
  290. ScriptRes.Add(' LONG(0)');
  291. ScriptRes.Add(' edata = . ; _edata = .;');
  292. ScriptRes.Add(' . = ALIGN(0x200);');
  293. ScriptRes.Add(' }');
  294. ScriptRes.Add(' .bss SIZEOF(.data) + ADDR(.data) :');
  295. ScriptRes.Add(' {');
  296. ScriptRes.Add(' _object.2 = . ;');
  297. ScriptRes.Add(' . += 32 ;');
  298. ScriptRes.Add(' *(.bss)');
  299. ScriptRes.Add(' *(.bss.*)');
  300. ScriptRes.Add(' *(COMMON)');
  301. ScriptRes.Add(' end = . ; _end = .;');
  302. ScriptRes.Add(' . = ALIGN(0x200);');
  303. ScriptRes.Add(' }');
  304. ScriptRes.Add(' /* Stabs debugging sections. */');
  305. ScriptRes.Add(' .stab 0 : { *(.stab) }');
  306. ScriptRes.Add(' .stabstr 0 : { *(.stabstr) }');
  307. ScriptRes.Add(' /* DWARF 2 */');
  308. ScriptRes.Add(' .debug_aranges 0 : { *(.debug_aranges) }');
  309. ScriptRes.Add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  310. ScriptRes.Add(' .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) }');
  311. ScriptRes.Add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  312. ScriptRes.Add(' .debug_line 0 : { *(.debug_line) }');
  313. ScriptRes.Add(' .debug_frame 0 : { *(.debug_frame) }');
  314. ScriptRes.Add(' .debug_str 0 : { *(.debug_str) }');
  315. ScriptRes.Add(' .debug_loc 0 : { *(.debug_loc) }');
  316. ScriptRes.Add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  317. ScriptRes.Add(' }');
  318. { Write path to search libraries }
  319. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  320. while assigned(HPath) do
  321. begin
  322. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  323. HPath:=TCmdStrListItem(HPath.Next);
  324. end;
  325. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  326. while assigned(HPath) do
  327. begin
  328. ScriptRes.Add('SEARCH_DIR("'+GetShortName(HPath.Str)+'")');
  329. HPath:=TCmdStrListItem(HPath.Next);
  330. end;
  331. { Write and Close response }
  332. ScriptRes.WriteToDisk;
  333. ScriptRes.Free;
  334. WriteScript:=True;
  335. end;
  336. function TExternalLinkerGo32v2.MakeExecutable:boolean;
  337. var
  338. binstr,
  339. cmdstr : TCmdStr;
  340. success : boolean;
  341. begin
  342. if not(cs_link_nolink in current_settings.globalswitches) then
  343. Message1(exec_i_linking,current_module.exefilename);
  344. { Write used files and libraries and our own ld script }
  345. WriteScript(false);
  346. WriteResponsefile(false);
  347. { Call linker }
  348. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  349. Replace(cmdstr,'$RES','@'+maybequoted(outputexedir+Info.ResName));
  350. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  351. success:=DoExec(FindUtil(utilsprefix+BinStr),cmdstr,true,false);
  352. { Remove ReponseFile }
  353. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  354. begin
  355. DeleteFile(outputexedir+Info.ResName);
  356. DeleteFile(outputexedir+Info.ScriptName);
  357. end;
  358. MakeExecutable:=success; { otherwise a recursive call to link method }
  359. end;
  360. {$ifdef notnecessary}
  361. procedure TExternalLinkerGo32v2.postprocessexecutable(const n : string);
  362. type
  363. tcoffheader=packed record
  364. mach : word;
  365. nsects : word;
  366. time : longint;
  367. sympos : longint;
  368. syms : longint;
  369. opthdr : word;
  370. flag : word;
  371. end;
  372. tcoffsechdr=packed record
  373. name : array[0..7] of char;
  374. vsize : longint;
  375. rvaofs : longint;
  376. datalen : longint;
  377. datapos : longint;
  378. relocpos : longint;
  379. lineno1 : longint;
  380. nrelocs : word;
  381. lineno2 : word;
  382. flags : longint;
  383. end;
  384. psecfill=^TSecfill;
  385. TSecfill=record
  386. fillpos,
  387. fillsize : longint;
  388. next : psecfill;
  389. end;
  390. var
  391. f : file;
  392. coffheader : tcoffheader;
  393. firstsecpos,
  394. maxfillsize,
  395. l : longint;
  396. coffsec : tcoffsechdr;
  397. secroot,hsecroot : psecfill;
  398. zerobuf : pointer;
  399. begin
  400. { when -s is used quit, because there is no .exe }
  401. if cs_link_nolink in current_settings.globalswitches then
  402. exit;
  403. { open file }
  404. assign(f,n);
  405. {$push}{$I-}
  406. reset(f,1);
  407. if ioresult<>0 then
  408. Message1(execinfo_f_cant_open_executable,n);
  409. { read headers }
  410. seek(f,2048);
  411. blockread(f,coffheader,sizeof(tcoffheader));
  412. { read section info }
  413. maxfillsize:=0;
  414. firstsecpos:=0;
  415. secroot:=nil;
  416. for l:=1to coffheader.nSects do
  417. begin
  418. blockread(f,coffsec,sizeof(tcoffsechdr));
  419. if coffsec.datapos>0 then
  420. begin
  421. if secroot=nil then
  422. firstsecpos:=coffsec.datapos;
  423. new(hsecroot);
  424. hsecroot^.fillpos:=coffsec.datapos+coffsec.vsize;
  425. hsecroot^.fillsize:=coffsec.datalen-coffsec.vsize;
  426. hsecroot^.next:=secroot;
  427. secroot:=hsecroot;
  428. if secroot^.fillsize>maxfillsize then
  429. maxfillsize:=secroot^.fillsize;
  430. end;
  431. end;
  432. if firstsecpos>0 then
  433. begin
  434. l:=firstsecpos-filepos(f);
  435. if l>maxfillsize then
  436. maxfillsize:=l;
  437. end
  438. else
  439. l:=0;
  440. { get zero buffer }
  441. getmem(zerobuf,maxfillsize);
  442. fillchar(zerobuf^,maxfillsize,0);
  443. { zero from sectioninfo until first section }
  444. blockwrite(f,zerobuf^,l);
  445. { zero section alignments }
  446. while assigned(secroot) do
  447. begin
  448. seek(f,secroot^.fillpos);
  449. blockwrite(f,zerobuf^,secroot^.fillsize);
  450. hsecroot:=secroot;
  451. secroot:=secroot^.next;
  452. dispose(hsecroot);
  453. end;
  454. freemem(zerobuf,maxfillsize);
  455. close(f);
  456. {$pop}
  457. i:=ioresult;
  458. postprocessexecutable:=true;
  459. end;
  460. {$endif}
  461. {*****************************************************************************
  462. Initialize
  463. *****************************************************************************}
  464. initialization
  465. RegisterLinker(ld_go32v2,TExternalLinkerGo32v2);
  466. RegisterLinker(ld_int_go32v2,TInternalLinkerGo32v2);
  467. RegisterTarget(system_i386_go32v2_info);
  468. end.