t_linux.pas 44 KB

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