t_linux.pas 44 KB

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