t_linux.pas 45 KB

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