PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/systems/t_linux.pas

https://github.com/slibre/freepascal
Pascal | 1558 lines | 1213 code | 108 blank | 237 comment | 99 complexity | 209a610774a1879467cb57f0d7d4b535 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, LGPL-3.0
  1. {
  2. Copyright (c) 1998-2008 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Linux 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_linux;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmdata,
  23. symsym,symdef,ppu,
  24. import,export,expunix,link;
  25. type
  26. timportliblinux=class(timportlib)
  27. procedure generatelib;override;
  28. end;
  29. texportliblinux=class(texportlibunix)
  30. procedure setfininame(list: TAsmList; const s: string); override;
  31. end;
  32. TLibcType=(libc5,glibc2,glibc21,uclibc);
  33. tlinkerlinux=class(texternallinker)
  34. private
  35. libctype: TLibcType;
  36. prtobj : string[80];
  37. reorder : boolean;
  38. linklibc: boolean;
  39. Function WriteResponseFile(isdll:boolean) : Boolean;
  40. public
  41. constructor Create;override;
  42. procedure SetDefaultInfo;override;
  43. procedure InitSysInitUnitName;override;
  44. function MakeExecutable:boolean;override;
  45. function MakeSharedLibrary:boolean;override;
  46. procedure LoadPredefinedLibraryOrder; override;
  47. end;
  48. TInternalLinkerLinux=class(TInternalLinker)
  49. private
  50. libctype: TLibcType;
  51. reorder: boolean;
  52. linklibc: boolean;
  53. prtobj: string[20];
  54. dynlinker: string[100];
  55. public
  56. constructor Create;override;
  57. procedure DefaultLinkScript;override;
  58. procedure InitSysInitUnitName;override;
  59. end;
  60. implementation
  61. uses
  62. SysUtils,
  63. cutils,cfileutl,cclasses,
  64. verbose,systems,globtype,globals,
  65. symconst,script,
  66. fmodule,
  67. aasmbase,aasmtai,aasmcpu,cpubase,
  68. cgbase,cgobj,cgutils,ogbase,ncgutil,
  69. comprsrc,
  70. ogelf,
  71. rescmn, i_linux
  72. ;
  73. {*****************************************************************************
  74. TIMPORTLIBLINUX
  75. *****************************************************************************}
  76. procedure timportliblinux.generatelib;
  77. var
  78. i : longint;
  79. ImportLibrary : TImportLibrary;
  80. begin
  81. for i:=0 to current_module.ImportLibraryList.Count-1 do
  82. begin
  83. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  84. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  85. end;
  86. end;
  87. {*****************************************************************************
  88. TEXPORTLIBLINUX
  89. *****************************************************************************}
  90. procedure texportliblinux.setfininame(list: TAsmList; const s: string);
  91. begin
  92. { the problem with not having a .fini section is that a finalization
  93. routine in regular code can get "smart" linked away -> reference it
  94. just like the debug info }
  95. new_section(list,sec_fpc,'links',0);
  96. list.concat(Tai_const.Createname(s,0));
  97. inherited setfininame(list,s);
  98. end;
  99. {*****************************************************************************
  100. TLINKERLINUX
  101. *****************************************************************************}
  102. procedure SetupLibrarySearchPath;
  103. begin
  104. if not Dontlinkstdlibpath Then
  105. {$ifdef x86_64}
  106. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  107. {$else}
  108. {$ifdef powerpc64}
  109. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  110. {$else powerpc64}
  111. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  112. {$endif powerpc64}
  113. {$endif x86_64}
  114. {$ifdef arm}
  115. { some newver Debian have the crt*.o files at uncommon locations,
  116. for other arm flavours, this cannot hurt }
  117. if not Dontlinkstdlibpath Then
  118. {$ifdef FPC_ARMHF}
  119. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabihf',true);
  120. {$endif FPC_ARMHF}
  121. {$ifdef FPC_ARMEL}
  122. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabi',true);
  123. {$endif}
  124. {$endif arm}
  125. end;
  126. {$ifdef m68k}
  127. { experimental, is this correct? }
  128. const defdynlinker='/lib/ld-linux.so.2';
  129. {$endif m68k}
  130. {$ifdef i386}
  131. const defdynlinker='/lib/ld-linux.so.2';
  132. {$endif}
  133. {$ifdef x86_64}
  134. const defdynlinker='/lib64/ld-linux-x86-64.so.2';
  135. {$endif x86_64}
  136. {$ifdef sparc}
  137. const defdynlinker='/lib/ld-linux.so.2';
  138. {$endif sparc}
  139. {$ifdef powerpc}
  140. const defdynlinker='/lib/ld.so.1';
  141. {$endif powerpc}
  142. {$ifdef powerpc64}
  143. const defdynlinker='/lib64/ld64.so.1';
  144. {$endif powerpc64}
  145. {$ifdef arm}
  146. {$ifdef FPC_ARMHF}
  147. const defdynlinker='/lib/ld-linux-armhf.so.3';
  148. {$else FPC_ARMHF}
  149. {$ifdef FPC_ARMEL}
  150. const defdynlinker='/lib/ld-linux.so.3';
  151. {$else FPC_ARMEL}
  152. const defdynlinker='/lib/ld-linux.so.2';
  153. {$endif FPC_ARMEL}
  154. {$endif FPC_ARMHF}
  155. {$endif arm}
  156. {$ifdef mips}
  157. const defdynlinker='/lib/ld.so.1';
  158. {$endif mips}
  159. procedure SetupDynlinker(out DynamicLinker:string;out libctype:TLibcType);
  160. begin
  161. {
  162. Search order:
  163. glibc 2.1+
  164. uclibc
  165. glibc 2.0
  166. If none is found (e.g. when cross compiling) glibc21 is assumed
  167. }
  168. if fileexists(sysrootpath+defdynlinker,false) then
  169. begin
  170. DynamicLinker:=defdynlinker;
  171. {$ifdef i386}
  172. libctype:=glibc21;
  173. {$else i386}
  174. libctype:=glibc2;
  175. {$endif i386}
  176. end
  177. else if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  178. begin
  179. DynamicLinker:='/lib/ld-uClibc.so.0';
  180. libctype:=uclibc;
  181. end
  182. {$ifdef i386}
  183. else if FileExists(sysrootpath+'/lib/ld-linux.so.1',false) then
  184. begin
  185. DynamicLinker:='/lib/ld-linux.so.1';
  186. libctype:=glibc2;
  187. end
  188. {$endif i386}
  189. else
  190. begin
  191. { when no dyn. linker is found, we are probably
  192. cross compiling, so use the default dyn. linker }
  193. DynamicLinker:=defdynlinker;
  194. {
  195. the default c startup script is gcrt0.as on all platforms
  196. except i386
  197. }
  198. {$ifdef i386}
  199. libctype:=glibc21;
  200. {$else i386}
  201. libctype:=glibc2;
  202. {$endif i386}
  203. end;
  204. end;
  205. function ModulesLinkToLibc:boolean;
  206. var
  207. hp: tmodule;
  208. begin
  209. { This is called very early, ImportLibraryList is not yet merged into linkothersharedlibs.
  210. The former contains library names qualified with prefix and suffix (coming from
  211. "external 'c' name 'foo' declarations), the latter contains raw names (from "$linklib c"
  212. directives). }
  213. hp:=tmodule(loaded_units.first);
  214. while assigned(hp) do
  215. begin
  216. result:=Assigned(hp.ImportLibraryList.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext));
  217. if result then break;
  218. result:=hp.linkothersharedlibs.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext);
  219. if result then break;
  220. result:=hp.linkothersharedlibs.find('c');
  221. if result then break;
  222. hp:=tmodule(hp.next);
  223. end;
  224. end;
  225. Constructor TLinkerLinux.Create;
  226. begin
  227. Inherited Create;
  228. SetupLibrarySearchPath;
  229. end;
  230. procedure TLinkerLinux.SetDefaultInfo;
  231. {
  232. This will also detect which libc version will be used
  233. }
  234. const
  235. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  236. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  237. {$ifdef powerpc} platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  238. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  239. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  240. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  241. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  242. {$ifdef mips}
  243. {$ifdef mipsel}
  244. platform_select='-EL';
  245. {$else}
  246. platform_select='-EB';
  247. {$endif}
  248. {$endif}
  249. begin
  250. with Info do
  251. begin
  252. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  253. { when we want to cross-link we need to override default library paths }
  254. if length(sysrootpath) > 0 then
  255. ExeCmd[1]:=ExeCmd[1]+' -T';
  256. ExeCmd[1]:=ExeCmd[1]+' $RES';
  257. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  258. DllCmd[2]:='strip --strip-unneeded $EXE';
  259. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  260. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  261. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  262. SetupDynlinker(DynamicLinker,libctype);
  263. end;
  264. end;
  265. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  266. // put your linkorder/linkalias overrides here.
  267. // Note: assumes only called when reordering/aliasing is used.
  268. Begin
  269. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  270. Begin
  271. LinkLibraryOrder.add('gcc','',15);
  272. LinkLibraryOrder.add('c','',100);
  273. LinkLibraryOrder.add('gmon','',120);
  274. LinkLibraryOrder.add('dl','',140);
  275. LinkLibraryOrder.add('pthread','',160);
  276. end;
  277. End;
  278. type
  279. tlibcnames=array [TLibcType] of string[8];
  280. const { libc5 glibc2 glibc21 uclibc }
  281. cprtnames: tlibcnames = ('cprt0', 'cprt0', 'cprt21', 'ucprt0');
  282. csinames: tlibcnames = ('si_c', 'si_c', 'si_c21', 'si_uc');
  283. gprtnames: tlibcnames = ('gprt0', 'gprt0', 'gprt21', 'ugprt0');
  284. gsinames: tlibcnames = ('si_g', 'si_g', 'si_c21g','si_ucg');
  285. defprtnames: array[boolean] of string[8] = ('prt0', 'dllprt0');
  286. defsinames: array[boolean] of string[8] = ('si_prc','si_dll');
  287. { uclibc and glibc21 are not available on x86_64! si_g is also absent. }
  288. Procedure TLinkerLinux.InitSysInitUnitName;
  289. begin
  290. linklibc:=ModulesLinkToLibc;
  291. reorder:=linklibc and ReOrderEntries;
  292. sysinitunit:=defsinames[current_module.islibrary];
  293. prtobj:=defprtnames[current_module.islibrary];
  294. if current_module.islibrary then
  295. exit;
  296. if cs_profile in current_settings.moduleswitches then
  297. begin
  298. prtobj:=gprtnames[libctype];
  299. sysinitunit:=gsinames[libctype];
  300. linklibc:=true;
  301. end
  302. else if linklibc then
  303. begin
  304. prtobj:=cprtnames[libctype];
  305. sysinitunit:=csinames[libctype];
  306. end;
  307. end;
  308. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  309. Var
  310. linkres : TLinkRes;
  311. i : longint;
  312. HPath : TCmdStrListItem;
  313. s,s1,s2 : TCmdStr;
  314. found1,
  315. found2 : boolean;
  316. linksToSharedLibFiles : boolean;
  317. begin
  318. result:=False;
  319. { set special options for some targets }
  320. if cs_profile in current_settings.moduleswitches then
  321. begin
  322. if not(libctype in [glibc2,glibc21]) then
  323. AddSharedLibrary('gmon');
  324. AddSharedLibrary('c');
  325. end;
  326. { Open link.res file }
  327. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  328. with linkres do
  329. begin
  330. { Write path to search libraries }
  331. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  332. while assigned(HPath) do
  333. begin
  334. Add('SEARCH_DIR("'+HPath.Str+'")');
  335. HPath:=TCmdStrListItem(HPath.Next);
  336. end;
  337. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  338. while assigned(HPath) do
  339. begin
  340. Add('SEARCH_DIR("'+HPath.Str+'")');
  341. HPath:=TCmdStrListItem(HPath.Next);
  342. end;
  343. { force local symbol resolution (i.e., inside the shared }
  344. { library itself) for all non-exorted symbols, otherwise }
  345. { several RTL symbols of FPC-compiled shared libraries }
  346. { will be bound to those of a single shared library or }
  347. { to the main program }
  348. if (isdll) then
  349. begin
  350. add('VERSION');
  351. add('{');
  352. add(' {');
  353. if not texportlibunix(exportlib).exportedsymnames.empty then
  354. begin
  355. add(' global:');
  356. repeat
  357. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  358. until texportlibunix(exportlib).exportedsymnames.empty;
  359. end;
  360. add(' local:');
  361. add(' *;');
  362. add(' };');
  363. add('}');
  364. end;
  365. StartSection('INPUT(');
  366. { add objectfiles, start with prt0 always }
  367. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  368. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  369. { try to add crti and crtbegin if linking to C }
  370. if linklibc and (libctype<>uclibc) then
  371. begin
  372. { crti.o must come first }
  373. if librarysearchpath.FindFile('crti.o',false,s) then
  374. AddFileName(s);
  375. { then the crtbegin* }
  376. if cs_create_pic in current_settings.moduleswitches then
  377. begin
  378. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  379. AddFileName(s);
  380. end
  381. else
  382. if (cs_link_staticflag in current_settings.globalswitches) and
  383. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  384. AddFileName(s)
  385. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  386. AddFileName(s);
  387. end;
  388. { main objectfiles }
  389. while not ObjectFiles.Empty do
  390. begin
  391. s:=ObjectFiles.GetFirst;
  392. if s<>'' then
  393. AddFileName(maybequoted(s));
  394. end;
  395. EndSection(')');
  396. { Write staticlibraries }
  397. if not StaticLibFiles.Empty then
  398. begin
  399. Add('GROUP(');
  400. While not StaticLibFiles.Empty do
  401. begin
  402. S:=StaticLibFiles.GetFirst;
  403. AddFileName(maybequoted(s))
  404. end;
  405. Add(')');
  406. end;
  407. // we must reorder here because the result could empty sharedlibfiles
  408. if reorder Then
  409. ExpandAndApplyOrder(SharedLibFiles);
  410. // after this point addition of shared libs not allowed.
  411. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  412. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  413. if (isdll) then
  414. begin
  415. Add('INPUT(');
  416. Add(info.DynamicLinker);
  417. Add(')');
  418. end;
  419. linksToSharedLibFiles := not SharedLibFiles.Empty;
  420. if not SharedLibFiles.Empty then
  421. begin
  422. if (SharedLibFiles.Count<>1) or
  423. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  424. reorder then
  425. begin
  426. Add('INPUT(');
  427. While not SharedLibFiles.Empty do
  428. begin
  429. S:=SharedLibFiles.GetFirst;
  430. if (s<>'c') or reorder then
  431. begin
  432. i:=Pos(target_info.sharedlibext,S);
  433. if i>0 then
  434. Delete(S,i,255);
  435. Add('-l'+s);
  436. end
  437. else
  438. begin
  439. linklibc:=true;
  440. end;
  441. end;
  442. Add(')');
  443. end
  444. else
  445. linklibc:=true;
  446. if (cs_link_staticflag in current_settings.globalswitches) or
  447. (linklibc and not reorder) then
  448. begin
  449. Add('GROUP(');
  450. { when we have -static for the linker the we also need libgcc }
  451. if (cs_link_staticflag in current_settings.globalswitches) then
  452. begin
  453. Add('-lgcc');
  454. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  455. Add('-lgcc_eh');
  456. end;
  457. { be sure that libc is the last lib }
  458. if linklibc and not reorder then
  459. Add('-lc');
  460. Add(')');
  461. end;
  462. end;
  463. { objects which must be at the end }
  464. if linklibc and (libctype<>uclibc) then
  465. begin
  466. if cs_create_pic in current_settings.moduleswitches then
  467. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  468. else
  469. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  470. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  471. if found1 or found2 then
  472. begin
  473. Add('INPUT(');
  474. if found1 then
  475. AddFileName(s1);
  476. if found2 then
  477. AddFileName(s2);
  478. Add(')');
  479. end;
  480. end;
  481. {Entry point. Only needed for executables, set on the linker command line for
  482. shared libraries. }
  483. if (not isdll) then
  484. if (linksToSharedLibFiles and not linklibc) then
  485. add('ENTRY(_dynamic_start)')
  486. else
  487. add('ENTRY(_start)');
  488. {$ifdef x86_64}
  489. {$define LINKERSCRIPT_INCLUDED}
  490. add('SECTIONS');
  491. add('{');
  492. {Read-only sections, merged into text segment:}
  493. if current_module.islibrary then
  494. add(' . = 0 + SIZEOF_HEADERS;')
  495. else
  496. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  497. add(' . = 0 + SIZEOF_HEADERS;');
  498. add(' .interp : { *(.interp) }');
  499. add(' .hash : { *(.hash) }');
  500. add(' .dynsym : { *(.dynsym) }');
  501. add(' .dynstr : { *(.dynstr) }');
  502. add(' .gnu.version : { *(.gnu.version) }');
  503. add(' .gnu.version_d : { *(.gnu.version_d) }');
  504. add(' .gnu.version_r : { *(.gnu.version_r) }');
  505. add(' .rel.dyn :');
  506. add(' {');
  507. add(' *(.rel.init)');
  508. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  509. add(' *(.rel.fini)');
  510. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  511. add(' *(.rel.data.rel.ro*)');
  512. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  513. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  514. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  515. add(' *(.rel.got)');
  516. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  517. add(' }');
  518. add(' .rela.dyn :');
  519. add(' {');
  520. add(' *(.rela.init)');
  521. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  522. add(' *(.rela.fini)');
  523. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  524. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  525. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  526. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  527. add(' *(.rela.got)');
  528. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  529. add(' }');
  530. add(' .rel.plt : { *(.rel.plt) }');
  531. add(' .rela.plt : { *(.rela.plt) }');
  532. add(' .init :');
  533. add(' {');
  534. add(' KEEP (*(.init))');
  535. add(' } =0x90909090');
  536. add(' .plt : { *(.plt) }');
  537. add(' .text :');
  538. add(' {');
  539. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  540. add(' KEEP (*(.text.*personality*))');
  541. {.gnu.warning sections are handled specially by elf32.em.}
  542. add(' *(.gnu.warning)');
  543. add(' } =0x90909090');
  544. add(' .fini :');
  545. add(' {');
  546. add(' KEEP (*(.fini))');
  547. add(' } =0x90909090');
  548. add(' PROVIDE (_etext = .);');
  549. add(' .rodata :');
  550. add(' {');
  551. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  552. add(' }');
  553. {Adjust the address for the data segment. We want to adjust up to
  554. the same address within the page on the next page up.}
  555. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  556. add(' .dynamic : { *(.dynamic) }');
  557. add(' .got : { *(.got .toc) }');
  558. add(' .got.plt : { *(.got.plt .toc.plt) }');
  559. add(' .data :');
  560. add(' {');
  561. add(' *(.data .data.* .gnu.linkonce.d.*)');
  562. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  563. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  564. add(' }');
  565. add(' PROVIDE (_edata = .);');
  566. add(' PROVIDE (edata = .);');
  567. {$ifdef zsegment_threadvars}
  568. add(' _z = .;');
  569. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  570. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  571. add(' . = _z + SIZEOF (.threadvar);');
  572. {$else}
  573. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  574. {$endif}
  575. add(' __bss_start = .;');
  576. add(' .bss :');
  577. add(' {');
  578. add(' *(.dynbss)');
  579. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  580. add(' *(COMMON)');
  581. {Align here to ensure that the .bss section occupies space up to
  582. _end. Align after .bss to ensure correct alignment even if the
  583. .bss section disappears because there are no input sections.}
  584. add(' . = ALIGN(32 / 8);');
  585. add(' }');
  586. add(' . = ALIGN(32 / 8);');
  587. add(' PROVIDE (_end = .);');
  588. add(' PROVIDE (end = .);');
  589. {Stabs debugging sections.}
  590. add(' .stab 0 : { *(.stab) }');
  591. add(' .stabstr 0 : { *(.stabstr) }');
  592. add(' /* DWARF debug sections.');
  593. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  594. add(' of the section so we begin them at 0. */');
  595. add(' /* DWARF 1 */');
  596. add(' .debug 0 : { *(.debug) }');
  597. add(' .line 0 : { *(.line) }');
  598. add(' /* GNU DWARF 1 extensions */');
  599. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  600. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  601. add(' /* DWARF 1.1 and DWARF 2 */');
  602. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  603. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  604. add(' /* DWARF 2 */');
  605. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  606. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  607. add(' .debug_line 0 : { *(.debug_line) }');
  608. add(' .debug_frame 0 : { *(.debug_frame) }');
  609. add(' .debug_str 0 : { *(.debug_str) }');
  610. add(' .debug_loc 0 : { *(.debug_loc) }');
  611. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  612. add(' /* SGI/MIPS DWARF 2 extensions */');
  613. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  614. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  615. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  616. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  617. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  618. add('}');
  619. {$endif x86_64}
  620. {$ifdef ARM}
  621. if target_info.abi=abi_eabi then
  622. begin
  623. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  624. add('/* Script for -z combreloc: combine and sort reloc sections */');
  625. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  626. add(' "elf32-littlearm")');
  627. add('OUTPUT_ARCH(arm)');
  628. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  629. add('SECTIONS');
  630. add('{');
  631. add(' /* Read-only sections, merged into text segment: */');
  632. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  633. add(' .interp : { *(.interp) }');
  634. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  635. add(' .hash : { *(.hash) }');
  636. add(' .gnu.hash : { *(.gnu.hash) }');
  637. add(' .dynsym : { *(.dynsym) }');
  638. add(' .dynstr : { *(.dynstr) }');
  639. add(' .gnu.version : { *(.gnu.version) }');
  640. add(' .gnu.version_d : { *(.gnu.version_d) }');
  641. add(' .gnu.version_r : { *(.gnu.version_r) }');
  642. add(' .rel.dyn :');
  643. add(' {');
  644. add(' *(.rel.init)');
  645. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  646. add(' *(.rel.fini)');
  647. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  648. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  649. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  650. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  651. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  652. add(' *(.rel.ctors)');
  653. add(' *(.rel.dtors)');
  654. add(' *(.rel.got)');
  655. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  656. add(' }');
  657. add(' .rela.dyn :');
  658. add(' {');
  659. add(' *(.rela.init)');
  660. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  661. add(' *(.rela.fini)');
  662. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  663. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  664. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  665. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  666. add(' *(.rela.ctors)');
  667. add(' *(.rela.dtors)');
  668. add(' *(.rela.got)');
  669. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  670. add(' }');
  671. add(' .rel.plt : { *(.rel.plt) }');
  672. add(' .rela.plt : { *(.rela.plt) }');
  673. add(' .init :');
  674. add(' {');
  675. add(' KEEP (*(.init))');
  676. add(' } =0');
  677. add(' .plt : { *(.plt) }');
  678. add(' .text :');
  679. add(' {');
  680. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  681. add(' KEEP (*(.text.*personality*))');
  682. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  683. add(' *(.gnu.warning)');
  684. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  685. add(' } =0');
  686. add(' .fini :');
  687. add(' {');
  688. add(' KEEP (*(.fini))');
  689. add(' } =0');
  690. add(' PROVIDE (__etext = .);');
  691. add(' PROVIDE (_etext = .);');
  692. add(' PROVIDE (etext = .);');
  693. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  694. add(' .rodata1 : { *(.rodata1) }');
  695. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  696. add(' __exidx_start = .;');
  697. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  698. add(' __exidx_end = .;');
  699. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  700. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  701. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  702. add(' /* Adjust the address for the data segment. We want to adjust up to');
  703. add(' the same address within the page on the next page up. */');
  704. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  705. add(' /* Exception handling */');
  706. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  707. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  708. add(' /* Thread Local Storage sections */');
  709. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  710. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  711. add(' .preinit_array :');
  712. add(' {');
  713. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  714. add(' KEEP (*(.preinit_array))');
  715. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  716. add(' }');
  717. add(' .init_array :');
  718. add(' {');
  719. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  720. add(' KEEP (*(SORT(.init_array.*)))');
  721. add(' KEEP (*(.init_array))');
  722. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  723. add(' }');
  724. add(' .fini_array :');
  725. add(' {');
  726. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  727. add(' KEEP (*(.fini_array))');
  728. add(' KEEP (*(SORT(.fini_array.*)))');
  729. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  730. add(' }');
  731. add(' .ctors :');
  732. add(' {');
  733. add(' /* gcc uses crtbegin.o to find the start of');
  734. add(' the constructors, so we make sure it is');
  735. add(' first. Because this is a wildcard, it');
  736. add(' doesn''t matter if the user does not');
  737. add(' actually link against crtbegin.o; the');
  738. add(' linker won''t look for a file to match a');
  739. add(' wildcard. The wildcard also means that it');
  740. add(' doesn''t matter which directory crtbegin.o');
  741. add(' is in. */');
  742. add(' KEEP (*crtbegin.o(.ctors))');
  743. add(' KEEP (*crtbegin?.o(.ctors))');
  744. add(' /* We don''t want to include the .ctor section from');
  745. add(' the crtend.o file until after the sorted ctors.');
  746. add(' The .ctor section from the crtend file contains the');
  747. add(' end of ctors marker and it must be last */');
  748. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  749. add(' KEEP (*(SORT(.ctors.*)))');
  750. add(' KEEP (*(.ctors))');
  751. add(' }');
  752. add(' .dtors :');
  753. add(' {');
  754. add(' KEEP (*crtbegin.o(.dtors))');
  755. add(' KEEP (*crtbegin?.o(.dtors))');
  756. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  757. add(' KEEP (*(SORT(.dtors.*)))');
  758. add(' KEEP (*(.dtors))');
  759. add(' }');
  760. add(' .jcr : { KEEP (*(.jcr)) }');
  761. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  762. add(' .dynamic : { *(.dynamic) }');
  763. add(' .got : { *(.got.plt) *(.got) }');
  764. add(' .data :');
  765. add(' {');
  766. add(' __data_start = . ;');
  767. add(' *(.data .data.* .gnu.linkonce.d.*)');
  768. { extra by FPC }
  769. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  770. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  771. add(' SORT(CONSTRUCTORS)');
  772. add(' }');
  773. add(' .data1 : { *(.data1) }');
  774. add(' _edata = .; PROVIDE (edata = .);');
  775. add(' __bss_start = .;');
  776. add(' __bss_start__ = .;');
  777. add(' .bss :');
  778. add(' {');
  779. add(' *(.dynbss)');
  780. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  781. add(' *(COMMON)');
  782. add(' /* Align here to ensure that the .bss section occupies space up to');
  783. add(' _end. Align after .bss to ensure correct alignment even if the');
  784. add(' .bss section disappears because there are no input sections.');
  785. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  786. add(' pad the .data section. */');
  787. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  788. add(' }');
  789. add(' _bss_end__ = . ; __bss_end__ = . ;');
  790. add(' . = ALIGN(32 / 8);');
  791. add(' . = ALIGN(32 / 8);');
  792. add(' __end__ = . ;');
  793. add(' _end = .; PROVIDE (end = .);');
  794. add(' /* Stabs debugging sections. */');
  795. add(' .stab 0 : { *(.stab) }');
  796. add(' .stabstr 0 : { *(.stabstr) }');
  797. add(' .stab.excl 0 : { *(.stab.excl) }');
  798. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  799. add(' .stab.index 0 : { *(.stab.index) }');
  800. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  801. add(' .comment 0 : { *(.comment) }');
  802. add(' /* DWARF debug sections.');
  803. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  804. add(' of the section so we begin them at 0. */');
  805. add(' /* DWARF 1 */');
  806. add(' .debug 0 : { *(.debug) }');
  807. add(' .line 0 : { *(.line) }');
  808. add(' /* GNU DWARF 1 extensions */');
  809. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  810. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  811. add(' /* DWARF 1.1 and DWARF 2 */');
  812. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  813. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  814. add(' /* DWARF 2 */');
  815. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  816. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  817. add(' .debug_line 0 : { *(.debug_line) }');
  818. add(' .debug_frame 0 : { *(.debug_frame) }');
  819. add(' .debug_str 0 : { *(.debug_str) }');
  820. add(' .debug_loc 0 : { *(.debug_loc) }');
  821. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  822. add(' /* SGI/MIPS DWARF 2 extensions */');
  823. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  824. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  825. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  826. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  827. add(' /* DWARF 3 */');
  828. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  829. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  830. add(' .stack 0x80000 :');
  831. add(' {');
  832. add(' _stack = .;');
  833. add(' *(.stack)');
  834. add(' }');
  835. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  836. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  837. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  838. add('}');
  839. end
  840. else
  841. {$endif ARM}
  842. {$ifndef LINKERSCRIPT_INCLUDED}
  843. begin
  844. {Sections.}
  845. add('SECTIONS');
  846. add('{');
  847. {Read-only sections, merged into text segment:}
  848. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 + SIZEOF_HEADERS;');
  849. add(' .interp : { *(.interp) }');
  850. add(' .hash : { *(.hash) }');
  851. add(' .dynsym : { *(.dynsym) }');
  852. add(' .dynstr : { *(.dynstr) }');
  853. add(' .gnu.version : { *(.gnu.version) }');
  854. add(' .gnu.version_d : { *(.gnu.version_d) }');
  855. add(' .gnu.version_r : { *(.gnu.version_r) }');
  856. add(' .rel.dyn :');
  857. add(' {');
  858. add(' *(.rel.init)');
  859. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  860. add(' *(.rel.fini)');
  861. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  862. add(' *(.rel.data.rel.ro*)');
  863. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  864. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  865. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  866. add(' *(.rel.got)');
  867. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  868. add(' }');
  869. add(' .rela.dyn :');
  870. add(' {');
  871. add(' *(.rela.init)');
  872. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  873. add(' *(.rela.fini)');
  874. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  875. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  876. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  877. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  878. add(' *(.rela.got)');
  879. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  880. add(' }');
  881. add(' .rel.plt : { *(.rel.plt) }');
  882. add(' .rela.plt : { *(.rela.plt) }');
  883. add(' .init :');
  884. add(' {');
  885. add(' KEEP (*(.init))');
  886. add(' } =0x90909090');
  887. add(' .plt : { *(.plt) }');
  888. add(' .text :');
  889. add(' {');
  890. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  891. add(' KEEP (*(.text.*personality*))');
  892. {.gnu.warning sections are handled specially by elf32.em.}
  893. add(' *(.gnu.warning)');
  894. add(' } =0x90909090');
  895. add(' .fini :');
  896. add(' {');
  897. add(' KEEP (*(.fini))');
  898. add(' } =0x90909090');
  899. add(' PROVIDE (_etext = .);');
  900. add(' .rodata :');
  901. add(' {');
  902. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  903. add(' }');
  904. {Adjust the address for the data segment. We want to adjust up to
  905. the same address within the page on the next page up.}
  906. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  907. add(' .dynamic : { *(.dynamic) }');
  908. add(' .got : { *(.got) }');
  909. add(' .got.plt : { *(.got.plt) }');
  910. add(' .data :');
  911. add(' {');
  912. add(' *(.data .data.* .gnu.linkonce.d.*)');
  913. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  914. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  915. add(' }');
  916. add(' PROVIDE (_edata = .);');
  917. add(' PROVIDE (edata = .);');
  918. {$ifdef zsegment_threadvars}
  919. add(' _z = .;');
  920. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  921. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  922. add(' . = _z + SIZEOF (.threadvar);');
  923. {$else}
  924. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  925. {$endif}
  926. add(' __bss_start = .;');
  927. add(' .bss :');
  928. add(' {');
  929. add(' *(.dynbss)');
  930. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  931. add(' *(COMMON)');
  932. {Align here to ensure that the .bss section occupies space up to
  933. _end. Align after .bss to ensure correct alignment even if the
  934. .bss section disappears because there are no input sections.}
  935. add(' . = ALIGN(32 / 8);');
  936. add(' }');
  937. add(' . = ALIGN(32 / 8);');
  938. add(' PROVIDE (_end = .);');
  939. add(' PROVIDE (end = .);');
  940. {Stabs debugging sections.}
  941. add(' .stab 0 : { *(.stab) }');
  942. add(' .stabstr 0 : { *(.stabstr) }');
  943. add('}');
  944. end;
  945. {$endif LINKERSCRIPT_INCLUDED}
  946. { Write and Close response }
  947. writetodisk;
  948. Free;
  949. end;
  950. WriteResponseFile:=True;
  951. end;
  952. function TLinkerLinux.MakeExecutable:boolean;
  953. var
  954. i : longint;
  955. binstr,
  956. cmdstr : TCmdStr;
  957. success : boolean;
  958. DynLinkStr : string;
  959. GCSectionsStr,
  960. StaticStr,
  961. StripStr : string[40];
  962. begin
  963. if not(cs_link_nolink in current_settings.globalswitches) then
  964. Message1(exec_i_linking,current_module.exefilename);
  965. { Create some replacements }
  966. StaticStr:='';
  967. StripStr:='';
  968. GCSectionsStr:='';
  969. DynLinkStr:='';
  970. if (cs_link_staticflag in current_settings.globalswitches) then
  971. StaticStr:='-static';
  972. if (cs_link_strip in current_settings.globalswitches) and
  973. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  974. StripStr:='-s';
  975. if (cs_link_map in current_settings.globalswitches) then
  976. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  977. if create_smartlink_sections then
  978. GCSectionsStr:='--gc-sections';
  979. If (cs_profile in current_settings.moduleswitches) or
  980. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  981. begin
  982. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  983. if cshared then
  984. DynLinkStr:=DynLinkStr+' --shared ';
  985. if rlinkpath<>'' then
  986. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  987. End;
  988. { Write used files and libraries }
  989. WriteResponseFile(false);
  990. { Call linker }
  991. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  992. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  993. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  994. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  995. Replace(cmdstr,'$STATIC',StaticStr);
  996. Replace(cmdstr,'$STRIP',StripStr);
  997. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  998. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  999. { create dynamic symbol table? }
  1000. if HasExports then
  1001. cmdstr:=cmdstr+' -E';
  1002. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1003. { Create external .dbg file with debuginfo }
  1004. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  1005. begin
  1006. for i:=1 to 3 do
  1007. begin
  1008. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  1009. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  1010. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  1011. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  1012. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1013. if not success then
  1014. break;
  1015. end;
  1016. end;
  1017. { Remove ReponseFile }
  1018. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1019. DeleteFile(outputexedir+Info.ResName);
  1020. MakeExecutable:=success; { otherwise a recursive call to link method }
  1021. end;
  1022. Function TLinkerLinux.MakeSharedLibrary:boolean;
  1023. var
  1024. InitStr,
  1025. FiniStr,
  1026. SoNameStr : string[80];
  1027. binstr,
  1028. cmdstr : TCmdStr;
  1029. success : boolean;
  1030. begin
  1031. MakeSharedLibrary:=false;
  1032. if not(cs_link_nolink in current_settings.globalswitches) then
  1033. Message1(exec_i_linking,current_module.sharedlibfilename);
  1034. { Write used files and libraries }
  1035. WriteResponseFile(true);
  1036. { Create some replacements }
  1037. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  1038. InitStr:='-init FPC_SHARED_LIB_START';
  1039. FiniStr:='-fini FPC_LIB_EXIT';
  1040. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  1041. { Call linker }
  1042. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1043. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1044. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1045. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1046. Replace(cmdstr,'$INIT',InitStr);
  1047. Replace(cmdstr,'$FINI',FiniStr);
  1048. Replace(cmdstr,'$SONAME',SoNameStr);
  1049. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1050. { Strip the library ? }
  1051. if success and (cs_link_strip in current_settings.globalswitches) then
  1052. begin
  1053. { only remove non global symbols and debugging info for a library }
  1054. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1055. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1056. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1057. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1058. end;
  1059. { Remove ReponseFile }
  1060. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1061. DeleteFile(outputexedir+Info.ResName);
  1062. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1063. end;
  1064. {*****************************************************************************
  1065. TINTERNALLINKERLINUX
  1066. *****************************************************************************}
  1067. constructor TInternalLinkerLinux.Create;
  1068. begin
  1069. inherited Create;
  1070. SetupLibrarySearchPath;
  1071. SetupDynlinker(dynlinker,libctype);
  1072. CExeOutput:=ElfExeOutputClass;
  1073. CObjInput:=TElfObjInput;
  1074. end;
  1075. procedure TInternalLinkerLinux.InitSysInitUnitName;
  1076. begin
  1077. linklibc:=ModulesLinkToLibc;
  1078. reorder:=linklibc and ReOrderEntries;
  1079. sysinitunit:=defsinames[current_module.islibrary];
  1080. prtobj:=defprtnames[current_module.islibrary];
  1081. if cs_profile in current_settings.moduleswitches then
  1082. begin
  1083. prtobj:=gprtnames[libctype];
  1084. sysinitunit:=gsinames[libctype];
  1085. linklibc:=true;
  1086. end
  1087. else if linklibc then
  1088. begin
  1089. prtobj:=cprtnames[libctype];
  1090. sysinitunit:=csinames[libctype];
  1091. end;
  1092. end;
  1093. const
  1094. relsec_prefix:array[boolean] of TCmdStr = ('rel','rela');
  1095. procedure TInternalLinkerLinux.DefaultLinkScript;
  1096. var
  1097. s,s1,s2,relprefix:TCmdStr;
  1098. found1,found2:boolean;
  1099. linkToSharedLibs:boolean;
  1100. procedure AddLibraryStatement(const s:TCmdStr);
  1101. var
  1102. i:longint;
  1103. s1,s2:TCmdStr;
  1104. begin
  1105. i:=pos(target_info.sharedClibext+'.',s);
  1106. if (i>0) then
  1107. s1:=target_info.sharedClibprefix+S
  1108. else
  1109. s1:=target_info.sharedClibprefix+S+target_info.sharedClibext;
  1110. { TODO: to be compatible with ld search algorithm, each found file
  1111. must be tested for target compatibility, incompatible ones should be skipped. }
  1112. { TODO: shall we search library without suffix if one with suffix is not found? }
  1113. if (not(cs_link_staticflag in current_settings.globalswitches)) and
  1114. FindLibraryFile(s1,'','',s2) then
  1115. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1116. { TODO: static libraries never have numeric suffix in their names }
  1117. else if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  1118. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1119. else
  1120. Comment(V_Error,'Import library not found for '+S);
  1121. end;
  1122. begin
  1123. if cs_profile in current_settings.moduleswitches then
  1124. begin
  1125. if not(libctype in [glibc2,glibc21]) then
  1126. AddSharedLibrary('gmon');
  1127. AddSharedLibrary('c');
  1128. end;
  1129. TElfExeOutput(exeoutput).interpreter:=stringdup(dynlinker);
  1130. { add objectfiles, start with prt0 always }
  1131. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  1132. LinkScript.Concat('READOBJECT '+ maybequoted(FindObjectFile(prtobj,'',false)));
  1133. { try to add crti and crtbegin if linking to C }
  1134. if linklibc and (libctype<>uclibc) then
  1135. begin
  1136. { crti.o must come first }
  1137. if librarysearchpath.FindFile('crti.o',false,s) then
  1138. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1139. { then the crtbegin* }
  1140. if cs_create_pic in current_settings.moduleswitches then
  1141. begin
  1142. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  1143. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1144. end
  1145. else
  1146. if (cs_link_staticflag in current_settings.globalswitches) and
  1147. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  1148. LinkScript.Concat('READOBJECT '+maybequoted(s))
  1149. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  1150. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1151. end;
  1152. ScriptAddSourceStatements(false);
  1153. { we must reorder here because the result could empty sharedlibfiles }
  1154. if reorder then
  1155. ExpandAndApplyOrder(SharedLibFiles);
  1156. { See tw9089*.pp: if more than one pure-Pascal shared libs are loaded,
  1157. and none have rtld in their DT_NEEDED, then rtld cannot finalize correctly. }
  1158. if IsSharedLibrary then
  1159. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(dynlinker));
  1160. linkToSharedLibs:=(not SharedLibFiles.Empty);
  1161. { Symbols declared as "external 'libx.so'" are added to ImportLibraryList, library
  1162. prefix/extension *not* stripped. TImportLibLinux copies these to SharedLibFiles,
  1163. stripping prefixes and extensions.
  1164. However extension won't be stripped if library is specified with numeric suffix
  1165. (like "libpango-1.0.so.0")
  1166. Libraries specified with $LINKLIB directive are directly added to SharedLibFiles
  1167. and won't be present in ImportLibraryList. }
  1168. while not SharedLibFiles.Empty do
  1169. begin
  1170. S:=SharedLibFiles.GetFirst;
  1171. if (S<>'c') or reorder then
  1172. AddLibraryStatement(S);
  1173. end;
  1174. if (cs_link_staticflag in current_settings.globalswitches) or
  1175. (linklibc and not reorder) then
  1176. begin
  1177. LinkScript.Concat('GROUP');
  1178. if (cs_link_staticflag in current_settings.globalswitches) then
  1179. begin
  1180. AddLibraryStatement('gcc');
  1181. AddLibraryStatement('gcc_eh');
  1182. end;
  1183. if linklibc and not reorder then
  1184. AddLibraryStatement('c');
  1185. LinkScript.Concat('ENDGROUP');
  1186. end;
  1187. { objects which must be at the end }
  1188. if linklibc and (libctype<>uclibc) then
  1189. begin
  1190. if cs_create_pic in current_settings.moduleswitches then
  1191. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  1192. else
  1193. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  1194. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  1195. if found1 then
  1196. LinkScript.Concat('READOBJECT '+maybequoted(s1));
  1197. if found2 then
  1198. LinkScript.Concat('READOBJECT '+maybequoted(s2));
  1199. end;
  1200. if (not IsSharedLibrary) then
  1201. if (linkToSharedLibs and not linklibc) then
  1202. LinkScript.Concat('ENTRYNAME _dynamic_start')
  1203. else
  1204. LinkScript.Concat('ENTRYNAME _start')
  1205. else
  1206. LinkScript.Concat('ISSHAREDLIBRARY');
  1207. relprefix:=relsec_prefix[ElfTarget.relocs_use_addend];
  1208. with LinkScript do
  1209. begin
  1210. Concat('HEADER');
  1211. Concat('EXESECTION .interp');
  1212. Concat(' OBJSECTION .interp');
  1213. Concat('ENDEXESECTION');
  1214. Concat('EXESECTION .note.ABI-tag');
  1215. Concat(' OBJSECTION .note.ABI-tag');
  1216. Concat('ENDEXESECTION');
  1217. Concat('EXESECTION .note.gnu.build-id');
  1218. Concat(' OBJSECTION .note.gnu.build-id');
  1219. Concat('ENDEXESECTION');
  1220. Concat('EXESECTION .hash');
  1221. Concat(' OBJSECTION .hash');
  1222. Concat('ENDEXESECTION');
  1223. Concat('EXESECTION .dynsym');
  1224. Concat(' OBJSECTION .dynsym');
  1225. Concat('ENDEXESECTION');
  1226. Concat('EXESECTION .dynstr');
  1227. Concat(' OBJSECTION .dynstr');
  1228. Concat('ENDEXESECTION');
  1229. Concat('EXESECTION .gnu.version');
  1230. Concat(' OBJSECTION .gnu.version');
  1231. Concat('ENDEXESECTION');
  1232. Concat('EXESECTION .gnu.version_d');
  1233. Concat(' OBJSECTION .gnu.version_d');
  1234. Concat('ENDEXESECTION');
  1235. Concat('EXESECTION .gnu.version_r');
  1236. Concat(' OBJSECTION .gnu.version_r');
  1237. Concat('ENDEXESECTION');
  1238. Concat('EXESECTION .'+relprefix+'.dyn');
  1239. Concat(' OBJSECTION .'+relprefix+'.dyn');
  1240. Concat('ENDEXESECTION');
  1241. Concat('EXESECTION .'+relprefix+'.plt');
  1242. Concat(' OBJSECTION .'+relprefix+'.plt');
  1243. Concat(' PROVIDE __'+relprefix+'_iplt_start');
  1244. Concat(' OBJSECTION .'+relprefix+'.iplt');
  1245. Concat(' PROVIDE __'+relprefix+'_iplt_end');
  1246. Concat('ENDEXESECTION');
  1247. Concat('EXESECTION .init');
  1248. Concat(' OBJSECTION .init');
  1249. Concat('ENDEXESECTION');
  1250. Concat('EXESECTION .plt');
  1251. Concat(' OBJSECTION .plt');
  1252. Concat('ENDEXESECTION');
  1253. Concat('EXESECTION .text');
  1254. Concat(' OBJSECTION .text*');
  1255. Concat('ENDEXESECTION');
  1256. { This is not in standard ld scripts, it is handled by 'orphan section' functionality }
  1257. Concat('EXESECTION __libc_thread_freeres_fn');
  1258. Concat(' PROVIDE __start__libc_thread_freeres_fn');
  1259. Concat(' OBJSECTION __libc_thread_freeres_fn');
  1260. Concat(' PROVIDE __stop__libc_thread_freeres_fn');
  1261. Concat('ENDEXESECTION');
  1262. Concat('EXESECTION __libc_freeres_fn');
  1263. Concat(' PROVIDE __start__libc_freeres_fn');
  1264. Concat(' OBJSECTION __libc_freeres_fn');
  1265. Concat(' PROVIDE __stop__libc_freeres_fn');
  1266. Concat('ENDEXESECTION');
  1267. Concat('EXESECTION .fini');
  1268. Concat(' OBJSECTION .fini');
  1269. Concat(' PROVIDE __etext');
  1270. Concat(' PROVIDE _etext');
  1271. Concat(' PROVIDE etext');
  1272. Concat('ENDEXESECTION');
  1273. Concat('EXESECTION .rodata');
  1274. Concat(' OBJSECTION .rodata*');
  1275. Concat('ENDEXESECTION');
  1276. {$ifdef arm}
  1277. Concat('EXESECTION .ARM.extab');
  1278. Concat(' OBJSECTION .ARM.extab*');
  1279. Concat('ENDEXESECTION');
  1280. Concat('EXESECTION .ARM.exidx');
  1281. Concat(' SYMBOL __exidx_start');
  1282. Concat(' OBJSECTION .ARM.exidx*');
  1283. Concat(' SYMBOL __exidx_end');
  1284. Concat('ENDEXESECTION');
  1285. {$endif}
  1286. Concat('EXESECTION .eh_frame');
  1287. Concat(' OBJSECTION .eh_frame');
  1288. Concat('ENDEXESECTION');
  1289. Concat('EXESECTION .gcc_except_table');
  1290. Concat(' OBJSECTION .gcc_except_table');
  1291. Concat(' OBJSECTION .gcc_except_table.*');
  1292. Concat('ENDEXESECTION');
  1293. Concat('EXESECTION .tdata');
  1294. Concat(' OBJSECTION .tdata');
  1295. Concat(' OBJSECTION .tdata.*');
  1296. Concat('ENDEXESECTION');
  1297. Concat('EXESECTION .tbss');
  1298. Concat(' OBJSECTION .tbss');
  1299. Concat(' OBJSECTION .tbss.*');
  1300. Concat('ENDEXESECTION');
  1301. Concat('EXESECTION .preinit_array');
  1302. Concat(' PROVIDE __preinit_array_start');
  1303. Concat(' OBJSECTION .preinit_array');
  1304. Concat(' PROVIDE __preinit_array_end');
  1305. Concat('ENDEXESECTION');
  1306. Concat('EXESECTION .init_array');
  1307. Concat(' PROVIDE __init_array_start');
  1308. { why the hell .ctors are both here and exesection .ctors below?? }
  1309. // KEEP ( *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
  1310. Concat(' OBJSECTION .init_array');
  1311. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
  1312. Concat('PROVIDE __init_array_end');
  1313. Concat('ENDEXESECTION');
  1314. Concat('EXESECTION .fini_array');
  1315. Concat(' PROVIDE __fini_array_start');
  1316. // KEEP ( *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
  1317. Concat(' OBJSECTION .fini_array');
  1318. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
  1319. Concat(' PROVIDE __fini_array_end');
  1320. Concat('ENDEXESECTION');
  1321. Concat('EXESECTION .ctors');
  1322. Concat(' OBJSECTION .ctors*');
  1323. Concat('ENDEXESECTION');
  1324. Concat('EXESECTION .dtors');
  1325. Concat(' OBJSECTION .dtors*');
  1326. Concat('ENDEXESECTION');
  1327. Concat('EXESECTION .jcr');
  1328. Concat(' OBJSECTION .jcr');
  1329. Concat('ENDEXESECTION');
  1330. Concat('EXESECTION .dynamic');
  1331. Concat(' OBJSECTION .dynamic');
  1332. Concat('ENDEXESECTION');
  1333. Concat('EXESECTION .got');
  1334. {$ifdef arm}
  1335. Concat(' OBJSECTION .got.plt');
  1336. {$endif arm}
  1337. Concat(' OBJSECTION .got');
  1338. Concat('ENDEXESECTION');
  1339. {$ifndef arm}
  1340. Concat('EXESECTION .got.plt');
  1341. Concat(' OBJSECTION .got.plt');
  1342. Concat('ENDEXESECTION');
  1343. {$endif arm}
  1344. Concat('EXESECTION .data');
  1345. Concat(' OBJSECTION .data*');
  1346. Concat(' OBJSECTION .fpc*');
  1347. Concat(' OBJSECTION fpc.resources');
  1348. Concat(' PROVIDE _edata');
  1349. Concat(' PROVIDE edata');
  1350. Concat('ENDEXESECTION');
  1351. Concat('EXESECTION .bss');
  1352. Concat(' OBJSECTION .dynbss');
  1353. Concat(' OBJSECTION .bss*');
  1354. Concat(' OBJSECTION fpc.reshandles');
  1355. Concat(' PROVIDE end');
  1356. Concat(' SYMBOL _end');
  1357. Concat('ENDEXESECTION');
  1358. { This is not in standard ld scripts, it is handled by 'orphan section' functionality }
  1359. Concat('EXESECTION __libc_freeres_ptrs');
  1360. Concat(' PROVIDE __start__libc_freeres_ptrs');
  1361. Concat(' OBJSECTION __libc_freeres_ptrs');
  1362. Concat(' PROVIDE __stop__libc_freeres_ptrs');
  1363. Concat('ENDEXESECTION');
  1364. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,'+
  1365. '.debug_abbrev,.debug_line,.debug_frame,.debug_str,.debug_loc,'+
  1366. '.debug_macinfo,.debug_weaknames,.debug_funcnames,.debug_typenames,.debug_varnames,.debug_ranges');
  1367. Concat('EXESECTION .stab');
  1368. Concat(' OBJSECTION .stab');
  1369. Concat('ENDEXESECTION');
  1370. Concat('EXESECTION .stabstr');
  1371. Concat(' OBJSECTION .stabstr');
  1372. Concat('ENDEXESECTION');
  1373. end;
  1374. end;
  1375. {*****************************************************************************
  1376. Initialize
  1377. *****************************************************************************}
  1378. initialization
  1379. {$ifdef i386}
  1380. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  1381. RegisterImport(system_i386_linux,timportliblinux);
  1382. RegisterExport(system_i386_linux,texportliblinux);
  1383. RegisterTarget(system_i386_linux_info);
  1384. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  1385. RegisterImport(system_x86_6432_linux,timportliblinux);
  1386. RegisterExport(system_x86_6432_linux,texportliblinux);
  1387. RegisterTarget(system_x86_6432_linux_info);
  1388. {$endif i386}
  1389. {$ifdef m68k}
  1390. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  1391. RegisterImport(system_m68k_linux,timportliblinux);
  1392. RegisterExport(system_m68k_linux,texportliblinux);
  1393. RegisterTarget(system_m68k_linux_info);
  1394. {$endif m68k}
  1395. {$ifdef powerpc}
  1396. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  1397. RegisterImport(system_powerpc_linux,timportliblinux);
  1398. RegisterExport(system_powerpc_linux,texportliblinux);
  1399. RegisterTarget(system_powerpc_linux_info);
  1400. {$endif powerpc}
  1401. {$ifdef powerpc64}
  1402. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  1403. RegisterImport(system_powerpc64_linux,timportliblinux);
  1404. RegisterExport(system_powerpc64_linux,texportliblinux);
  1405. RegisterTarget(system_powerpc64_linux_info);
  1406. {$endif powerpc64}
  1407. {$ifdef alpha}
  1408. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  1409. RegisterImport(system_alpha_linux,timportliblinux);
  1410. RegisterExport(system_alpha_linux,texportliblinux);
  1411. RegisterTarget(system_alpha_linux_info);
  1412. {$endif alpha}
  1413. {$ifdef x86_64}
  1414. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  1415. RegisterImport(system_x86_64_linux,timportliblinux);
  1416. RegisterExport(system_x86_64_linux,texportliblinux);
  1417. RegisterTarget(system_x86_64_linux_info);
  1418. {$endif x86_64}
  1419. {$ifdef SPARC}
  1420. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  1421. RegisterImport(system_SPARC_linux,timportliblinux);
  1422. RegisterExport(system_SPARC_linux,texportliblinux);
  1423. RegisterTarget(system_SPARC_linux_info);
  1424. {$endif SPARC}
  1425. {$ifdef ARM}
  1426. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  1427. RegisterImport(system_arm_linux,timportliblinux);
  1428. RegisterExport(system_arm_linux,texportliblinux);
  1429. RegisterTarget(system_arm_linux_info);
  1430. {$endif ARM}
  1431. {$ifdef MIPS}
  1432. {$ifdef MIPSEL}
  1433. RegisterExternalLinker(system_mipsel_linux_info,TLinkerLinux);
  1434. RegisterImport(system_mipsel_linux,timportliblinux);
  1435. RegisterExport(system_mipsel_linux,texportliblinux);
  1436. RegisterTarget(system_mipsel_linux_info);
  1437. {$else MIPS}
  1438. RegisterExternalLinker(system_mipseb_linux_info,TLinkerLinux);
  1439. RegisterImport(system_mipseb_linux,timportliblinux);
  1440. RegisterExport(system_mipseb_linux,texportliblinux);
  1441. RegisterTarget(system_mipseb_linux_info);
  1442. {$endif MIPSEL}
  1443. {$endif MIPS}
  1444. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1445. end.