t_linux.pas 45 KB

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