t_linux.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. {
  2. Copyright (c) 1998-2008 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) Linux target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit t_linux;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmdata,
  23. symsym,symdef,ppu,
  24. import,export,expunix,link;
  25. type
  26. timportliblinux=class(timportlib)
  27. procedure generatelib;override;
  28. end;
  29. texportliblinux=class(texportlibunix)
  30. procedure setfininame(list: TAsmList; const s: string); override;
  31. end;
  32. TLibcType=(libc5,glibc2,glibc21,uclibc);
  33. tlinkerlinux=class(texternallinker)
  34. private
  35. libctype: TLibcType;
  36. prtobj : string[80];
  37. reorder : boolean;
  38. linklibc: boolean;
  39. Function WriteResponseFile(isdll:boolean) : Boolean;
  40. public
  41. constructor Create;override;
  42. procedure SetDefaultInfo;override;
  43. procedure InitSysInitUnitName;override;
  44. function MakeExecutable:boolean;override;
  45. function MakeSharedLibrary:boolean;override;
  46. procedure LoadPredefinedLibraryOrder; override;
  47. end;
  48. implementation
  49. uses
  50. SysUtils,
  51. cutils,cfileutl,cclasses,
  52. verbose,systems,globtype,globals,
  53. symconst,script,
  54. fmodule,
  55. aasmbase,aasmtai,aasmcpu,cpubase,
  56. cgbase,cgobj,cgutils,ogbase,ncgutil,
  57. comprsrc,
  58. rescmn, i_linux
  59. ;
  60. {*****************************************************************************
  61. TIMPORTLIBLINUX
  62. *****************************************************************************}
  63. procedure timportliblinux.generatelib;
  64. var
  65. i : longint;
  66. ImportLibrary : TImportLibrary;
  67. begin
  68. for i:=0 to current_module.ImportLibraryList.Count-1 do
  69. begin
  70. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  71. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  72. end;
  73. end;
  74. {*****************************************************************************
  75. TEXPORTLIBLINUX
  76. *****************************************************************************}
  77. procedure texportliblinux.setfininame(list: TAsmList; const s: string);
  78. begin
  79. { the problem with not having a .fini section is that a finalization
  80. routine in regular code can get "smart" linked away -> reference it
  81. just like the debug info }
  82. new_section(list,sec_fpc,'links',0);
  83. list.concat(Tai_const.Createname(s,0));
  84. inherited setfininame(list,s);
  85. end;
  86. {*****************************************************************************
  87. TLINKERLINUX
  88. *****************************************************************************}
  89. procedure SetupLibrarySearchPath;
  90. begin
  91. if not Dontlinkstdlibpath Then
  92. {$ifdef x86_64}
  93. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  94. {$else}
  95. {$ifdef powerpc64}
  96. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  97. {$else powerpc64}
  98. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  99. {$endif powerpc64}
  100. {$endif x86_64}
  101. {$ifdef arm}
  102. {$ifdef FPC_ARMHF}
  103. { at least raspian has the crt*.o files at an uncommon location,
  104. for other arm flavours, this cannot hurt }
  105. if not Dontlinkstdlibpath Then
  106. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabihf',true);
  107. {$endif FPC_ARMHF}
  108. {$endif arm}
  109. end;
  110. {$ifdef m68k}
  111. { experimental, is this correct? }
  112. const defdynlinker='/lib/ld-linux.so.2';
  113. {$endif m68k}
  114. {$ifdef i386}
  115. const defdynlinker='/lib/ld-linux.so.2';
  116. {$endif}
  117. {$ifdef x86_64}
  118. const defdynlinker='/lib64/ld-linux-x86-64.so.2';
  119. {$endif x86_64}
  120. {$ifdef sparc}
  121. const defdynlinker='/lib/ld-linux.so.2';
  122. {$endif sparc}
  123. {$ifdef powerpc}
  124. const defdynlinker='/lib/ld.so.1';
  125. {$endif powerpc}
  126. {$ifdef powerpc64}
  127. const defdynlinker='/lib64/ld64.so.1';
  128. {$endif powerpc64}
  129. {$ifdef arm}
  130. {$ifdef FPC_ARMHF}
  131. const defdynlinker='/lib/arm-linux-gnueabihf/ld-linux.so.3';
  132. {$else FPC_ARMHF}
  133. {$ifdef FPC_ARMEL}
  134. const defdynlinker='/lib/ld-linux.so.3';
  135. {$else FPC_ARMEL}
  136. const defdynlinker='/lib/ld-linux.so.2';
  137. {$endif FPC_ARMEL}
  138. {$endif FPC_ARMHF}
  139. {$endif arm}
  140. {$ifdef mips}
  141. const defdynlinker='/lib/ld.so.1';
  142. {$endif mips}
  143. procedure SetupDynlinker(out DynamicLinker:string;out libctype:TLibcType);
  144. begin
  145. {
  146. Search order:
  147. glibc 2.1+
  148. uclibc
  149. glibc 2.0
  150. If none is found (e.g. when cross compiling) glibc21 is assumed
  151. }
  152. if fileexists(sysrootpath+defdynlinker,false) then
  153. begin
  154. DynamicLinker:=defdynlinker;
  155. {$ifdef i386}
  156. libctype:=glibc21;
  157. {$else i386}
  158. libctype:=glibc2;
  159. {$endif i386}
  160. end
  161. else if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  162. begin
  163. DynamicLinker:='/lib/ld-uClibc.so.0';
  164. libctype:=uclibc;
  165. end
  166. {$ifdef i386}
  167. else if FileExists(sysrootpath+'/lib/ld-linux.so.1',false) then
  168. begin
  169. DynamicLinker:='/lib/ld-linux.so.1';
  170. libctype:=glibc2;
  171. end
  172. {$endif i386}
  173. else
  174. begin
  175. { when no dyn. linker is found, we are probably
  176. cross compiling, so use the default dyn. linker }
  177. DynamicLinker:=defdynlinker;
  178. {
  179. the default c startup script is gcrt0.as on all platforms
  180. except i386
  181. }
  182. {$ifdef i386}
  183. libctype:=glibc21;
  184. {$else i386}
  185. libctype:=glibc2;
  186. {$endif i386}
  187. end;
  188. end;
  189. function ModulesLinkToLibc:boolean;
  190. var
  191. hp: tmodule;
  192. begin
  193. result:=false;
  194. hp:=tmodule(loaded_units.first);
  195. while assigned(hp) do
  196. begin
  197. result:=hp.linkothersharedlibs.find('c');
  198. if result then break;
  199. hp:=tmodule(hp.next);
  200. end;
  201. end;
  202. Constructor TLinkerLinux.Create;
  203. begin
  204. Inherited Create;
  205. SetupLibrarySearchPath;
  206. end;
  207. procedure TLinkerLinux.SetDefaultInfo;
  208. {
  209. This will also detect which libc version will be used
  210. }
  211. const
  212. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  213. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  214. {$ifdef powerpc} platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  215. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  216. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  217. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  218. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  219. {$ifdef mips}
  220. {$ifdef mipsel}
  221. platform_select='-EL';
  222. {$else}
  223. platform_select='-EB';
  224. {$endif}
  225. {$endif}
  226. begin
  227. with Info do
  228. begin
  229. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  230. { when we want to cross-link we need to override default library paths }
  231. if length(sysrootpath) > 0 then
  232. ExeCmd[1]:=ExeCmd[1]+' -T';
  233. ExeCmd[1]:=ExeCmd[1]+' $RES';
  234. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  235. DllCmd[2]:='strip --strip-unneeded $EXE';
  236. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  237. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  238. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  239. SetupDynlinker(DynamicLinker,libctype);
  240. end;
  241. end;
  242. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  243. // put your linkorder/linkalias overrides here.
  244. // Note: assumes only called when reordering/aliasing is used.
  245. Begin
  246. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  247. Begin
  248. LinkLibraryOrder.add('gcc','',15);
  249. LinkLibraryOrder.add('c','',100);
  250. LinkLibraryOrder.add('gmon','',120);
  251. LinkLibraryOrder.add('dl','',140);
  252. LinkLibraryOrder.add('pthread','',160);
  253. end;
  254. End;
  255. type
  256. tlibcnames=array [TLibcType] of string[8];
  257. const { libc5 glibc2 glibc21 uclibc }
  258. cprtnames: tlibcnames = ('cprt0', 'cprt0', 'cprt21', 'ucprt0');
  259. csinames: tlibcnames = ('si_c', 'si_c', 'si_c21', 'si_uc');
  260. gprtnames: tlibcnames = ('gprt0', 'gprt0', 'gprt21', 'ugprt0');
  261. gsinames: tlibcnames = ('si_g', 'si_g', 'si_c21g','si_ucg');
  262. defprtnames: array[boolean] of string[8] = ('prt0', 'dllprt0');
  263. defsinames: array[boolean] of string[8] = ('si_prc','si_dll');
  264. { uclibc and glibc21 are not available on x86_64! si_g is also absent. }
  265. Procedure TLinkerLinux.InitSysInitUnitName;
  266. begin
  267. linklibc:=ModulesLinkToLibc;
  268. reorder:=linklibc and ReOrderEntries;
  269. sysinitunit:=defsinames[current_module.islibrary];
  270. prtobj:=defprtnames[current_module.islibrary];
  271. if current_module.islibrary then
  272. exit;
  273. if cs_profile in current_settings.moduleswitches then
  274. begin
  275. prtobj:=gprtnames[libctype];
  276. sysinitunit:=gsinames[libctype];
  277. linklibc:=true;
  278. end
  279. else if linklibc then
  280. begin
  281. prtobj:=cprtnames[libctype];
  282. sysinitunit:=csinames[libctype];
  283. end;
  284. end;
  285. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  286. Var
  287. linkres : TLinkRes;
  288. i : longint;
  289. HPath : TCmdStrListItem;
  290. s,s1,s2 : TCmdStr;
  291. found1,
  292. found2 : boolean;
  293. linksToSharedLibFiles : boolean;
  294. begin
  295. result:=False;
  296. { set special options for some targets }
  297. if cs_profile in current_settings.moduleswitches then
  298. begin
  299. if not(libctype in [glibc2,glibc21]) then
  300. AddSharedLibrary('gmon');
  301. AddSharedLibrary('c');
  302. end;
  303. { Open link.res file }
  304. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  305. with linkres do
  306. begin
  307. { Write path to search libraries }
  308. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  309. while assigned(HPath) do
  310. begin
  311. Add('SEARCH_DIR("'+HPath.Str+'")');
  312. HPath:=TCmdStrListItem(HPath.Next);
  313. end;
  314. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  315. while assigned(HPath) do
  316. begin
  317. Add('SEARCH_DIR("'+HPath.Str+'")');
  318. HPath:=TCmdStrListItem(HPath.Next);
  319. end;
  320. { force local symbol resolution (i.e., inside the shared }
  321. { library itself) for all non-exorted symbols, otherwise }
  322. { several RTL symbols of FPC-compiled shared libraries }
  323. { will be bound to those of a single shared library or }
  324. { to the main program }
  325. if (isdll) then
  326. begin
  327. add('VERSION');
  328. add('{');
  329. add(' {');
  330. if not texportlibunix(exportlib).exportedsymnames.empty then
  331. begin
  332. add(' global:');
  333. repeat
  334. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  335. until texportlibunix(exportlib).exportedsymnames.empty;
  336. end;
  337. add(' local:');
  338. add(' *;');
  339. add(' };');
  340. add('}');
  341. end;
  342. StartSection('INPUT(');
  343. { add objectfiles, start with prt0 always }
  344. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  345. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  346. { try to add crti and crtbegin if linking to C }
  347. if linklibc and (libctype<>uclibc) then
  348. begin
  349. { crti.o must come first }
  350. if librarysearchpath.FindFile('crti.o',false,s) then
  351. AddFileName(s);
  352. { then the crtbegin* }
  353. if cs_create_pic in current_settings.moduleswitches then
  354. begin
  355. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  356. AddFileName(s);
  357. end
  358. else
  359. if (cs_link_staticflag in current_settings.globalswitches) and
  360. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  361. AddFileName(s)
  362. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  363. AddFileName(s);
  364. end;
  365. { main objectfiles }
  366. while not ObjectFiles.Empty do
  367. begin
  368. s:=ObjectFiles.GetFirst;
  369. if s<>'' then
  370. AddFileName(maybequoted(s));
  371. end;
  372. EndSection(')');
  373. { Write staticlibraries }
  374. if not StaticLibFiles.Empty then
  375. begin
  376. Add('GROUP(');
  377. While not StaticLibFiles.Empty do
  378. begin
  379. S:=StaticLibFiles.GetFirst;
  380. AddFileName(maybequoted(s))
  381. end;
  382. Add(')');
  383. end;
  384. // we must reorder here because the result could empty sharedlibfiles
  385. if reorder Then
  386. ExpandAndApplyOrder(SharedLibFiles);
  387. // after this point addition of shared libs not allowed.
  388. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  389. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  390. if (isdll) then
  391. begin
  392. Add('INPUT(');
  393. Add(info.DynamicLinker);
  394. Add(')');
  395. end;
  396. linksToSharedLibFiles := not SharedLibFiles.Empty;
  397. if not SharedLibFiles.Empty then
  398. begin
  399. if (SharedLibFiles.Count<>1) or
  400. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  401. reorder then
  402. begin
  403. Add('INPUT(');
  404. While not SharedLibFiles.Empty do
  405. begin
  406. S:=SharedLibFiles.GetFirst;
  407. if (s<>'c') or reorder then
  408. begin
  409. i:=Pos(target_info.sharedlibext,S);
  410. if i>0 then
  411. Delete(S,i,255);
  412. Add('-l'+s);
  413. end
  414. else
  415. begin
  416. linklibc:=true;
  417. end;
  418. end;
  419. Add(')');
  420. end
  421. else
  422. linklibc:=true;
  423. if (cs_link_staticflag in current_settings.globalswitches) or
  424. (linklibc and not reorder) then
  425. begin
  426. Add('GROUP(');
  427. { when we have -static for the linker the we also need libgcc }
  428. if (cs_link_staticflag in current_settings.globalswitches) then
  429. begin
  430. Add('-lgcc');
  431. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  432. Add('-lgcc_eh');
  433. end;
  434. { be sure that libc is the last lib }
  435. if linklibc and not reorder then
  436. Add('-lc');
  437. Add(')');
  438. end;
  439. end;
  440. { objects which must be at the end }
  441. if linklibc and (libctype<>uclibc) then
  442. begin
  443. if cs_create_pic in current_settings.moduleswitches then
  444. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  445. else
  446. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  447. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  448. if found1 or found2 then
  449. begin
  450. Add('INPUT(');
  451. if found1 then
  452. AddFileName(s1);
  453. if found2 then
  454. AddFileName(s2);
  455. Add(')');
  456. end;
  457. end;
  458. {Entry point. Only needed for executables, set on the linker command line for
  459. shared libraries. }
  460. if (not isdll) then
  461. if (linksToSharedLibFiles and not linklibc) then
  462. add('ENTRY(_dynamic_start)')
  463. else
  464. add('ENTRY(_start)');
  465. {$ifdef x86_64}
  466. {$define LINKERSCRIPT_INCLUDED}
  467. add('SECTIONS');
  468. add('{');
  469. {Read-only sections, merged into text segment:}
  470. if current_module.islibrary then
  471. add(' . = 0 + SIZEOF_HEADERS;')
  472. else
  473. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  474. add(' . = 0 + SIZEOF_HEADERS;');
  475. add(' .interp : { *(.interp) }');
  476. add(' .hash : { *(.hash) }');
  477. add(' .dynsym : { *(.dynsym) }');
  478. add(' .dynstr : { *(.dynstr) }');
  479. add(' .gnu.version : { *(.gnu.version) }');
  480. add(' .gnu.version_d : { *(.gnu.version_d) }');
  481. add(' .gnu.version_r : { *(.gnu.version_r) }');
  482. add(' .rel.dyn :');
  483. add(' {');
  484. add(' *(.rel.init)');
  485. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  486. add(' *(.rel.fini)');
  487. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  488. add(' *(.rel.data.rel.ro*)');
  489. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  490. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  491. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  492. add(' *(.rel.got)');
  493. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  494. add(' }');
  495. add(' .rela.dyn :');
  496. add(' {');
  497. add(' *(.rela.init)');
  498. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  499. add(' *(.rela.fini)');
  500. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  501. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  502. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  503. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  504. add(' *(.rela.got)');
  505. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  506. add(' }');
  507. add(' .rel.plt : { *(.rel.plt) }');
  508. add(' .rela.plt : { *(.rela.plt) }');
  509. add(' .init :');
  510. add(' {');
  511. add(' KEEP (*(.init))');
  512. add(' } =0x90909090');
  513. add(' .plt : { *(.plt) }');
  514. add(' .text :');
  515. add(' {');
  516. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  517. add(' KEEP (*(.text.*personality*))');
  518. {.gnu.warning sections are handled specially by elf32.em.}
  519. add(' *(.gnu.warning)');
  520. add(' } =0x90909090');
  521. add(' .fini :');
  522. add(' {');
  523. add(' KEEP (*(.fini))');
  524. add(' } =0x90909090');
  525. add(' PROVIDE (_etext = .);');
  526. add(' .rodata :');
  527. add(' {');
  528. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  529. add(' }');
  530. {Adjust the address for the data segment. We want to adjust up to
  531. the same address within the page on the next page up.}
  532. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  533. add(' .dynamic : { *(.dynamic) }');
  534. add(' .got : { *(.got .toc) }');
  535. add(' .got.plt : { *(.got.plt .toc.plt) }');
  536. add(' .data :');
  537. add(' {');
  538. add(' *(.data .data.* .gnu.linkonce.d.*)');
  539. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  540. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  541. add(' }');
  542. add(' PROVIDE (_edata = .);');
  543. add(' PROVIDE (edata = .);');
  544. {$ifdef zsegment_threadvars}
  545. add(' _z = .;');
  546. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  547. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  548. add(' . = _z + SIZEOF (.threadvar);');
  549. {$else}
  550. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  551. {$endif}
  552. add(' __bss_start = .;');
  553. add(' .bss :');
  554. add(' {');
  555. add(' *(.dynbss)');
  556. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  557. add(' *(COMMON)');
  558. {Align here to ensure that the .bss section occupies space up to
  559. _end. Align after .bss to ensure correct alignment even if the
  560. .bss section disappears because there are no input sections.}
  561. add(' . = ALIGN(32 / 8);');
  562. add(' }');
  563. add(' . = ALIGN(32 / 8);');
  564. add(' PROVIDE (_end = .);');
  565. add(' PROVIDE (end = .);');
  566. {Stabs debugging sections.}
  567. add(' .stab 0 : { *(.stab) }');
  568. add(' .stabstr 0 : { *(.stabstr) }');
  569. add(' /* DWARF debug sections.');
  570. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  571. add(' of the section so we begin them at 0. */');
  572. add(' /* DWARF 1 */');
  573. add(' .debug 0 : { *(.debug) }');
  574. add(' .line 0 : { *(.line) }');
  575. add(' /* GNU DWARF 1 extensions */');
  576. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  577. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  578. add(' /* DWARF 1.1 and DWARF 2 */');
  579. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  580. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  581. add(' /* DWARF 2 */');
  582. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  583. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  584. add(' .debug_line 0 : { *(.debug_line) }');
  585. add(' .debug_frame 0 : { *(.debug_frame) }');
  586. add(' .debug_str 0 : { *(.debug_str) }');
  587. add(' .debug_loc 0 : { *(.debug_loc) }');
  588. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  589. add(' /* SGI/MIPS DWARF 2 extensions */');
  590. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  591. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  592. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  593. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  594. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  595. add('}');
  596. {$endif x86_64}
  597. {$ifdef ARM}
  598. if target_info.abi=abi_eabi then
  599. begin
  600. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  601. add('/* Script for -z combreloc: combine and sort reloc sections */');
  602. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  603. add(' "elf32-littlearm")');
  604. add('OUTPUT_ARCH(arm)');
  605. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  606. add('SECTIONS');
  607. add('{');
  608. add(' /* Read-only sections, merged into text segment: */');
  609. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  610. add(' .interp : { *(.interp) }');
  611. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  612. add(' .hash : { *(.hash) }');
  613. add(' .gnu.hash : { *(.gnu.hash) }');
  614. add(' .dynsym : { *(.dynsym) }');
  615. add(' .dynstr : { *(.dynstr) }');
  616. add(' .gnu.version : { *(.gnu.version) }');
  617. add(' .gnu.version_d : { *(.gnu.version_d) }');
  618. add(' .gnu.version_r : { *(.gnu.version_r) }');
  619. add(' .rel.dyn :');
  620. add(' {');
  621. add(' *(.rel.init)');
  622. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  623. add(' *(.rel.fini)');
  624. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  625. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  626. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  627. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  628. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  629. add(' *(.rel.ctors)');
  630. add(' *(.rel.dtors)');
  631. add(' *(.rel.got)');
  632. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  633. add(' }');
  634. add(' .rela.dyn :');
  635. add(' {');
  636. add(' *(.rela.init)');
  637. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  638. add(' *(.rela.fini)');
  639. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  640. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  641. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  642. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  643. add(' *(.rela.ctors)');
  644. add(' *(.rela.dtors)');
  645. add(' *(.rela.got)');
  646. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  647. add(' }');
  648. add(' .rel.plt : { *(.rel.plt) }');
  649. add(' .rela.plt : { *(.rela.plt) }');
  650. add(' .init :');
  651. add(' {');
  652. add(' KEEP (*(.init))');
  653. add(' } =0');
  654. add(' .plt : { *(.plt) }');
  655. add(' .text :');
  656. add(' {');
  657. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  658. add(' KEEP (*(.text.*personality*))');
  659. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  660. add(' *(.gnu.warning)');
  661. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  662. add(' } =0');
  663. add(' .fini :');
  664. add(' {');
  665. add(' KEEP (*(.fini))');
  666. add(' } =0');
  667. add(' PROVIDE (__etext = .);');
  668. add(' PROVIDE (_etext = .);');
  669. add(' PROVIDE (etext = .);');
  670. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  671. add(' .rodata1 : { *(.rodata1) }');
  672. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  673. add(' __exidx_start = .;');
  674. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  675. add(' __exidx_end = .;');
  676. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  677. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  678. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  679. add(' /* Adjust the address for the data segment. We want to adjust up to');
  680. add(' the same address within the page on the next page up. */');
  681. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  682. add(' /* Exception handling */');
  683. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  684. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  685. add(' /* Thread Local Storage sections */');
  686. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  687. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  688. add(' .preinit_array :');
  689. add(' {');
  690. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  691. add(' KEEP (*(.preinit_array))');
  692. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  693. add(' }');
  694. add(' .init_array :');
  695. add(' {');
  696. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  697. add(' KEEP (*(SORT(.init_array.*)))');
  698. add(' KEEP (*(.init_array))');
  699. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  700. add(' }');
  701. add(' .fini_array :');
  702. add(' {');
  703. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  704. add(' KEEP (*(.fini_array))');
  705. add(' KEEP (*(SORT(.fini_array.*)))');
  706. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  707. add(' }');
  708. add(' .ctors :');
  709. add(' {');
  710. add(' /* gcc uses crtbegin.o to find the start of');
  711. add(' the constructors, so we make sure it is');
  712. add(' first. Because this is a wildcard, it');
  713. add(' doesn''t matter if the user does not');
  714. add(' actually link against crtbegin.o; the');
  715. add(' linker won''t look for a file to match a');
  716. add(' wildcard. The wildcard also means that it');
  717. add(' doesn''t matter which directory crtbegin.o');
  718. add(' is in. */');
  719. add(' KEEP (*crtbegin.o(.ctors))');
  720. add(' KEEP (*crtbegin?.o(.ctors))');
  721. add(' /* We don''t want to include the .ctor section from');
  722. add(' the crtend.o file until after the sorted ctors.');
  723. add(' The .ctor section from the crtend file contains the');
  724. add(' end of ctors marker and it must be last */');
  725. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  726. add(' KEEP (*(SORT(.ctors.*)))');
  727. add(' KEEP (*(.ctors))');
  728. add(' }');
  729. add(' .dtors :');
  730. add(' {');
  731. add(' KEEP (*crtbegin.o(.dtors))');
  732. add(' KEEP (*crtbegin?.o(.dtors))');
  733. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  734. add(' KEEP (*(SORT(.dtors.*)))');
  735. add(' KEEP (*(.dtors))');
  736. add(' }');
  737. add(' .jcr : { KEEP (*(.jcr)) }');
  738. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  739. add(' .dynamic : { *(.dynamic) }');
  740. add(' .got : { *(.got.plt) *(.got) }');
  741. add(' .data :');
  742. add(' {');
  743. add(' __data_start = . ;');
  744. add(' *(.data .data.* .gnu.linkonce.d.*)');
  745. { extra by FPC }
  746. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  747. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  748. add(' SORT(CONSTRUCTORS)');
  749. add(' }');
  750. add(' .data1 : { *(.data1) }');
  751. add(' _edata = .; PROVIDE (edata = .);');
  752. add(' __bss_start = .;');
  753. add(' __bss_start__ = .;');
  754. add(' .bss :');
  755. add(' {');
  756. add(' *(.dynbss)');
  757. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  758. add(' *(COMMON)');
  759. add(' /* Align here to ensure that the .bss section occupies space up to');
  760. add(' _end. Align after .bss to ensure correct alignment even if the');
  761. add(' .bss section disappears because there are no input sections.');
  762. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  763. add(' pad the .data section. */');
  764. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  765. add(' }');
  766. add(' _bss_end__ = . ; __bss_end__ = . ;');
  767. add(' . = ALIGN(32 / 8);');
  768. add(' . = ALIGN(32 / 8);');
  769. add(' __end__ = . ;');
  770. add(' _end = .; PROVIDE (end = .);');
  771. add(' /* Stabs debugging sections. */');
  772. add(' .stab 0 : { *(.stab) }');
  773. add(' .stabstr 0 : { *(.stabstr) }');
  774. add(' .stab.excl 0 : { *(.stab.excl) }');
  775. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  776. add(' .stab.index 0 : { *(.stab.index) }');
  777. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  778. add(' .comment 0 : { *(.comment) }');
  779. add(' /* DWARF debug sections.');
  780. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  781. add(' of the section so we begin them at 0. */');
  782. add(' /* DWARF 1 */');
  783. add(' .debug 0 : { *(.debug) }');
  784. add(' .line 0 : { *(.line) }');
  785. add(' /* GNU DWARF 1 extensions */');
  786. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  787. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  788. add(' /* DWARF 1.1 and DWARF 2 */');
  789. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  790. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  791. add(' /* DWARF 2 */');
  792. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  793. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  794. add(' .debug_line 0 : { *(.debug_line) }');
  795. add(' .debug_frame 0 : { *(.debug_frame) }');
  796. add(' .debug_str 0 : { *(.debug_str) }');
  797. add(' .debug_loc 0 : { *(.debug_loc) }');
  798. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  799. add(' /* SGI/MIPS DWARF 2 extensions */');
  800. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  801. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  802. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  803. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  804. add(' /* DWARF 3 */');
  805. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  806. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  807. add(' .stack 0x80000 :');
  808. add(' {');
  809. add(' _stack = .;');
  810. add(' *(.stack)');
  811. add(' }');
  812. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  813. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  814. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  815. add('}');
  816. end
  817. else
  818. {$endif ARM}
  819. {$ifndef LINKERSCRIPT_INCLUDED}
  820. begin
  821. {Sections.}
  822. add('SECTIONS');
  823. add('{');
  824. {Read-only sections, merged into text segment:}
  825. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 + SIZEOF_HEADERS;');
  826. add(' .interp : { *(.interp) }');
  827. add(' .hash : { *(.hash) }');
  828. add(' .dynsym : { *(.dynsym) }');
  829. add(' .dynstr : { *(.dynstr) }');
  830. add(' .gnu.version : { *(.gnu.version) }');
  831. add(' .gnu.version_d : { *(.gnu.version_d) }');
  832. add(' .gnu.version_r : { *(.gnu.version_r) }');
  833. add(' .rel.dyn :');
  834. add(' {');
  835. add(' *(.rel.init)');
  836. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  837. add(' *(.rel.fini)');
  838. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  839. add(' *(.rel.data.rel.ro*)');
  840. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  841. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  842. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  843. add(' *(.rel.got)');
  844. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  845. add(' }');
  846. add(' .rela.dyn :');
  847. add(' {');
  848. add(' *(.rela.init)');
  849. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  850. add(' *(.rela.fini)');
  851. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  852. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  853. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  854. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  855. add(' *(.rela.got)');
  856. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  857. add(' }');
  858. add(' .rel.plt : { *(.rel.plt) }');
  859. add(' .rela.plt : { *(.rela.plt) }');
  860. add(' .init :');
  861. add(' {');
  862. add(' KEEP (*(.init))');
  863. add(' } =0x90909090');
  864. add(' .plt : { *(.plt) }');
  865. add(' .text :');
  866. add(' {');
  867. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  868. add(' KEEP (*(.text.*personality*))');
  869. {.gnu.warning sections are handled specially by elf32.em.}
  870. add(' *(.gnu.warning)');
  871. add(' } =0x90909090');
  872. add(' .fini :');
  873. add(' {');
  874. add(' KEEP (*(.fini))');
  875. add(' } =0x90909090');
  876. add(' PROVIDE (_etext = .);');
  877. add(' .rodata :');
  878. add(' {');
  879. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  880. add(' }');
  881. {Adjust the address for the data segment. We want to adjust up to
  882. the same address within the page on the next page up.}
  883. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  884. add(' .dynamic : { *(.dynamic) }');
  885. add(' .got : { *(.got) }');
  886. add(' .got.plt : { *(.got.plt) }');
  887. add(' .data :');
  888. add(' {');
  889. add(' *(.data .data.* .gnu.linkonce.d.*)');
  890. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  891. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  892. add(' }');
  893. add(' PROVIDE (_edata = .);');
  894. add(' PROVIDE (edata = .);');
  895. {$ifdef zsegment_threadvars}
  896. add(' _z = .;');
  897. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  898. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  899. add(' . = _z + SIZEOF (.threadvar);');
  900. {$else}
  901. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  902. {$endif}
  903. add(' __bss_start = .;');
  904. add(' .bss :');
  905. add(' {');
  906. add(' *(.dynbss)');
  907. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  908. add(' *(COMMON)');
  909. {Align here to ensure that the .bss section occupies space up to
  910. _end. Align after .bss to ensure correct alignment even if the
  911. .bss section disappears because there are no input sections.}
  912. add(' . = ALIGN(32 / 8);');
  913. add(' }');
  914. add(' . = ALIGN(32 / 8);');
  915. add(' PROVIDE (_end = .);');
  916. add(' PROVIDE (end = .);');
  917. {Stabs debugging sections.}
  918. add(' .stab 0 : { *(.stab) }');
  919. add(' .stabstr 0 : { *(.stabstr) }');
  920. add('}');
  921. end;
  922. {$endif LINKERSCRIPT_INCLUDED}
  923. { Write and Close response }
  924. writetodisk;
  925. Free;
  926. end;
  927. WriteResponseFile:=True;
  928. end;
  929. function TLinkerLinux.MakeExecutable:boolean;
  930. var
  931. i : longint;
  932. binstr,
  933. cmdstr : TCmdStr;
  934. success : boolean;
  935. DynLinkStr : string;
  936. GCSectionsStr,
  937. StaticStr,
  938. StripStr : string[40];
  939. begin
  940. if not(cs_link_nolink in current_settings.globalswitches) then
  941. Message1(exec_i_linking,current_module.exefilename);
  942. { Create some replacements }
  943. StaticStr:='';
  944. StripStr:='';
  945. GCSectionsStr:='';
  946. DynLinkStr:='';
  947. if (cs_link_staticflag in current_settings.globalswitches) then
  948. StaticStr:='-static';
  949. if (cs_link_strip in current_settings.globalswitches) and
  950. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  951. StripStr:='-s';
  952. if (cs_link_map in current_settings.globalswitches) then
  953. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  954. if create_smartlink_sections then
  955. GCSectionsStr:='--gc-sections';
  956. If (cs_profile in current_settings.moduleswitches) or
  957. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  958. begin
  959. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  960. if cshared then
  961. DynLinkStr:=DynLinkStr+' --shared ';
  962. if rlinkpath<>'' then
  963. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  964. End;
  965. { Write used files and libraries }
  966. WriteResponseFile(false);
  967. { Call linker }
  968. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  969. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  970. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  971. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  972. Replace(cmdstr,'$STATIC',StaticStr);
  973. Replace(cmdstr,'$STRIP',StripStr);
  974. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  975. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  976. { create dynamic symbol table? }
  977. if HasExports then
  978. cmdstr:=cmdstr+' -E';
  979. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  980. { Create external .dbg file with debuginfo }
  981. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  982. begin
  983. for i:=1 to 3 do
  984. begin
  985. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  986. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  987. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  988. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  989. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  990. if not success then
  991. break;
  992. end;
  993. end;
  994. { Remove ReponseFile }
  995. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  996. DeleteFile(outputexedir+Info.ResName);
  997. MakeExecutable:=success; { otherwise a recursive call to link method }
  998. end;
  999. Function TLinkerLinux.MakeSharedLibrary:boolean;
  1000. var
  1001. InitStr,
  1002. FiniStr,
  1003. SoNameStr : string[80];
  1004. binstr,
  1005. cmdstr : TCmdStr;
  1006. success : boolean;
  1007. begin
  1008. MakeSharedLibrary:=false;
  1009. if not(cs_link_nolink in current_settings.globalswitches) then
  1010. Message1(exec_i_linking,current_module.sharedlibfilename);
  1011. { Write used files and libraries }
  1012. WriteResponseFile(true);
  1013. { Create some replacements }
  1014. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  1015. InitStr:='-init FPC_SHARED_LIB_START';
  1016. FiniStr:='-fini FPC_LIB_EXIT';
  1017. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  1018. { Call linker }
  1019. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1020. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1021. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1022. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1023. Replace(cmdstr,'$INIT',InitStr);
  1024. Replace(cmdstr,'$FINI',FiniStr);
  1025. Replace(cmdstr,'$SONAME',SoNameStr);
  1026. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1027. { Strip the library ? }
  1028. if success and (cs_link_strip in current_settings.globalswitches) then
  1029. begin
  1030. { only remove non global symbols and debugging info for a library }
  1031. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1032. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1033. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1034. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1035. end;
  1036. { Remove ReponseFile }
  1037. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1038. DeleteFile(outputexedir+Info.ResName);
  1039. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1040. end;
  1041. {*****************************************************************************
  1042. Initialize
  1043. *****************************************************************************}
  1044. initialization
  1045. {$ifdef i386}
  1046. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  1047. RegisterImport(system_i386_linux,timportliblinux);
  1048. RegisterExport(system_i386_linux,texportliblinux);
  1049. RegisterTarget(system_i386_linux_info);
  1050. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  1051. RegisterImport(system_x86_6432_linux,timportliblinux);
  1052. RegisterExport(system_x86_6432_linux,texportliblinux);
  1053. RegisterTarget(system_x86_6432_linux_info);
  1054. {$endif i386}
  1055. {$ifdef m68k}
  1056. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  1057. RegisterImport(system_m68k_linux,timportliblinux);
  1058. RegisterExport(system_m68k_linux,texportliblinux);
  1059. RegisterTarget(system_m68k_linux_info);
  1060. {$endif m68k}
  1061. {$ifdef powerpc}
  1062. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  1063. RegisterImport(system_powerpc_linux,timportliblinux);
  1064. RegisterExport(system_powerpc_linux,texportliblinux);
  1065. RegisterTarget(system_powerpc_linux_info);
  1066. {$endif powerpc}
  1067. {$ifdef powerpc64}
  1068. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  1069. RegisterImport(system_powerpc64_linux,timportliblinux);
  1070. RegisterExport(system_powerpc64_linux,texportliblinux);
  1071. RegisterTarget(system_powerpc64_linux_info);
  1072. {$endif powerpc64}
  1073. {$ifdef alpha}
  1074. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  1075. RegisterImport(system_alpha_linux,timportliblinux);
  1076. RegisterExport(system_alpha_linux,texportliblinux);
  1077. RegisterTarget(system_alpha_linux_info);
  1078. {$endif alpha}
  1079. {$ifdef x86_64}
  1080. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  1081. RegisterImport(system_x86_64_linux,timportliblinux);
  1082. RegisterExport(system_x86_64_linux,texportliblinux);
  1083. RegisterTarget(system_x86_64_linux_info);
  1084. {$endif x86_64}
  1085. {$ifdef SPARC}
  1086. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  1087. RegisterImport(system_SPARC_linux,timportliblinux);
  1088. RegisterExport(system_SPARC_linux,texportliblinux);
  1089. RegisterTarget(system_SPARC_linux_info);
  1090. {$endif SPARC}
  1091. {$ifdef ARM}
  1092. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  1093. RegisterImport(system_arm_linux,timportliblinux);
  1094. RegisterExport(system_arm_linux,texportliblinux);
  1095. RegisterTarget(system_arm_linux_info);
  1096. {$endif ARM}
  1097. {$ifdef MIPS}
  1098. {$ifdef MIPSEL}
  1099. RegisterExternalLinker(system_mipsel_linux_info,TLinkerLinux);
  1100. RegisterImport(system_mipsel_linux,timportliblinux);
  1101. RegisterExport(system_mipsel_linux,texportliblinux);
  1102. RegisterTarget(system_mipsel_linux_info);
  1103. {$else MIPS}
  1104. RegisterExternalLinker(system_mipseb_linux_info,TLinkerLinux);
  1105. RegisterImport(system_mipseb_linux,timportliblinux);
  1106. RegisterExport(system_mipseb_linux,texportliblinux);
  1107. RegisterTarget(system_mipseb_linux_info);
  1108. {$endif MIPSEL}
  1109. {$endif MIPS}
  1110. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1111. end.