t_linux.pas 49 KB

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