2
0

t_linux.pas 44 KB

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