t_linux.pas 46 KB

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