t_linux.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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. function postprocessexecutable(const fn: string; isdll: boolean): boolean;
  41. public
  42. constructor Create;override;
  43. procedure SetDefaultInfo;override;
  44. procedure InitSysInitUnitName;override;
  45. function MakeExecutable:boolean;override;
  46. function MakeSharedLibrary:boolean;override;
  47. procedure LoadPredefinedLibraryOrder; override;
  48. end;
  49. TInternalLinkerLinux=class(TInternalLinker)
  50. private
  51. libctype: TLibcType;
  52. reorder: boolean;
  53. linklibc: boolean;
  54. prtobj: string[20];
  55. dynlinker: string[100];
  56. public
  57. constructor Create;override;
  58. procedure DefaultLinkScript;override;
  59. procedure InitSysInitUnitName;override;
  60. end;
  61. implementation
  62. uses
  63. SysUtils,
  64. cutils,cfileutl,cclasses,
  65. verbose,systems,globtype,globals,
  66. cscript,
  67. fmodule,
  68. aasmbase,aasmtai,aasmcpu,cpubase,
  69. cgbase,ogbase,
  70. comprsrc,
  71. ogelf,owar,
  72. rescmn, i_linux
  73. ;
  74. {*****************************************************************************
  75. TIMPORTLIBLINUX
  76. *****************************************************************************}
  77. procedure timportliblinux.generatelib;
  78. var
  79. i : longint;
  80. ImportLibrary : TImportLibrary;
  81. begin
  82. for i:=0 to current_module.ImportLibraryList.Count-1 do
  83. begin
  84. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  85. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  86. end;
  87. end;
  88. {*****************************************************************************
  89. TEXPORTLIBLINUX
  90. *****************************************************************************}
  91. procedure texportliblinux.setfininame(list: TAsmList; const s: string);
  92. begin
  93. { the problem with not having a .fini section is that a finalization
  94. routine in regular code can get "smart" linked away -> reference it
  95. just like the debug info }
  96. new_section(list,sec_fpc,'links',0);
  97. list.concat(Tai_const.Createname(s,0));
  98. inherited setfininame(list,s);
  99. end;
  100. {*****************************************************************************
  101. TLINKERLINUX
  102. *****************************************************************************}
  103. procedure SetupLibrarySearchPath;
  104. begin
  105. if not Dontlinkstdlibpath Then
  106. begin
  107. {$ifdef x86_64}
  108. { some linuxes might not have the lib64 variants (Arch, LFS }
  109. { don't use PathExists checks, as we need to take sysroots and
  110. cross-compiling into account }
  111. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/X11R6/lib',true);
  112. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/X11R6/lib64',true);
  113. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib',true);
  114. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib64',true);
  115. { /lib64 should be the really first, so add it before everything else }
  116. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib',true);
  117. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib64',true);
  118. {$else}
  119. {$ifdef powerpc64}
  120. if target_info.abi<>abi_powerpc_elfv2 then
  121. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/X11R6/lib64',true)
  122. else
  123. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/powerpc64le-linux-gnu;=/usr/X11R6/powerpc64le-linux-gnu',true);
  124. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib',true);
  125. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib64',true);
  126. { /lib64 should be the really first, so add it before everything else }
  127. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib',true);
  128. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib64',true);
  129. {$else powerpc64}
  130. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib;=/usr/lib;=/usr/X11R6/lib',true);
  131. {$endif powerpc64}
  132. {$endif x86_64}
  133. {$ifdef arm}
  134. { some newer Debian have the crt*.o files at uncommon locations,
  135. for other arm flavours, this cannot hurt }
  136. if target_info.abi=abi_eabihf then
  137. begin
  138. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/arm-linux-gnueabihf',true);
  139. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/arm-linux-gnueabihf',true);
  140. end;
  141. if target_info.abi=abi_eabi then
  142. begin
  143. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/arm-linux-gnueabi',true);
  144. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/arm-linux-gnueabi',true);
  145. end;
  146. {$endif arm}
  147. {$ifdef x86_64}
  148. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/x86_64-linux-gnu',true);
  149. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/x86_64-linux-gnu',true);
  150. {$endif x86_64}
  151. {$ifdef i386}
  152. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/i386-linux-gnu',true);
  153. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/i386-linux-gnu',true);
  154. {$endif i386}
  155. {$ifdef aarch64}
  156. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib64',true);
  157. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/aarch64-linux-gnu',true);
  158. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/aarch64-linux-gnu',true);
  159. {$endif aarch64}
  160. {$ifdef powerpc}
  161. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/powerpc-linux-gnu',true);
  162. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/powerpc-linux-gnu',true);
  163. {$endif powerpc}
  164. {$ifdef m68k}
  165. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/m68k-linux-gnu',true);
  166. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/m68k-linux-gnu',true);
  167. {$endif m68k}
  168. {$ifdef mipsel}
  169. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/mipsel-linux-gnu',true);
  170. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/mipsel-linux-gnu',true);
  171. {$endif mipsel}
  172. {$ifdef mips}
  173. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/mips-linux-gnu',true);
  174. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/mips-linux-gnu',true);
  175. {$endif mips}
  176. {$ifdef sparc64}
  177. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/sparc64-linux-gnu',true);
  178. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/sparc64-linux-gnu',true);
  179. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib64',true);
  180. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib64',true);
  181. {$endif sparc64}
  182. {$ifdef riscv32}
  183. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/riscv32-linux-gnu',true);
  184. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/riscv32-linux-gnu',true);
  185. {$endif riscv32}
  186. {$ifdef riscv64}
  187. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/riscv64-linux-gnu',true);
  188. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/lib/riscv64-linux-gnu',true);
  189. {$endif riscv64}
  190. {$ifdef loongarch64}
  191. LibrarySearchPath.AddLibraryPath(sysrootpath,'=/usr/lib/loongarch64-linux-gnu',true);
  192. {$endif loongarch64}
  193. end;
  194. end;
  195. {$ifdef m68k}
  196. const defdynlinker='/lib/ld.so.1';
  197. {$endif m68k}
  198. {$ifdef i386}
  199. const defdynlinker='/lib/ld-linux.so.2';
  200. {$endif}
  201. {$ifdef x86_64}
  202. const defdynlinker='/lib64/ld-linux-x86-64.so.2';
  203. {$endif x86_64}
  204. {$ifdef sparc}
  205. const defdynlinker='/lib/ld-linux.so.2';
  206. {$endif sparc}
  207. {$ifdef powerpc}
  208. const defdynlinker='/lib/ld.so.1';
  209. {$endif powerpc}
  210. {$ifdef powerpc64}
  211. const defdynlinkerv1='/lib64/ld64.so.1';
  212. const defdynlinkerv2='/lib64/ld64.so.2';
  213. var defdynlinker: string;
  214. {$endif powerpc64}
  215. {$ifdef arm}
  216. {$ifdef FPC_ARMHF}
  217. const defdynlinker='/lib/ld-linux-armhf.so.3';
  218. {$else FPC_ARMHF}
  219. {$ifdef FPC_ARMEL}
  220. const defdynlinker='/lib/ld-linux.so.3';
  221. {$else FPC_ARMEL}
  222. const defdynlinker='/lib/ld-linux.so.2';
  223. {$endif FPC_ARMEL}
  224. {$endif FPC_ARMHF}
  225. {$endif arm}
  226. {$ifdef aarch64}
  227. const defdynlinker='/lib/ld-linux-aarch64.so.1';
  228. {$endif aarch64}
  229. {$ifdef mips}
  230. const defdynlinker='/lib/ld.so.1';
  231. {$endif mips}
  232. {$ifdef sparc64}
  233. const defdynlinker='/lib64/ld-linux.so.2';
  234. {$endif sparc64}
  235. {$ifdef riscv32}
  236. const defdynlinker_generic='/lib32/ld.so.1';
  237. const defdynlinker_soft_float='/lib/ld-linux-riscv32-ilp32.so.1';
  238. const defdynlinker_single_float='/lib/ld-linux-riscv32-ilp32f.so.1';
  239. const defdynlinker_double_float='/lib/ld-linux-riscv32-ilp32d.so.1';
  240. { const defdynlinker_quad_float='/lib/ld-linux-riscv32-ilp32q.so.1'; not yet in ABI list }
  241. var defdynlinker : string;
  242. {$endif riscv32}
  243. {$ifdef riscv64}
  244. const defdynlinker_soft_float='/lib/ld-linux-riscv64-lp64.so.1';
  245. const defdynlinker_single_float='/lib/ld-linux-riscv64-lp64f.so.1';
  246. const defdynlinker_double_float='/lib/ld-linux-riscv64-lp64d.so.1';
  247. { const defdynlinker_quad_float='/lib/ld-linux-riscv64-lp64q.so.1'; not yet in ABI list }
  248. var defdynlinker : string;
  249. {$endif riscv64}
  250. {$ifdef xtensa}
  251. const defdynlinker='/lib/ld.so.1';
  252. {$endif xtensa}
  253. {$ifdef loongarch64}
  254. const defdynlinker='/lib64/ld-linux-loongarch-lp64d.so.1';
  255. {$endif loongarch64}
  256. procedure SetupDynlinker(out DynamicLinker:string;out libctype:TLibcType);
  257. begin
  258. {$ifdef powerpc64}
  259. if defdynlinker='' then
  260. if target_info.abi=abi_powerpc_sysv then
  261. defdynlinker:=defdynlinkerv1
  262. else
  263. defdynlinker:=defdynlinkerv2;
  264. {$endif powerpc64}
  265. {$ifdef riscv32}
  266. if defdynlinker='' then
  267. begin
  268. case target_info.abi of
  269. abi_riscv_ilp32:
  270. defdynlinker:=defdynlinker_soft_float;
  271. abi_riscv_ilp32f:
  272. defdynlinker:=defdynlinker_single_float;
  273. abi_riscv_ilp32d:
  274. defdynlinker:=defdynlinker_double_float;
  275. else
  276. defdynlinker:=defdynlinker_generic;
  277. end;
  278. end;
  279. {$endif riscv32}
  280. {$ifdef riscv64}
  281. if defdynlinker='' then
  282. begin
  283. case target_info.abi of
  284. abi_riscv_lp64:
  285. defdynlinker:=defdynlinker_soft_float;
  286. abi_riscv_lp64f:
  287. defdynlinker:=defdynlinker_single_float;
  288. abi_riscv_lp64d:
  289. defdynlinker:=defdynlinker_double_float;
  290. else
  291. defdynlinker:=defdynlinker_double_float;
  292. end;
  293. end;
  294. {$endif riscv64}
  295. {
  296. Search order:
  297. glibc 2.1+
  298. uclibc
  299. glibc 2.0
  300. If none is found (e.g. when cross compiling) glibc21 is assumed
  301. }
  302. if fileexists(sysrootpath+defdynlinker,false) then
  303. begin
  304. DynamicLinker:=defdynlinker;
  305. {$ifdef i386}
  306. libctype:=glibc21;
  307. {$else i386}
  308. libctype:=glibc2;
  309. {$endif i386}
  310. end
  311. else if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  312. begin
  313. DynamicLinker:='/lib/ld-uClibc.so.0';
  314. libctype:=uclibc;
  315. end
  316. {$ifdef i386}
  317. else if FileExists(sysrootpath+'/lib/ld-linux.so.1',false) then
  318. begin
  319. DynamicLinker:='/lib/ld-linux.so.1';
  320. libctype:=glibc2;
  321. end
  322. {$endif i386}
  323. else
  324. begin
  325. { when no dyn. linker is found, we are probably
  326. cross compiling, so use the default dyn. linker }
  327. DynamicLinker:=defdynlinker;
  328. {
  329. the default c startup script is gcrt0.as on all platforms
  330. except i386
  331. }
  332. {$ifdef i386}
  333. libctype:=glibc21;
  334. {$else i386}
  335. libctype:=glibc2;
  336. {$endif i386}
  337. end;
  338. end;
  339. function ModulesLinkToLibc:boolean;
  340. var
  341. hp: tmodule;
  342. begin
  343. result:=false;
  344. { This is called very early, ImportLibraryList is not yet merged into linkothersharedlibs.
  345. The former contains library names qualified with prefix and suffix (coming from
  346. "external 'c' name 'foo' declarations), the latter contains raw names (from "$linklib c"
  347. directives). }
  348. hp:=tmodule(loaded_units.first);
  349. while assigned(hp) do
  350. begin
  351. result:=Assigned(hp.ImportLibraryList.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext));
  352. if result then break;
  353. result:=hp.linkothersharedlibs.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext);
  354. if result then break;
  355. result:=hp.linkothersharedlibs.find('c');
  356. if result then break;
  357. hp:=tmodule(hp.next);
  358. end;
  359. end;
  360. Constructor TLinkerLinux.Create;
  361. begin
  362. Inherited Create;
  363. SetupLibrarySearchPath;
  364. end;
  365. procedure TLinkerLinux.SetDefaultInfo;
  366. {
  367. This will also detect which libc version will be used
  368. }
  369. var
  370. target_opt: string;
  371. emulation_opt: string;
  372. platformopt: string;
  373. LdProgram: string;
  374. begin
  375. target_opt:='';
  376. emulation_opt:='';
  377. platformopt:='';
  378. LdProgram:='';
  379. {$ifdef i386}
  380. target_opt:=' -b elf32-i386';
  381. emulation_opt:=' -m elf_i386';
  382. {$endif}
  383. {$ifdef x86_64}
  384. target_opt:=' -b elf64-x86-64';
  385. emulation_opt:=' -m elf_x86_64';
  386. {$endif}
  387. {$ifdef powerpc}
  388. target_opt:=' -b elf32-powerpc';
  389. emulation_opt:=' -m elf32ppclinux';
  390. {$endif}
  391. {$ifdef sparc}
  392. target_opt:=' -b elf32-sparc';
  393. emulation_opt:=' -m elf32_sparc';
  394. {$endif}
  395. {$ifdef sparc64}
  396. target_opt:=' -b elf64-sparc';
  397. emulation_opt:=' -m elf64_sparc';
  398. {$endif}
  399. {$ifdef arm} target_opt:='';{$endif} {unknown :( }
  400. {$ifdef aarch64} target_opt:='';{$endif} {unknown :( }
  401. {$ifdef m68k} target_opt:='';{$endif} {unknown :( }
  402. {$ifdef mips32}
  403. {$ifdef mipsel}
  404. platformopt:=' -EL';
  405. emulation_opt:=' -m elf32ltsmip';
  406. {$else}
  407. platformopt:=' -EB';
  408. emulation_opt:=' -m elf32btsmip';
  409. {$endif}
  410. {$endif}
  411. {$ifdef mips64}
  412. {$ifdef mips64el}
  413. platformopt:=' -EL';
  414. emulation_opt:=' -m elf64ltsmip';
  415. {$else}
  416. platformopt:=' -EB';
  417. emulation_opt:=' -m elf64btsmip';
  418. {$endif}
  419. {$endif}
  420. {$ifdef riscv32}
  421. target_opt:=' -m elf32lriscv';
  422. {$endif}
  423. {$ifdef riscv64}
  424. target_opt:=' -m elf64lriscv';
  425. {$endif}
  426. {$ifdef loongarch64}
  427. target_opt:='';
  428. {$endif}
  429. {$ifdef powerpc64}
  430. if (target_info.abi=abi_powerpc_elfv2) and
  431. (target_info.endian=endian_little) then
  432. begin
  433. target_opt:=' -b elf64-powerpcle';
  434. emulation_opt:=' -m elf64lppc';
  435. end
  436. else
  437. begin
  438. target_opt:=' -b elf64-powerpc';
  439. emulation_opt:=' -m elf64ppc';
  440. end;
  441. {$endif powerpc64}
  442. {$ifdef xtensa}
  443. if target_info.endian=endian_little then
  444. begin
  445. target_opt:=' -b elf32-xtensa-le';
  446. emulation_opt:=' -m elf32xtensa';
  447. end
  448. else
  449. begin
  450. target_opt:=' -b elf32-xtensa-be';
  451. emulation_opt:=' -m elf32xtensa';
  452. end;
  453. if target_info.abi=abi_xtensa_call0 then
  454. platformopt:=platformopt+' --abi-call0'
  455. else if target_info.abi=abi_xtensa_windowed then
  456. platformopt:=platformopt+' --abi-windowed';
  457. {$endif}
  458. {$ifdef arm}
  459. platformopt:=' -z noexecstack';
  460. {$endif arm}
  461. if cs_link_lld in current_settings.globalswitches then
  462. begin
  463. LdProgram:='ld.lld';
  464. target_opt:=' -b elf';
  465. end
  466. else
  467. LdProgram:='ld';
  468. with Info do
  469. begin
  470. ExeCmd[1]:=LdProgram+target_opt+emulation_opt+platformopt+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP $LTO $RPATH -L. -o $EXE';
  471. DllCmd[1]:=LdProgram+target_opt+emulation_opt+platformopt+' $OPT $INIT $FINI $SONAME $MAP $LTO $RPATH -shared $GCSECTIONS -L. -o $EXE';
  472. { when we want to cross-link we need to override default library paths;
  473. when targeting binutils 2.19 or later, we use the "INSERT" command to
  474. augment the default linkerscript, which also requires -T (normally that
  475. option means "completely replace the default linkerscript) }
  476. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) or
  477. (length(sysrootpath)>0) then
  478. begin
  479. ExeCmd[1]:=ExeCmd[1]+' -T';
  480. DllCmd[1]:=DllCmd[1]+' -T';
  481. end;
  482. ExeCmd[1]:=ExeCmd[1]+' $RES';
  483. DllCmd[1]:=DllCmd[1]+' $RES';
  484. DllCmd[2]:='strip --strip-unneeded $EXE';
  485. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  486. ExtDbgCmd[2]:='objcopy "--add-gnu-debuglink=$DBGX" $EXE';
  487. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  488. SetupDynlinker(DynamicLinker,libctype);
  489. end;
  490. end;
  491. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  492. // put your linkorder/linkalias overrides here.
  493. // Note: assumes only called when reordering/aliasing is used.
  494. Begin
  495. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  496. Begin
  497. LinkLibraryOrder.add('gcc','',15);
  498. LinkLibraryOrder.add('c','',100);
  499. LinkLibraryOrder.add('gmon','',120);
  500. LinkLibraryOrder.add('dl','',140);
  501. LinkLibraryOrder.add('pthread','',160);
  502. end;
  503. End;
  504. type
  505. tlibcnames=array [TLibcType] of string[8];
  506. const { libc5 glibc2 glibc21 uclibc }
  507. cprtnames: tlibcnames = ('cprt0', 'cprt0', 'cprt21', 'ucprt0');
  508. csinames: tlibcnames = ('si_c', 'si_c', 'si_c21', 'si_uc');
  509. gprtnames: tlibcnames = ('gprt0', 'gprt0', 'gprt21', 'ugprt0');
  510. gsinames: tlibcnames = ('si_g', 'si_g', 'si_c21g','si_ucg');
  511. defprtnames: array[boolean] of string[8] = ('prt0', 'dllprt0');
  512. defsinames: array[boolean] of string[8] = ('si_prc','si_dll');
  513. { uclibc and glibc21 are not available on x86_64! si_g is also absent. }
  514. Procedure TLinkerLinux.InitSysInitUnitName;
  515. begin
  516. linklibc:=ModulesLinkToLibc;
  517. reorder:=linklibc and ReOrderEntries;
  518. sysinitunit:=defsinames[current_module.islibrary];
  519. prtobj:=defprtnames[current_module.islibrary];
  520. if current_module.islibrary then
  521. exit;
  522. if cs_profile in current_settings.moduleswitches then
  523. begin
  524. prtobj:=gprtnames[libctype];
  525. sysinitunit:=gsinames[libctype];
  526. linklibc:=true;
  527. end
  528. else if linklibc then
  529. begin
  530. prtobj:=cprtnames[libctype];
  531. sysinitunit:=csinames[libctype];
  532. end;
  533. end;
  534. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  535. Var
  536. linkres : TLinkRes;
  537. i : longint;
  538. HPath : TCmdStrListItem;
  539. s,s1,s2 : TCmdStr;
  540. found1,
  541. found2 : boolean;
  542. linksToSharedLibFiles, libraryadded: boolean;
  543. begin
  544. result:=False;
  545. { set special options for some targets }
  546. if cs_profile in current_settings.moduleswitches then
  547. begin
  548. if not(libctype in [glibc2,glibc21]) then
  549. AddSharedLibrary('gmon');
  550. AddSharedLibrary('c');
  551. end;
  552. { Open link.res file }
  553. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  554. with linkres do
  555. begin
  556. { Write path to search libraries }
  557. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  558. while assigned(HPath) do
  559. begin
  560. Add('SEARCH_DIR("'+HPath.Str+'")');
  561. HPath:=TCmdStrListItem(HPath.Next);
  562. end;
  563. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  564. while assigned(HPath) do
  565. begin
  566. Add('SEARCH_DIR("'+HPath.Str+'")');
  567. HPath:=TCmdStrListItem(HPath.Next);
  568. end;
  569. { force local symbol resolution (i.e., inside the shared }
  570. { library itself) for all non-exorted symbols, otherwise }
  571. { several RTL symbols of FPC-compiled shared libraries }
  572. { will be bound to those of a single shared library or }
  573. { to the main program }
  574. if (isdll) then
  575. begin
  576. add('VERSION');
  577. add('{');
  578. add(' {');
  579. if not texportlibunix(exportlib).exportedsymnames.empty then
  580. begin
  581. add(' global:');
  582. repeat
  583. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  584. until texportlibunix(exportlib).exportedsymnames.empty;
  585. end;
  586. add(' local:');
  587. add(' *;');
  588. add(' };');
  589. add('}');
  590. end;
  591. StartSection('INPUT(');
  592. { add objectfiles, start with prt0 always }
  593. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  594. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  595. { try to add crti and crtbegin if linking to C }
  596. if linklibc and (libctype<>uclibc) then
  597. begin
  598. { crti.o must come first }
  599. if librarysearchpath.FindFile('crti.o',false,s) then
  600. AddFileName(s)
  601. else
  602. Message1(exec_w_init_file_not_found,'crti.o');
  603. { then the crtbegin* }
  604. if (cs_create_pic in current_settings.moduleswitches)
  605. {$ifdef RISCV}
  606. { on RISC-V we need to use always the *S.o variants
  607. if shared libraries are involved }
  608. or (not SharedLibFiles.Empty)
  609. {$endif RISCV}
  610. then
  611. begin
  612. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  613. AddFileName(s)
  614. else
  615. Message1(exec_w_init_file_not_found,'crtbeginS.o');
  616. end
  617. else
  618. if (cs_link_staticflag in current_settings.globalswitches) then
  619. begin
  620. if librarysearchpath.FindFile('crtbeginT.o',false,s) then
  621. AddFileName(s)
  622. else
  623. Message1(exec_w_init_file_not_found,'crtbeginT.o');
  624. end
  625. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  626. AddFileName(s)
  627. else
  628. Message1(exec_w_init_file_not_found,'crtbegin.o');
  629. end;
  630. { main objectfiles }
  631. while not ObjectFiles.Empty do
  632. begin
  633. s:=ObjectFiles.GetFirst;
  634. if s<>'' then
  635. AddFileName(maybequoted(s));
  636. end;
  637. EndSection(')');
  638. { Write staticlibraries }
  639. if not StaticLibFiles.Empty then
  640. begin
  641. Add('GROUP(');
  642. While not StaticLibFiles.Empty do
  643. begin
  644. S:=StaticLibFiles.GetFirst;
  645. AddFileName(maybequoted(s))
  646. end;
  647. Add(')');
  648. end;
  649. // we must reorder here because the result could empty sharedlibfiles
  650. if reorder Then
  651. ExpandAndApplyOrder(SharedLibFiles);
  652. // after this point addition of shared libs not allowed.
  653. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  654. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  655. if isdll and not linklibc then
  656. begin
  657. Add('INPUT(');
  658. Add(sysrootpath+info.DynamicLinker);
  659. Add(')');
  660. end;
  661. linksToSharedLibFiles := not SharedLibFiles.Empty;
  662. if not SharedLibFiles.Empty then
  663. begin
  664. if (SharedLibFiles.Count<>1) or
  665. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  666. reorder then
  667. begin
  668. libraryadded:=false;
  669. Add('INPUT(');
  670. While not SharedLibFiles.Empty do
  671. begin
  672. S:=SharedLibFiles.GetFirst;
  673. if (s<>'c') or reorder then
  674. begin
  675. i:=Pos(target_info.sharedlibext,S);
  676. if i>0 then
  677. Delete(S,i,255);
  678. Add('-l'+s);
  679. libraryadded:=true;
  680. end
  681. else
  682. linklibc:=true;
  683. end;
  684. { link explicitly against the dyn. linker in case we are using section threadvars and
  685. if we link against any other library. We need __tls_get_addr from the dyn. linker in this case.
  686. This does not hurt as in case we use a dyn. library we depend on the dyn. linker anyways.
  687. All this does not apply if we link anyways against libc }
  688. if libraryadded and not(linklibc) and not(isdll) and (tf_section_threadvars in target_info.flags) then
  689. Add('-l:'+ExtractFileName(defdynlinker));
  690. Add(')');
  691. end
  692. else
  693. linklibc:=true;
  694. if (cs_link_staticflag in current_settings.globalswitches) or
  695. (linklibc and not reorder) then
  696. begin
  697. Add('GROUP(');
  698. { when we have -static for the linker the we also need libgcc }
  699. if (cs_link_staticflag in current_settings.globalswitches) then
  700. begin
  701. Add('-lgcc');
  702. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  703. Add('-lgcc_eh');
  704. end;
  705. { be sure that libc is the last lib }
  706. if linklibc and not reorder then
  707. Add('-lc');
  708. Add(')');
  709. end;
  710. end;
  711. { objects which must be at the end }
  712. if linklibc and (libctype<>uclibc) then
  713. begin
  714. if (cs_create_pic in current_settings.moduleswitches)
  715. {$ifdef RISCV}
  716. { on RISC-V we need to use always the *S.o variants
  717. if shared libraries are involved }
  718. or linksToSharedLibFiles
  719. {$endif RISCV}
  720. then
  721. begin
  722. found1:=librarysearchpath.FindFile('crtendS.o',false,s1);
  723. if not(found1) then
  724. Message1(exec_w_init_file_not_found,'crtendS.o');
  725. end
  726. else
  727. begin
  728. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  729. if not(found1) then
  730. Message1(exec_w_init_file_not_found,'crtend.o');
  731. end;
  732. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  733. if not(found2) then
  734. Message1(exec_w_init_file_not_found,'crtn.o');
  735. if found1 or found2 then
  736. begin
  737. Add('INPUT(');
  738. if found1 then
  739. AddFileName(s1);
  740. if found2 then
  741. AddFileName(s2);
  742. Add(')');
  743. end;
  744. end;
  745. { Entry point. Only needed for executables, as for shared lubraries we use
  746. the -init command line option instead
  747. The "ENTRY" linkerscript command does not have any effect when augmenting
  748. a linker script, so use the command line parameter instead }
  749. if (not isdll) then
  750. if (linksToSharedLibFiles and not linklibc) then
  751. info.ExeCmd[1]:=info.ExeCmd[1]+' -e _dynamic_start'
  752. else
  753. info.ExeCmd[1]:=info.ExeCmd[1]+' -e _start';
  754. add('SECTIONS');
  755. add('{');
  756. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) then
  757. { we can't use ".data", as that would hide the .data from the
  758. original linker script in combination with the INSERT at the end }
  759. add(' .fpcdata :')
  760. else
  761. add(' .data :');
  762. add(' {');
  763. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  764. add(' }');
  765. if not(cs_debuginfo in current_settings.moduleswitches) and
  766. not(tf_use_psabieh in target_info.flags) then
  767. add(' /DISCARD/ : {*(.debug_frame)}');
  768. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  769. add('}');
  770. { this "INSERT" means "merge into the original linker script, even if
  771. -T is used" }
  772. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) then
  773. add('INSERT AFTER .data;');
  774. { Write and Close response }
  775. writetodisk;
  776. Free;
  777. end;
  778. WriteResponseFile:=True;
  779. end;
  780. function TLinkerLinux.MakeExecutable:boolean;
  781. var
  782. i : longint;
  783. binstr,
  784. cmdstr,
  785. mapstr,
  786. ltostr,
  787. rpathstr,
  788. sanitizerLibraryDir, s: TCmdStr;
  789. success : boolean;
  790. DynLinkStr : ansistring;
  791. GCSectionsStr,
  792. StaticStr,
  793. StripStr : string[40];
  794. begin
  795. if not(cs_link_nolink in current_settings.globalswitches) then
  796. Message1(exec_i_linking,current_module.exefilename);
  797. { Create some replacements }
  798. StaticStr:='';
  799. StripStr:='';
  800. GCSectionsStr:='';
  801. DynLinkStr:='';
  802. mapstr:='';
  803. ltostr:='';
  804. rpathstr:='';
  805. if (cs_link_staticflag in current_settings.globalswitches) then
  806. StaticStr:='-static';
  807. if (cs_link_strip in current_settings.globalswitches) and
  808. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  809. StripStr:='-s';
  810. if (cs_link_map in current_settings.globalswitches) then
  811. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  812. if (cs_link_smart in current_settings.globalswitches) and
  813. create_smartlink_sections then
  814. GCSectionsStr:='--gc-sections';
  815. If (cs_profile in current_settings.moduleswitches) or
  816. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  817. begin
  818. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  819. if cshared then
  820. DynLinkStr:=DynLinkStr+' --shared ';
  821. if rlinkpath<>'' then
  822. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  823. End;
  824. { add custom LTO library if using custom clang }
  825. if (cs_lto in current_settings.moduleswitches) and
  826. not(cs_link_on_target in current_settings.globalswitches) and
  827. (utilsdirectory<>'') and
  828. FileExists(utilsdirectory+'/../lib/LLVMgold.so',true) then
  829. begin
  830. ltostr:='-plugin '+maybequoted(utilsdirectory+'/../lib/LLVMgold.so ');
  831. end;
  832. if AddSanitizerLibrariesAndGetSearchDir('linux',sanitizerLibraryDir) then
  833. begin
  834. rpathstr:='-rpath '+maybequoted(sanitizerLibraryDir);
  835. end;
  836. { Write used files and libraries }
  837. WriteResponseFile(false);
  838. { Call linker }
  839. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  840. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  841. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  842. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  843. Replace(cmdstr,'$STATIC',StaticStr);
  844. Replace(cmdstr,'$STRIP',StripStr);
  845. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  846. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  847. Replace(cmdstr,'$MAP',mapstr);
  848. Replace(cmdstr,'$LTO',ltostr);
  849. Replace(cmdstr,'$RPATH',rpathstr);
  850. { create dynamic symbol table? }
  851. if HasExports then
  852. cmdstr:=cmdstr+' -E';
  853. { create eh_frame_hdr section? }
  854. if tf_use_psabieh in target_info.flags then
  855. cmdstr:=cmdstr+ ' --eh-frame-hdr';
  856. if cs_large in current_settings.globalswitches then
  857. cmdstr:=cmdstr+' --no-relax';
  858. s:=FindUtil(utilsprefix+BinStr+'.bfd',false);
  859. if FileExists(s, True) then
  860. binstr:=s
  861. else
  862. { fallback to ld for very old or custom binutils }
  863. binstr:=FindUtil(utilsprefix+BinStr);
  864. success:=DoExec(binstr,CmdStr,true,false);
  865. { Create external .dbg file with debuginfo }
  866. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  867. begin
  868. for i:=1 to 3 do
  869. begin
  870. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  871. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  872. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  873. Replace(cmdstr,'$DBGX',current_module.dbgfilename);
  874. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  875. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  876. if not success then
  877. break;
  878. end;
  879. end;
  880. { Remove ReponseFile }
  881. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  882. DeleteFile(outputexedir+Info.ResName);
  883. { Post process,
  884. as it only writes sections sizes so far, do this only if V_Info is set }
  885. if success and CheckVerbosity(V_Info) and not(cs_link_nolink in current_settings.globalswitches) then
  886. { do not change success here as we are only writing some info, so if this fails, it does not matter }
  887. { success:= }PostProcessExecutable(current_module.exefilename,false);
  888. MakeExecutable:=success; { otherwise a recursive call to link method }
  889. end;
  890. Function TLinkerLinux.MakeSharedLibrary:boolean;
  891. var
  892. InitStr,
  893. FiniStr,
  894. GCSectionsStr,
  895. SoNameStr : string[80];
  896. binstr,
  897. cmdstr,
  898. mapstr,
  899. ltostr,
  900. rpathstr,
  901. sanitizerLibraryDir: TCmdStr;
  902. success : boolean;
  903. begin
  904. MakeSharedLibrary:=false;
  905. mapstr:='';
  906. ltostr:='';
  907. rpathstr:='';
  908. if not(cs_link_nolink in current_settings.globalswitches) then
  909. Message1(exec_i_linking,current_module.sharedlibfilename);
  910. if (cs_link_smart in current_settings.globalswitches) and
  911. create_smartlink_sections then
  912. GCSectionsStr:='--gc-sections'
  913. else
  914. GCSectionsStr:='';
  915. { Write used files and libraries }
  916. WriteResponseFile(true);
  917. { Create some replacements }
  918. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  919. InitStr:='-init FPC_SHARED_LIB_START';
  920. FiniStr:='-fini FPC_LIB_EXIT';
  921. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  922. if (cs_link_map in current_settings.globalswitches) then
  923. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  924. { add custom LTO library if using custom clang }
  925. if (cs_lto in current_settings.moduleswitches) and
  926. not(cs_link_on_target in current_settings.globalswitches) and
  927. (utilsdirectory<>'') and
  928. FileExists(utilsdirectory+'/../lib/LLVMgold.so',true) then
  929. begin
  930. ltostr:='-plugin '+maybequoted(utilsdirectory+'/../lib/LLVMgold.so ');
  931. end;
  932. if AddSanitizerLibrariesAndGetSearchDir('linux',sanitizerLibraryDir) then
  933. begin
  934. rpathstr:='-rpath '+maybequoted(sanitizerLibraryDir)
  935. end;
  936. { Call linker }
  937. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  938. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  939. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  940. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  941. Replace(cmdstr,'$INIT',InitStr);
  942. Replace(cmdstr,'$FINI',FiniStr);
  943. Replace(cmdstr,'$SONAME',SoNameStr);
  944. Replace(cmdstr,'$MAP',mapstr);
  945. Replace(cmdstr,'$LTO',ltostr);
  946. Replace(cmdstr,'$RPATH',rpathstr);
  947. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  948. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  949. { Strip the library ? }
  950. if success and (cs_link_strip in current_settings.globalswitches) then
  951. begin
  952. { only remove non global symbols and debugging info for a library }
  953. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  954. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  955. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  956. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  957. end;
  958. { Remove ReponseFile }
  959. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  960. DeleteFile(outputexedir+Info.ResName);
  961. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  962. end;
  963. function TLinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  964. begin
  965. Result:=PostProcessELFExecutable(fn,isdll);
  966. end;
  967. {*****************************************************************************
  968. TINTERNALLINKERLINUX
  969. *****************************************************************************}
  970. constructor TInternalLinkerLinux.Create;
  971. begin
  972. inherited Create;
  973. SetupLibrarySearchPath;
  974. SetupDynlinker(dynlinker,libctype);
  975. CArObjectReader:=TArObjectReader;
  976. CExeOutput:=ElfExeOutputClass;
  977. CObjInput:=TElfObjInput;
  978. end;
  979. procedure TInternalLinkerLinux.InitSysInitUnitName;
  980. begin
  981. linklibc:=ModulesLinkToLibc;
  982. reorder:=linklibc and ReOrderEntries;
  983. sysinitunit:=defsinames[current_module.islibrary];
  984. prtobj:=defprtnames[current_module.islibrary];
  985. if cs_profile in current_settings.moduleswitches then
  986. begin
  987. prtobj:=gprtnames[libctype];
  988. sysinitunit:=gsinames[libctype];
  989. linklibc:=true;
  990. end
  991. else if linklibc then
  992. begin
  993. prtobj:=cprtnames[libctype];
  994. sysinitunit:=csinames[libctype];
  995. end;
  996. end;
  997. const
  998. relsec_prefix:array[boolean] of TCmdStr = ('rel','rela');
  999. procedure TInternalLinkerLinux.DefaultLinkScript;
  1000. var
  1001. s,s1,s2,relprefix:TCmdStr;
  1002. found1,found2:boolean;
  1003. linkToSharedLibs:boolean;
  1004. procedure AddLibraryStatement(const s:TCmdStr);
  1005. var
  1006. i:longint;
  1007. s1,s2:TCmdStr;
  1008. begin
  1009. i:=pos(target_info.sharedClibext+'.',s);
  1010. if (i>0) then
  1011. s1:=target_info.sharedClibprefix+S
  1012. else
  1013. s1:=target_info.sharedClibprefix+S+target_info.sharedClibext;
  1014. { TODO: to be compatible with ld search algorithm, each found file
  1015. must be tested for target compatibility, incompatible ones should be skipped. }
  1016. { TODO: shall we search library without suffix if one with suffix is not found? }
  1017. if (not(cs_link_staticflag in current_settings.globalswitches)) and
  1018. FindLibraryFile(s1,'','',s2) then
  1019. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1020. { TODO: static libraries never have numeric suffix in their names }
  1021. else if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  1022. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1023. else
  1024. Comment(V_Error,'Import library not found for '+S);
  1025. end;
  1026. begin
  1027. if cs_profile in current_settings.moduleswitches then
  1028. begin
  1029. if not(libctype in [glibc2,glibc21]) then
  1030. AddSharedLibrary('gmon');
  1031. AddSharedLibrary('c');
  1032. end;
  1033. TElfExeOutput(exeoutput).interpreter:=stringdup(dynlinker);
  1034. { add objectfiles, start with prt0 always }
  1035. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  1036. LinkScript.Concat('READOBJECT '+ maybequoted(FindObjectFile(prtobj,'',false)));
  1037. { try to add crti and crtbegin if linking to C }
  1038. if linklibc and (libctype<>uclibc) then
  1039. begin
  1040. { crti.o must come first }
  1041. if librarysearchpath.FindFile('crti.o',false,s) then
  1042. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1043. { then the crtbegin* }
  1044. if cs_create_pic in current_settings.moduleswitches then
  1045. begin
  1046. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  1047. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1048. end
  1049. else
  1050. if (cs_link_staticflag in current_settings.globalswitches) and
  1051. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  1052. LinkScript.Concat('READOBJECT '+maybequoted(s))
  1053. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  1054. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1055. end;
  1056. ScriptAddSourceStatements(false);
  1057. { we must reorder here because the result could empty sharedlibfiles }
  1058. if reorder then
  1059. ExpandAndApplyOrder(SharedLibFiles);
  1060. { See tw9089*.pp: if more than one pure-Pascal shared libs are loaded,
  1061. and none have rtld in their DT_NEEDED, then rtld cannot finalize correctly. }
  1062. if IsSharedLibrary then
  1063. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(sysrootpath+dynlinker));
  1064. linkToSharedLibs:=(not SharedLibFiles.Empty);
  1065. { Symbols declared as "external 'libx.so'" are added to ImportLibraryList, library
  1066. prefix/extension *not* stripped. TImportLibLinux copies these to SharedLibFiles,
  1067. stripping prefixes and extensions.
  1068. However extension won't be stripped if library is specified with numeric suffix
  1069. (like "libpango-1.0.so.0")
  1070. Libraries specified with $LINKLIB directive are directly added to SharedLibFiles
  1071. and won't be present in ImportLibraryList. }
  1072. while not SharedLibFiles.Empty do
  1073. begin
  1074. S:=SharedLibFiles.GetFirst;
  1075. if (S<>'c') or reorder then
  1076. AddLibraryStatement(S);
  1077. end;
  1078. if (cs_link_staticflag in current_settings.globalswitches) or
  1079. (linklibc and not reorder) then
  1080. begin
  1081. LinkScript.Concat('GROUP');
  1082. if (cs_link_staticflag in current_settings.globalswitches) then
  1083. begin
  1084. AddLibraryStatement('gcc');
  1085. AddLibraryStatement('gcc_eh');
  1086. end;
  1087. if linklibc and not reorder then
  1088. AddLibraryStatement('c');
  1089. LinkScript.Concat('ENDGROUP');
  1090. end;
  1091. { objects which must be at the end }
  1092. if linklibc and (libctype<>uclibc) then
  1093. begin
  1094. if cs_create_pic in current_settings.moduleswitches then
  1095. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  1096. else
  1097. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  1098. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  1099. if found1 then
  1100. LinkScript.Concat('READOBJECT '+maybequoted(s1));
  1101. if found2 then
  1102. LinkScript.Concat('READOBJECT '+maybequoted(s2));
  1103. end;
  1104. if (not IsSharedLibrary) then
  1105. if (linkToSharedLibs and not linklibc) then
  1106. LinkScript.Concat('ENTRYNAME _dynamic_start')
  1107. else
  1108. LinkScript.Concat('ENTRYNAME _start')
  1109. else
  1110. LinkScript.Concat('ISSHAREDLIBRARY');
  1111. relprefix:=relsec_prefix[ElfTarget.relocs_use_addend];
  1112. with LinkScript do
  1113. begin
  1114. Concat('HEADER');
  1115. Concat('EXESECTION .interp');
  1116. Concat(' OBJSECTION .interp');
  1117. Concat('ENDEXESECTION');
  1118. Concat('EXESECTION .note.ABI-tag');
  1119. Concat(' OBJSECTION .note.ABI-tag');
  1120. Concat('ENDEXESECTION');
  1121. Concat('EXESECTION .note.gnu.build-id');
  1122. Concat(' OBJSECTION .note.gnu.build-id');
  1123. Concat('ENDEXESECTION');
  1124. Concat('EXESECTION .hash');
  1125. Concat(' OBJSECTION .hash');
  1126. Concat('ENDEXESECTION');
  1127. Concat('EXESECTION .dynsym');
  1128. Concat(' OBJSECTION .dynsym');
  1129. Concat('ENDEXESECTION');
  1130. Concat('EXESECTION .dynstr');
  1131. Concat(' OBJSECTION .dynstr');
  1132. Concat('ENDEXESECTION');
  1133. Concat('EXESECTION .gnu.version');
  1134. Concat(' OBJSECTION .gnu.version');
  1135. Concat('ENDEXESECTION');
  1136. Concat('EXESECTION .gnu.version_d');
  1137. Concat(' OBJSECTION .gnu.version_d');
  1138. Concat('ENDEXESECTION');
  1139. Concat('EXESECTION .gnu.version_r');
  1140. Concat(' OBJSECTION .gnu.version_r');
  1141. Concat('ENDEXESECTION');
  1142. Concat('EXESECTION .'+relprefix+'.dyn');
  1143. Concat(' OBJSECTION .'+relprefix+'.dyn');
  1144. Concat('ENDEXESECTION');
  1145. Concat('EXESECTION .'+relprefix+'.plt');
  1146. Concat(' OBJSECTION .'+relprefix+'.plt');
  1147. Concat(' PROVIDE __'+relprefix+'_iplt_start');
  1148. Concat(' OBJSECTION .'+relprefix+'.iplt');
  1149. Concat(' PROVIDE __'+relprefix+'_iplt_end');
  1150. Concat('ENDEXESECTION');
  1151. Concat('EXESECTION .init');
  1152. Concat(' OBJSECTION .init');
  1153. Concat('ENDEXESECTION');
  1154. Concat('EXESECTION .plt');
  1155. Concat(' OBJSECTION .plt');
  1156. Concat('ENDEXESECTION');
  1157. Concat('EXESECTION .text');
  1158. Concat(' OBJSECTION .text*');
  1159. Concat('ENDEXESECTION');
  1160. Concat('EXESECTION .fini');
  1161. Concat(' OBJSECTION .fini');
  1162. Concat(' PROVIDE __etext');
  1163. Concat(' PROVIDE _etext');
  1164. Concat(' PROVIDE etext');
  1165. Concat('ENDEXESECTION');
  1166. Concat('EXESECTION .rodata');
  1167. Concat(' OBJSECTION .rodata*');
  1168. Concat('ENDEXESECTION');
  1169. {$ifdef arm}
  1170. Concat('EXESECTION .ARM.extab');
  1171. Concat(' OBJSECTION .ARM.extab*');
  1172. Concat('ENDEXESECTION');
  1173. Concat('EXESECTION .ARM.exidx');
  1174. Concat(' SYMBOL __exidx_start');
  1175. Concat(' OBJSECTION .ARM.exidx*');
  1176. Concat(' SYMBOL __exidx_end');
  1177. Concat('ENDEXESECTION');
  1178. {$endif}
  1179. Concat('EXESECTION .eh_frame');
  1180. Concat(' OBJSECTION .eh_frame');
  1181. Concat('ENDEXESECTION');
  1182. Concat('EXESECTION .gcc_except_table');
  1183. Concat(' OBJSECTION .gcc_except_table');
  1184. Concat(' OBJSECTION .gcc_except_table.*');
  1185. Concat('ENDEXESECTION');
  1186. Concat('EXESECTION .tdata');
  1187. Concat(' OBJSECTION .tdata');
  1188. Concat(' OBJSECTION .tdata.*');
  1189. Concat('ENDEXESECTION');
  1190. Concat('EXESECTION .tbss');
  1191. Concat(' OBJSECTION .tbss');
  1192. Concat(' OBJSECTION .tbss.*');
  1193. Concat('ENDEXESECTION');
  1194. Concat('EXESECTION .preinit_array');
  1195. Concat(' PROVIDE __preinit_array_start');
  1196. Concat(' OBJSECTION .preinit_array');
  1197. Concat(' PROVIDE __preinit_array_end');
  1198. Concat('ENDEXESECTION');
  1199. Concat('EXESECTION .init_array');
  1200. Concat(' PROVIDE __init_array_start');
  1201. { why the hell .ctors are both here and exesection .ctors below?? }
  1202. // KEEP ( *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
  1203. Concat(' OBJSECTION .init_array');
  1204. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
  1205. Concat('PROVIDE __init_array_end');
  1206. Concat('ENDEXESECTION');
  1207. Concat('EXESECTION .fini_array');
  1208. Concat(' PROVIDE __fini_array_start');
  1209. // KEEP ( *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
  1210. Concat(' OBJSECTION .fini_array');
  1211. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
  1212. Concat(' PROVIDE __fini_array_end');
  1213. Concat('ENDEXESECTION');
  1214. Concat('EXESECTION .ctors');
  1215. Concat(' OBJSECTION .ctors*');
  1216. Concat('ENDEXESECTION');
  1217. Concat('EXESECTION .dtors');
  1218. Concat(' OBJSECTION .dtors*');
  1219. Concat('ENDEXESECTION');
  1220. Concat('EXESECTION .jcr');
  1221. Concat(' OBJSECTION .jcr');
  1222. Concat('ENDEXESECTION');
  1223. Concat('EXESECTION .dynamic');
  1224. Concat(' OBJSECTION .dynamic');
  1225. Concat('ENDEXESECTION');
  1226. {$ifndef mips}
  1227. Concat('EXESECTION .got');
  1228. {$ifdef arm}
  1229. Concat(' OBJSECTION .got.plt');
  1230. {$endif arm}
  1231. Concat(' OBJSECTION .got');
  1232. Concat('ENDEXESECTION');
  1233. {$endif mips}
  1234. {$ifndef arm}
  1235. Concat('EXESECTION .got.plt');
  1236. Concat(' OBJSECTION .got.plt');
  1237. Concat('ENDEXESECTION');
  1238. {$endif arm}
  1239. Concat('EXESECTION .data');
  1240. Concat(' OBJSECTION .data*');
  1241. Concat(' OBJSECTION .fpc*');
  1242. Concat(' OBJSECTION fpc.resources');
  1243. Concat(' PROVIDE _edata');
  1244. Concat(' PROVIDE edata');
  1245. Concat('ENDEXESECTION');
  1246. {$ifdef mips}
  1247. Concat('EXESECTION .got');
  1248. Concat(' OBJSECTION .got');
  1249. Concat('ENDEXESECTION');
  1250. {$endif mips}
  1251. Concat('EXESECTION .bss');
  1252. Concat(' OBJSECTION .dynbss');
  1253. Concat(' OBJSECTION .bss*');
  1254. Concat(' OBJSECTION fpc.reshandles');
  1255. Concat(' PROVIDE end');
  1256. Concat(' SYMBOL _end');
  1257. Concat('ENDEXESECTION');
  1258. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,'+
  1259. '.debug_abbrev,.debug_line,.debug_frame,.debug_str,.debug_loc,'+
  1260. '.debug_macinfo,.debug_weaknames,.debug_funcnames,.debug_typenames,.debug_varnames,.debug_ranges');
  1261. Concat('EXESECTION .stab');
  1262. Concat(' OBJSECTION .stab');
  1263. Concat('ENDEXESECTION');
  1264. Concat('EXESECTION .stabstr');
  1265. Concat(' OBJSECTION .stabstr');
  1266. Concat('ENDEXESECTION');
  1267. end;
  1268. end;
  1269. {*****************************************************************************
  1270. Initialize
  1271. *****************************************************************************}
  1272. initialization
  1273. RegisterLinker(ld_linux,TLinkerLinux);
  1274. RegisterLinker(ld_int_linux,TInternalLinkerLinux);
  1275. {$ifdef i386}
  1276. RegisterImport(system_i386_linux,timportliblinux);
  1277. RegisterExport(system_i386_linux,texportliblinux);
  1278. RegisterTarget(system_i386_linux_info);
  1279. {$endif i386}
  1280. {$ifdef m68k}
  1281. RegisterImport(system_m68k_linux,timportliblinux);
  1282. RegisterExport(system_m68k_linux,texportliblinux);
  1283. RegisterTarget(system_m68k_linux_info);
  1284. {$endif m68k}
  1285. {$ifdef powerpc}
  1286. RegisterImport(system_powerpc_linux,timportliblinux);
  1287. RegisterExport(system_powerpc_linux,texportliblinux);
  1288. RegisterTarget(system_powerpc_linux_info);
  1289. {$endif powerpc}
  1290. {$ifdef powerpc64}
  1291. { default to little endian either when compiling with -dppc64le, or when
  1292. compiling on a little endian ppc64 platform }
  1293. {$if defined(ppc64le) or (defined(cpupowerpc64) and defined(FPC_LITTLE_ENDIAN))}
  1294. system_powerpc64_linux_info.endian:=endian_little;
  1295. system_powerpc64_linux_info.abi:=abi_powerpc_elfv2;
  1296. {$endif}
  1297. RegisterImport(system_powerpc64_linux,timportliblinux);
  1298. RegisterExport(system_powerpc64_linux,texportliblinux);
  1299. RegisterTarget(system_powerpc64_linux_info);
  1300. {$endif powerpc64}
  1301. {$ifdef x86_64}
  1302. RegisterImport(system_x86_64_linux,timportliblinux);
  1303. RegisterExport(system_x86_64_linux,texportliblinux);
  1304. RegisterTarget(system_x86_64_linux_info);
  1305. RegisterTarget(system_x86_6432_linux_info);
  1306. {$endif x86_64}
  1307. {$ifdef SPARC}
  1308. RegisterImport(system_SPARC_linux,timportliblinux);
  1309. RegisterExport(system_SPARC_linux,texportliblinux);
  1310. RegisterTarget(system_SPARC_linux_info);
  1311. {$endif SPARC}
  1312. {$ifdef SPARC64}
  1313. RegisterImport(system_SPARC64_linux,timportliblinux);
  1314. RegisterExport(system_SPARC64_linux,texportliblinux);
  1315. RegisterTarget(system_SPARC64_linux_info);
  1316. {$endif SPARC64}
  1317. {$ifdef ARM}
  1318. RegisterImport(system_arm_linux,timportliblinux);
  1319. RegisterExport(system_arm_linux,texportliblinux);
  1320. RegisterTarget(system_arm_linux_info);
  1321. {$endif ARM}
  1322. {$ifdef aarch64}
  1323. RegisterImport(system_aarch64_linux,timportliblinux);
  1324. RegisterExport(system_aarch64_linux,texportliblinux);
  1325. RegisterTarget(system_aarch64_linux_info);
  1326. {$endif aarch64}
  1327. {$ifdef MIPS32}
  1328. {$ifdef MIPSEL}
  1329. RegisterImport(system_mipsel_linux,timportliblinux);
  1330. RegisterExport(system_mipsel_linux,texportliblinux);
  1331. RegisterTarget(system_mipsel_linux_info);
  1332. {$else MIPSEL}
  1333. RegisterImport(system_mipseb_linux,timportliblinux);
  1334. RegisterExport(system_mipseb_linux,texportliblinux);
  1335. RegisterTarget(system_mipseb_linux_info);
  1336. {$endif MIPSEL}
  1337. {$endif MIPS32}
  1338. {$ifdef MIPS64EB}
  1339. RegisterImport(system_mips64_linux,timportliblinux);
  1340. RegisterExport(system_mips64_linux,texportliblinux);
  1341. RegisterTarget(system_mips64_linux_info);
  1342. {$endif MIPS64EB}
  1343. {$ifdef MIPS64EL}
  1344. RegisterImport(system_mips64el_linux,timportliblinux);
  1345. RegisterExport(system_mips64el_linux,texportliblinux);
  1346. RegisterTarget(system_mips64el_linux_info);
  1347. {$endif MIPS64EL}
  1348. {$ifdef riscv32}
  1349. RegisterImport(system_riscv32_linux,timportliblinux);
  1350. RegisterExport(system_riscv32_linux,texportliblinux);
  1351. RegisterTarget(system_riscv32_linux_info);
  1352. {$endif riscv32}
  1353. {$ifdef riscv64}
  1354. RegisterImport(system_riscv64_linux,timportliblinux);
  1355. RegisterExport(system_riscv64_linux,texportliblinux);
  1356. RegisterTarget(system_riscv64_linux_info);
  1357. {$endif riscv64}
  1358. {$ifdef xtensa}
  1359. RegisterImport(system_xtensa_linux,timportliblinux);
  1360. RegisterExport(system_xtensa_linux,texportliblinux);
  1361. RegisterTarget(system_xtensa_linux_info);
  1362. {$endif xtensa}
  1363. {$ifdef loongarch64}
  1364. RegisterImport(system_loongarch64_linux,timportliblinux);
  1365. RegisterExport(system_loongarch64_linux,texportliblinux);
  1366. RegisterTarget(system_loongarch64_linux_info);
  1367. {$endif loongarch64}
  1368. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1369. end.