t_linux.pas 44 KB

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