t_linux.pas 43 KB

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