t_linux.pas 47 KB

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