t_linux.pas 45 KB

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