t_linux.pas 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. TInternalLinkerLinux=class(TInternalLinker)
  49. private
  50. libctype: TLibcType;
  51. reorder: boolean;
  52. linklibc: boolean;
  53. prtobj: string[20];
  54. dynlinker: string[100];
  55. public
  56. constructor Create;override;
  57. procedure DefaultLinkScript;override;
  58. procedure InitSysInitUnitName;override;
  59. end;
  60. implementation
  61. uses
  62. SysUtils,
  63. cutils,cfileutl,cclasses,
  64. verbose,systems,globtype,globals,
  65. symconst,script,
  66. fmodule,
  67. aasmbase,aasmtai,aasmcpu,cpubase,
  68. cgbase,cgobj,cgutils,ogbase,ncgutil,
  69. comprsrc,
  70. ogelf,
  71. rescmn, i_linux
  72. ;
  73. {*****************************************************************************
  74. TIMPORTLIBLINUX
  75. *****************************************************************************}
  76. procedure timportliblinux.generatelib;
  77. var
  78. i : longint;
  79. ImportLibrary : TImportLibrary;
  80. begin
  81. for i:=0 to current_module.ImportLibraryList.Count-1 do
  82. begin
  83. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  84. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  85. end;
  86. end;
  87. {*****************************************************************************
  88. TEXPORTLIBLINUX
  89. *****************************************************************************}
  90. procedure texportliblinux.setfininame(list: TAsmList; const s: string);
  91. begin
  92. { the problem with not having a .fini section is that a finalization
  93. routine in regular code can get "smart" linked away -> reference it
  94. just like the debug info }
  95. new_section(list,sec_fpc,'links',0);
  96. list.concat(Tai_const.Createname(s,0));
  97. inherited setfininame(list,s);
  98. end;
  99. {*****************************************************************************
  100. TLINKERLINUX
  101. *****************************************************************************}
  102. procedure SetupLibrarySearchPath;
  103. begin
  104. if not Dontlinkstdlibpath Then
  105. {$ifdef x86_64}
  106. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  107. {$else}
  108. {$ifdef powerpc64}
  109. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  110. {$else powerpc64}
  111. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  112. {$endif powerpc64}
  113. {$endif x86_64}
  114. {$ifdef arm}
  115. { some newer Debian have the crt*.o files at uncommon locations,
  116. for other arm flavours, this cannot hurt }
  117. if not Dontlinkstdlibpath Then
  118. {$ifdef FPC_ARMHF}
  119. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabihf',true);
  120. {$endif FPC_ARMHF}
  121. {$ifdef FPC_ARMEL}
  122. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabi',true);
  123. {$endif}
  124. {$endif arm}
  125. {$ifdef x86_64}
  126. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/x86_64-linux-gnu',true);
  127. {$endif x86_64}
  128. {$ifdef i386}
  129. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/i386-linux-gnu',true);
  130. {$endif i386}
  131. end;
  132. {$ifdef m68k}
  133. const defdynlinker='/lib/ld.so.1';
  134. {$endif m68k}
  135. {$ifdef i386}
  136. const defdynlinker='/lib/ld-linux.so.2';
  137. {$endif}
  138. {$ifdef x86_64}
  139. const defdynlinker='/lib64/ld-linux-x86-64.so.2';
  140. {$endif x86_64}
  141. {$ifdef sparc}
  142. const defdynlinker='/lib/ld-linux.so.2';
  143. {$endif sparc}
  144. {$ifdef powerpc}
  145. const defdynlinker='/lib/ld.so.1';
  146. {$endif powerpc}
  147. {$ifdef powerpc64}
  148. const defdynlinker='/lib64/ld64.so.1';
  149. {$endif powerpc64}
  150. {$ifdef arm}
  151. {$ifdef FPC_ARMHF}
  152. const defdynlinker='/lib/ld-linux-armhf.so.3';
  153. {$else FPC_ARMHF}
  154. {$ifdef FPC_ARMEL}
  155. const defdynlinker='/lib/ld-linux.so.3';
  156. {$else FPC_ARMEL}
  157. const defdynlinker='/lib/ld-linux.so.2';
  158. {$endif FPC_ARMEL}
  159. {$endif FPC_ARMHF}
  160. {$endif arm}
  161. {$ifdef aarch64}
  162. const defdynlinker='/lib/ld-linux-aarch64.so.1';
  163. {$endif aarch64}
  164. {$ifdef mips}
  165. const defdynlinker='/lib/ld.so.1';
  166. {$endif mips}
  167. procedure SetupDynlinker(out DynamicLinker:string;out libctype:TLibcType);
  168. begin
  169. {
  170. Search order:
  171. glibc 2.1+
  172. uclibc
  173. glibc 2.0
  174. If none is found (e.g. when cross compiling) glibc21 is assumed
  175. }
  176. if fileexists(sysrootpath+defdynlinker,false) then
  177. begin
  178. DynamicLinker:=defdynlinker;
  179. {$ifdef i386}
  180. libctype:=glibc21;
  181. {$else i386}
  182. libctype:=glibc2;
  183. {$endif i386}
  184. end
  185. else if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  186. begin
  187. DynamicLinker:='/lib/ld-uClibc.so.0';
  188. libctype:=uclibc;
  189. end
  190. {$ifdef i386}
  191. else if FileExists(sysrootpath+'/lib/ld-linux.so.1',false) then
  192. begin
  193. DynamicLinker:='/lib/ld-linux.so.1';
  194. libctype:=glibc2;
  195. end
  196. {$endif i386}
  197. else
  198. begin
  199. { when no dyn. linker is found, we are probably
  200. cross compiling, so use the default dyn. linker }
  201. DynamicLinker:=defdynlinker;
  202. {
  203. the default c startup script is gcrt0.as on all platforms
  204. except i386
  205. }
  206. {$ifdef i386}
  207. libctype:=glibc21;
  208. {$else i386}
  209. libctype:=glibc2;
  210. {$endif i386}
  211. end;
  212. end;
  213. function ModulesLinkToLibc:boolean;
  214. var
  215. hp: tmodule;
  216. begin
  217. { This is called very early, ImportLibraryList is not yet merged into linkothersharedlibs.
  218. The former contains library names qualified with prefix and suffix (coming from
  219. "external 'c' name 'foo' declarations), the latter contains raw names (from "$linklib c"
  220. directives). }
  221. hp:=tmodule(loaded_units.first);
  222. while assigned(hp) do
  223. begin
  224. result:=Assigned(hp.ImportLibraryList.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext));
  225. if result then break;
  226. result:=hp.linkothersharedlibs.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext);
  227. if result then break;
  228. result:=hp.linkothersharedlibs.find('c');
  229. if result then break;
  230. hp:=tmodule(hp.next);
  231. end;
  232. end;
  233. Constructor TLinkerLinux.Create;
  234. begin
  235. Inherited Create;
  236. SetupLibrarySearchPath;
  237. end;
  238. procedure TLinkerLinux.SetDefaultInfo;
  239. {
  240. This will also detect which libc version will be used
  241. }
  242. const
  243. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  244. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  245. {$ifdef powerpc} platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  246. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  247. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  248. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  249. {$ifdef aarch64} platform_select='';{$endif} {unknown :( }
  250. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  251. {$ifdef mips}
  252. {$ifdef mipsel}
  253. platform_select='-EL';
  254. {$else}
  255. platform_select='-EB';
  256. {$endif}
  257. {$endif}
  258. begin
  259. with Info do
  260. begin
  261. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  262. { when we want to cross-link we need to override default library paths }
  263. if length(sysrootpath) > 0 then
  264. ExeCmd[1]:=ExeCmd[1]+' -T';
  265. ExeCmd[1]:=ExeCmd[1]+' $RES';
  266. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES';
  267. DllCmd[2]:='strip --strip-unneeded $EXE';
  268. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  269. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  270. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  271. SetupDynlinker(DynamicLinker,libctype);
  272. end;
  273. end;
  274. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  275. // put your linkorder/linkalias overrides here.
  276. // Note: assumes only called when reordering/aliasing is used.
  277. Begin
  278. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  279. Begin
  280. LinkLibraryOrder.add('gcc','',15);
  281. LinkLibraryOrder.add('c','',100);
  282. LinkLibraryOrder.add('gmon','',120);
  283. LinkLibraryOrder.add('dl','',140);
  284. LinkLibraryOrder.add('pthread','',160);
  285. end;
  286. End;
  287. type
  288. tlibcnames=array [TLibcType] of string[8];
  289. const { libc5 glibc2 glibc21 uclibc }
  290. cprtnames: tlibcnames = ('cprt0', 'cprt0', 'cprt21', 'ucprt0');
  291. csinames: tlibcnames = ('si_c', 'si_c', 'si_c21', 'si_uc');
  292. gprtnames: tlibcnames = ('gprt0', 'gprt0', 'gprt21', 'ugprt0');
  293. gsinames: tlibcnames = ('si_g', 'si_g', 'si_c21g','si_ucg');
  294. defprtnames: array[boolean] of string[8] = ('prt0', 'dllprt0');
  295. defsinames: array[boolean] of string[8] = ('si_prc','si_dll');
  296. { uclibc and glibc21 are not available on x86_64! si_g is also absent. }
  297. Procedure TLinkerLinux.InitSysInitUnitName;
  298. begin
  299. linklibc:=ModulesLinkToLibc;
  300. reorder:=linklibc and ReOrderEntries;
  301. sysinitunit:=defsinames[current_module.islibrary];
  302. prtobj:=defprtnames[current_module.islibrary];
  303. if current_module.islibrary then
  304. exit;
  305. if cs_profile in current_settings.moduleswitches then
  306. begin
  307. prtobj:=gprtnames[libctype];
  308. sysinitunit:=gsinames[libctype];
  309. linklibc:=true;
  310. end
  311. else if linklibc then
  312. begin
  313. prtobj:=cprtnames[libctype];
  314. sysinitunit:=csinames[libctype];
  315. end;
  316. end;
  317. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  318. Var
  319. linkres : TLinkRes;
  320. i : longint;
  321. HPath : TCmdStrListItem;
  322. s,s1,s2 : TCmdStr;
  323. found1,
  324. found2 : boolean;
  325. linksToSharedLibFiles : boolean;
  326. begin
  327. result:=False;
  328. { set special options for some targets }
  329. if cs_profile in current_settings.moduleswitches then
  330. begin
  331. if not(libctype in [glibc2,glibc21]) then
  332. AddSharedLibrary('gmon');
  333. AddSharedLibrary('c');
  334. end;
  335. { Open link.res file }
  336. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  337. with linkres do
  338. begin
  339. { Write path to search libraries }
  340. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  341. while assigned(HPath) do
  342. begin
  343. Add('SEARCH_DIR("'+HPath.Str+'")');
  344. HPath:=TCmdStrListItem(HPath.Next);
  345. end;
  346. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  347. while assigned(HPath) do
  348. begin
  349. Add('SEARCH_DIR("'+HPath.Str+'")');
  350. HPath:=TCmdStrListItem(HPath.Next);
  351. end;
  352. { force local symbol resolution (i.e., inside the shared }
  353. { library itself) for all non-exorted symbols, otherwise }
  354. { several RTL symbols of FPC-compiled shared libraries }
  355. { will be bound to those of a single shared library or }
  356. { to the main program }
  357. if (isdll) then
  358. begin
  359. add('VERSION');
  360. add('{');
  361. add(' {');
  362. if not texportlibunix(exportlib).exportedsymnames.empty then
  363. begin
  364. add(' global:');
  365. repeat
  366. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  367. until texportlibunix(exportlib).exportedsymnames.empty;
  368. end;
  369. add(' local:');
  370. add(' *;');
  371. add(' };');
  372. add('}');
  373. end;
  374. StartSection('INPUT(');
  375. { add objectfiles, start with prt0 always }
  376. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  377. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  378. { try to add crti and crtbegin if linking to C }
  379. if linklibc and (libctype<>uclibc) then
  380. begin
  381. { crti.o must come first }
  382. if librarysearchpath.FindFile('crti.o',false,s) then
  383. AddFileName(s)
  384. else
  385. Message1(exec_w_init_file_not_found,'crti.o');
  386. { then the crtbegin* }
  387. if cs_create_pic in current_settings.moduleswitches then
  388. begin
  389. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  390. AddFileName(s)
  391. else
  392. Message1(exec_w_init_file_not_found,'crtbeginS.o');
  393. end
  394. else
  395. if (cs_link_staticflag in current_settings.globalswitches) then
  396. begin
  397. if librarysearchpath.FindFile('crtbeginT.o',false,s) then
  398. AddFileName(s)
  399. else
  400. Message1(exec_w_init_file_not_found,'crtbeginT.o');
  401. end
  402. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  403. AddFileName(s)
  404. else
  405. Message1(exec_w_init_file_not_found,'crtbegin.o');
  406. end;
  407. { main objectfiles }
  408. while not ObjectFiles.Empty do
  409. begin
  410. s:=ObjectFiles.GetFirst;
  411. if s<>'' then
  412. AddFileName(maybequoted(s));
  413. end;
  414. EndSection(')');
  415. { Write staticlibraries }
  416. if not StaticLibFiles.Empty then
  417. begin
  418. Add('GROUP(');
  419. While not StaticLibFiles.Empty do
  420. begin
  421. S:=StaticLibFiles.GetFirst;
  422. AddFileName(maybequoted(s))
  423. end;
  424. Add(')');
  425. end;
  426. // we must reorder here because the result could empty sharedlibfiles
  427. if reorder Then
  428. ExpandAndApplyOrder(SharedLibFiles);
  429. // after this point addition of shared libs not allowed.
  430. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  431. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  432. if (isdll) then
  433. begin
  434. Add('INPUT(');
  435. Add(info.DynamicLinker);
  436. Add(')');
  437. end;
  438. linksToSharedLibFiles := not SharedLibFiles.Empty;
  439. if not SharedLibFiles.Empty then
  440. begin
  441. if (SharedLibFiles.Count<>1) or
  442. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  443. reorder then
  444. begin
  445. Add('INPUT(');
  446. While not SharedLibFiles.Empty do
  447. begin
  448. S:=SharedLibFiles.GetFirst;
  449. if (s<>'c') or reorder then
  450. begin
  451. i:=Pos(target_info.sharedlibext,S);
  452. if i>0 then
  453. Delete(S,i,255);
  454. Add('-l'+s);
  455. end
  456. else
  457. begin
  458. linklibc:=true;
  459. end;
  460. end;
  461. Add(')');
  462. end
  463. else
  464. linklibc:=true;
  465. if (cs_link_staticflag in current_settings.globalswitches) or
  466. (linklibc and not reorder) then
  467. begin
  468. Add('GROUP(');
  469. { when we have -static for the linker the we also need libgcc }
  470. if (cs_link_staticflag in current_settings.globalswitches) then
  471. begin
  472. Add('-lgcc');
  473. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  474. Add('-lgcc_eh');
  475. end;
  476. { be sure that libc is the last lib }
  477. if linklibc and not reorder then
  478. Add('-lc');
  479. Add(')');
  480. end;
  481. end;
  482. { objects which must be at the end }
  483. if linklibc and (libctype<>uclibc) then
  484. begin
  485. if cs_create_pic in current_settings.moduleswitches then
  486. begin
  487. found1:=librarysearchpath.FindFile('crtendS.o',false,s1);
  488. if not(found1) then
  489. Message1(exec_w_init_file_not_found,'crtendS.o');
  490. end
  491. else
  492. begin
  493. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  494. if not(found1) then
  495. Message1(exec_w_init_file_not_found,'crtend.o');
  496. end;
  497. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  498. if not(found2) then
  499. Message1(exec_w_init_file_not_found,'crtn.o');
  500. if found1 or found2 then
  501. begin
  502. Add('INPUT(');
  503. if found1 then
  504. AddFileName(s1);
  505. if found2 then
  506. AddFileName(s2);
  507. Add(')');
  508. end;
  509. end;
  510. {Entry point. Only needed for executables, set on the linker command line for
  511. shared libraries. }
  512. if (not isdll) then
  513. if (linksToSharedLibFiles and not linklibc) then
  514. add('ENTRY(_dynamic_start)')
  515. else
  516. add('ENTRY(_start)');
  517. {$ifdef x86_64}
  518. {$define LINKERSCRIPT_INCLUDED}
  519. add('SECTIONS');
  520. add('{');
  521. {Read-only sections, merged into text segment:}
  522. if current_module.islibrary then
  523. add(' . = 0 + SIZEOF_HEADERS;')
  524. else
  525. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  526. add(' . = 0 + SIZEOF_HEADERS;');
  527. add(' .interp : { *(.interp) }');
  528. add(' .hash : { *(.hash) }');
  529. add(' .dynsym : { *(.dynsym) }');
  530. add(' .dynstr : { *(.dynstr) }');
  531. add(' .gnu.version : { *(.gnu.version) }');
  532. add(' .gnu.version_d : { *(.gnu.version_d) }');
  533. add(' .gnu.version_r : { *(.gnu.version_r) }');
  534. add(' .rel.dyn :');
  535. add(' {');
  536. add(' *(.rel.init)');
  537. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  538. add(' *(.rel.fini)');
  539. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  540. add(' *(.rel.data.rel.ro*)');
  541. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  542. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  543. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  544. add(' *(.rel.got)');
  545. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  546. add(' }');
  547. add(' .rela.dyn :');
  548. add(' {');
  549. add(' *(.rela.init)');
  550. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  551. add(' *(.rela.fini)');
  552. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  553. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  554. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  555. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  556. add(' *(.rela.got)');
  557. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  558. add(' }');
  559. add(' .rel.plt : { *(.rel.plt) }');
  560. add(' .rela.plt : { *(.rela.plt) }');
  561. add(' .init :');
  562. add(' {');
  563. add(' KEEP (*(.init))');
  564. add(' } =0x90909090');
  565. add(' .plt : { *(.plt) }');
  566. add(' .text :');
  567. add(' {');
  568. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  569. add(' KEEP (*(.text.*personality*))');
  570. {.gnu.warning sections are handled specially by elf32.em.}
  571. add(' *(.gnu.warning)');
  572. add(' } =0x90909090');
  573. add(' .fini :');
  574. add(' {');
  575. add(' KEEP (*(.fini))');
  576. add(' } =0x90909090');
  577. add(' PROVIDE (_etext = .);');
  578. add(' .rodata :');
  579. add(' {');
  580. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  581. add(' }');
  582. {Adjust the address for the data segment. We want to adjust up to
  583. the same address within the page on the next page up.}
  584. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  585. add(' .dynamic : { *(.dynamic) }');
  586. add(' .got : { *(.got .toc) }');
  587. add(' .got.plt : { *(.got.plt .toc.plt) }');
  588. add(' .data :');
  589. add(' {');
  590. add(' *(.data .data.* .gnu.linkonce.d.*)');
  591. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  592. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  593. add(' }');
  594. add(' PROVIDE (_edata = .);');
  595. add(' PROVIDE (edata = .);');
  596. {$ifdef zsegment_threadvars}
  597. add(' _z = .;');
  598. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  599. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  600. add(' . = _z + SIZEOF (.threadvar);');
  601. {$else}
  602. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  603. {$endif}
  604. add(' __bss_start = .;');
  605. add(' .bss :');
  606. add(' {');
  607. add(' *(.dynbss)');
  608. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  609. add(' *(COMMON)');
  610. {Align here to ensure that the .bss section occupies space up to
  611. _end. Align after .bss to ensure correct alignment even if the
  612. .bss section disappears because there are no input sections.}
  613. add(' . = ALIGN(32 / 8);');
  614. add('}');
  615. add(' . = ALIGN(32 / 8);');
  616. add(' PROVIDE (_end = .);');
  617. add(' PROVIDE (end = .);');
  618. {Stabs debugging sections.}
  619. add(' .stab 0 : { *(.stab) }');
  620. add(' .stabstr 0 : { *(.stabstr) }');
  621. add(' /* DWARF debug sections.');
  622. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  623. add(' of the section so we begin them at 0. */');
  624. add(' /* DWARF 1 */');
  625. add(' .debug 0 : { *(.debug) }');
  626. add(' .line 0 : { *(.line) }');
  627. add(' /* GNU DWARF 1 extensions */');
  628. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  629. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  630. add(' /* DWARF 1.1 and DWARF 2 */');
  631. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  632. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  633. add(' /* DWARF 2 */');
  634. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  635. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  636. add(' .debug_line 0 : { *(.debug_line) }');
  637. add(' .debug_frame 0 : { *(.debug_frame) }');
  638. add(' .debug_str 0 : { *(.debug_str) }');
  639. add(' .debug_loc 0 : { *(.debug_loc) }');
  640. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  641. add(' /* SGI/MIPS DWARF 2 extensions */');
  642. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  643. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  644. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  645. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  646. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  647. add('}');
  648. {$endif x86_64}
  649. {$ifdef AArch64}
  650. {$define LINKERSCRIPT_INCLUDED}
  651. if isdll or (sysrootpath='') then begin
  652. { On other architectures, supplying a complete linker script
  653. without the -T option just results in:
  654. warning: link.res contains output sections; did you forget -T?
  655. However, with a recent aarch64 linker the result is:
  656. /usr/bin/ld: internal error ../../ld/ldlang.c 5221
  657. So in these cases, where -T will not be used, we supply a
  658. minimal linker script with just the FPC-specific part: }
  659. add('SECTIONS');
  660. add('{');
  661. add(' .data :');
  662. add(' {');
  663. { extra by FPC }
  664. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  665. add(' }');
  666. add('}');
  667. end else begin
  668. { Complete linker script for aarch64-linux: }
  669. add('SECTIONS');
  670. add('{');
  671. add(' /* Read-only sections, merged into text segment: */');
  672. add(' PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;');
  673. add(' .interp : { *(.interp) }');
  674. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  675. add(' .hash : { *(.hash) }');
  676. add(' .gnu.hash : { *(.gnu.hash) }');
  677. add(' .dynsym : { *(.dynsym) }');
  678. add(' .dynstr : { *(.dynstr) }');
  679. add(' .gnu.version : { *(.gnu.version) }');
  680. add(' .gnu.version_d : { *(.gnu.version_d) }');
  681. add(' .gnu.version_r : { *(.gnu.version_r) }');
  682. add(' .rela.dyn :');
  683. add(' {');
  684. add(' *(.rela.init)');
  685. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  686. add(' *(.rela.fini)');
  687. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  688. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  689. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  690. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  691. add(' *(.rela.ctors)');
  692. add(' *(.rela.dtors)');
  693. add(' *(.rela.got)');
  694. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  695. add(' *(.rela.ifunc)');
  696. add(' }');
  697. add(' .rela.plt :');
  698. add(' {');
  699. add(' *(.rela.plt)');
  700. add(' PROVIDE_HIDDEN (__rela_iplt_start = .);');
  701. add(' *(.rela.iplt)');
  702. add(' PROVIDE_HIDDEN (__rela_iplt_end = .);');
  703. add(' }');
  704. add(' .init :');
  705. add(' {');
  706. add(' KEEP (*(SORT_NONE(.init)))');
  707. add(' } =0');
  708. add(' .plt : ALIGN(16) { *(.plt) *(.iplt) }');
  709. add(' .text :');
  710. add(' {');
  711. add(' *(.text.unlikely .text.*_unlikely .text.unlikely.*)');
  712. add(' *(.text.exit .text.exit.*)');
  713. add(' *(.text.startup .text.startup.*)');
  714. add(' *(.text.hot .text.hot.*)');
  715. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  716. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  717. add(' *(.gnu.warning)');
  718. add(' } =0');
  719. add(' .fini :');
  720. add(' {');
  721. add(' KEEP (*(SORT_NONE(.fini)))');
  722. add(' } =0');
  723. add(' PROVIDE (__etext = .);');
  724. add(' PROVIDE (_etext = .);');
  725. add(' PROVIDE (etext = .);');
  726. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  727. add(' .rodata1 : { *(.rodata1) }');
  728. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  729. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  730. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table');
  731. add(' .gcc_except_table.*) }');
  732. add(' /* These sections are generated by the Sun/Oracle C++ compiler. */');
  733. add(' .exception_ranges : ONLY_IF_RO { *(.exception_ranges');
  734. add(' .exception_ranges*) }');
  735. add(' /* Adjust the address for the data segment. We want to adjust up to');
  736. add(' the same address within the page on the next page up. */');
  737. add(' . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE));');
  738. add(' /* Exception handling */');
  739. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  740. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  741. add(' .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) }');
  742. add(' /* Thread Local Storage sections */');
  743. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  744. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  745. add(' .preinit_array :');
  746. add(' {');
  747. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  748. add(' KEEP (*(.preinit_array))');
  749. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  750. add(' }');
  751. add(' .init_array :');
  752. add(' {');
  753. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  754. add(' KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))');
  755. add(' KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))');
  756. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  757. add(' }');
  758. add(' .fini_array :');
  759. add(' {');
  760. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  761. add(' KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))');
  762. add(' KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))');
  763. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  764. add(' }');
  765. add(' .ctors :');
  766. add(' {');
  767. add(' /* gcc uses crtbegin.o to find the start of');
  768. add(' the constructors, so we make sure it is');
  769. add(' first. Because this is a wildcard, it');
  770. add(' doesn''t matter if the user does not');
  771. add(' actually link against crtbegin.o; the');
  772. add(' linker won''t look for a file to match a');
  773. add(' wildcard. The wildcard also means that it');
  774. add(' doesn''t matter which directory crtbegin.o');
  775. add(' is in. */');
  776. add(' KEEP (*crtbegin.o(.ctors))');
  777. add(' KEEP (*crtbegin?.o(.ctors))');
  778. add(' /* We don''t want to include the .ctor section from');
  779. add(' the crtend.o file until after the sorted ctors.');
  780. add(' The .ctor section from the crtend file contains the');
  781. add(' end of ctors marker and it must be last */');
  782. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  783. add(' KEEP (*(SORT(.ctors.*)))');
  784. add(' KEEP (*(.ctors))');
  785. add(' }');
  786. add(' .dtors :');
  787. add(' {');
  788. add(' KEEP (*crtbegin.o(.dtors))');
  789. add(' KEEP (*crtbegin?.o(.dtors))');
  790. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  791. add(' KEEP (*(SORT(.dtors.*)))');
  792. add(' KEEP (*(.dtors))');
  793. add(' }');
  794. add(' .jcr : { KEEP (*(.jcr)) }');
  795. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }');
  796. add(' .dynamic : { *(.dynamic) }');
  797. add(' .got : { *(.got) *(.igot) }');
  798. add(' . = DATA_SEGMENT_RELRO_END (24, .);');
  799. add(' .got.plt : { *(.got.plt) *(.igot.plt) }');
  800. add(' .data :');
  801. add(' {');
  802. add(' PROVIDE (__data_start = .);');
  803. { extra by FPC }
  804. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  805. add(' *(.data .data.* .gnu.linkonce.d.*)');
  806. add(' SORT(CONSTRUCTORS)');
  807. add(' }');
  808. add(' .data1 : { *(.data1) }');
  809. add(' _edata = .; PROVIDE (edata = .);');
  810. add(' . = .;');
  811. add(' __bss_start = .;');
  812. add(' __bss_start__ = .;');
  813. add(' .bss :');
  814. add(' {');
  815. add(' *(.dynbss)');
  816. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  817. add(' *(COMMON)');
  818. add(' /* Align here to ensure that the .bss section occupies space up to');
  819. add(' _end. Align after .bss to ensure correct alignment even if the');
  820. add(' .bss section disappears because there are no input sections.');
  821. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  822. add(' pad the .data section. */');
  823. add(' . = ALIGN(. != 0 ? 64 / 8 : 1);');
  824. add(' }');
  825. add(' _bss_end__ = . ; __bss_end__ = . ;');
  826. add(' . = ALIGN(64 / 8);');
  827. add(' . = SEGMENT_START("ldata-segment", .);');
  828. add(' . = ALIGN(64 / 8);');
  829. add(' __end__ = . ;');
  830. add(' _end = .; PROVIDE (end = .);');
  831. add(' . = DATA_SEGMENT_END (.);');
  832. add(' /* Stabs debugging sections. */');
  833. add(' .stab 0 : { *(.stab) }');
  834. add(' .stabstr 0 : { *(.stabstr) }');
  835. add(' .stab.excl 0 : { *(.stab.excl) }');
  836. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  837. add(' .stab.index 0 : { *(.stab.index) }');
  838. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  839. add(' .comment 0 : { *(.comment) }');
  840. add(' /* DWARF debug sections.');
  841. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  842. add(' of the section so we begin them at 0. */');
  843. add(' /* DWARF 1 */');
  844. add(' .debug 0 : { *(.debug) }');
  845. add(' .line 0 : { *(.line) }');
  846. add(' /* GNU DWARF 1 extensions */');
  847. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  848. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  849. add(' /* DWARF 1.1 and DWARF 2 */');
  850. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  851. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  852. add(' /* DWARF 2 */');
  853. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  854. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  855. add(' .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end ) }');
  856. add(' .debug_frame 0 : { *(.debug_frame) }');
  857. add(' .debug_str 0 : { *(.debug_str) }');
  858. add(' .debug_loc 0 : { *(.debug_loc) }');
  859. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  860. add(' /* SGI/MIPS DWARF 2 extensions */');
  861. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  862. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  863. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  864. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  865. add(' /* DWARF 3 */');
  866. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  867. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  868. add(' /* DWARF Extension. */');
  869. add(' .debug_macro 0 : { *(.debug_macro) }');
  870. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  871. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  872. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  873. add('}');
  874. end;
  875. {$endif AArch64}
  876. {$ifdef ARM}
  877. if target_info.abi in [abi_eabi,abi_eabihf] then
  878. begin
  879. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  880. add('/* Script for -z combreloc: combine and sort reloc sections */');
  881. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  882. add(' "elf32-littlearm")');
  883. add('OUTPUT_ARCH(arm)');
  884. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  885. add('SECTIONS');
  886. add('{');
  887. add(' /* Read-only sections, merged into text segment: */');
  888. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  889. add(' .interp : { *(.interp) }');
  890. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  891. add(' .hash : { *(.hash) }');
  892. add(' .gnu.hash : { *(.gnu.hash) }');
  893. add(' .dynsym : { *(.dynsym) }');
  894. add(' .dynstr : { *(.dynstr) }');
  895. add(' .gnu.version : { *(.gnu.version) }');
  896. add(' .gnu.version_d : { *(.gnu.version_d) }');
  897. add(' .gnu.version_r : { *(.gnu.version_r) }');
  898. add(' .rel.dyn :');
  899. add(' {');
  900. add(' *(.rel.init)');
  901. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  902. add(' *(.rel.fini)');
  903. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  904. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  905. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  906. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  907. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  908. add(' *(.rel.ctors)');
  909. add(' *(.rel.dtors)');
  910. add(' *(.rel.got)');
  911. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  912. add(' }');
  913. add(' .rela.dyn :');
  914. add(' {');
  915. add(' *(.rela.init)');
  916. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  917. add(' *(.rela.fini)');
  918. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  919. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  920. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  921. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  922. add(' *(.rela.ctors)');
  923. add(' *(.rela.dtors)');
  924. add(' *(.rela.got)');
  925. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  926. add(' }');
  927. add(' .rel.plt : { *(.rel.plt) }');
  928. add(' .rela.plt : { *(.rela.plt) }');
  929. add(' .init :');
  930. add(' {');
  931. add(' KEEP (*(.init))');
  932. add(' } =0');
  933. add(' .plt : { *(.plt) }');
  934. add(' .text :');
  935. add(' {');
  936. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  937. add(' KEEP (*(.text.*personality*))');
  938. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  939. add(' *(.gnu.warning)');
  940. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  941. add(' } =0');
  942. add(' .fini :');
  943. add(' {');
  944. add(' KEEP (*(.fini))');
  945. add(' } =0');
  946. add(' PROVIDE (__etext = .);');
  947. add(' PROVIDE (_etext = .);');
  948. add(' PROVIDE (etext = .);');
  949. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  950. add(' .rodata1 : { *(.rodata1) }');
  951. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  952. add(' __exidx_start = .;');
  953. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  954. add(' __exidx_end = .;');
  955. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  956. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  957. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  958. add(' /* Adjust the address for the data segment. We want to adjust up to');
  959. add(' the same address within the page on the next page up. */');
  960. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  961. add(' /* Exception handling */');
  962. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  963. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  964. add(' /* Thread Local Storage sections */');
  965. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  966. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  967. add(' .preinit_array :');
  968. add(' {');
  969. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  970. add(' KEEP (*(.preinit_array))');
  971. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  972. add(' }');
  973. add(' .init_array :');
  974. add(' {');
  975. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  976. add(' KEEP (*(SORT(.init_array.*)))');
  977. add(' KEEP (*(.init_array))');
  978. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  979. add(' }');
  980. add(' .fini_array :');
  981. add(' {');
  982. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  983. add(' KEEP (*(.fini_array))');
  984. add(' KEEP (*(SORT(.fini_array.*)))');
  985. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  986. add(' }');
  987. add(' .ctors :');
  988. add(' {');
  989. add(' /* gcc uses crtbegin.o to find the start of');
  990. add(' the constructors, so we make sure it is');
  991. add(' first. Because this is a wildcard, it');
  992. add(' doesn''t matter if the user does not');
  993. add(' actually link against crtbegin.o; the');
  994. add(' linker won''t look for a file to match a');
  995. add(' wildcard. The wildcard also means that it');
  996. add(' doesn''t matter which directory crtbegin.o');
  997. add(' is in. */');
  998. add(' KEEP (*crtbegin.o(.ctors))');
  999. add(' KEEP (*crtbegin?.o(.ctors))');
  1000. add(' /* We don''t want to include the .ctor section from');
  1001. add(' the crtend.o file until after the sorted ctors.');
  1002. add(' The .ctor section from the crtend file contains the');
  1003. add(' end of ctors marker and it must be last */');
  1004. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  1005. add(' KEEP (*(SORT(.ctors.*)))');
  1006. add(' KEEP (*(.ctors))');
  1007. add(' }');
  1008. add(' .dtors :');
  1009. add(' {');
  1010. add(' KEEP (*crtbegin.o(.dtors))');
  1011. add(' KEEP (*crtbegin?.o(.dtors))');
  1012. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  1013. add(' KEEP (*(SORT(.dtors.*)))');
  1014. add(' KEEP (*(.dtors))');
  1015. add(' }');
  1016. add(' .jcr : { KEEP (*(.jcr)) }');
  1017. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  1018. add(' .dynamic : { *(.dynamic) }');
  1019. add(' .got : { *(.got.plt) *(.got) }');
  1020. add(' .data :');
  1021. add(' {');
  1022. add(' __data_start = . ;');
  1023. add(' *(.data .data.* .gnu.linkonce.d.*)');
  1024. { extra by FPC }
  1025. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  1026. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  1027. add(' SORT(CONSTRUCTORS)');
  1028. add(' }');
  1029. add(' .data1 : { *(.data1) }');
  1030. add(' _edata = .; PROVIDE (edata = .);');
  1031. add(' __bss_start = .;');
  1032. add(' __bss_start__ = .;');
  1033. add(' .bss :');
  1034. add(' {');
  1035. add(' *(.dynbss)');
  1036. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1037. add(' *(COMMON)');
  1038. add(' /* Align here to ensure that the .bss section occupies space up to');
  1039. add(' _end. Align after .bss to ensure correct alignment even if the');
  1040. add(' .bss section disappears because there are no input sections.');
  1041. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  1042. add(' pad the .data section. */');
  1043. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  1044. add(' }');
  1045. add(' _bss_end__ = . ; __bss_end__ = . ;');
  1046. add(' . = ALIGN(32 / 8);');
  1047. add(' . = ALIGN(32 / 8);');
  1048. add(' __end__ = . ;');
  1049. add(' _end = .; PROVIDE (end = .);');
  1050. add(' /* Stabs debugging sections. */');
  1051. add(' .stab 0 : { *(.stab) }');
  1052. add(' .stabstr 0 : { *(.stabstr) }');
  1053. add(' .stab.excl 0 : { *(.stab.excl) }');
  1054. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  1055. add(' .stab.index 0 : { *(.stab.index) }');
  1056. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  1057. add(' .comment 0 : { *(.comment) }');
  1058. add(' /* DWARF debug sections.');
  1059. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  1060. add(' of the section so we begin them at 0. */');
  1061. add(' /* DWARF 1 */');
  1062. add(' .debug 0 : { *(.debug) }');
  1063. add(' .line 0 : { *(.line) }');
  1064. add(' /* GNU DWARF 1 extensions */');
  1065. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  1066. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  1067. add(' /* DWARF 1.1 and DWARF 2 */');
  1068. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  1069. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  1070. add(' /* DWARF 2 */');
  1071. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  1072. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  1073. add(' .debug_line 0 : { *(.debug_line) }');
  1074. add(' .debug_frame 0 : { *(.debug_frame) }');
  1075. add(' .debug_str 0 : { *(.debug_str) }');
  1076. add(' .debug_loc 0 : { *(.debug_loc) }');
  1077. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  1078. add(' /* SGI/MIPS DWARF 2 extensions */');
  1079. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  1080. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  1081. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  1082. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  1083. add(' /* DWARF 3 */');
  1084. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  1085. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  1086. add(' .stack 0x80000 :');
  1087. add(' {');
  1088. add(' _stack = .;');
  1089. add(' *(.stack)');
  1090. add(' }');
  1091. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  1092. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  1093. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  1094. add('}');
  1095. end
  1096. else
  1097. {$endif ARM}
  1098. {$ifndef LINKERSCRIPT_INCLUDED}
  1099. begin
  1100. {Sections.}
  1101. add('SECTIONS');
  1102. add('{');
  1103. {Read-only sections, merged into text segment:}
  1104. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 + SIZEOF_HEADERS;');
  1105. add(' .interp : { *(.interp) }');
  1106. add(' .hash : { *(.hash) }');
  1107. add(' .dynsym : { *(.dynsym) }');
  1108. add(' .dynstr : { *(.dynstr) }');
  1109. add(' .gnu.version : { *(.gnu.version) }');
  1110. add(' .gnu.version_d : { *(.gnu.version_d) }');
  1111. add(' .gnu.version_r : { *(.gnu.version_r) }');
  1112. add(' .rel.dyn :');
  1113. add(' {');
  1114. add(' *(.rel.init)');
  1115. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  1116. add(' *(.rel.fini)');
  1117. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  1118. add(' *(.rel.data.rel.ro*)');
  1119. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  1120. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  1121. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  1122. add(' *(.rel.got)');
  1123. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  1124. add(' }');
  1125. add(' .rela.dyn :');
  1126. add(' {');
  1127. add(' *(.rela.init)');
  1128. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  1129. add(' *(.rela.fini)');
  1130. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  1131. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  1132. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  1133. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  1134. add(' *(.rela.got)');
  1135. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  1136. add(' }');
  1137. add(' .rel.plt : { *(.rel.plt) }');
  1138. add(' .rela.plt : { *(.rela.plt) }');
  1139. add(' .init :');
  1140. add(' {');
  1141. add(' KEEP (*(.init))');
  1142. add(' } =0x90909090');
  1143. add(' .plt : { *(.plt) }');
  1144. add(' .text :');
  1145. add(' {');
  1146. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  1147. add(' KEEP (*(.text.*personality*))');
  1148. {.gnu.warning sections are handled specially by elf32.em.}
  1149. add(' *(.gnu.warning)');
  1150. add(' } =0x90909090');
  1151. add(' .fini :');
  1152. add(' {');
  1153. add(' KEEP (*(.fini))');
  1154. add(' } =0x90909090');
  1155. add(' PROVIDE (_etext = .);');
  1156. add(' .rodata :');
  1157. add(' {');
  1158. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  1159. add(' }');
  1160. {Adjust the address for the data segment. We want to adjust up to
  1161. the same address within the page on the next page up.}
  1162. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  1163. add(' .dynamic : { *(.dynamic) }');
  1164. add(' .got : { *(.got) }');
  1165. add(' .got.plt : { *(.got.plt) }');
  1166. add(' .data :');
  1167. add(' {');
  1168. add(' *(.data .data.* .gnu.linkonce.d.*)');
  1169. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  1170. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  1171. add(' }');
  1172. add(' PROVIDE (_edata = .);');
  1173. add(' PROVIDE (edata = .);');
  1174. {$ifdef zsegment_threadvars}
  1175. add(' _z = .;');
  1176. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  1177. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  1178. add(' . = _z + SIZEOF (.threadvar);');
  1179. {$else}
  1180. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  1181. {$endif}
  1182. add(' __bss_start = .;');
  1183. add(' .bss :');
  1184. add(' {');
  1185. add(' *(.dynbss)');
  1186. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1187. add(' *(COMMON)');
  1188. {Align here to ensure that the .bss section occupies space up to
  1189. _end. Align after .bss to ensure correct alignment even if the
  1190. .bss section disappears because there are no input sections.}
  1191. add(' . = ALIGN(32 / 8);');
  1192. add(' }');
  1193. add(' . = ALIGN(32 / 8);');
  1194. add(' PROVIDE (_end = .);');
  1195. add(' PROVIDE (end = .);');
  1196. {Stabs debugging sections.}
  1197. add(' .stab 0 : { *(.stab) }');
  1198. add(' .stabstr 0 : { *(.stabstr) }');
  1199. add('}');
  1200. end;
  1201. {$endif LINKERSCRIPT_INCLUDED}
  1202. { Write and Close response }
  1203. writetodisk;
  1204. Free;
  1205. end;
  1206. WriteResponseFile:=True;
  1207. end;
  1208. function TLinkerLinux.MakeExecutable:boolean;
  1209. var
  1210. i : longint;
  1211. binstr,
  1212. cmdstr : TCmdStr;
  1213. success : boolean;
  1214. DynLinkStr : string;
  1215. GCSectionsStr,
  1216. StaticStr,
  1217. StripStr : string[40];
  1218. begin
  1219. if not(cs_link_nolink in current_settings.globalswitches) then
  1220. Message1(exec_i_linking,current_module.exefilename);
  1221. { Create some replacements }
  1222. StaticStr:='';
  1223. StripStr:='';
  1224. GCSectionsStr:='';
  1225. DynLinkStr:='';
  1226. if (cs_link_staticflag in current_settings.globalswitches) then
  1227. StaticStr:='-static';
  1228. if (cs_link_strip in current_settings.globalswitches) and
  1229. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1230. StripStr:='-s';
  1231. if (cs_link_map in current_settings.globalswitches) then
  1232. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  1233. if (cs_link_smart in current_settings.globalswitches) and
  1234. create_smartlink_sections then
  1235. GCSectionsStr:='--gc-sections';
  1236. If (cs_profile in current_settings.moduleswitches) or
  1237. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  1238. begin
  1239. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  1240. if cshared then
  1241. DynLinkStr:=DynLinkStr+' --shared ';
  1242. if rlinkpath<>'' then
  1243. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  1244. End;
  1245. { Write used files and libraries }
  1246. WriteResponseFile(false);
  1247. { Call linker }
  1248. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1249. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  1250. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1251. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1252. Replace(cmdstr,'$STATIC',StaticStr);
  1253. Replace(cmdstr,'$STRIP',StripStr);
  1254. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1255. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1256. { create dynamic symbol table? }
  1257. if HasExports then
  1258. cmdstr:=cmdstr+' -E';
  1259. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1260. { Create external .dbg file with debuginfo }
  1261. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  1262. begin
  1263. for i:=1 to 3 do
  1264. begin
  1265. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  1266. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  1267. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  1268. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  1269. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1270. if not success then
  1271. break;
  1272. end;
  1273. end;
  1274. { Remove ReponseFile }
  1275. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1276. DeleteFile(outputexedir+Info.ResName);
  1277. MakeExecutable:=success; { otherwise a recursive call to link method }
  1278. end;
  1279. Function TLinkerLinux.MakeSharedLibrary:boolean;
  1280. var
  1281. InitStr,
  1282. FiniStr,
  1283. SoNameStr : string[80];
  1284. binstr,
  1285. cmdstr : TCmdStr;
  1286. success : boolean;
  1287. begin
  1288. MakeSharedLibrary:=false;
  1289. if not(cs_link_nolink in current_settings.globalswitches) then
  1290. Message1(exec_i_linking,current_module.sharedlibfilename);
  1291. { Write used files and libraries }
  1292. WriteResponseFile(true);
  1293. { Create some replacements }
  1294. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  1295. InitStr:='-init FPC_SHARED_LIB_START';
  1296. FiniStr:='-fini FPC_LIB_EXIT';
  1297. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  1298. { Call linker }
  1299. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1300. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1301. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1302. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1303. Replace(cmdstr,'$INIT',InitStr);
  1304. Replace(cmdstr,'$FINI',FiniStr);
  1305. Replace(cmdstr,'$SONAME',SoNameStr);
  1306. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1307. { Strip the library ? }
  1308. if success and (cs_link_strip in current_settings.globalswitches) then
  1309. begin
  1310. { only remove non global symbols and debugging info for a library }
  1311. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1312. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1313. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1314. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1315. end;
  1316. { Remove ReponseFile }
  1317. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1318. DeleteFile(outputexedir+Info.ResName);
  1319. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1320. end;
  1321. {*****************************************************************************
  1322. TINTERNALLINKERLINUX
  1323. *****************************************************************************}
  1324. constructor TInternalLinkerLinux.Create;
  1325. begin
  1326. inherited Create;
  1327. SetupLibrarySearchPath;
  1328. SetupDynlinker(dynlinker,libctype);
  1329. CExeOutput:=ElfExeOutputClass;
  1330. CObjInput:=TElfObjInput;
  1331. end;
  1332. procedure TInternalLinkerLinux.InitSysInitUnitName;
  1333. begin
  1334. linklibc:=ModulesLinkToLibc;
  1335. reorder:=linklibc and ReOrderEntries;
  1336. sysinitunit:=defsinames[current_module.islibrary];
  1337. prtobj:=defprtnames[current_module.islibrary];
  1338. if cs_profile in current_settings.moduleswitches then
  1339. begin
  1340. prtobj:=gprtnames[libctype];
  1341. sysinitunit:=gsinames[libctype];
  1342. linklibc:=true;
  1343. end
  1344. else if linklibc then
  1345. begin
  1346. prtobj:=cprtnames[libctype];
  1347. sysinitunit:=csinames[libctype];
  1348. end;
  1349. end;
  1350. const
  1351. relsec_prefix:array[boolean] of TCmdStr = ('rel','rela');
  1352. procedure TInternalLinkerLinux.DefaultLinkScript;
  1353. var
  1354. s,s1,s2,relprefix:TCmdStr;
  1355. found1,found2:boolean;
  1356. linkToSharedLibs:boolean;
  1357. procedure AddLibraryStatement(const s:TCmdStr);
  1358. var
  1359. i:longint;
  1360. s1,s2:TCmdStr;
  1361. begin
  1362. i:=pos(target_info.sharedClibext+'.',s);
  1363. if (i>0) then
  1364. s1:=target_info.sharedClibprefix+S
  1365. else
  1366. s1:=target_info.sharedClibprefix+S+target_info.sharedClibext;
  1367. { TODO: to be compatible with ld search algorithm, each found file
  1368. must be tested for target compatibility, incompatible ones should be skipped. }
  1369. { TODO: shall we search library without suffix if one with suffix is not found? }
  1370. if (not(cs_link_staticflag in current_settings.globalswitches)) and
  1371. FindLibraryFile(s1,'','',s2) then
  1372. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1373. { TODO: static libraries never have numeric suffix in their names }
  1374. else if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  1375. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1376. else
  1377. Comment(V_Error,'Import library not found for '+S);
  1378. end;
  1379. begin
  1380. if cs_profile in current_settings.moduleswitches then
  1381. begin
  1382. if not(libctype in [glibc2,glibc21]) then
  1383. AddSharedLibrary('gmon');
  1384. AddSharedLibrary('c');
  1385. end;
  1386. TElfExeOutput(exeoutput).interpreter:=stringdup(dynlinker);
  1387. { add objectfiles, start with prt0 always }
  1388. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  1389. LinkScript.Concat('READOBJECT '+ maybequoted(FindObjectFile(prtobj,'',false)));
  1390. { try to add crti and crtbegin if linking to C }
  1391. if linklibc and (libctype<>uclibc) then
  1392. begin
  1393. { crti.o must come first }
  1394. if librarysearchpath.FindFile('crti.o',false,s) then
  1395. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1396. { then the crtbegin* }
  1397. if cs_create_pic in current_settings.moduleswitches then
  1398. begin
  1399. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  1400. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1401. end
  1402. else
  1403. if (cs_link_staticflag in current_settings.globalswitches) and
  1404. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  1405. LinkScript.Concat('READOBJECT '+maybequoted(s))
  1406. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  1407. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1408. end;
  1409. ScriptAddSourceStatements(false);
  1410. { we must reorder here because the result could empty sharedlibfiles }
  1411. if reorder then
  1412. ExpandAndApplyOrder(SharedLibFiles);
  1413. { See tw9089*.pp: if more than one pure-Pascal shared libs are loaded,
  1414. and none have rtld in their DT_NEEDED, then rtld cannot finalize correctly. }
  1415. if IsSharedLibrary then
  1416. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(sysrootpath+dynlinker));
  1417. linkToSharedLibs:=(not SharedLibFiles.Empty);
  1418. { Symbols declared as "external 'libx.so'" are added to ImportLibraryList, library
  1419. prefix/extension *not* stripped. TImportLibLinux copies these to SharedLibFiles,
  1420. stripping prefixes and extensions.
  1421. However extension won't be stripped if library is specified with numeric suffix
  1422. (like "libpango-1.0.so.0")
  1423. Libraries specified with $LINKLIB directive are directly added to SharedLibFiles
  1424. and won't be present in ImportLibraryList. }
  1425. while not SharedLibFiles.Empty do
  1426. begin
  1427. S:=SharedLibFiles.GetFirst;
  1428. if (S<>'c') or reorder then
  1429. AddLibraryStatement(S);
  1430. end;
  1431. if (cs_link_staticflag in current_settings.globalswitches) or
  1432. (linklibc and not reorder) then
  1433. begin
  1434. LinkScript.Concat('GROUP');
  1435. if (cs_link_staticflag in current_settings.globalswitches) then
  1436. begin
  1437. AddLibraryStatement('gcc');
  1438. AddLibraryStatement('gcc_eh');
  1439. end;
  1440. if linklibc and not reorder then
  1441. AddLibraryStatement('c');
  1442. LinkScript.Concat('ENDGROUP');
  1443. end;
  1444. { objects which must be at the end }
  1445. if linklibc and (libctype<>uclibc) then
  1446. begin
  1447. if cs_create_pic in current_settings.moduleswitches then
  1448. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  1449. else
  1450. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  1451. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  1452. if found1 then
  1453. LinkScript.Concat('READOBJECT '+maybequoted(s1));
  1454. if found2 then
  1455. LinkScript.Concat('READOBJECT '+maybequoted(s2));
  1456. end;
  1457. if (not IsSharedLibrary) then
  1458. if (linkToSharedLibs and not linklibc) then
  1459. LinkScript.Concat('ENTRYNAME _dynamic_start')
  1460. else
  1461. LinkScript.Concat('ENTRYNAME _start')
  1462. else
  1463. LinkScript.Concat('ISSHAREDLIBRARY');
  1464. relprefix:=relsec_prefix[ElfTarget.relocs_use_addend];
  1465. with LinkScript do
  1466. begin
  1467. Concat('HEADER');
  1468. Concat('EXESECTION .interp');
  1469. Concat(' OBJSECTION .interp');
  1470. Concat('ENDEXESECTION');
  1471. Concat('EXESECTION .note.ABI-tag');
  1472. Concat(' OBJSECTION .note.ABI-tag');
  1473. Concat('ENDEXESECTION');
  1474. Concat('EXESECTION .note.gnu.build-id');
  1475. Concat(' OBJSECTION .note.gnu.build-id');
  1476. Concat('ENDEXESECTION');
  1477. Concat('EXESECTION .hash');
  1478. Concat(' OBJSECTION .hash');
  1479. Concat('ENDEXESECTION');
  1480. Concat('EXESECTION .dynsym');
  1481. Concat(' OBJSECTION .dynsym');
  1482. Concat('ENDEXESECTION');
  1483. Concat('EXESECTION .dynstr');
  1484. Concat(' OBJSECTION .dynstr');
  1485. Concat('ENDEXESECTION');
  1486. Concat('EXESECTION .gnu.version');
  1487. Concat(' OBJSECTION .gnu.version');
  1488. Concat('ENDEXESECTION');
  1489. Concat('EXESECTION .gnu.version_d');
  1490. Concat(' OBJSECTION .gnu.version_d');
  1491. Concat('ENDEXESECTION');
  1492. Concat('EXESECTION .gnu.version_r');
  1493. Concat(' OBJSECTION .gnu.version_r');
  1494. Concat('ENDEXESECTION');
  1495. Concat('EXESECTION .'+relprefix+'.dyn');
  1496. Concat(' OBJSECTION .'+relprefix+'.dyn');
  1497. Concat('ENDEXESECTION');
  1498. Concat('EXESECTION .'+relprefix+'.plt');
  1499. Concat(' OBJSECTION .'+relprefix+'.plt');
  1500. Concat(' PROVIDE __'+relprefix+'_iplt_start');
  1501. Concat(' OBJSECTION .'+relprefix+'.iplt');
  1502. Concat(' PROVIDE __'+relprefix+'_iplt_end');
  1503. Concat('ENDEXESECTION');
  1504. Concat('EXESECTION .init');
  1505. Concat(' OBJSECTION .init');
  1506. Concat('ENDEXESECTION');
  1507. Concat('EXESECTION .plt');
  1508. Concat(' OBJSECTION .plt');
  1509. Concat('ENDEXESECTION');
  1510. Concat('EXESECTION .text');
  1511. Concat(' OBJSECTION .text*');
  1512. Concat('ENDEXESECTION');
  1513. Concat('EXESECTION .fini');
  1514. Concat(' OBJSECTION .fini');
  1515. Concat(' PROVIDE __etext');
  1516. Concat(' PROVIDE _etext');
  1517. Concat(' PROVIDE etext');
  1518. Concat('ENDEXESECTION');
  1519. Concat('EXESECTION .rodata');
  1520. Concat(' OBJSECTION .rodata*');
  1521. Concat('ENDEXESECTION');
  1522. {$ifdef arm}
  1523. Concat('EXESECTION .ARM.extab');
  1524. Concat(' OBJSECTION .ARM.extab*');
  1525. Concat('ENDEXESECTION');
  1526. Concat('EXESECTION .ARM.exidx');
  1527. Concat(' SYMBOL __exidx_start');
  1528. Concat(' OBJSECTION .ARM.exidx*');
  1529. Concat(' SYMBOL __exidx_end');
  1530. Concat('ENDEXESECTION');
  1531. {$endif}
  1532. Concat('EXESECTION .eh_frame');
  1533. Concat(' OBJSECTION .eh_frame');
  1534. Concat('ENDEXESECTION');
  1535. Concat('EXESECTION .gcc_except_table');
  1536. Concat(' OBJSECTION .gcc_except_table');
  1537. Concat(' OBJSECTION .gcc_except_table.*');
  1538. Concat('ENDEXESECTION');
  1539. Concat('EXESECTION .tdata');
  1540. Concat(' OBJSECTION .tdata');
  1541. Concat(' OBJSECTION .tdata.*');
  1542. Concat('ENDEXESECTION');
  1543. Concat('EXESECTION .tbss');
  1544. Concat(' OBJSECTION .tbss');
  1545. Concat(' OBJSECTION .tbss.*');
  1546. Concat('ENDEXESECTION');
  1547. Concat('EXESECTION .preinit_array');
  1548. Concat(' PROVIDE __preinit_array_start');
  1549. Concat(' OBJSECTION .preinit_array');
  1550. Concat(' PROVIDE __preinit_array_end');
  1551. Concat('ENDEXESECTION');
  1552. Concat('EXESECTION .init_array');
  1553. Concat(' PROVIDE __init_array_start');
  1554. { why the hell .ctors are both here and exesection .ctors below?? }
  1555. // KEEP ( *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
  1556. Concat(' OBJSECTION .init_array');
  1557. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
  1558. Concat('PROVIDE __init_array_end');
  1559. Concat('ENDEXESECTION');
  1560. Concat('EXESECTION .fini_array');
  1561. Concat(' PROVIDE __fini_array_start');
  1562. // KEEP ( *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
  1563. Concat(' OBJSECTION .fini_array');
  1564. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
  1565. Concat(' PROVIDE __fini_array_end');
  1566. Concat('ENDEXESECTION');
  1567. Concat('EXESECTION .ctors');
  1568. Concat(' OBJSECTION .ctors*');
  1569. Concat('ENDEXESECTION');
  1570. Concat('EXESECTION .dtors');
  1571. Concat(' OBJSECTION .dtors*');
  1572. Concat('ENDEXESECTION');
  1573. Concat('EXESECTION .jcr');
  1574. Concat(' OBJSECTION .jcr');
  1575. Concat('ENDEXESECTION');
  1576. Concat('EXESECTION .dynamic');
  1577. Concat(' OBJSECTION .dynamic');
  1578. Concat('ENDEXESECTION');
  1579. {$ifndef mips}
  1580. Concat('EXESECTION .got');
  1581. {$ifdef arm}
  1582. Concat(' OBJSECTION .got.plt');
  1583. {$endif arm}
  1584. Concat(' OBJSECTION .got');
  1585. Concat('ENDEXESECTION');
  1586. {$endif mips}
  1587. {$ifndef arm}
  1588. Concat('EXESECTION .got.plt');
  1589. Concat(' OBJSECTION .got.plt');
  1590. Concat('ENDEXESECTION');
  1591. {$endif arm}
  1592. Concat('EXESECTION .data');
  1593. Concat(' OBJSECTION .data*');
  1594. Concat(' OBJSECTION .fpc*');
  1595. Concat(' OBJSECTION fpc.resources');
  1596. Concat(' PROVIDE _edata');
  1597. Concat(' PROVIDE edata');
  1598. Concat('ENDEXESECTION');
  1599. {$ifdef mips}
  1600. Concat('EXESECTION .got');
  1601. Concat(' OBJSECTION .got');
  1602. Concat('ENDEXESECTION');
  1603. {$endif mips}
  1604. Concat('EXESECTION .bss');
  1605. Concat(' OBJSECTION .dynbss');
  1606. Concat(' OBJSECTION .bss*');
  1607. Concat(' OBJSECTION fpc.reshandles');
  1608. Concat(' PROVIDE end');
  1609. Concat(' SYMBOL _end');
  1610. Concat('ENDEXESECTION');
  1611. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,'+
  1612. '.debug_abbrev,.debug_line,.debug_frame,.debug_str,.debug_loc,'+
  1613. '.debug_macinfo,.debug_weaknames,.debug_funcnames,.debug_typenames,.debug_varnames,.debug_ranges');
  1614. Concat('EXESECTION .stab');
  1615. Concat(' OBJSECTION .stab');
  1616. Concat('ENDEXESECTION');
  1617. Concat('EXESECTION .stabstr');
  1618. Concat(' OBJSECTION .stabstr');
  1619. Concat('ENDEXESECTION');
  1620. end;
  1621. end;
  1622. {*****************************************************************************
  1623. Initialize
  1624. *****************************************************************************}
  1625. initialization
  1626. RegisterLinker(ld_linux,TLinkerLinux);
  1627. RegisterLinker(ld_int_linux,TInternalLinkerLinux);
  1628. {$ifdef i386}
  1629. RegisterImport(system_i386_linux,timportliblinux);
  1630. RegisterExport(system_i386_linux,texportliblinux);
  1631. RegisterTarget(system_i386_linux_info);
  1632. {$endif i386}
  1633. {$ifdef m68k}
  1634. RegisterImport(system_m68k_linux,timportliblinux);
  1635. RegisterExport(system_m68k_linux,texportliblinux);
  1636. RegisterTarget(system_m68k_linux_info);
  1637. {$endif m68k}
  1638. {$ifdef powerpc}
  1639. RegisterImport(system_powerpc_linux,timportliblinux);
  1640. RegisterExport(system_powerpc_linux,texportliblinux);
  1641. RegisterTarget(system_powerpc_linux_info);
  1642. {$endif powerpc}
  1643. {$ifdef powerpc64}
  1644. RegisterImport(system_powerpc64_linux,timportliblinux);
  1645. RegisterExport(system_powerpc64_linux,texportliblinux);
  1646. RegisterTarget(system_powerpc64_linux_info);
  1647. {$endif powerpc64}
  1648. {$ifdef alpha}
  1649. RegisterImport(system_alpha_linux,timportliblinux);
  1650. RegisterExport(system_alpha_linux,texportliblinux);
  1651. RegisterTarget(system_alpha_linux_info);
  1652. {$endif alpha}
  1653. {$ifdef x86_64}
  1654. RegisterImport(system_x86_64_linux,timportliblinux);
  1655. RegisterExport(system_x86_64_linux,texportliblinux);
  1656. RegisterTarget(system_x86_64_linux_info);
  1657. {$endif x86_64}
  1658. {$ifdef SPARC}
  1659. RegisterImport(system_SPARC_linux,timportliblinux);
  1660. RegisterExport(system_SPARC_linux,texportliblinux);
  1661. RegisterTarget(system_SPARC_linux_info);
  1662. {$endif SPARC}
  1663. {$ifdef ARM}
  1664. RegisterImport(system_arm_linux,timportliblinux);
  1665. RegisterExport(system_arm_linux,texportliblinux);
  1666. RegisterTarget(system_arm_linux_info);
  1667. {$endif ARM}
  1668. {$ifdef aarch64}
  1669. RegisterImport(system_aarch64_linux,timportliblinux);
  1670. RegisterExport(system_aarch64_linux,texportliblinux);
  1671. RegisterTarget(system_aarch64_linux_info);
  1672. {$endif aarch64}
  1673. {$ifdef MIPS}
  1674. {$ifdef MIPSEL}
  1675. RegisterImport(system_mipsel_linux,timportliblinux);
  1676. RegisterExport(system_mipsel_linux,texportliblinux);
  1677. RegisterTarget(system_mipsel_linux_info);
  1678. {$else MIPS}
  1679. RegisterImport(system_mipseb_linux,timportliblinux);
  1680. RegisterExport(system_mipseb_linux,texportliblinux);
  1681. RegisterTarget(system_mipseb_linux_info);
  1682. {$endif MIPSEL}
  1683. {$endif MIPS}
  1684. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1685. end.