t_linux.pas 47 KB

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