t_linux.pas 50 KB

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