t_linux.pas 48 KB

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