t_linux.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  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 then
  485. begin
  486. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  487. AddFileName(s)
  488. else
  489. Message1(exec_w_init_file_not_found,'crtbeginS.o');
  490. end
  491. else
  492. if (cs_link_staticflag in current_settings.globalswitches) then
  493. begin
  494. if librarysearchpath.FindFile('crtbeginT.o',false,s) then
  495. AddFileName(s)
  496. else
  497. Message1(exec_w_init_file_not_found,'crtbeginT.o');
  498. end
  499. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  500. AddFileName(s)
  501. else
  502. Message1(exec_w_init_file_not_found,'crtbegin.o');
  503. end;
  504. { main objectfiles }
  505. while not ObjectFiles.Empty do
  506. begin
  507. s:=ObjectFiles.GetFirst;
  508. if s<>'' then
  509. AddFileName(maybequoted(s));
  510. end;
  511. EndSection(')');
  512. { Write staticlibraries }
  513. if not StaticLibFiles.Empty then
  514. begin
  515. Add('GROUP(');
  516. While not StaticLibFiles.Empty do
  517. begin
  518. S:=StaticLibFiles.GetFirst;
  519. AddFileName(maybequoted(s))
  520. end;
  521. Add(')');
  522. end;
  523. // we must reorder here because the result could empty sharedlibfiles
  524. if reorder Then
  525. ExpandAndApplyOrder(SharedLibFiles);
  526. // after this point addition of shared libs not allowed.
  527. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  528. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  529. if isdll and not linklibc then
  530. begin
  531. Add('INPUT(');
  532. Add(sysrootpath+info.DynamicLinker);
  533. Add(')');
  534. end;
  535. linksToSharedLibFiles := not SharedLibFiles.Empty;
  536. if not SharedLibFiles.Empty then
  537. begin
  538. if (SharedLibFiles.Count<>1) or
  539. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  540. reorder then
  541. begin
  542. libraryadded:=false;
  543. Add('INPUT(');
  544. While not SharedLibFiles.Empty do
  545. begin
  546. S:=SharedLibFiles.GetFirst;
  547. if (s<>'c') or reorder then
  548. begin
  549. i:=Pos(target_info.sharedlibext,S);
  550. if i>0 then
  551. Delete(S,i,255);
  552. Add('-l'+s);
  553. libraryadded:=true;
  554. end
  555. else
  556. linklibc:=true;
  557. end;
  558. { link explicitly against the dyn. linker in case we are using section threadvars and
  559. if we link against any other library. We need __tls_get_addr from the dyn. linker in this case.
  560. This does not hurt as in case we use a dyn. library we depend on the dyn. linker anyways.
  561. All this does not apply if we link anyways against libc }
  562. if libraryadded and not(linklibc) and not(isdll) and (tf_section_threadvars in target_info.flags) then
  563. Add('-l:'+ExtractFileName(defdynlinker));
  564. Add(')');
  565. end
  566. else
  567. linklibc:=true;
  568. if (cs_link_staticflag in current_settings.globalswitches) or
  569. (linklibc and not reorder) then
  570. begin
  571. Add('GROUP(');
  572. { when we have -static for the linker the we also need libgcc }
  573. if (cs_link_staticflag in current_settings.globalswitches) then
  574. begin
  575. Add('-lgcc');
  576. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  577. Add('-lgcc_eh');
  578. end;
  579. { be sure that libc is the last lib }
  580. if linklibc and not reorder then
  581. Add('-lc');
  582. Add(')');
  583. end;
  584. end;
  585. { objects which must be at the end }
  586. if linklibc and (libctype<>uclibc) then
  587. begin
  588. if cs_create_pic in current_settings.moduleswitches then
  589. begin
  590. found1:=librarysearchpath.FindFile('crtendS.o',false,s1);
  591. if not(found1) then
  592. Message1(exec_w_init_file_not_found,'crtendS.o');
  593. end
  594. else
  595. begin
  596. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  597. if not(found1) then
  598. Message1(exec_w_init_file_not_found,'crtend.o');
  599. end;
  600. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  601. if not(found2) then
  602. Message1(exec_w_init_file_not_found,'crtn.o');
  603. if found1 or found2 then
  604. begin
  605. Add('INPUT(');
  606. if found1 then
  607. AddFileName(s1);
  608. if found2 then
  609. AddFileName(s2);
  610. Add(')');
  611. end;
  612. end;
  613. { Entry point. Only needed for executables, as for shared lubraries we use
  614. the -init command line option instead
  615. The "ENTRY" linkerscript command does not have any effect when augmenting
  616. a linker script, so use the command line parameter instead }
  617. if (not isdll) then
  618. if (linksToSharedLibFiles and not linklibc) then
  619. info.ExeCmd[1]:=info.ExeCmd[1]+' -e _dynamic_start'
  620. else
  621. info.ExeCmd[1]:=info.ExeCmd[1]+' -e _start';
  622. add('SECTIONS');
  623. add('{');
  624. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) then
  625. { we can't use ".data", as that would hide the .data from the
  626. original linker script in combination with the INSERT at the end }
  627. add(' .fpcdata :')
  628. else
  629. add(' .data :');
  630. add(' {');
  631. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  632. add(' }');
  633. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  634. add('}');
  635. { this "INSERT" means "merge into the original linker script, even if
  636. -T is used" }
  637. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) then
  638. add('INSERT AFTER .data;');
  639. { Write and Close response }
  640. writetodisk;
  641. Free;
  642. end;
  643. WriteResponseFile:=True;
  644. end;
  645. function TLinkerLinux.MakeExecutable:boolean;
  646. var
  647. i : longint;
  648. binstr,
  649. cmdstr,
  650. mapstr,
  651. ltostr : TCmdStr;
  652. success : boolean;
  653. DynLinkStr : string;
  654. GCSectionsStr,
  655. StaticStr,
  656. StripStr : string[40];
  657. begin
  658. if not(cs_link_nolink in current_settings.globalswitches) then
  659. Message1(exec_i_linking,current_module.exefilename);
  660. { Create some replacements }
  661. StaticStr:='';
  662. StripStr:='';
  663. GCSectionsStr:='';
  664. DynLinkStr:='';
  665. mapstr:='';
  666. ltostr:='';
  667. if (cs_link_staticflag in current_settings.globalswitches) then
  668. StaticStr:='-static';
  669. if (cs_link_strip in current_settings.globalswitches) and
  670. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  671. StripStr:='-s';
  672. if (cs_link_map in current_settings.globalswitches) then
  673. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  674. if (cs_link_smart in current_settings.globalswitches) and
  675. create_smartlink_sections then
  676. GCSectionsStr:='--gc-sections';
  677. If (cs_profile in current_settings.moduleswitches) or
  678. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  679. begin
  680. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  681. if cshared then
  682. DynLinkStr:=DynLinkStr+' --shared ';
  683. if rlinkpath<>'' then
  684. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  685. End;
  686. { add custom LTO library if using custom clang }
  687. if (cs_lto in current_settings.moduleswitches) and
  688. not(cs_link_on_target in current_settings.globalswitches) and
  689. (utilsdirectory<>'') and
  690. FileExists(utilsdirectory+'/../lib/LLVMgold.so',true) then
  691. begin
  692. ltostr:='-plugin '+maybequoted(utilsdirectory+'/../lib/LLVMgold.so ');
  693. end;
  694. { Write used files and libraries }
  695. WriteResponseFile(false);
  696. { Call linker }
  697. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  698. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  699. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  700. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  701. Replace(cmdstr,'$STATIC',StaticStr);
  702. Replace(cmdstr,'$STRIP',StripStr);
  703. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  704. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  705. Replace(cmdstr,'$MAP',mapstr);
  706. Replace(cmdstr,'$LTO',ltostr);
  707. { create dynamic symbol table? }
  708. if HasExports then
  709. cmdstr:=cmdstr+' -E';
  710. { create eh_frame_hdr section? }
  711. if tf_use_psabieh in target_info.flags then
  712. cmdstr:=cmdstr+ ' --eh-frame-hdr';
  713. if cs_large in current_settings.globalswitches then
  714. cmdstr:=cmdstr+' --no-relax';
  715. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  716. { Create external .dbg file with debuginfo }
  717. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  718. begin
  719. for i:=1 to 3 do
  720. begin
  721. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  722. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  723. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  724. Replace(cmdstr,'$DBGX',current_module.dbgfilename);
  725. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  726. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  727. if not success then
  728. break;
  729. end;
  730. end;
  731. { Remove ReponseFile }
  732. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  733. DeleteFile(outputexedir+Info.ResName);
  734. { Post process,
  735. as it only writes sections sizes so far, do this only if V_Info is set }
  736. if success and CheckVerbosity(V_Info) and not(cs_link_nolink in current_settings.globalswitches) then
  737. { do not change success here as we are only writing some info, so if this fails, it does not matter }
  738. { success:= }PostProcessExecutable(current_module.exefilename,false);
  739. MakeExecutable:=success; { otherwise a recursive call to link method }
  740. end;
  741. Function TLinkerLinux.MakeSharedLibrary:boolean;
  742. var
  743. InitStr,
  744. FiniStr,
  745. GCSectionsStr,
  746. SoNameStr : string[80];
  747. binstr,
  748. cmdstr,
  749. mapstr,
  750. ltostr : TCmdStr;
  751. success : boolean;
  752. begin
  753. MakeSharedLibrary:=false;
  754. mapstr:='';
  755. ltostr:='';
  756. if not(cs_link_nolink in current_settings.globalswitches) then
  757. Message1(exec_i_linking,current_module.sharedlibfilename);
  758. if (cs_link_smart in current_settings.globalswitches) and
  759. create_smartlink_sections then
  760. GCSectionsStr:='--gc-sections'
  761. else
  762. GCSectionsStr:='';
  763. { Write used files and libraries }
  764. WriteResponseFile(true);
  765. { Create some replacements }
  766. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  767. InitStr:='-init FPC_SHARED_LIB_START';
  768. FiniStr:='-fini FPC_LIB_EXIT';
  769. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  770. if (cs_link_map in current_settings.globalswitches) then
  771. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  772. { add custom LTO library if using custom clang }
  773. if (cs_lto in current_settings.moduleswitches) and
  774. not(cs_link_on_target in current_settings.globalswitches) and
  775. (utilsdirectory<>'') and
  776. FileExists(utilsdirectory+'/../lib/LLVMgold.so',true) then
  777. begin
  778. ltostr:='-plugin '+maybequoted(utilsdirectory+'/../lib/LLVMgold.so ');
  779. end;
  780. { Call linker }
  781. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  782. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  783. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  784. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  785. Replace(cmdstr,'$INIT',InitStr);
  786. Replace(cmdstr,'$FINI',FiniStr);
  787. Replace(cmdstr,'$SONAME',SoNameStr);
  788. Replace(cmdstr,'$MAP',mapstr);
  789. Replace(cmdstr,'$LTO',ltostr);
  790. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  791. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  792. { Strip the library ? }
  793. if success and (cs_link_strip in current_settings.globalswitches) then
  794. begin
  795. { only remove non global symbols and debugging info for a library }
  796. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  797. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  798. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  799. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  800. end;
  801. { Remove ReponseFile }
  802. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  803. DeleteFile(outputexedir+Info.ResName);
  804. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  805. end;
  806. function TLinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  807. begin
  808. Result:=PostProcessELFExecutable(fn,isdll);
  809. end;
  810. {*****************************************************************************
  811. TINTERNALLINKERLINUX
  812. *****************************************************************************}
  813. constructor TInternalLinkerLinux.Create;
  814. begin
  815. inherited Create;
  816. SetupLibrarySearchPath;
  817. SetupDynlinker(dynlinker,libctype);
  818. CArObjectReader:=TArObjectReader;
  819. CExeOutput:=ElfExeOutputClass;
  820. CObjInput:=TElfObjInput;
  821. end;
  822. procedure TInternalLinkerLinux.InitSysInitUnitName;
  823. begin
  824. linklibc:=ModulesLinkToLibc;
  825. reorder:=linklibc and ReOrderEntries;
  826. sysinitunit:=defsinames[current_module.islibrary];
  827. prtobj:=defprtnames[current_module.islibrary];
  828. if cs_profile in current_settings.moduleswitches then
  829. begin
  830. prtobj:=gprtnames[libctype];
  831. sysinitunit:=gsinames[libctype];
  832. linklibc:=true;
  833. end
  834. else if linklibc then
  835. begin
  836. prtobj:=cprtnames[libctype];
  837. sysinitunit:=csinames[libctype];
  838. end;
  839. end;
  840. const
  841. relsec_prefix:array[boolean] of TCmdStr = ('rel','rela');
  842. procedure TInternalLinkerLinux.DefaultLinkScript;
  843. var
  844. s,s1,s2,relprefix:TCmdStr;
  845. found1,found2:boolean;
  846. linkToSharedLibs:boolean;
  847. procedure AddLibraryStatement(const s:TCmdStr);
  848. var
  849. i:longint;
  850. s1,s2:TCmdStr;
  851. begin
  852. i:=pos(target_info.sharedClibext+'.',s);
  853. if (i>0) then
  854. s1:=target_info.sharedClibprefix+S
  855. else
  856. s1:=target_info.sharedClibprefix+S+target_info.sharedClibext;
  857. { TODO: to be compatible with ld search algorithm, each found file
  858. must be tested for target compatibility, incompatible ones should be skipped. }
  859. { TODO: shall we search library without suffix if one with suffix is not found? }
  860. if (not(cs_link_staticflag in current_settings.globalswitches)) and
  861. FindLibraryFile(s1,'','',s2) then
  862. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  863. { TODO: static libraries never have numeric suffix in their names }
  864. else if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  865. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  866. else
  867. Comment(V_Error,'Import library not found for '+S);
  868. end;
  869. begin
  870. if cs_profile in current_settings.moduleswitches then
  871. begin
  872. if not(libctype in [glibc2,glibc21]) then
  873. AddSharedLibrary('gmon');
  874. AddSharedLibrary('c');
  875. end;
  876. TElfExeOutput(exeoutput).interpreter:=stringdup(dynlinker);
  877. { add objectfiles, start with prt0 always }
  878. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  879. LinkScript.Concat('READOBJECT '+ maybequoted(FindObjectFile(prtobj,'',false)));
  880. { try to add crti and crtbegin if linking to C }
  881. if linklibc and (libctype<>uclibc) then
  882. begin
  883. { crti.o must come first }
  884. if librarysearchpath.FindFile('crti.o',false,s) then
  885. LinkScript.Concat('READOBJECT '+maybequoted(s));
  886. { then the crtbegin* }
  887. if cs_create_pic in current_settings.moduleswitches then
  888. begin
  889. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  890. LinkScript.Concat('READOBJECT '+maybequoted(s));
  891. end
  892. else
  893. if (cs_link_staticflag in current_settings.globalswitches) and
  894. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  895. LinkScript.Concat('READOBJECT '+maybequoted(s))
  896. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  897. LinkScript.Concat('READOBJECT '+maybequoted(s));
  898. end;
  899. ScriptAddSourceStatements(false);
  900. { we must reorder here because the result could empty sharedlibfiles }
  901. if reorder then
  902. ExpandAndApplyOrder(SharedLibFiles);
  903. { See tw9089*.pp: if more than one pure-Pascal shared libs are loaded,
  904. and none have rtld in their DT_NEEDED, then rtld cannot finalize correctly. }
  905. if IsSharedLibrary then
  906. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(sysrootpath+dynlinker));
  907. linkToSharedLibs:=(not SharedLibFiles.Empty);
  908. { Symbols declared as "external 'libx.so'" are added to ImportLibraryList, library
  909. prefix/extension *not* stripped. TImportLibLinux copies these to SharedLibFiles,
  910. stripping prefixes and extensions.
  911. However extension won't be stripped if library is specified with numeric suffix
  912. (like "libpango-1.0.so.0")
  913. Libraries specified with $LINKLIB directive are directly added to SharedLibFiles
  914. and won't be present in ImportLibraryList. }
  915. while not SharedLibFiles.Empty do
  916. begin
  917. S:=SharedLibFiles.GetFirst;
  918. if (S<>'c') or reorder then
  919. AddLibraryStatement(S);
  920. end;
  921. if (cs_link_staticflag in current_settings.globalswitches) or
  922. (linklibc and not reorder) then
  923. begin
  924. LinkScript.Concat('GROUP');
  925. if (cs_link_staticflag in current_settings.globalswitches) then
  926. begin
  927. AddLibraryStatement('gcc');
  928. AddLibraryStatement('gcc_eh');
  929. end;
  930. if linklibc and not reorder then
  931. AddLibraryStatement('c');
  932. LinkScript.Concat('ENDGROUP');
  933. end;
  934. { objects which must be at the end }
  935. if linklibc and (libctype<>uclibc) then
  936. begin
  937. if cs_create_pic in current_settings.moduleswitches then
  938. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  939. else
  940. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  941. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  942. if found1 then
  943. LinkScript.Concat('READOBJECT '+maybequoted(s1));
  944. if found2 then
  945. LinkScript.Concat('READOBJECT '+maybequoted(s2));
  946. end;
  947. if (not IsSharedLibrary) then
  948. if (linkToSharedLibs and not linklibc) then
  949. LinkScript.Concat('ENTRYNAME _dynamic_start')
  950. else
  951. LinkScript.Concat('ENTRYNAME _start')
  952. else
  953. LinkScript.Concat('ISSHAREDLIBRARY');
  954. relprefix:=relsec_prefix[ElfTarget.relocs_use_addend];
  955. with LinkScript do
  956. begin
  957. Concat('HEADER');
  958. Concat('EXESECTION .interp');
  959. Concat(' OBJSECTION .interp');
  960. Concat('ENDEXESECTION');
  961. Concat('EXESECTION .note.ABI-tag');
  962. Concat(' OBJSECTION .note.ABI-tag');
  963. Concat('ENDEXESECTION');
  964. Concat('EXESECTION .note.gnu.build-id');
  965. Concat(' OBJSECTION .note.gnu.build-id');
  966. Concat('ENDEXESECTION');
  967. Concat('EXESECTION .hash');
  968. Concat(' OBJSECTION .hash');
  969. Concat('ENDEXESECTION');
  970. Concat('EXESECTION .dynsym');
  971. Concat(' OBJSECTION .dynsym');
  972. Concat('ENDEXESECTION');
  973. Concat('EXESECTION .dynstr');
  974. Concat(' OBJSECTION .dynstr');
  975. Concat('ENDEXESECTION');
  976. Concat('EXESECTION .gnu.version');
  977. Concat(' OBJSECTION .gnu.version');
  978. Concat('ENDEXESECTION');
  979. Concat('EXESECTION .gnu.version_d');
  980. Concat(' OBJSECTION .gnu.version_d');
  981. Concat('ENDEXESECTION');
  982. Concat('EXESECTION .gnu.version_r');
  983. Concat(' OBJSECTION .gnu.version_r');
  984. Concat('ENDEXESECTION');
  985. Concat('EXESECTION .'+relprefix+'.dyn');
  986. Concat(' OBJSECTION .'+relprefix+'.dyn');
  987. Concat('ENDEXESECTION');
  988. Concat('EXESECTION .'+relprefix+'.plt');
  989. Concat(' OBJSECTION .'+relprefix+'.plt');
  990. Concat(' PROVIDE __'+relprefix+'_iplt_start');
  991. Concat(' OBJSECTION .'+relprefix+'.iplt');
  992. Concat(' PROVIDE __'+relprefix+'_iplt_end');
  993. Concat('ENDEXESECTION');
  994. Concat('EXESECTION .init');
  995. Concat(' OBJSECTION .init');
  996. Concat('ENDEXESECTION');
  997. Concat('EXESECTION .plt');
  998. Concat(' OBJSECTION .plt');
  999. Concat('ENDEXESECTION');
  1000. Concat('EXESECTION .text');
  1001. Concat(' OBJSECTION .text*');
  1002. Concat('ENDEXESECTION');
  1003. Concat('EXESECTION .fini');
  1004. Concat(' OBJSECTION .fini');
  1005. Concat(' PROVIDE __etext');
  1006. Concat(' PROVIDE _etext');
  1007. Concat(' PROVIDE etext');
  1008. Concat('ENDEXESECTION');
  1009. Concat('EXESECTION .rodata');
  1010. Concat(' OBJSECTION .rodata*');
  1011. Concat('ENDEXESECTION');
  1012. {$ifdef arm}
  1013. Concat('EXESECTION .ARM.extab');
  1014. Concat(' OBJSECTION .ARM.extab*');
  1015. Concat('ENDEXESECTION');
  1016. Concat('EXESECTION .ARM.exidx');
  1017. Concat(' SYMBOL __exidx_start');
  1018. Concat(' OBJSECTION .ARM.exidx*');
  1019. Concat(' SYMBOL __exidx_end');
  1020. Concat('ENDEXESECTION');
  1021. {$endif}
  1022. Concat('EXESECTION .eh_frame');
  1023. Concat(' OBJSECTION .eh_frame');
  1024. Concat('ENDEXESECTION');
  1025. Concat('EXESECTION .gcc_except_table');
  1026. Concat(' OBJSECTION .gcc_except_table');
  1027. Concat(' OBJSECTION .gcc_except_table.*');
  1028. Concat('ENDEXESECTION');
  1029. Concat('EXESECTION .tdata');
  1030. Concat(' OBJSECTION .tdata');
  1031. Concat(' OBJSECTION .tdata.*');
  1032. Concat('ENDEXESECTION');
  1033. Concat('EXESECTION .tbss');
  1034. Concat(' OBJSECTION .tbss');
  1035. Concat(' OBJSECTION .tbss.*');
  1036. Concat('ENDEXESECTION');
  1037. Concat('EXESECTION .preinit_array');
  1038. Concat(' PROVIDE __preinit_array_start');
  1039. Concat(' OBJSECTION .preinit_array');
  1040. Concat(' PROVIDE __preinit_array_end');
  1041. Concat('ENDEXESECTION');
  1042. Concat('EXESECTION .init_array');
  1043. Concat(' PROVIDE __init_array_start');
  1044. { why the hell .ctors are both here and exesection .ctors below?? }
  1045. // KEEP ( *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
  1046. Concat(' OBJSECTION .init_array');
  1047. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
  1048. Concat('PROVIDE __init_array_end');
  1049. Concat('ENDEXESECTION');
  1050. Concat('EXESECTION .fini_array');
  1051. Concat(' PROVIDE __fini_array_start');
  1052. // KEEP ( *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
  1053. Concat(' OBJSECTION .fini_array');
  1054. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
  1055. Concat(' PROVIDE __fini_array_end');
  1056. Concat('ENDEXESECTION');
  1057. Concat('EXESECTION .ctors');
  1058. Concat(' OBJSECTION .ctors*');
  1059. Concat('ENDEXESECTION');
  1060. Concat('EXESECTION .dtors');
  1061. Concat(' OBJSECTION .dtors*');
  1062. Concat('ENDEXESECTION');
  1063. Concat('EXESECTION .jcr');
  1064. Concat(' OBJSECTION .jcr');
  1065. Concat('ENDEXESECTION');
  1066. Concat('EXESECTION .dynamic');
  1067. Concat(' OBJSECTION .dynamic');
  1068. Concat('ENDEXESECTION');
  1069. {$ifndef mips}
  1070. Concat('EXESECTION .got');
  1071. {$ifdef arm}
  1072. Concat(' OBJSECTION .got.plt');
  1073. {$endif arm}
  1074. Concat(' OBJSECTION .got');
  1075. Concat('ENDEXESECTION');
  1076. {$endif mips}
  1077. {$ifndef arm}
  1078. Concat('EXESECTION .got.plt');
  1079. Concat(' OBJSECTION .got.plt');
  1080. Concat('ENDEXESECTION');
  1081. {$endif arm}
  1082. Concat('EXESECTION .data');
  1083. Concat(' OBJSECTION .data*');
  1084. Concat(' OBJSECTION .fpc*');
  1085. Concat(' OBJSECTION fpc.resources');
  1086. Concat(' PROVIDE _edata');
  1087. Concat(' PROVIDE edata');
  1088. Concat('ENDEXESECTION');
  1089. {$ifdef mips}
  1090. Concat('EXESECTION .got');
  1091. Concat(' OBJSECTION .got');
  1092. Concat('ENDEXESECTION');
  1093. {$endif mips}
  1094. Concat('EXESECTION .bss');
  1095. Concat(' OBJSECTION .dynbss');
  1096. Concat(' OBJSECTION .bss*');
  1097. Concat(' OBJSECTION fpc.reshandles');
  1098. Concat(' PROVIDE end');
  1099. Concat(' SYMBOL _end');
  1100. Concat('ENDEXESECTION');
  1101. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,'+
  1102. '.debug_abbrev,.debug_line,.debug_frame,.debug_str,.debug_loc,'+
  1103. '.debug_macinfo,.debug_weaknames,.debug_funcnames,.debug_typenames,.debug_varnames,.debug_ranges');
  1104. Concat('EXESECTION .stab');
  1105. Concat(' OBJSECTION .stab');
  1106. Concat('ENDEXESECTION');
  1107. Concat('EXESECTION .stabstr');
  1108. Concat(' OBJSECTION .stabstr');
  1109. Concat('ENDEXESECTION');
  1110. end;
  1111. end;
  1112. {*****************************************************************************
  1113. Initialize
  1114. *****************************************************************************}
  1115. initialization
  1116. RegisterLinker(ld_linux,TLinkerLinux);
  1117. RegisterLinker(ld_int_linux,TInternalLinkerLinux);
  1118. {$ifdef i386}
  1119. RegisterImport(system_i386_linux,timportliblinux);
  1120. RegisterExport(system_i386_linux,texportliblinux);
  1121. RegisterTarget(system_i386_linux_info);
  1122. {$endif i386}
  1123. {$ifdef m68k}
  1124. RegisterImport(system_m68k_linux,timportliblinux);
  1125. RegisterExport(system_m68k_linux,texportliblinux);
  1126. RegisterTarget(system_m68k_linux_info);
  1127. {$endif m68k}
  1128. {$ifdef powerpc}
  1129. RegisterImport(system_powerpc_linux,timportliblinux);
  1130. RegisterExport(system_powerpc_linux,texportliblinux);
  1131. RegisterTarget(system_powerpc_linux_info);
  1132. {$endif powerpc}
  1133. {$ifdef powerpc64}
  1134. { default to little endian either when compiling with -dppc64le, or when
  1135. compiling on a little endian ppc64 platform }
  1136. {$if defined(ppc64le) or (defined(cpupowerpc64) and defined(FPC_LITTLE_ENDIAN))}
  1137. system_powerpc64_linux_info.endian:=endian_little;
  1138. system_powerpc64_linux_info.abi:=abi_powerpc_elfv2;
  1139. {$endif}
  1140. RegisterImport(system_powerpc64_linux,timportliblinux);
  1141. RegisterExport(system_powerpc64_linux,texportliblinux);
  1142. RegisterTarget(system_powerpc64_linux_info);
  1143. {$endif powerpc64}
  1144. {$ifdef x86_64}
  1145. RegisterImport(system_x86_64_linux,timportliblinux);
  1146. RegisterExport(system_x86_64_linux,texportliblinux);
  1147. RegisterTarget(system_x86_64_linux_info);
  1148. RegisterTarget(system_x86_6432_linux_info);
  1149. {$endif x86_64}
  1150. {$ifdef SPARC}
  1151. RegisterImport(system_SPARC_linux,timportliblinux);
  1152. RegisterExport(system_SPARC_linux,texportliblinux);
  1153. RegisterTarget(system_SPARC_linux_info);
  1154. {$endif SPARC}
  1155. {$ifdef SPARC64}
  1156. RegisterImport(system_SPARC64_linux,timportliblinux);
  1157. RegisterExport(system_SPARC64_linux,texportliblinux);
  1158. RegisterTarget(system_SPARC64_linux_info);
  1159. {$endif SPARC64}
  1160. {$ifdef ARM}
  1161. RegisterImport(system_arm_linux,timportliblinux);
  1162. RegisterExport(system_arm_linux,texportliblinux);
  1163. RegisterTarget(system_arm_linux_info);
  1164. {$endif ARM}
  1165. {$ifdef aarch64}
  1166. RegisterImport(system_aarch64_linux,timportliblinux);
  1167. RegisterExport(system_aarch64_linux,texportliblinux);
  1168. RegisterTarget(system_aarch64_linux_info);
  1169. {$endif aarch64}
  1170. {$ifdef MIPS}
  1171. {$ifdef MIPSEL}
  1172. RegisterImport(system_mipsel_linux,timportliblinux);
  1173. RegisterExport(system_mipsel_linux,texportliblinux);
  1174. RegisterTarget(system_mipsel_linux_info);
  1175. {$else MIPS}
  1176. RegisterImport(system_mipseb_linux,timportliblinux);
  1177. RegisterExport(system_mipseb_linux,texportliblinux);
  1178. RegisterTarget(system_mipseb_linux_info);
  1179. {$endif MIPSEL}
  1180. {$endif MIPS}
  1181. {$ifdef riscv32}
  1182. RegisterImport(system_riscv32_linux,timportliblinux);
  1183. RegisterExport(system_riscv32_linux,texportliblinux);
  1184. RegisterTarget(system_riscv32_linux_info);
  1185. {$endif riscv32}
  1186. {$ifdef riscv64}
  1187. RegisterImport(system_riscv64_linux,timportliblinux);
  1188. RegisterExport(system_riscv64_linux,texportliblinux);
  1189. RegisterTarget(system_riscv64_linux_info);
  1190. {$endif riscv64}
  1191. {$ifdef xtensa}
  1192. RegisterImport(system_xtensa_linux,timportliblinux);
  1193. RegisterExport(system_xtensa_linux,texportliblinux);
  1194. RegisterTarget(system_xtensa_linux_info);
  1195. {$endif xtensa}
  1196. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1197. end.