t_linux.pas 45 KB

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