t_linux.pas 70 KB

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