t_linux.pas 45 KB

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