t_linux.pas 32 KB

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