t_linux.pas 47 KB

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