t_linux.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. {
  2. Copyright (c) 1998-2002 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. tlinkerlinux=class(texternallinker)
  33. private
  34. libctype:(libc5,glibc2,glibc21,uclibc);
  35. cprtobj,
  36. gprtobj,
  37. prtobj : string[80];
  38. reorder : boolean;
  39. linklibc: boolean;
  40. Function WriteResponseFile(isdll:boolean) : Boolean;
  41. public
  42. constructor Create;override;
  43. procedure SetDefaultInfo;override;
  44. procedure InitSysInitUnitName;override;
  45. function MakeExecutable:boolean;override;
  46. function MakeSharedLibrary:boolean;override;
  47. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  48. procedure LoadPredefinedLibraryOrder; override;
  49. end;
  50. implementation
  51. uses
  52. SysUtils,
  53. cutils,cfileutils,cclasses,
  54. verbose,systems,globtype,globals,
  55. symconst,script,
  56. fmodule,
  57. aasmbase,aasmtai,aasmcpu,cpubase,
  58. cgbase,cgobj,cgutils,ogbase,ncgutil,
  59. comprsrc,
  60. i_linux
  61. ;
  62. {*****************************************************************************
  63. TIMPORTLIBLINUX
  64. *****************************************************************************}
  65. procedure timportliblinux.generatelib;
  66. var
  67. i : longint;
  68. ImportLibrary : TImportLibrary;
  69. begin
  70. for i:=0 to current_module.ImportLibraryList.Count-1 do
  71. begin
  72. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  73. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  74. end;
  75. end;
  76. {*****************************************************************************
  77. TEXPORTLIBLINUX
  78. *****************************************************************************}
  79. procedure texportliblinux.setfininame(list: TAsmList; const s: string);
  80. begin
  81. { the problem with not having a .fini section is that a finalization
  82. routine in regular code can get "smart" linked away -> reference it
  83. just like the debug info }
  84. list.concat(Tai_section.create(sec_fpc,'links',0));
  85. list.concat(Tai_const.Createname(s,0));
  86. inherited setfininame(list,s);
  87. end;
  88. {*****************************************************************************
  89. TLINKERLINUX
  90. *****************************************************************************}
  91. Constructor TLinkerLinux.Create;
  92. begin
  93. Inherited Create;
  94. if not Dontlinkstdlibpath Then
  95. {$ifdef x86_64}
  96. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  97. {$else}
  98. {$ifdef powerpc64}
  99. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  100. {$else powerpc64}
  101. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  102. {$endif powerpc64}
  103. {$endif x86_64}
  104. end;
  105. procedure TLinkerLinux.SetDefaultInfo;
  106. {
  107. This will also detect which libc version will be used
  108. }
  109. const
  110. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  111. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  112. {$ifdef powerpc}platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  113. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  114. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  115. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  116. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  117. var
  118. defdynlinker: string;
  119. begin
  120. with Info do
  121. begin
  122. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  123. { when we want to cross-link we need to override default library paths }
  124. if length(sysrootpath) > 0 then
  125. ExeCmd[1]:=ExeCmd[1]+' -T';
  126. ExeCmd[1]:=ExeCmd[1]+' $RES';
  127. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  128. DllCmd[2]:='strip --strip-unneeded $EXE';
  129. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  130. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  131. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  132. {$ifdef m68k}
  133. { experimental, is this correct? }
  134. defdynlinker:='/lib/ld-linux.so.2';
  135. {$endif m68k}
  136. {$ifdef i386}
  137. defdynlinker:='/lib/ld-linux.so.1';
  138. {$endif}
  139. {$ifdef x86_64}
  140. defdynlinker:='/lib64/ld-linux-x86-64.so.2';
  141. {$endif x86_64}
  142. {$ifdef sparc}
  143. defdynlinker:='/lib/ld-linux.so.2';
  144. {$endif sparc}
  145. {$ifdef powerpc}
  146. defdynlinker:='/lib/ld.so.1';
  147. {$endif powerpc}
  148. {$ifdef powerpc64}
  149. defdynlinker:='/lib64/ld64.so.1';
  150. {$endif powerpc64}
  151. {$ifdef arm}
  152. defdynlinker:='/lib/ld-linux.so.2';
  153. {$endif arm}
  154. {
  155. Search order:
  156. glibc 2.1+
  157. uclibc
  158. glibc 2.0
  159. If none is found (e.g. when cross compiling) glibc21 is assumed
  160. }
  161. {$ifdef i386}
  162. if FileExists(sysrootpath+'/lib/ld-linux.so.2',false) then
  163. begin
  164. DynamicLinker:='/lib/ld-linux.so.2';
  165. libctype:=glibc21;
  166. end
  167. else
  168. {$endif i386}
  169. if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  170. begin
  171. dynamiclinker:='/lib/ld-uClibc.so.0';
  172. libctype:=uclibc;
  173. end
  174. else if fileexists(sysrootpath+defdynlinker,false) then
  175. begin
  176. DynamicLinker:=defdynlinker;
  177. libctype:=glibc2;
  178. end
  179. else
  180. begin
  181. { when no dyn. linker is found, we are probably
  182. cross compiling, so use the default dyn. linker }
  183. DynamicLinker:=defdynlinker;
  184. {
  185. the default c startup script is gcrt0.as on all platforms
  186. except i386
  187. }
  188. {$ifdef i386}
  189. libctype:=glibc21;
  190. {$else i386}
  191. libctype:=glibc2;
  192. {$endif i386}
  193. end;
  194. end;
  195. end;
  196. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  197. // put your linkorder/linkalias overrides here.
  198. // Note: assumes only called when reordering/aliasing is used.
  199. Begin
  200. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  201. Begin
  202. LinkLibraryOrder.add('gcc','',15);
  203. LinkLibraryOrder.add('c','',100);
  204. LinkLibraryOrder.add('gmon','',120);
  205. LinkLibraryOrder.add('dl','',140);
  206. LinkLibraryOrder.add('pthread','',160);
  207. end;
  208. End;
  209. Procedure TLinkerLinux.InitSysInitUnitName;
  210. var
  211. csysinitunit,
  212. gsysinitunit : string[20];
  213. hp : tmodule;
  214. begin
  215. hp:=tmodule(loaded_units.first);
  216. while assigned(hp) do
  217. begin
  218. linklibc := hp.linkothersharedlibs.find('c');
  219. if linklibc then break;
  220. hp:=tmodule(hp.next);
  221. end;
  222. reorder := linklibc and ReOrderEntries;
  223. if current_module.islibrary then
  224. begin
  225. sysinitunit:='dll';
  226. csysinitunit:='dll';
  227. gsysinitunit:='dll';
  228. prtobj:='dllprt0';
  229. cprtobj:='dllprt0';
  230. gprtobj:='dllprt0';
  231. end
  232. else
  233. begin
  234. prtobj:='prt0';
  235. sysinitunit:='prc';
  236. case libctype of
  237. glibc21:
  238. begin
  239. cprtobj:='cprt21';
  240. gprtobj:='gprt21';
  241. csysinitunit:='c21';
  242. gsysinitunit:='c21g';
  243. end;
  244. uclibc:
  245. begin
  246. cprtobj:='ucprt0';
  247. gprtobj:='ugprt0';
  248. csysinitunit:='uc';
  249. gsysinitunit:='ucg';
  250. end
  251. else
  252. cprtobj:='cprt0';
  253. gprtobj:='gprt0';
  254. csysinitunit:='c';
  255. gsysinitunit:='g';
  256. end;
  257. end;
  258. if cs_profile in current_settings.moduleswitches then
  259. begin
  260. prtobj:=gprtobj;
  261. sysinitunit:=gsysinitunit;
  262. linklibc:=true;
  263. end
  264. else
  265. begin
  266. if linklibc then
  267. begin
  268. prtobj:=cprtobj;
  269. sysinitunit:=csysinitunit;
  270. end;
  271. end;
  272. sysinitunit:='si_'+sysinitunit;
  273. end;
  274. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  275. Var
  276. linkres : TLinkRes;
  277. i : longint;
  278. HPath : TCmdStrListItem;
  279. s,s1,s2 : TCmdStr;
  280. found1,
  281. found2 : boolean;
  282. begin
  283. result:=False;
  284. { set special options for some targets }
  285. if cs_profile in current_settings.moduleswitches then
  286. begin
  287. if not(libctype in [glibc2,glibc21]) then
  288. AddSharedLibrary('gmon');
  289. AddSharedLibrary('c');
  290. end;
  291. { Open link.res file }
  292. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  293. with linkres do
  294. begin
  295. { Write path to search libraries }
  296. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  297. while assigned(HPath) do
  298. begin
  299. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  300. HPath:=TCmdStrListItem(HPath.Next);
  301. end;
  302. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  303. while assigned(HPath) do
  304. begin
  305. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  306. HPath:=TCmdStrListItem(HPath.Next);
  307. end;
  308. { force local symbol resolution (i.e., inside the shared }
  309. { library itself) for all non-exorted symbols, otherwise }
  310. { several RTL symbols of FPC-compiled shared libraries }
  311. { will be bound to those of a single shared library or }
  312. { to the main program }
  313. if (isdll) then
  314. begin
  315. add('VERSION');
  316. add('{');
  317. add(' {');
  318. if not texportlibunix(exportlib).exportedsymnames.empty then
  319. begin
  320. add(' global:');
  321. repeat
  322. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  323. until texportlibunix(exportlib).exportedsymnames.empty;
  324. end;
  325. add(' local:');
  326. add(' *;');
  327. add(' };');
  328. add('}');
  329. end;
  330. StartSection('INPUT(');
  331. { add objectfiles, start with prt0 always }
  332. if not (target_info.system in system_internal_sysinit) and (prtobj<>'') then
  333. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  334. { try to add crti and crtbegin if linking to C }
  335. if linklibc and (libctype<>uclibc) then
  336. begin
  337. { x86_64 requires this to use entry/exit code with pic,
  338. see also issue #8210 regarding a discussion
  339. no idea about the other non i386 CPUs (FK)
  340. }
  341. {$ifdef x86_64}
  342. if current_module.islibrary then
  343. begin
  344. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  345. AddFileName(s);
  346. end
  347. else
  348. {$endif x86_64}
  349. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  350. AddFileName(s);
  351. if librarysearchpath.FindFile('crti.o',false,s) then
  352. AddFileName(s);
  353. end;
  354. { main objectfiles }
  355. while not ObjectFiles.Empty do
  356. begin
  357. s:=ObjectFiles.GetFirst;
  358. if s<>'' then
  359. AddFileName(maybequoted(s));
  360. end;
  361. EndSection(')');
  362. { Write staticlibraries }
  363. if not StaticLibFiles.Empty then
  364. begin
  365. Add('GROUP(');
  366. While not StaticLibFiles.Empty do
  367. begin
  368. S:=StaticLibFiles.GetFirst;
  369. AddFileName(maybequoted(s))
  370. end;
  371. Add(')');
  372. end;
  373. // we must reorder here because the result could empty sharedlibfiles
  374. if reorder Then
  375. ExpandAndApplyOrder(SharedLibFiles);
  376. // after this point addition of shared libs not allowed.
  377. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  378. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  379. if not SharedLibFiles.Empty then
  380. begin
  381. Add('INPUT(');
  382. While not SharedLibFiles.Empty do
  383. begin
  384. S:=SharedLibFiles.GetFirst;
  385. if (s<>'c') or reorder then
  386. begin
  387. i:=Pos(target_info.sharedlibext,S);
  388. if i>0 then
  389. Delete(S,i,255);
  390. Add('-l'+s);
  391. end
  392. else
  393. begin
  394. linklibc:=true;
  395. end;
  396. end;
  397. { be sure that libc is the last lib }
  398. if linklibc and not reorder then
  399. Add('-lc');
  400. { when we have -static for the linker the we also need libgcc }
  401. if (cs_link_staticflag in current_settings.globalswitches) then
  402. Add('-lgcc');
  403. Add(')');
  404. end;
  405. { objects which must be at the end }
  406. if linklibc and (libctype<>uclibc) then
  407. begin
  408. { x86_64 requires this to use entry/exit code with pic,
  409. see also issue #8210 regarding a discussion
  410. no idea about the other non i386 CPUs (FK)
  411. }
  412. {$ifdef x86_64}
  413. if current_module.islibrary then
  414. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  415. else
  416. {$endif x86_64}
  417. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  418. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  419. if found1 or found2 then
  420. begin
  421. Add('INPUT(');
  422. if found1 then
  423. AddFileName(s1);
  424. if found2 then
  425. AddFileName(s2);
  426. Add(')');
  427. end;
  428. end;
  429. {Entry point.}
  430. add('ENTRY(_start)');
  431. {$ifdef x86_64}
  432. {$define LINKERSCRIPT_INCLUDED}
  433. add('SECTIONS');
  434. add('{');
  435. {Read-only sections, merged into text segment:}
  436. if current_module.islibrary then
  437. add(' . = 0 + SIZEOF_HEADERS;')
  438. else
  439. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  440. add(' . = 0 + SIZEOF_HEADERS;');
  441. add(' .interp : { *(.interp) }');
  442. add(' .hash : { *(.hash) }');
  443. add(' .dynsym : { *(.dynsym) }');
  444. add(' .dynstr : { *(.dynstr) }');
  445. add(' .gnu.version : { *(.gnu.version) }');
  446. add(' .gnu.version_d : { *(.gnu.version_d) }');
  447. add(' .gnu.version_r : { *(.gnu.version_r) }');
  448. add(' .rel.dyn :');
  449. add(' {');
  450. add(' *(.rel.init)');
  451. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  452. add(' *(.rel.fini)');
  453. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  454. add(' *(.rel.data.rel.ro*)');
  455. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  456. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  457. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  458. add(' *(.rel.got)');
  459. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  460. add(' }');
  461. add(' .rela.dyn :');
  462. add(' {');
  463. add(' *(.rela.init)');
  464. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  465. add(' *(.rela.fini)');
  466. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  467. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  468. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  469. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  470. add(' *(.rela.got)');
  471. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  472. add(' }');
  473. add(' .rel.plt : { *(.rel.plt) }');
  474. add(' .rela.plt : { *(.rela.plt) }');
  475. add(' .init :');
  476. add(' {');
  477. add(' KEEP (*(.init))');
  478. add(' } =0x90909090');
  479. add(' .plt : { *(.plt) }');
  480. add(' .text :');
  481. add(' {');
  482. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  483. add(' KEEP (*(.text.*personality*))');
  484. {.gnu.warning sections are handled specially by elf32.em.}
  485. add(' *(.gnu.warning)');
  486. add(' } =0x90909090');
  487. add(' .fini :');
  488. add(' {');
  489. add(' KEEP (*(.fini))');
  490. add(' } =0x90909090');
  491. add(' PROVIDE (_etext = .);');
  492. add(' .rodata :');
  493. add(' {');
  494. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  495. add(' }');
  496. {Adjust the address for the data segment. We want to adjust up to
  497. the same address within the page on the next page up.}
  498. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  499. add(' .dynamic : { *(.dynamic) }');
  500. add(' .got : { *(.got .toc) }');
  501. add(' .got.plt : { *(.got.plt .toc.plt) }');
  502. add(' .data :');
  503. add(' {');
  504. add(' *(.data .data.* .gnu.linkonce.d.*)');
  505. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  506. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  507. add(' }');
  508. add(' PROVIDE (_edata = .);');
  509. add(' PROVIDE (edata = .);');
  510. {$ifdef zsegment_threadvars}
  511. add(' _z = .;');
  512. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  513. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  514. add(' . = _z + SIZEOF (.threadvar);');
  515. {$else}
  516. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  517. {$endif}
  518. add(' __bss_start = .;');
  519. add(' .bss :');
  520. add(' {');
  521. add(' *(.dynbss)');
  522. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  523. add(' *(COMMON)');
  524. {Align here to ensure that the .bss section occupies space up to
  525. _end. Align after .bss to ensure correct alignment even if the
  526. .bss section disappears because there are no input sections.}
  527. add(' . = ALIGN(32 / 8);');
  528. add(' }');
  529. add(' . = ALIGN(32 / 8);');
  530. add(' PROVIDE (_end = .);');
  531. add(' PROVIDE (end = .);');
  532. {Stabs debugging sections.}
  533. add(' .stab 0 : { *(.stab) }');
  534. add(' .stabstr 0 : { *(.stabstr) }');
  535. add(' /* DWARF debug sections.');
  536. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  537. add(' of the section so we begin them at 0. */');
  538. add(' /* DWARF 1 */');
  539. add(' .debug 0 : { *(.debug) }');
  540. add(' .line 0 : { *(.line) }');
  541. add(' /* GNU DWARF 1 extensions */');
  542. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  543. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  544. add(' /* DWARF 1.1 and DWARF 2 */');
  545. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  546. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  547. add(' /* DWARF 2 */');
  548. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  549. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  550. add(' .debug_line 0 : { *(.debug_line) }');
  551. add(' .debug_frame 0 : { *(.debug_frame) }');
  552. add(' .debug_str 0 : { *(.debug_str) }');
  553. add(' .debug_loc 0 : { *(.debug_loc) }');
  554. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  555. add(' /* SGI/MIPS DWARF 2 extensions */');
  556. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  557. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  558. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  559. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  560. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  561. add('}');
  562. {$endif x86_64}
  563. {$ifndef LINKERSCRIPT_INCLUDED}
  564. {Sections.}
  565. add('SECTIONS');
  566. add('{');
  567. {Read-only sections, merged into text segment:}
  568. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 +0x100;');
  569. add(' .interp : { *(.interp) }');
  570. add(' .hash : { *(.hash) }');
  571. add(' .dynsym : { *(.dynsym) }');
  572. add(' .dynstr : { *(.dynstr) }');
  573. add(' .gnu.version : { *(.gnu.version) }');
  574. add(' .gnu.version_d : { *(.gnu.version_d) }');
  575. add(' .gnu.version_r : { *(.gnu.version_r) }');
  576. add(' .rel.dyn :');
  577. add(' {');
  578. add(' *(.rel.init)');
  579. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  580. add(' *(.rel.fini)');
  581. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  582. add(' *(.rel.data.rel.ro*)');
  583. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  584. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  585. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  586. add(' *(.rel.got)');
  587. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  588. add(' }');
  589. add(' .rela.dyn :');
  590. add(' {');
  591. add(' *(.rela.init)');
  592. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  593. add(' *(.rela.fini)');
  594. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  595. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  596. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  597. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  598. add(' *(.rela.got)');
  599. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  600. add(' }');
  601. add(' .rel.plt : { *(.rel.plt) }');
  602. add(' .rela.plt : { *(.rela.plt) }');
  603. add(' .init :');
  604. add(' {');
  605. add(' KEEP (*(.init))');
  606. add(' } =0x90909090');
  607. add(' .plt : { *(.plt) }');
  608. add(' .text :');
  609. add(' {');
  610. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  611. add(' KEEP (*(.text.*personality*))');
  612. {.gnu.warning sections are handled specially by elf32.em.}
  613. add(' *(.gnu.warning)');
  614. add(' } =0x90909090');
  615. add(' .fini :');
  616. add(' {');
  617. add(' KEEP (*(.fini))');
  618. add(' } =0x90909090');
  619. add(' PROVIDE (_etext = .);');
  620. add(' .rodata :');
  621. add(' {');
  622. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  623. add(' }');
  624. {Adjust the address for the data segment. We want to adjust up to
  625. the same address within the page on the next page up.}
  626. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  627. add(' .dynamic : { *(.dynamic) }');
  628. add(' .got : { *(.got) }');
  629. add(' .got.plt : { *(.got.plt) }');
  630. add(' .data :');
  631. add(' {');
  632. add(' *(.data .data.* .gnu.linkonce.d.*)');
  633. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  634. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  635. add(' }');
  636. add(' PROVIDE (_edata = .);');
  637. add(' PROVIDE (edata = .);');
  638. {$ifdef zsegment_threadvars}
  639. add(' _z = .;');
  640. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  641. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  642. add(' . = _z + SIZEOF (.threadvar);');
  643. {$else}
  644. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  645. {$endif}
  646. add(' __bss_start = .;');
  647. add(' .bss :');
  648. add(' {');
  649. add(' *(.dynbss)');
  650. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  651. add(' *(COMMON)');
  652. {Align here to ensure that the .bss section occupies space up to
  653. _end. Align after .bss to ensure correct alignment even if the
  654. .bss section disappears because there are no input sections.}
  655. add(' . = ALIGN(32 / 8);');
  656. add(' }');
  657. add(' . = ALIGN(32 / 8);');
  658. add(' PROVIDE (_end = .);');
  659. add(' PROVIDE (end = .);');
  660. {Stabs debugging sections.}
  661. add(' .stab 0 : { *(.stab) }');
  662. add(' .stabstr 0 : { *(.stabstr) }');
  663. add('}');
  664. {$endif LINKERSCRIPT_INCLUDED}
  665. { Write and Close response }
  666. writetodisk;
  667. Free;
  668. end;
  669. WriteResponseFile:=True;
  670. end;
  671. function TLinkerLinux.MakeExecutable:boolean;
  672. var
  673. i : longint;
  674. binstr,
  675. cmdstr : TCmdStr;
  676. success : boolean;
  677. DynLinkStr : string;
  678. GCSectionsStr,
  679. StaticStr,
  680. StripStr : string[40];
  681. begin
  682. if not(cs_link_nolink in current_settings.globalswitches) then
  683. Message1(exec_i_linking,current_module.exefilename^);
  684. { Create some replacements }
  685. StaticStr:='';
  686. StripStr:='';
  687. GCSectionsStr:='';
  688. DynLinkStr:='';
  689. if (cs_link_staticflag in current_settings.globalswitches) then
  690. StaticStr:='-static';
  691. if (cs_link_strip in current_settings.globalswitches) and
  692. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  693. StripStr:='-s';
  694. if (cs_link_map in current_settings.globalswitches) then
  695. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  696. if create_smartlink_sections then
  697. GCSectionsStr:='--gc-sections';
  698. If (cs_profile in current_settings.moduleswitches) or
  699. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  700. begin
  701. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  702. if cshared then
  703. DynLinkStr:=DynLinkStr+' --shared ';
  704. if rlinkpath<>'' then
  705. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  706. End;
  707. { Write used files and libraries }
  708. WriteResponseFile(false);
  709. { Call linker }
  710. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  711. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  712. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  713. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  714. Replace(cmdstr,'$STATIC',StaticStr);
  715. Replace(cmdstr,'$STRIP',StripStr);
  716. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  717. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  718. { create dynamic symbol table? }
  719. if HasExports then
  720. cmdstr:=cmdstr+' -E';
  721. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  722. { Create external .dbg file with debuginfo }
  723. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  724. begin
  725. for i:=1 to 3 do
  726. begin
  727. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  728. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  729. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename^)));
  730. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename^));
  731. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  732. if not success then
  733. break;
  734. end;
  735. end;
  736. { Remove ReponseFile }
  737. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  738. DeleteFile(outputexedir+Info.ResName);
  739. if (success) then
  740. success:=PostProcessExecutable(current_module.exefilename^,false);
  741. MakeExecutable:=success; { otherwise a recursive call to link method }
  742. end;
  743. Function TLinkerLinux.MakeSharedLibrary:boolean;
  744. var
  745. InitStr,
  746. FiniStr,
  747. SoNameStr : string[80];
  748. binstr,
  749. cmdstr : TCmdStr;
  750. success : boolean;
  751. begin
  752. MakeSharedLibrary:=false;
  753. if not(cs_link_nolink in current_settings.globalswitches) then
  754. Message1(exec_i_linking,current_module.sharedlibfilename^);
  755. { Write used files and libraries }
  756. WriteResponseFile(true);
  757. { Create some replacements }
  758. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  759. InitStr:='-init FPC_LIB_START';
  760. FiniStr:='-fini FPC_LIB_EXIT';
  761. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  762. { Call linker }
  763. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  764. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  765. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  766. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  767. Replace(cmdstr,'$INIT',InitStr);
  768. Replace(cmdstr,'$FINI',FiniStr);
  769. Replace(cmdstr,'$SONAME',SoNameStr);
  770. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  771. { Strip the library ? }
  772. if success and (cs_link_strip in current_settings.globalswitches) then
  773. begin
  774. { only remove non global symbols and debugging info for a library }
  775. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  776. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  777. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  778. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  779. end;
  780. { Remove ReponseFile }
  781. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  782. DeleteFile(outputexedir+Info.ResName);
  783. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  784. end;
  785. function tlinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  786. var
  787. cmdstr: string;
  788. begin
  789. result:=True;
  790. if HasResources and
  791. (target_res.id=res_elf) then
  792. begin
  793. cmdstr:=' -f -i '+maybequoted(fn);
  794. result:=DoExec(FindUtil(utilsprefix+'fpcres'),cmdstr,false,false);
  795. end;
  796. end;
  797. {*****************************************************************************
  798. Initialize
  799. *****************************************************************************}
  800. initialization
  801. {$ifdef i386}
  802. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  803. RegisterImport(system_i386_linux,timportliblinux);
  804. RegisterExport(system_i386_linux,texportliblinux);
  805. RegisterTarget(system_i386_linux_info);
  806. RegisterRes(res_elf32_info,TWinLikeResourceFile);
  807. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  808. RegisterImport(system_x86_6432_linux,timportliblinux);
  809. RegisterExport(system_x86_6432_linux,texportliblinux);
  810. RegisterTarget(system_x86_6432_linux_info);
  811. {$endif i386}
  812. {$ifdef m68k}
  813. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  814. RegisterImport(system_m68k_linux,timportliblinux);
  815. RegisterExport(system_m68k_linux,texportliblinux);
  816. RegisterTarget(system_m68k_linux_info);
  817. {$endif m68k}
  818. {$ifdef powerpc}
  819. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  820. RegisterImport(system_powerpc_linux,timportliblinux);
  821. RegisterExport(system_powerpc_linux,texportliblinux);
  822. RegisterTarget(system_powerpc_linux_info);
  823. {$endif powerpc}
  824. {$ifdef powerpc64}
  825. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  826. RegisterImport(system_powerpc64_linux,timportliblinux);
  827. RegisterExport(system_powerpc64_linux,texportliblinux);
  828. RegisterTarget(system_powerpc64_linux_info);
  829. {$endif powerpc64}
  830. {$ifdef alpha}
  831. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  832. RegisterImport(system_alpha_linux,timportliblinux);
  833. RegisterExport(system_alpha_linux,texportliblinux);
  834. RegisterTarget(system_alpha_linux_info);
  835. {$endif alpha}
  836. {$ifdef x86_64}
  837. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  838. RegisterImport(system_x86_64_linux,timportliblinux);
  839. RegisterExport(system_x86_64_linux,texportliblinux);
  840. RegisterTarget(system_x86_64_linux_info);
  841. RegisterRes(res_elf64_info,TWinLikeResourceFile);
  842. {$endif x86_64}
  843. {$ifdef SPARC}
  844. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  845. RegisterImport(system_SPARC_linux,timportliblinux);
  846. RegisterExport(system_SPARC_linux,texportliblinux);
  847. RegisterTarget(system_SPARC_linux_info);
  848. {$endif SPARC}
  849. {$ifdef ARM}
  850. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  851. RegisterImport(system_arm_linux,timportliblinux);
  852. RegisterExport(system_arm_linux,texportliblinux);
  853. RegisterTarget(system_arm_linux_info);
  854. {$endif ARM}
  855. end.