t_go32v2.pas 16 KB

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