t_linux.pas 45 KB

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