t_linux.pas 44 KB

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