t_linux.pas 45 KB

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