t_linux.pas 74 KB

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