t_linux.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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. if cs_create_pic in current_settings.moduleswitches then
  352. begin
  353. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  354. AddFileName(s);
  355. end
  356. else
  357. if (cs_link_staticflag in current_settings.globalswitches) and
  358. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  359. AddFileName(s)
  360. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  361. AddFileName(s);
  362. end;
  363. { main objectfiles }
  364. while not ObjectFiles.Empty do
  365. begin
  366. s:=ObjectFiles.GetFirst;
  367. if s<>'' then
  368. AddFileName(maybequoted(s));
  369. end;
  370. EndSection(')');
  371. { Write staticlibraries }
  372. if not StaticLibFiles.Empty then
  373. begin
  374. Add('GROUP(');
  375. While not StaticLibFiles.Empty do
  376. begin
  377. S:=StaticLibFiles.GetFirst;
  378. AddFileName(maybequoted(s))
  379. end;
  380. Add(')');
  381. end;
  382. // we must reorder here because the result could empty sharedlibfiles
  383. if reorder Then
  384. ExpandAndApplyOrder(SharedLibFiles);
  385. // after this point addition of shared libs not allowed.
  386. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  387. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  388. if not SharedLibFiles.Empty then
  389. begin
  390. if (SharedLibFiles.Count<>1) or
  391. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  392. reorder then
  393. begin
  394. Add('INPUT(');
  395. While not SharedLibFiles.Empty do
  396. begin
  397. S:=SharedLibFiles.GetFirst;
  398. if (s<>'c') or reorder then
  399. begin
  400. i:=Pos(target_info.sharedlibext,S);
  401. if i>0 then
  402. Delete(S,i,255);
  403. Add('-l'+s);
  404. end
  405. else
  406. begin
  407. linklibc:=true;
  408. end;
  409. end;
  410. Add(')');
  411. end
  412. else
  413. linklibc:=true;
  414. if (cs_link_staticflag in current_settings.globalswitches) or
  415. (linklibc and not reorder) then
  416. begin
  417. Add('GROUP(');
  418. { when we have -static for the linker the we also need libgcc }
  419. if (cs_link_staticflag in current_settings.globalswitches) then
  420. begin
  421. Add('-lgcc');
  422. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  423. Add('-lgcc_eh');
  424. end;
  425. { be sure that libc is the last lib }
  426. if linklibc and not reorder then
  427. Add('-lc');
  428. Add(')');
  429. end;
  430. end;
  431. { objects which must be at the end }
  432. if linklibc and (libctype<>uclibc) then
  433. begin
  434. if cs_create_pic in current_settings.moduleswitches then
  435. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  436. else
  437. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  438. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  439. if found1 or found2 then
  440. begin
  441. Add('INPUT(');
  442. if found1 then
  443. AddFileName(s1);
  444. if found2 then
  445. AddFileName(s2);
  446. Add(')');
  447. end;
  448. end;
  449. {Entry point.}
  450. add('ENTRY(_start)');
  451. {$ifdef x86_64}
  452. {$define LINKERSCRIPT_INCLUDED}
  453. add('SECTIONS');
  454. add('{');
  455. {Read-only sections, merged into text segment:}
  456. if current_module.islibrary then
  457. add(' . = 0 + SIZEOF_HEADERS;')
  458. else
  459. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  460. add(' . = 0 + SIZEOF_HEADERS;');
  461. add(' .interp : { *(.interp) }');
  462. add(' .hash : { *(.hash) }');
  463. add(' .dynsym : { *(.dynsym) }');
  464. add(' .dynstr : { *(.dynstr) }');
  465. add(' .gnu.version : { *(.gnu.version) }');
  466. add(' .gnu.version_d : { *(.gnu.version_d) }');
  467. add(' .gnu.version_r : { *(.gnu.version_r) }');
  468. add(' .rel.dyn :');
  469. add(' {');
  470. add(' *(.rel.init)');
  471. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  472. add(' *(.rel.fini)');
  473. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  474. add(' *(.rel.data.rel.ro*)');
  475. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  476. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  477. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  478. add(' *(.rel.got)');
  479. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  480. add(' }');
  481. add(' .rela.dyn :');
  482. add(' {');
  483. add(' *(.rela.init)');
  484. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  485. add(' *(.rela.fini)');
  486. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  487. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  488. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  489. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  490. add(' *(.rela.got)');
  491. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  492. add(' }');
  493. add(' .rel.plt : { *(.rel.plt) }');
  494. add(' .rela.plt : { *(.rela.plt) }');
  495. add(' .init :');
  496. add(' {');
  497. add(' KEEP (*(.init))');
  498. add(' } =0x90909090');
  499. add(' .plt : { *(.plt) }');
  500. add(' .text :');
  501. add(' {');
  502. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  503. add(' KEEP (*(.text.*personality*))');
  504. {.gnu.warning sections are handled specially by elf32.em.}
  505. add(' *(.gnu.warning)');
  506. add(' } =0x90909090');
  507. add(' .fini :');
  508. add(' {');
  509. add(' KEEP (*(.fini))');
  510. add(' } =0x90909090');
  511. add(' PROVIDE (_etext = .);');
  512. add(' .rodata :');
  513. add(' {');
  514. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  515. add(' }');
  516. {Adjust the address for the data segment. We want to adjust up to
  517. the same address within the page on the next page up.}
  518. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  519. add(' .dynamic : { *(.dynamic) }');
  520. add(' .got : { *(.got .toc) }');
  521. add(' .got.plt : { *(.got.plt .toc.plt) }');
  522. add(' .data :');
  523. add(' {');
  524. add(' *(.data .data.* .gnu.linkonce.d.*)');
  525. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  526. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  527. add(' }');
  528. add(' PROVIDE (_edata = .);');
  529. add(' PROVIDE (edata = .);');
  530. {$ifdef zsegment_threadvars}
  531. add(' _z = .;');
  532. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  533. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  534. add(' . = _z + SIZEOF (.threadvar);');
  535. {$else}
  536. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  537. {$endif}
  538. add(' __bss_start = .;');
  539. add(' .bss :');
  540. add(' {');
  541. add(' *(.dynbss)');
  542. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  543. add(' *(COMMON)');
  544. {Align here to ensure that the .bss section occupies space up to
  545. _end. Align after .bss to ensure correct alignment even if the
  546. .bss section disappears because there are no input sections.}
  547. add(' . = ALIGN(32 / 8);');
  548. add(' }');
  549. add(' . = ALIGN(32 / 8);');
  550. add(' PROVIDE (_end = .);');
  551. add(' PROVIDE (end = .);');
  552. {Stabs debugging sections.}
  553. add(' .stab 0 : { *(.stab) }');
  554. add(' .stabstr 0 : { *(.stabstr) }');
  555. add(' /* DWARF debug sections.');
  556. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  557. add(' of the section so we begin them at 0. */');
  558. add(' /* DWARF 1 */');
  559. add(' .debug 0 : { *(.debug) }');
  560. add(' .line 0 : { *(.line) }');
  561. add(' /* GNU DWARF 1 extensions */');
  562. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  563. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  564. add(' /* DWARF 1.1 and DWARF 2 */');
  565. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  566. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  567. add(' /* DWARF 2 */');
  568. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  569. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  570. add(' .debug_line 0 : { *(.debug_line) }');
  571. add(' .debug_frame 0 : { *(.debug_frame) }');
  572. add(' .debug_str 0 : { *(.debug_str) }');
  573. add(' .debug_loc 0 : { *(.debug_loc) }');
  574. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  575. add(' /* SGI/MIPS DWARF 2 extensions */');
  576. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  577. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  578. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  579. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  580. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  581. add('}');
  582. {$endif x86_64}
  583. {$ifdef ARM}
  584. if target_info.abi=abi_eabi then
  585. begin
  586. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  587. add('/* Script for -z combreloc: combine and sort reloc sections */');
  588. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  589. add(' "elf32-littlearm")');
  590. add('OUTPUT_ARCH(arm)');
  591. add('ENTRY(_start)');
  592. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  593. add('SECTIONS');
  594. add('{');
  595. add(' /* Read-only sections, merged into text segment: */');
  596. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  597. add(' .interp : { *(.interp) }');
  598. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  599. add(' .hash : { *(.hash) }');
  600. add(' .gnu.hash : { *(.gnu.hash) }');
  601. add(' .dynsym : { *(.dynsym) }');
  602. add(' .dynstr : { *(.dynstr) }');
  603. add(' .gnu.version : { *(.gnu.version) }');
  604. add(' .gnu.version_d : { *(.gnu.version_d) }');
  605. add(' .gnu.version_r : { *(.gnu.version_r) }');
  606. add(' .rel.dyn :');
  607. add(' {');
  608. add(' *(.rel.init)');
  609. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  610. add(' *(.rel.fini)');
  611. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  612. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  613. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  614. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  615. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  616. add(' *(.rel.ctors)');
  617. add(' *(.rel.dtors)');
  618. add(' *(.rel.got)');
  619. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  620. add(' }');
  621. add(' .rela.dyn :');
  622. add(' {');
  623. add(' *(.rela.init)');
  624. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  625. add(' *(.rela.fini)');
  626. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  627. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  628. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  629. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  630. add(' *(.rela.ctors)');
  631. add(' *(.rela.dtors)');
  632. add(' *(.rela.got)');
  633. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  634. add(' }');
  635. add(' .rel.plt : { *(.rel.plt) }');
  636. add(' .rela.plt : { *(.rela.plt) }');
  637. add(' .init :');
  638. add(' {');
  639. add(' KEEP (*(.init))');
  640. add(' } =0');
  641. add(' .plt : { *(.plt) }');
  642. add(' .text :');
  643. add(' {');
  644. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  645. add(' KEEP (*(.text.*personality*))');
  646. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  647. add(' *(.gnu.warning)');
  648. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  649. add(' } =0');
  650. add(' .fini :');
  651. add(' {');
  652. add(' KEEP (*(.fini))');
  653. add(' } =0');
  654. add(' PROVIDE (__etext = .);');
  655. add(' PROVIDE (_etext = .);');
  656. add(' PROVIDE (etext = .);');
  657. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  658. add(' .rodata1 : { *(.rodata1) }');
  659. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  660. add(' __exidx_start = .;');
  661. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  662. add(' __exidx_end = .;');
  663. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  664. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  665. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  666. add(' /* Adjust the address for the data segment. We want to adjust up to');
  667. add(' the same address within the page on the next page up. */');
  668. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  669. add(' /* Exception handling */');
  670. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  671. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  672. add(' /* Thread Local Storage sections */');
  673. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  674. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  675. add(' .preinit_array :');
  676. add(' {');
  677. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  678. add(' KEEP (*(.preinit_array))');
  679. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  680. add(' }');
  681. add(' .init_array :');
  682. add(' {');
  683. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  684. add(' KEEP (*(SORT(.init_array.*)))');
  685. add(' KEEP (*(.init_array))');
  686. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  687. add(' }');
  688. add(' .fini_array :');
  689. add(' {');
  690. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  691. add(' KEEP (*(.fini_array))');
  692. add(' KEEP (*(SORT(.fini_array.*)))');
  693. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  694. add(' }');
  695. add(' .ctors :');
  696. add(' {');
  697. add(' /* gcc uses crtbegin.o to find the start of');
  698. add(' the constructors, so we make sure it is');
  699. add(' first. Because this is a wildcard, it');
  700. add(' doesn''t matter if the user does not');
  701. add(' actually link against crtbegin.o; the');
  702. add(' linker won''t look for a file to match a');
  703. add(' wildcard. The wildcard also means that it');
  704. add(' doesn''t matter which directory crtbegin.o');
  705. add(' is in. */');
  706. add(' KEEP (*crtbegin.o(.ctors))');
  707. add(' KEEP (*crtbegin?.o(.ctors))');
  708. add(' /* We don''t want to include the .ctor section from');
  709. add(' the crtend.o file until after the sorted ctors.');
  710. add(' The .ctor section from the crtend file contains the');
  711. add(' end of ctors marker and it must be last */');
  712. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  713. add(' KEEP (*(SORT(.ctors.*)))');
  714. add(' KEEP (*(.ctors))');
  715. add(' }');
  716. add(' .dtors :');
  717. add(' {');
  718. add(' KEEP (*crtbegin.o(.dtors))');
  719. add(' KEEP (*crtbegin?.o(.dtors))');
  720. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  721. add(' KEEP (*(SORT(.dtors.*)))');
  722. add(' KEEP (*(.dtors))');
  723. add(' }');
  724. add(' .jcr : { KEEP (*(.jcr)) }');
  725. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  726. add(' .dynamic : { *(.dynamic) }');
  727. add(' .got : { *(.got.plt) *(.got) }');
  728. add(' .data :');
  729. add(' {');
  730. add(' __data_start = . ;');
  731. add(' *(.data .data.* .gnu.linkonce.d.*)');
  732. { extra by FPC }
  733. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  734. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  735. add(' SORT(CONSTRUCTORS)');
  736. add(' }');
  737. add(' .data1 : { *(.data1) }');
  738. add(' _edata = .; PROVIDE (edata = .);');
  739. add(' __bss_start = .;');
  740. add(' __bss_start__ = .;');
  741. add(' .bss :');
  742. add(' {');
  743. add(' *(.dynbss)');
  744. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  745. add(' *(COMMON)');
  746. add(' /* Align here to ensure that the .bss section occupies space up to');
  747. add(' _end. Align after .bss to ensure correct alignment even if the');
  748. add(' .bss section disappears because there are no input sections.');
  749. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  750. add(' pad the .data section. */');
  751. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  752. add(' }');
  753. add(' _bss_end__ = . ; __bss_end__ = . ;');
  754. add(' . = ALIGN(32 / 8);');
  755. add(' . = ALIGN(32 / 8);');
  756. add(' __end__ = . ;');
  757. add(' _end = .; PROVIDE (end = .);');
  758. add(' /* Stabs debugging sections. */');
  759. add(' .stab 0 : { *(.stab) }');
  760. add(' .stabstr 0 : { *(.stabstr) }');
  761. add(' .stab.excl 0 : { *(.stab.excl) }');
  762. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  763. add(' .stab.index 0 : { *(.stab.index) }');
  764. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  765. add(' .comment 0 : { *(.comment) }');
  766. add(' /* DWARF debug sections.');
  767. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  768. add(' of the section so we begin them at 0. */');
  769. add(' /* DWARF 1 */');
  770. add(' .debug 0 : { *(.debug) }');
  771. add(' .line 0 : { *(.line) }');
  772. add(' /* GNU DWARF 1 extensions */');
  773. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  774. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  775. add(' /* DWARF 1.1 and DWARF 2 */');
  776. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  777. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  778. add(' /* DWARF 2 */');
  779. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  780. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  781. add(' .debug_line 0 : { *(.debug_line) }');
  782. add(' .debug_frame 0 : { *(.debug_frame) }');
  783. add(' .debug_str 0 : { *(.debug_str) }');
  784. add(' .debug_loc 0 : { *(.debug_loc) }');
  785. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  786. add(' /* SGI/MIPS DWARF 2 extensions */');
  787. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  788. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  789. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  790. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  791. add(' /* DWARF 3 */');
  792. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  793. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  794. add(' .stack 0x80000 :');
  795. add(' {');
  796. add(' _stack = .;');
  797. add(' *(.stack)');
  798. add(' }');
  799. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  800. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  801. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  802. add('}');
  803. end
  804. else
  805. {$endif ARM}
  806. {$ifndef LINKERSCRIPT_INCLUDED}
  807. begin
  808. {Sections.}
  809. add('SECTIONS');
  810. add('{');
  811. {Read-only sections, merged into text segment:}
  812. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 + SIZEOF_HEADERS;');
  813. add(' .interp : { *(.interp) }');
  814. add(' .hash : { *(.hash) }');
  815. add(' .dynsym : { *(.dynsym) }');
  816. add(' .dynstr : { *(.dynstr) }');
  817. add(' .gnu.version : { *(.gnu.version) }');
  818. add(' .gnu.version_d : { *(.gnu.version_d) }');
  819. add(' .gnu.version_r : { *(.gnu.version_r) }');
  820. add(' .rel.dyn :');
  821. add(' {');
  822. add(' *(.rel.init)');
  823. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  824. add(' *(.rel.fini)');
  825. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  826. add(' *(.rel.data.rel.ro*)');
  827. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  828. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  829. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  830. add(' *(.rel.got)');
  831. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  832. add(' }');
  833. add(' .rela.dyn :');
  834. add(' {');
  835. add(' *(.rela.init)');
  836. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  837. add(' *(.rela.fini)');
  838. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  839. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  840. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  841. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  842. add(' *(.rela.got)');
  843. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  844. add(' }');
  845. add(' .rel.plt : { *(.rel.plt) }');
  846. add(' .rela.plt : { *(.rela.plt) }');
  847. add(' .init :');
  848. add(' {');
  849. add(' KEEP (*(.init))');
  850. add(' } =0x90909090');
  851. add(' .plt : { *(.plt) }');
  852. add(' .text :');
  853. add(' {');
  854. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  855. add(' KEEP (*(.text.*personality*))');
  856. {.gnu.warning sections are handled specially by elf32.em.}
  857. add(' *(.gnu.warning)');
  858. add(' } =0x90909090');
  859. add(' .fini :');
  860. add(' {');
  861. add(' KEEP (*(.fini))');
  862. add(' } =0x90909090');
  863. add(' PROVIDE (_etext = .);');
  864. add(' .rodata :');
  865. add(' {');
  866. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  867. add(' }');
  868. {Adjust the address for the data segment. We want to adjust up to
  869. the same address within the page on the next page up.}
  870. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  871. add(' .dynamic : { *(.dynamic) }');
  872. add(' .got : { *(.got) }');
  873. add(' .got.plt : { *(.got.plt) }');
  874. add(' .data :');
  875. add(' {');
  876. add(' *(.data .data.* .gnu.linkonce.d.*)');
  877. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  878. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  879. add(' }');
  880. add(' PROVIDE (_edata = .);');
  881. add(' PROVIDE (edata = .);');
  882. {$ifdef zsegment_threadvars}
  883. add(' _z = .;');
  884. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  885. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  886. add(' . = _z + SIZEOF (.threadvar);');
  887. {$else}
  888. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  889. {$endif}
  890. add(' __bss_start = .;');
  891. add(' .bss :');
  892. add(' {');
  893. add(' *(.dynbss)');
  894. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  895. add(' *(COMMON)');
  896. {Align here to ensure that the .bss section occupies space up to
  897. _end. Align after .bss to ensure correct alignment even if the
  898. .bss section disappears because there are no input sections.}
  899. add(' . = ALIGN(32 / 8);');
  900. add(' }');
  901. add(' . = ALIGN(32 / 8);');
  902. add(' PROVIDE (_end = .);');
  903. add(' PROVIDE (end = .);');
  904. {Stabs debugging sections.}
  905. add(' .stab 0 : { *(.stab) }');
  906. add(' .stabstr 0 : { *(.stabstr) }');
  907. add('}');
  908. end;
  909. {$endif LINKERSCRIPT_INCLUDED}
  910. { Write and Close response }
  911. writetodisk;
  912. Free;
  913. end;
  914. WriteResponseFile:=True;
  915. end;
  916. function TLinkerLinux.MakeExecutable:boolean;
  917. var
  918. i : longint;
  919. binstr,
  920. cmdstr : TCmdStr;
  921. success : boolean;
  922. DynLinkStr : string;
  923. GCSectionsStr,
  924. StaticStr,
  925. StripStr : string[40];
  926. begin
  927. if not(cs_link_nolink in current_settings.globalswitches) then
  928. Message1(exec_i_linking,current_module.exefilename^);
  929. { Create some replacements }
  930. StaticStr:='';
  931. StripStr:='';
  932. GCSectionsStr:='';
  933. DynLinkStr:='';
  934. if (cs_link_staticflag in current_settings.globalswitches) then
  935. StaticStr:='-static';
  936. if (cs_link_strip in current_settings.globalswitches) and
  937. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  938. StripStr:='-s';
  939. if (cs_link_map in current_settings.globalswitches) then
  940. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  941. if create_smartlink_sections then
  942. GCSectionsStr:='--gc-sections';
  943. If (cs_profile in current_settings.moduleswitches) or
  944. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  945. begin
  946. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  947. if cshared then
  948. DynLinkStr:=DynLinkStr+' --shared ';
  949. if rlinkpath<>'' then
  950. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  951. End;
  952. { Write used files and libraries }
  953. WriteResponseFile(false);
  954. { Call linker }
  955. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  956. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  957. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  958. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  959. Replace(cmdstr,'$STATIC',StaticStr);
  960. Replace(cmdstr,'$STRIP',StripStr);
  961. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  962. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  963. { create dynamic symbol table? }
  964. if HasExports then
  965. cmdstr:=cmdstr+' -E';
  966. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  967. { Create external .dbg file with debuginfo }
  968. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  969. begin
  970. for i:=1 to 3 do
  971. begin
  972. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  973. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  974. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename^)));
  975. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename^));
  976. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  977. if not success then
  978. break;
  979. end;
  980. end;
  981. { Remove ReponseFile }
  982. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  983. DeleteFile(outputexedir+Info.ResName);
  984. MakeExecutable:=success; { otherwise a recursive call to link method }
  985. end;
  986. Function TLinkerLinux.MakeSharedLibrary:boolean;
  987. var
  988. InitStr,
  989. FiniStr,
  990. SoNameStr : string[80];
  991. binstr,
  992. cmdstr : TCmdStr;
  993. success : boolean;
  994. begin
  995. MakeSharedLibrary:=false;
  996. if not(cs_link_nolink in current_settings.globalswitches) then
  997. Message1(exec_i_linking,current_module.sharedlibfilename^);
  998. { Write used files and libraries }
  999. WriteResponseFile(true);
  1000. { Create some replacements }
  1001. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  1002. InitStr:='-init FPC_SHARED_LIB_START';
  1003. FiniStr:='-fini FPC_LIB_EXIT';
  1004. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  1005. { Call linker }
  1006. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1007. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1008. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1009. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1010. Replace(cmdstr,'$INIT',InitStr);
  1011. Replace(cmdstr,'$FINI',FiniStr);
  1012. Replace(cmdstr,'$SONAME',SoNameStr);
  1013. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1014. { Strip the library ? }
  1015. if success and (cs_link_strip in current_settings.globalswitches) then
  1016. begin
  1017. { only remove non global symbols and debugging info for a library }
  1018. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1019. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1020. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1021. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1022. end;
  1023. { Remove ReponseFile }
  1024. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1025. DeleteFile(outputexedir+Info.ResName);
  1026. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1027. end;
  1028. {*****************************************************************************
  1029. Initialize
  1030. *****************************************************************************}
  1031. initialization
  1032. {$ifdef i386}
  1033. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  1034. RegisterImport(system_i386_linux,timportliblinux);
  1035. RegisterExport(system_i386_linux,texportliblinux);
  1036. RegisterTarget(system_i386_linux_info);
  1037. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  1038. RegisterImport(system_x86_6432_linux,timportliblinux);
  1039. RegisterExport(system_x86_6432_linux,texportliblinux);
  1040. RegisterTarget(system_x86_6432_linux_info);
  1041. {$endif i386}
  1042. {$ifdef m68k}
  1043. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  1044. RegisterImport(system_m68k_linux,timportliblinux);
  1045. RegisterExport(system_m68k_linux,texportliblinux);
  1046. RegisterTarget(system_m68k_linux_info);
  1047. {$endif m68k}
  1048. {$ifdef powerpc}
  1049. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  1050. RegisterImport(system_powerpc_linux,timportliblinux);
  1051. RegisterExport(system_powerpc_linux,texportliblinux);
  1052. RegisterTarget(system_powerpc_linux_info);
  1053. {$endif powerpc}
  1054. {$ifdef powerpc64}
  1055. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  1056. RegisterImport(system_powerpc64_linux,timportliblinux);
  1057. RegisterExport(system_powerpc64_linux,texportliblinux);
  1058. RegisterTarget(system_powerpc64_linux_info);
  1059. {$endif powerpc64}
  1060. {$ifdef alpha}
  1061. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  1062. RegisterImport(system_alpha_linux,timportliblinux);
  1063. RegisterExport(system_alpha_linux,texportliblinux);
  1064. RegisterTarget(system_alpha_linux_info);
  1065. {$endif alpha}
  1066. {$ifdef x86_64}
  1067. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  1068. RegisterImport(system_x86_64_linux,timportliblinux);
  1069. RegisterExport(system_x86_64_linux,texportliblinux);
  1070. RegisterTarget(system_x86_64_linux_info);
  1071. {$endif x86_64}
  1072. {$ifdef SPARC}
  1073. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  1074. RegisterImport(system_SPARC_linux,timportliblinux);
  1075. RegisterExport(system_SPARC_linux,texportliblinux);
  1076. RegisterTarget(system_SPARC_linux_info);
  1077. {$endif SPARC}
  1078. {$ifdef ARM}
  1079. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  1080. RegisterImport(system_arm_linux,timportliblinux);
  1081. RegisterExport(system_arm_linux,texportliblinux);
  1082. RegisterTarget(system_arm_linux_info);
  1083. {$endif ARM}
  1084. {$ifdef MIPS}
  1085. {$ifdef MIPSEL}
  1086. RegisterExternalLinker(system_mipsel_linux_info,TLinkerLinux);
  1087. RegisterImport(system_mipsel_linux,timportliblinux);
  1088. RegisterExport(system_mipsel_linux,texportliblinux);
  1089. RegisterTarget(system_mipsel_linux_info);
  1090. {$else MIPS}
  1091. RegisterExternalLinker(system_mips_linux_info,TLinkerLinux);
  1092. RegisterImport(system_mips_linux,timportliblinux);
  1093. RegisterExport(system_mips_linux,texportliblinux);
  1094. RegisterTarget(system_mips_linux_info);
  1095. {$endif MIPSEL}
  1096. {$endif MIPS}
  1097. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1098. end.