t_linux.pas 45 KB

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