t_linux.pas 43 KB

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