t_linux.pas 44 KB

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