t_linux.pas 44 KB

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