t_linux.pas 50 KB

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