t_linux.pas 46 KB

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