t_linux.pas 47 KB

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