t_linux.pas 31 KB

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