t_linux.pas 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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. public
  41. constructor Create;override;
  42. procedure SetDefaultInfo;override;
  43. procedure InitSysInitUnitName;override;
  44. function MakeExecutable:boolean;override;
  45. function MakeSharedLibrary:boolean;override;
  46. procedure LoadPredefinedLibraryOrder; override;
  47. end;
  48. TInternalLinkerLinux=class(TInternalLinker)
  49. private
  50. libctype: TLibcType;
  51. reorder: boolean;
  52. linklibc: boolean;
  53. prtobj: string[20];
  54. dynlinker: string[100];
  55. public
  56. constructor Create;override;
  57. procedure DefaultLinkScript;override;
  58. procedure InitSysInitUnitName;override;
  59. end;
  60. implementation
  61. uses
  62. SysUtils,
  63. cutils,cfileutl,cclasses,
  64. verbose,systems,globtype,globals,
  65. cscript,
  66. fmodule,
  67. aasmbase,aasmtai,aasmcpu,cpubase,
  68. cgbase,ogbase,
  69. comprsrc,
  70. ogelf,owar,
  71. rescmn, i_linux
  72. ;
  73. {*****************************************************************************
  74. TIMPORTLIBLINUX
  75. *****************************************************************************}
  76. procedure timportliblinux.generatelib;
  77. var
  78. i : longint;
  79. ImportLibrary : TImportLibrary;
  80. begin
  81. for i:=0 to current_module.ImportLibraryList.Count-1 do
  82. begin
  83. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  84. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  85. end;
  86. end;
  87. {*****************************************************************************
  88. TEXPORTLIBLINUX
  89. *****************************************************************************}
  90. procedure texportliblinux.setfininame(list: TAsmList; const s: string);
  91. begin
  92. { the problem with not having a .fini section is that a finalization
  93. routine in regular code can get "smart" linked away -> reference it
  94. just like the debug info }
  95. new_section(list,sec_fpc,'links',0);
  96. list.concat(Tai_const.Createname(s,0));
  97. inherited setfininame(list,s);
  98. end;
  99. {*****************************************************************************
  100. TLINKERLINUX
  101. *****************************************************************************}
  102. procedure SetupLibrarySearchPath;
  103. begin
  104. if not Dontlinkstdlibpath Then
  105. begin
  106. {$ifdef x86_64}
  107. { some linuxes might not have the lib64 variants (Arch, LFS }
  108. if PathExists('/usr/X11R6/lib64',true) then
  109. LibrarySearchPath.AddPath(sysrootpath,'/usr/X11R6/lib64',true)
  110. else if PathExists('/usr/X11R6/lib',true) then
  111. LibrarySearchPath.AddPath(sysrootpath,'/usr/X11R6/lib',true);
  112. if PathExists('/usr/lib64',true) then
  113. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib64',true)
  114. else if PathExists('/usr/lib',true) then
  115. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib',true);
  116. { /lib64 should be the really first, so add it before everything else }
  117. if PathExists('/lib64',true) then
  118. LibrarySearchPath.AddPath(sysrootpath,'/lib64',true)
  119. else if PathExists('/lib',true) then
  120. LibrarySearchPath.AddPath(sysrootpath,'/lib',true);
  121. {$else}
  122. {$ifdef powerpc64}
  123. if target_info.abi<>abi_powerpc_elfv2 then
  124. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true)
  125. else
  126. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib/powerpc64le-linux-gnu;/usr/X11R6/powerpc64le-linux-gnu',true);
  127. {$else powerpc64}
  128. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  129. {$endif powerpc64}
  130. {$endif x86_64}
  131. {$ifdef arm}
  132. { some newer Debian have the crt*.o files at uncommon locations,
  133. for other arm flavours, this cannot hurt }
  134. {$ifdef FPC_ARMHF}
  135. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabihf',true);
  136. {$endif FPC_ARMHF}
  137. {$ifdef FPC_ARMEL}
  138. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/arm-linux-gnueabi',true);
  139. {$endif}
  140. {$endif arm}
  141. {$ifdef x86_64}
  142. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/x86_64-linux-gnu',true);
  143. {$endif x86_64}
  144. {$ifdef i386}
  145. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/i386-linux-gnu',true);
  146. {$endif i386}
  147. {$ifdef aarch64}
  148. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/aarch64-linux-gnu',true);
  149. {$endif aarch64}
  150. {$ifdef powerpc}
  151. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/powerpc-linux-gnu',true);
  152. {$endif powerpc}
  153. {$ifdef m68k}
  154. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/m68k-linux-gnu',true);
  155. {$endif m68k}
  156. {$ifdef sparc64}
  157. LibrarySearchPath.AddPath(sysrootpath,'/usr/lib/sparc64-linux-gnu',true);
  158. {$endif sparc64}
  159. end;
  160. end;
  161. {$ifdef m68k}
  162. const defdynlinker='/lib/ld.so.1';
  163. {$endif m68k}
  164. {$ifdef i386}
  165. const defdynlinker='/lib/ld-linux.so.2';
  166. {$endif}
  167. {$ifdef x86_64}
  168. const defdynlinker='/lib64/ld-linux-x86-64.so.2';
  169. {$endif x86_64}
  170. {$ifdef sparc}
  171. const defdynlinker='/lib/ld-linux.so.2';
  172. {$endif sparc}
  173. {$ifdef powerpc}
  174. const defdynlinker='/lib/ld.so.1';
  175. {$endif powerpc}
  176. {$ifdef powerpc64}
  177. const defdynlinkerv1='/lib64/ld64.so.1';
  178. const defdynlinkerv2='/lib64/ld64.so.2';
  179. var defdynlinker: string;
  180. {$endif powerpc64}
  181. {$ifdef arm}
  182. {$ifdef FPC_ARMHF}
  183. const defdynlinker='/lib/ld-linux-armhf.so.3';
  184. {$else FPC_ARMHF}
  185. {$ifdef FPC_ARMEL}
  186. const defdynlinker='/lib/ld-linux.so.3';
  187. {$else FPC_ARMEL}
  188. const defdynlinker='/lib/ld-linux.so.2';
  189. {$endif FPC_ARMEL}
  190. {$endif FPC_ARMHF}
  191. {$endif arm}
  192. {$ifdef aarch64}
  193. const defdynlinker='/lib/ld-linux-aarch64.so.1';
  194. {$endif aarch64}
  195. {$ifdef mips}
  196. const defdynlinker='/lib/ld.so.1';
  197. {$endif mips}
  198. {$ifdef sparc64}
  199. const defdynlinker='/lib64/ld-linux.so.2';
  200. {$endif sparc64}
  201. {$ifdef riscv32}
  202. const defdynlinker='/lib32/ld.so.1';
  203. {$endif riscv32}
  204. {$ifdef riscv64}
  205. const defdynlinker='/lib/ld.so.1';
  206. {$endif riscv64}
  207. procedure SetupDynlinker(out DynamicLinker:string;out libctype:TLibcType);
  208. begin
  209. {$ifdef powerpc64}
  210. if defdynlinker='' then
  211. if target_info.abi=abi_powerpc_sysv then
  212. defdynlinker:=defdynlinkerv1
  213. else
  214. defdynlinker:=defdynlinkerv2;
  215. {$endif powerpc64}
  216. {
  217. Search order:
  218. glibc 2.1+
  219. uclibc
  220. glibc 2.0
  221. If none is found (e.g. when cross compiling) glibc21 is assumed
  222. }
  223. if fileexists(sysrootpath+defdynlinker,false) then
  224. begin
  225. DynamicLinker:=defdynlinker;
  226. {$ifdef i386}
  227. libctype:=glibc21;
  228. {$else i386}
  229. libctype:=glibc2;
  230. {$endif i386}
  231. end
  232. else if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  233. begin
  234. DynamicLinker:='/lib/ld-uClibc.so.0';
  235. libctype:=uclibc;
  236. end
  237. {$ifdef i386}
  238. else if FileExists(sysrootpath+'/lib/ld-linux.so.1',false) then
  239. begin
  240. DynamicLinker:='/lib/ld-linux.so.1';
  241. libctype:=glibc2;
  242. end
  243. {$endif i386}
  244. else
  245. begin
  246. { when no dyn. linker is found, we are probably
  247. cross compiling, so use the default dyn. linker }
  248. DynamicLinker:=defdynlinker;
  249. {
  250. the default c startup script is gcrt0.as on all platforms
  251. except i386
  252. }
  253. {$ifdef i386}
  254. libctype:=glibc21;
  255. {$else i386}
  256. libctype:=glibc2;
  257. {$endif i386}
  258. end;
  259. end;
  260. function ModulesLinkToLibc:boolean;
  261. var
  262. hp: tmodule;
  263. begin
  264. { This is called very early, ImportLibraryList is not yet merged into linkothersharedlibs.
  265. The former contains library names qualified with prefix and suffix (coming from
  266. "external 'c' name 'foo' declarations), the latter contains raw names (from "$linklib c"
  267. directives). }
  268. hp:=tmodule(loaded_units.first);
  269. while assigned(hp) do
  270. begin
  271. result:=Assigned(hp.ImportLibraryList.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext));
  272. if result then break;
  273. result:=hp.linkothersharedlibs.find(target_info.sharedClibprefix+'c'+target_info.sharedClibext);
  274. if result then break;
  275. result:=hp.linkothersharedlibs.find('c');
  276. if result then break;
  277. hp:=tmodule(hp.next);
  278. end;
  279. end;
  280. Constructor TLinkerLinux.Create;
  281. begin
  282. Inherited Create;
  283. SetupLibrarySearchPath;
  284. end;
  285. procedure TLinkerLinux.SetDefaultInfo;
  286. {
  287. This will also detect which libc version will be used
  288. }
  289. const
  290. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  291. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  292. {$ifdef powerpc} platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  293. {$ifdef POWERPC64} platform_select='';{$endif}
  294. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  295. {$ifdef sparc64} platform_select='-b elf64-sparc -m elf64_sparc';{$endif}
  296. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  297. {$ifdef aarch64} platform_select='';{$endif} {unknown :( }
  298. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  299. {$ifdef mips}
  300. {$ifdef mipsel}
  301. platform_select='-EL';
  302. {$else}
  303. platform_select='-EB';
  304. {$endif}
  305. {$endif}
  306. {$ifdef riscv32} platform_select='';{$endif} {unknown :( }
  307. {$ifdef riscv64} platform_select='';{$endif} {unknown :( }
  308. var
  309. platformopt: string;
  310. begin
  311. platformopt:='';
  312. {$ifdef powerpc64}
  313. if (target_info.abi=abi_powerpc_elfv2) and
  314. (target_info.endian=endian_little) then
  315. platformopt:=' -b elf64-powerpcle -m elf64lppc'
  316. else
  317. platformopt:=' -b elf64-powerpc -m elf64ppc';
  318. {$endif powerpc64}
  319. with Info do
  320. begin
  321. ExeCmd[1]:='ld '+platform_select+platformopt+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP $MAP -L. -o $EXE';
  322. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME $MAP -shared $GCSECTIONS -L. -o $EXE';
  323. { when we want to cross-link we need to override default library paths;
  324. when targeting binutils 2.19 or later, we use the "INSERT" command to
  325. augment the default linkerscript, which also requires -T (normally that
  326. option means "completely replace the default linkerscript) }
  327. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) or
  328. (length(sysrootpath)>0) then
  329. begin
  330. ExeCmd[1]:=ExeCmd[1]+' -T';
  331. DllCmd[1]:=DllCmd[1]+' -T';
  332. end;
  333. ExeCmd[1]:=ExeCmd[1]+' $RES';
  334. DllCmd[1]:=DllCmd[1]+' $RES';
  335. DllCmd[2]:='strip --strip-unneeded $EXE';
  336. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  337. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  338. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  339. SetupDynlinker(DynamicLinker,libctype);
  340. end;
  341. end;
  342. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  343. // put your linkorder/linkalias overrides here.
  344. // Note: assumes only called when reordering/aliasing is used.
  345. Begin
  346. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  347. Begin
  348. LinkLibraryOrder.add('gcc','',15);
  349. LinkLibraryOrder.add('c','',100);
  350. LinkLibraryOrder.add('gmon','',120);
  351. LinkLibraryOrder.add('dl','',140);
  352. LinkLibraryOrder.add('pthread','',160);
  353. end;
  354. End;
  355. type
  356. tlibcnames=array [TLibcType] of string[8];
  357. const { libc5 glibc2 glibc21 uclibc }
  358. cprtnames: tlibcnames = ('cprt0', 'cprt0', 'cprt21', 'ucprt0');
  359. csinames: tlibcnames = ('si_c', 'si_c', 'si_c21', 'si_uc');
  360. gprtnames: tlibcnames = ('gprt0', 'gprt0', 'gprt21', 'ugprt0');
  361. gsinames: tlibcnames = ('si_g', 'si_g', 'si_c21g','si_ucg');
  362. defprtnames: array[boolean] of string[8] = ('prt0', 'dllprt0');
  363. defsinames: array[boolean] of string[8] = ('si_prc','si_dll');
  364. { uclibc and glibc21 are not available on x86_64! si_g is also absent. }
  365. Procedure TLinkerLinux.InitSysInitUnitName;
  366. begin
  367. linklibc:=ModulesLinkToLibc;
  368. reorder:=linklibc and ReOrderEntries;
  369. sysinitunit:=defsinames[current_module.islibrary];
  370. prtobj:=defprtnames[current_module.islibrary];
  371. if current_module.islibrary then
  372. exit;
  373. if cs_profile in current_settings.moduleswitches then
  374. begin
  375. prtobj:=gprtnames[libctype];
  376. sysinitunit:=gsinames[libctype];
  377. linklibc:=true;
  378. end
  379. else if linklibc then
  380. begin
  381. prtobj:=cprtnames[libctype];
  382. sysinitunit:=csinames[libctype];
  383. end;
  384. end;
  385. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  386. Var
  387. linkres : TLinkRes;
  388. i : longint;
  389. HPath : TCmdStrListItem;
  390. s,s1,s2 : TCmdStr;
  391. found1,
  392. found2 : boolean;
  393. linksToSharedLibFiles : boolean;
  394. begin
  395. result:=False;
  396. { set special options for some targets }
  397. if cs_profile in current_settings.moduleswitches then
  398. begin
  399. if not(libctype in [glibc2,glibc21]) then
  400. AddSharedLibrary('gmon');
  401. AddSharedLibrary('c');
  402. end;
  403. { Open link.res file }
  404. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  405. with linkres do
  406. begin
  407. { Write path to search libraries }
  408. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  409. while assigned(HPath) do
  410. begin
  411. Add('SEARCH_DIR("'+HPath.Str+'")');
  412. HPath:=TCmdStrListItem(HPath.Next);
  413. end;
  414. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  415. while assigned(HPath) do
  416. begin
  417. Add('SEARCH_DIR("'+HPath.Str+'")');
  418. HPath:=TCmdStrListItem(HPath.Next);
  419. end;
  420. { force local symbol resolution (i.e., inside the shared }
  421. { library itself) for all non-exorted symbols, otherwise }
  422. { several RTL symbols of FPC-compiled shared libraries }
  423. { will be bound to those of a single shared library or }
  424. { to the main program }
  425. if (isdll) then
  426. begin
  427. add('VERSION');
  428. add('{');
  429. add(' {');
  430. if not texportlibunix(exportlib).exportedsymnames.empty then
  431. begin
  432. add(' global:');
  433. repeat
  434. add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  435. until texportlibunix(exportlib).exportedsymnames.empty;
  436. end;
  437. add(' local:');
  438. add(' *;');
  439. add(' };');
  440. add('}');
  441. end;
  442. StartSection('INPUT(');
  443. { add objectfiles, start with prt0 always }
  444. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  445. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  446. { try to add crti and crtbegin if linking to C }
  447. if linklibc and (libctype<>uclibc) then
  448. begin
  449. { crti.o must come first }
  450. if librarysearchpath.FindFile('crti.o',false,s) then
  451. AddFileName(s)
  452. else
  453. Message1(exec_w_init_file_not_found,'crti.o');
  454. { then the crtbegin* }
  455. if cs_create_pic in current_settings.moduleswitches then
  456. begin
  457. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  458. AddFileName(s)
  459. else
  460. Message1(exec_w_init_file_not_found,'crtbeginS.o');
  461. end
  462. else
  463. if (cs_link_staticflag in current_settings.globalswitches) then
  464. begin
  465. if librarysearchpath.FindFile('crtbeginT.o',false,s) then
  466. AddFileName(s)
  467. else
  468. Message1(exec_w_init_file_not_found,'crtbeginT.o');
  469. end
  470. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  471. AddFileName(s)
  472. else
  473. Message1(exec_w_init_file_not_found,'crtbegin.o');
  474. end;
  475. { main objectfiles }
  476. while not ObjectFiles.Empty do
  477. begin
  478. s:=ObjectFiles.GetFirst;
  479. if s<>'' then
  480. AddFileName(maybequoted(s));
  481. end;
  482. EndSection(')');
  483. { Write staticlibraries }
  484. if not StaticLibFiles.Empty then
  485. begin
  486. Add('GROUP(');
  487. While not StaticLibFiles.Empty do
  488. begin
  489. S:=StaticLibFiles.GetFirst;
  490. AddFileName(maybequoted(s))
  491. end;
  492. Add(')');
  493. end;
  494. // we must reorder here because the result could empty sharedlibfiles
  495. if reorder Then
  496. ExpandAndApplyOrder(SharedLibFiles);
  497. // after this point addition of shared libs not allowed.
  498. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  499. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  500. if (isdll) then
  501. begin
  502. Add('INPUT(');
  503. Add(sysrootpath+info.DynamicLinker);
  504. Add(')');
  505. end;
  506. linksToSharedLibFiles := not SharedLibFiles.Empty;
  507. if not SharedLibFiles.Empty then
  508. begin
  509. if (SharedLibFiles.Count<>1) or
  510. (TCmdStrListItem(SharedLibFiles.First).Str<>'c') or
  511. reorder then
  512. begin
  513. Add('INPUT(');
  514. While not SharedLibFiles.Empty do
  515. begin
  516. S:=SharedLibFiles.GetFirst;
  517. if (s<>'c') or reorder then
  518. begin
  519. i:=Pos(target_info.sharedlibext,S);
  520. if i>0 then
  521. Delete(S,i,255);
  522. Add('-l'+s);
  523. end
  524. else
  525. begin
  526. linklibc:=true;
  527. end;
  528. end;
  529. Add(')');
  530. end
  531. else
  532. linklibc:=true;
  533. if (cs_link_staticflag in current_settings.globalswitches) or
  534. (linklibc and not reorder) then
  535. begin
  536. Add('GROUP(');
  537. { when we have -static for the linker the we also need libgcc }
  538. if (cs_link_staticflag in current_settings.globalswitches) then
  539. begin
  540. Add('-lgcc');
  541. if librarysearchpath.FindFile('libgcc_eh.a',false,s1) then
  542. Add('-lgcc_eh');
  543. end;
  544. { be sure that libc is the last lib }
  545. if linklibc and not reorder then
  546. Add('-lc');
  547. Add(')');
  548. end;
  549. end;
  550. { objects which must be at the end }
  551. if linklibc and (libctype<>uclibc) then
  552. begin
  553. if cs_create_pic in current_settings.moduleswitches then
  554. begin
  555. found1:=librarysearchpath.FindFile('crtendS.o',false,s1);
  556. if not(found1) then
  557. Message1(exec_w_init_file_not_found,'crtendS.o');
  558. end
  559. else
  560. begin
  561. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  562. if not(found1) then
  563. Message1(exec_w_init_file_not_found,'crtend.o');
  564. end;
  565. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  566. if not(found2) then
  567. Message1(exec_w_init_file_not_found,'crtn.o');
  568. if found1 or found2 then
  569. begin
  570. Add('INPUT(');
  571. if found1 then
  572. AddFileName(s1);
  573. if found2 then
  574. AddFileName(s2);
  575. Add(')');
  576. end;
  577. end;
  578. { Entry point. Only needed for executables, as for shared lubraries we use
  579. the -init command line option instead
  580. The "ENTRY" linkerscript command does not have any effect when augmenting
  581. a linker script, so use the command line parameter instead }
  582. if (not isdll) then
  583. if (linksToSharedLibFiles and not linklibc) then
  584. info.ExeCmd[1]:=info.ExeCmd[1]+' -e _dynamic_start'
  585. else
  586. info.ExeCmd[1]:=info.ExeCmd[1]+' -e _start';
  587. { If we are using the default sysroot, use the default linker script and
  588. just augment it with the FPC-specific parts.
  589. }
  590. if sysrootpath='' then
  591. begin
  592. add('SECTIONS');
  593. add('{');
  594. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) then
  595. { we can't use ".data", as that would hide the .data from the
  596. original linker script in combination with the INSERT at the end }
  597. add(' .fpcdata :')
  598. else
  599. add(' .data :');
  600. add(' {');
  601. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  602. add(' }');
  603. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  604. add('}');
  605. { this "INSERT" means "merge into the original linker script, even if
  606. -T is used" }
  607. if not(cs_link_pre_binutils_2_19 in current_settings.globalswitches) then
  608. add('INSERT AFTER .data;');
  609. end
  610. else
  611. begin
  612. {$ifdef x86_64}
  613. {$define LINKERSCRIPT_INCLUDED}
  614. add('SECTIONS');
  615. add('{');
  616. {Read-only sections, merged into text segment:}
  617. if current_module.islibrary then
  618. add(' . = 0 + SIZEOF_HEADERS;')
  619. else
  620. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  621. add(' . = 0 + SIZEOF_HEADERS;');
  622. add(' .interp : { *(.interp) }');
  623. add(' .hash : { *(.hash) }');
  624. add(' .dynsym : { *(.dynsym) }');
  625. add(' .dynstr : { *(.dynstr) }');
  626. add(' .gnu.version : { *(.gnu.version) }');
  627. add(' .gnu.version_d : { *(.gnu.version_d) }');
  628. add(' .gnu.version_r : { *(.gnu.version_r) }');
  629. add(' .rel.dyn :');
  630. add(' {');
  631. add(' *(.rel.init)');
  632. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  633. add(' *(.rel.fini)');
  634. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  635. add(' *(.rel.data.rel.ro*)');
  636. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  637. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  638. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  639. add(' *(.rel.got)');
  640. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  641. add(' }');
  642. add(' .rela.dyn :');
  643. add(' {');
  644. add(' *(.rela.init)');
  645. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  646. add(' *(.rela.fini)');
  647. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  648. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  649. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  650. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  651. add(' *(.rela.got)');
  652. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  653. add(' }');
  654. add(' .rel.plt : { *(.rel.plt) }');
  655. add(' .rela.plt : { *(.rela.plt) }');
  656. add(' .init :');
  657. add(' {');
  658. add(' KEEP (*(.init))');
  659. add(' } =0x90909090');
  660. add(' .plt : { *(.plt) }');
  661. add(' .text :');
  662. add(' {');
  663. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  664. add(' KEEP (*(.text.*personality*))');
  665. {.gnu.warning sections are handled specially by elf32.em.}
  666. add(' *(.gnu.warning)');
  667. add(' } =0x90909090');
  668. add(' .fini :');
  669. add(' {');
  670. add(' KEEP (*(.fini))');
  671. add(' } =0x90909090');
  672. add(' PROVIDE (_etext = .);');
  673. add(' .rodata :');
  674. add(' {');
  675. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  676. add(' }');
  677. {Adjust the address for the data segment. We want to adjust up to
  678. the same address within the page on the next page up.}
  679. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  680. add(' .dynamic : { *(.dynamic) }');
  681. add(' .got : { *(.got .toc) }');
  682. add(' .got.plt : { *(.got.plt .toc.plt) }');
  683. add(' .data :');
  684. add(' {');
  685. add(' *(.data .data.* .gnu.linkonce.d.*)');
  686. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  687. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  688. add(' }');
  689. add(' PROVIDE (_edata = .);');
  690. add(' PROVIDE (edata = .);');
  691. {$ifdef zsegment_threadvars}
  692. add(' _z = .;');
  693. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  694. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  695. add(' . = _z + SIZEOF (.threadvar);');
  696. {$else}
  697. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  698. {$endif}
  699. add(' __bss_start = .;');
  700. add(' .bss :');
  701. add(' {');
  702. add(' *(.dynbss)');
  703. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  704. add(' *(COMMON)');
  705. {Align here to ensure that the .bss section occupies space up to
  706. _end. Align after .bss to ensure correct alignment even if the
  707. .bss section disappears because there are no input sections.}
  708. add(' . = ALIGN(32 / 8);');
  709. add('}');
  710. add(' . = ALIGN(32 / 8);');
  711. add(' PROVIDE (_end = .);');
  712. add(' PROVIDE (end = .);');
  713. {Stabs debugging sections.}
  714. add(' .stab 0 : { *(.stab) }');
  715. add(' .stabstr 0 : { *(.stabstr) }');
  716. add(' /* DWARF debug sections.');
  717. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  718. add(' of the section so we begin them at 0. */');
  719. add(' /* DWARF 1 */');
  720. add(' .debug 0 : { *(.debug) }');
  721. add(' .line 0 : { *(.line) }');
  722. add(' /* GNU DWARF 1 extensions */');
  723. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  724. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  725. add(' /* DWARF 1.1 and DWARF 2 */');
  726. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  727. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  728. add(' /* DWARF 2 */');
  729. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  730. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  731. add(' .debug_line 0 : { *(.debug_line) }');
  732. add(' .debug_frame 0 : { *(.debug_frame) }');
  733. add(' .debug_str 0 : { *(.debug_str) }');
  734. add(' .debug_loc 0 : { *(.debug_loc) }');
  735. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  736. add(' /* SGI/MIPS DWARF 2 extensions */');
  737. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  738. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  739. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  740. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  741. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  742. add('}');
  743. {$endif x86_64}
  744. {$ifdef AArch64}
  745. {$define LINKERSCRIPT_INCLUDED}
  746. { Complete linker script for aarch64-linux: }
  747. add('SECTIONS');
  748. add('{');
  749. add(' /* Read-only sections, merged into text segment: */');
  750. add(' PROVIDE (__executable_start = SEGMENT_START("text-segment", 0x400000)); . = SEGMENT_START("text-segment", 0x400000) + SIZEOF_HEADERS;');
  751. add(' .interp : { *(.interp) }');
  752. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  753. add(' .hash : { *(.hash) }');
  754. add(' .gnu.hash : { *(.gnu.hash) }');
  755. add(' .dynsym : { *(.dynsym) }');
  756. add(' .dynstr : { *(.dynstr) }');
  757. add(' .gnu.version : { *(.gnu.version) }');
  758. add(' .gnu.version_d : { *(.gnu.version_d) }');
  759. add(' .gnu.version_r : { *(.gnu.version_r) }');
  760. add(' .rela.dyn :');
  761. add(' {');
  762. add(' *(.rela.init)');
  763. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  764. add(' *(.rela.fini)');
  765. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  766. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  767. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  768. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  769. add(' *(.rela.ctors)');
  770. add(' *(.rela.dtors)');
  771. add(' *(.rela.got)');
  772. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  773. add(' *(.rela.ifunc)');
  774. add(' }');
  775. add(' .rela.plt :');
  776. add(' {');
  777. add(' *(.rela.plt)');
  778. add(' PROVIDE_HIDDEN (__rela_iplt_start = .);');
  779. add(' *(.rela.iplt)');
  780. add(' PROVIDE_HIDDEN (__rela_iplt_end = .);');
  781. add(' }');
  782. add(' .init :');
  783. add(' {');
  784. add(' KEEP (*(SORT_NONE(.init)))');
  785. add(' } =0');
  786. add(' .plt : ALIGN(16) { *(.plt) *(.iplt) }');
  787. add(' .text :');
  788. add(' {');
  789. add(' *(.text.unlikely .text.*_unlikely .text.unlikely.*)');
  790. add(' *(.text.exit .text.exit.*)');
  791. add(' *(.text.startup .text.startup.*)');
  792. add(' *(.text.hot .text.hot.*)');
  793. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  794. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  795. add(' *(.gnu.warning)');
  796. add(' } =0');
  797. add(' .fini :');
  798. add(' {');
  799. add(' KEEP (*(SORT_NONE(.fini)))');
  800. add(' } =0');
  801. add(' PROVIDE (__etext = .);');
  802. add(' PROVIDE (_etext = .);');
  803. add(' PROVIDE (etext = .);');
  804. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  805. add(' .rodata1 : { *(.rodata1) }');
  806. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  807. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  808. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table');
  809. add(' .gcc_except_table.*) }');
  810. add(' /* These sections are generated by the Sun/Oracle C++ compiler. */');
  811. add(' .exception_ranges : ONLY_IF_RO { *(.exception_ranges');
  812. add(' .exception_ranges*) }');
  813. add(' /* Adjust the address for the data segment. We want to adjust up to');
  814. add(' the same address within the page on the next page up. */');
  815. add(' . = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & (CONSTANT (MAXPAGESIZE) - 1)); . = DATA_SEGMENT_ALIGN (CONSTANT (MAXPAGESIZE), CONSTANT (COMMONPAGESIZE));');
  816. add(' /* Exception handling */');
  817. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  818. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  819. add(' .exception_ranges : ONLY_IF_RW { *(.exception_ranges .exception_ranges*) }');
  820. add(' /* Thread Local Storage sections */');
  821. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  822. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  823. add(' .preinit_array :');
  824. add(' {');
  825. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  826. add(' KEEP (*(.preinit_array))');
  827. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  828. add(' }');
  829. add(' .init_array :');
  830. add(' {');
  831. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  832. add(' KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))');
  833. add(' KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))');
  834. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  835. add(' }');
  836. add(' .fini_array :');
  837. add(' {');
  838. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  839. add(' KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))');
  840. add(' KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))');
  841. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  842. add(' }');
  843. add(' .ctors :');
  844. add(' {');
  845. add(' /* gcc uses crtbegin.o to find the start of');
  846. add(' the constructors, so we make sure it is');
  847. add(' first. Because this is a wildcard, it');
  848. add(' doesn''t matter if the user does not');
  849. add(' actually link against crtbegin.o; the');
  850. add(' linker won''t look for a file to match a');
  851. add(' wildcard. The wildcard also means that it');
  852. add(' doesn''t matter which directory crtbegin.o');
  853. add(' is in. */');
  854. add(' KEEP (*crtbegin.o(.ctors))');
  855. add(' KEEP (*crtbegin?.o(.ctors))');
  856. add(' /* We don''t want to include the .ctor section from');
  857. add(' the crtend.o file until after the sorted ctors.');
  858. add(' The .ctor section from the crtend file contains the');
  859. add(' end of ctors marker and it must be last */');
  860. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  861. add(' KEEP (*(SORT(.ctors.*)))');
  862. add(' KEEP (*(.ctors))');
  863. add(' }');
  864. add(' .dtors :');
  865. add(' {');
  866. add(' KEEP (*crtbegin.o(.dtors))');
  867. add(' KEEP (*crtbegin?.o(.dtors))');
  868. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  869. add(' KEEP (*(SORT(.dtors.*)))');
  870. add(' KEEP (*(.dtors))');
  871. add(' }');
  872. add(' .jcr : { KEEP (*(.jcr)) }');
  873. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro .data.rel.ro.* .gnu.linkonce.d.rel.ro.*) }');
  874. add(' .dynamic : { *(.dynamic) }');
  875. add(' .got : { *(.got) *(.igot) }');
  876. add(' . = DATA_SEGMENT_RELRO_END (24, .);');
  877. add(' .got.plt : { *(.got.plt) *(.igot.plt) }');
  878. add(' .data :');
  879. add(' {');
  880. add(' PROVIDE (__data_start = .);');
  881. { extra by FPC }
  882. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  883. add(' *(.data .data.* .gnu.linkonce.d.*)');
  884. add(' SORT(CONSTRUCTORS)');
  885. add(' }');
  886. add(' .data1 : { *(.data1) }');
  887. add(' _edata = .; PROVIDE (edata = .);');
  888. add(' . = .;');
  889. add(' __bss_start = .;');
  890. add(' __bss_start__ = .;');
  891. add(' .bss :');
  892. add(' {');
  893. add(' *(.dynbss)');
  894. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  895. add(' *(COMMON)');
  896. add(' /* Align here to ensure that the .bss section occupies space up to');
  897. add(' _end. Align after .bss to ensure correct alignment even if the');
  898. add(' .bss section disappears because there are no input sections.');
  899. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  900. add(' pad the .data section. */');
  901. add(' . = ALIGN(. != 0 ? 64 / 8 : 1);');
  902. add(' }');
  903. add(' _bss_end__ = . ; __bss_end__ = . ;');
  904. add(' . = ALIGN(64 / 8);');
  905. add(' . = SEGMENT_START("ldata-segment", .);');
  906. add(' . = ALIGN(64 / 8);');
  907. add(' __end__ = . ;');
  908. add(' _end = .; PROVIDE (end = .);');
  909. add(' . = DATA_SEGMENT_END (.);');
  910. add(' /* Stabs debugging sections. */');
  911. add(' .stab 0 : { *(.stab) }');
  912. add(' .stabstr 0 : { *(.stabstr) }');
  913. add(' .stab.excl 0 : { *(.stab.excl) }');
  914. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  915. add(' .stab.index 0 : { *(.stab.index) }');
  916. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  917. add(' .comment 0 : { *(.comment) }');
  918. add(' /* DWARF debug sections.');
  919. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  920. add(' of the section so we begin them at 0. */');
  921. add(' /* DWARF 1 */');
  922. add(' .debug 0 : { *(.debug) }');
  923. add(' .line 0 : { *(.line) }');
  924. add(' /* GNU DWARF 1 extensions */');
  925. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  926. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  927. add(' /* DWARF 1.1 and DWARF 2 */');
  928. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  929. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  930. add(' /* DWARF 2 */');
  931. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  932. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  933. add(' .debug_line 0 : { *(.debug_line .debug_line.* .debug_line_end ) }');
  934. add(' .debug_frame 0 : { *(.debug_frame) }');
  935. add(' .debug_str 0 : { *(.debug_str) }');
  936. add(' .debug_loc 0 : { *(.debug_loc) }');
  937. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  938. add(' /* SGI/MIPS DWARF 2 extensions */');
  939. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  940. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  941. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  942. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  943. add(' /* DWARF 3 */');
  944. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  945. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  946. add(' /* DWARF Extension. */');
  947. add(' .debug_macro 0 : { *(.debug_macro) }');
  948. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  949. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  950. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) *(.gnu.lto_*) }');
  951. add('}');
  952. {$endif AArch64}
  953. {$ifdef ARM}
  954. if target_info.abi in [abi_eabi,abi_eabihf] then
  955. begin
  956. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  957. add('/* Script for -z combreloc: combine and sort reloc sections */');
  958. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  959. add(' "elf32-littlearm")');
  960. add('OUTPUT_ARCH(arm)');
  961. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  962. add('SECTIONS');
  963. add('{');
  964. add(' /* Read-only sections, merged into text segment: */');
  965. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000 + SIZEOF_HEADERS;');
  966. add(' .interp : { *(.interp) }');
  967. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  968. add(' .hash : { *(.hash) }');
  969. add(' .gnu.hash : { *(.gnu.hash) }');
  970. add(' .dynsym : { *(.dynsym) }');
  971. add(' .dynstr : { *(.dynstr) }');
  972. add(' .gnu.version : { *(.gnu.version) }');
  973. add(' .gnu.version_d : { *(.gnu.version_d) }');
  974. add(' .gnu.version_r : { *(.gnu.version_r) }');
  975. add(' .rel.dyn :');
  976. add(' {');
  977. add(' *(.rel.init)');
  978. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  979. add(' *(.rel.fini)');
  980. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  981. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  982. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  983. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  984. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  985. add(' *(.rel.ctors)');
  986. add(' *(.rel.dtors)');
  987. add(' *(.rel.got)');
  988. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  989. add(' }');
  990. add(' .rela.dyn :');
  991. add(' {');
  992. add(' *(.rela.init)');
  993. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  994. add(' *(.rela.fini)');
  995. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  996. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  997. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  998. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  999. add(' *(.rela.ctors)');
  1000. add(' *(.rela.dtors)');
  1001. add(' *(.rela.got)');
  1002. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  1003. add(' }');
  1004. add(' .rel.plt : { *(.rel.plt) }');
  1005. add(' .rela.plt : { *(.rela.plt) }');
  1006. add(' .init :');
  1007. add(' {');
  1008. add(' KEEP (*(.init))');
  1009. add(' } =0');
  1010. add(' .plt : { *(.plt) }');
  1011. add(' .text :');
  1012. add(' {');
  1013. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  1014. add(' KEEP (*(.text.*personality*))');
  1015. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  1016. add(' *(.gnu.warning)');
  1017. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  1018. add(' } =0');
  1019. add(' .fini :');
  1020. add(' {');
  1021. add(' KEEP (*(.fini))');
  1022. add(' } =0');
  1023. add(' PROVIDE (__etext = .);');
  1024. add(' PROVIDE (_etext = .);');
  1025. add(' PROVIDE (etext = .);');
  1026. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  1027. add(' .rodata1 : { *(.rodata1) }');
  1028. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  1029. add(' __exidx_start = .;');
  1030. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  1031. add(' __exidx_end = .;');
  1032. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  1033. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  1034. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  1035. add(' /* Adjust the address for the data segment. We want to adjust up to');
  1036. add(' the same address within the page on the next page up. */');
  1037. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  1038. add(' /* Exception handling */');
  1039. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  1040. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  1041. add(' /* Thread Local Storage sections */');
  1042. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  1043. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  1044. add(' .preinit_array :');
  1045. add(' {');
  1046. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  1047. add(' KEEP (*(.preinit_array))');
  1048. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  1049. add(' }');
  1050. add(' .init_array :');
  1051. add(' {');
  1052. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  1053. add(' KEEP (*(SORT(.init_array.*)))');
  1054. add(' KEEP (*(.init_array))');
  1055. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  1056. add(' }');
  1057. add(' .fini_array :');
  1058. add(' {');
  1059. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  1060. add(' KEEP (*(.fini_array))');
  1061. add(' KEEP (*(SORT(.fini_array.*)))');
  1062. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  1063. add(' }');
  1064. add(' .ctors :');
  1065. add(' {');
  1066. add(' /* gcc uses crtbegin.o to find the start of');
  1067. add(' the constructors, so we make sure it is');
  1068. add(' first. Because this is a wildcard, it');
  1069. add(' doesn''t matter if the user does not');
  1070. add(' actually link against crtbegin.o; the');
  1071. add(' linker won''t look for a file to match a');
  1072. add(' wildcard. The wildcard also means that it');
  1073. add(' doesn''t matter which directory crtbegin.o');
  1074. add(' is in. */');
  1075. add(' KEEP (*crtbegin.o(.ctors))');
  1076. add(' KEEP (*crtbegin?.o(.ctors))');
  1077. add(' /* We don''t want to include the .ctor section from');
  1078. add(' the crtend.o file until after the sorted ctors.');
  1079. add(' The .ctor section from the crtend file contains the');
  1080. add(' end of ctors marker and it must be last */');
  1081. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  1082. add(' KEEP (*(SORT(.ctors.*)))');
  1083. add(' KEEP (*(.ctors))');
  1084. add(' }');
  1085. add(' .dtors :');
  1086. add(' {');
  1087. add(' KEEP (*crtbegin.o(.dtors))');
  1088. add(' KEEP (*crtbegin?.o(.dtors))');
  1089. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  1090. add(' KEEP (*(SORT(.dtors.*)))');
  1091. add(' KEEP (*(.dtors))');
  1092. add(' }');
  1093. add(' .jcr : { KEEP (*(.jcr)) }');
  1094. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  1095. add(' .dynamic : { *(.dynamic) }');
  1096. add(' .got : { *(.got.plt) *(.got) }');
  1097. add(' .data :');
  1098. add(' {');
  1099. add(' __data_start = . ;');
  1100. add(' *(.data .data.* .gnu.linkonce.d.*)');
  1101. { extra by FPC }
  1102. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  1103. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  1104. add(' SORT(CONSTRUCTORS)');
  1105. add(' }');
  1106. add(' .data1 : { *(.data1) }');
  1107. add(' _edata = .; PROVIDE (edata = .);');
  1108. add(' __bss_start = .;');
  1109. add(' __bss_start__ = .;');
  1110. add(' .bss :');
  1111. add(' {');
  1112. add(' *(.dynbss)');
  1113. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1114. add(' *(COMMON)');
  1115. add(' /* Align here to ensure that the .bss section occupies space up to');
  1116. add(' _end. Align after .bss to ensure correct alignment even if the');
  1117. add(' .bss section disappears because there are no input sections.');
  1118. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  1119. add(' pad the .data section. */');
  1120. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  1121. add(' }');
  1122. add(' _bss_end__ = . ; __bss_end__ = . ;');
  1123. add(' . = ALIGN(32 / 8);');
  1124. add(' . = ALIGN(32 / 8);');
  1125. add(' __end__ = . ;');
  1126. add(' _end = .; PROVIDE (end = .);');
  1127. add(' /* Stabs debugging sections. */');
  1128. add(' .stab 0 : { *(.stab) }');
  1129. add(' .stabstr 0 : { *(.stabstr) }');
  1130. add(' .stab.excl 0 : { *(.stab.excl) }');
  1131. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  1132. add(' .stab.index 0 : { *(.stab.index) }');
  1133. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  1134. add(' .comment 0 : { *(.comment) }');
  1135. add(' /* DWARF debug sections.');
  1136. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  1137. add(' of the section so we begin them at 0. */');
  1138. add(' /* DWARF 1 */');
  1139. add(' .debug 0 : { *(.debug) }');
  1140. add(' .line 0 : { *(.line) }');
  1141. add(' /* GNU DWARF 1 extensions */');
  1142. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  1143. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  1144. add(' /* DWARF 1.1 and DWARF 2 */');
  1145. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  1146. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  1147. add(' /* DWARF 2 */');
  1148. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  1149. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  1150. add(' .debug_line 0 : { *(.debug_line) }');
  1151. add(' .debug_frame 0 : { *(.debug_frame) }');
  1152. add(' .debug_str 0 : { *(.debug_str) }');
  1153. add(' .debug_loc 0 : { *(.debug_loc) }');
  1154. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  1155. add(' /* SGI/MIPS DWARF 2 extensions */');
  1156. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  1157. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  1158. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  1159. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  1160. add(' /* DWARF 3 */');
  1161. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  1162. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  1163. add(' .stack 0x80000 :');
  1164. add(' {');
  1165. add(' _stack = .;');
  1166. add(' *(.stack)');
  1167. add(' }');
  1168. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  1169. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  1170. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  1171. add('}');
  1172. end
  1173. else
  1174. {$endif ARM}
  1175. {$ifndef LINKERSCRIPT_INCLUDED}
  1176. begin
  1177. {Sections.}
  1178. add('SECTIONS');
  1179. add('{');
  1180. {Read-only sections, merged into text segment:}
  1181. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 + SIZEOF_HEADERS;');
  1182. add(' .interp : { *(.interp) }');
  1183. add(' .hash : { *(.hash) }');
  1184. add(' .dynsym : { *(.dynsym) }');
  1185. add(' .dynstr : { *(.dynstr) }');
  1186. add(' .gnu.version : { *(.gnu.version) }');
  1187. add(' .gnu.version_d : { *(.gnu.version_d) }');
  1188. add(' .gnu.version_r : { *(.gnu.version_r) }');
  1189. add(' .rel.dyn :');
  1190. add(' {');
  1191. add(' *(.rel.init)');
  1192. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  1193. add(' *(.rel.fini)');
  1194. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  1195. add(' *(.rel.data.rel.ro*)');
  1196. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  1197. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  1198. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  1199. add(' *(.rel.got)');
  1200. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  1201. add(' }');
  1202. add(' .rela.dyn :');
  1203. add(' {');
  1204. add(' *(.rela.init)');
  1205. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  1206. add(' *(.rela.fini)');
  1207. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  1208. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  1209. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  1210. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  1211. add(' *(.rela.got)');
  1212. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  1213. add(' }');
  1214. add(' .rel.plt : { *(.rel.plt) }');
  1215. add(' .rela.plt : { *(.rela.plt) }');
  1216. add(' .init :');
  1217. add(' {');
  1218. add(' KEEP (*(.init))');
  1219. add(' } =0x90909090');
  1220. add(' .plt : { *(.plt) }');
  1221. add(' .text :');
  1222. add(' {');
  1223. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  1224. add(' KEEP (*(.text.*personality*))');
  1225. {.gnu.warning sections are handled specially by elf32.em.}
  1226. add(' *(.gnu.warning)');
  1227. add(' } =0x90909090');
  1228. add(' .fini :');
  1229. add(' {');
  1230. add(' KEEP (*(.fini))');
  1231. add(' } =0x90909090');
  1232. add(' PROVIDE (_etext = .);');
  1233. add(' .rodata :');
  1234. add(' {');
  1235. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  1236. add(' }');
  1237. {Adjust the address for the data segment. We want to adjust up to
  1238. the same address within the page on the next page up.}
  1239. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  1240. add(' .dynamic : { *(.dynamic) }');
  1241. add(' .got : { *(.got) }');
  1242. add(' .got.plt : { *(.got.plt) }');
  1243. add(' .data :');
  1244. add(' {');
  1245. add(' *(.data .data.* .gnu.linkonce.d.*)');
  1246. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  1247. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  1248. add(' }');
  1249. add(' PROVIDE (_edata = .);');
  1250. add(' PROVIDE (edata = .);');
  1251. {$ifdef zsegment_threadvars}
  1252. add(' _z = .;');
  1253. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  1254. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  1255. add(' . = _z + SIZEOF (.threadvar);');
  1256. {$else}
  1257. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  1258. {$endif}
  1259. add(' __bss_start = .;');
  1260. add(' .bss :');
  1261. add(' {');
  1262. add(' *(.dynbss)');
  1263. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  1264. add(' *(COMMON)');
  1265. {Align here to ensure that the .bss section occupies space up to
  1266. _end. Align after .bss to ensure correct alignment even if the
  1267. .bss section disappears because there are no input sections.}
  1268. add(' . = ALIGN(32 / 8);');
  1269. add(' }');
  1270. add(' . = ALIGN(32 / 8);');
  1271. add(' PROVIDE (_end = .);');
  1272. add(' PROVIDE (end = .);');
  1273. {Stabs debugging sections.}
  1274. add(' .stab 0 : { *(.stab) }');
  1275. add(' .stabstr 0 : { *(.stabstr) }');
  1276. add('}');
  1277. end;
  1278. {$endif LINKERSCRIPT_INCLUDED}
  1279. end;
  1280. { Write and Close response }
  1281. writetodisk;
  1282. Free;
  1283. end;
  1284. WriteResponseFile:=True;
  1285. end;
  1286. function TLinkerLinux.MakeExecutable:boolean;
  1287. var
  1288. i : longint;
  1289. binstr,
  1290. cmdstr,
  1291. mapstr : TCmdStr;
  1292. success : boolean;
  1293. DynLinkStr : string;
  1294. GCSectionsStr,
  1295. StaticStr,
  1296. StripStr : string[40];
  1297. begin
  1298. if not(cs_link_nolink in current_settings.globalswitches) then
  1299. Message1(exec_i_linking,current_module.exefilename);
  1300. { Create some replacements }
  1301. StaticStr:='';
  1302. StripStr:='';
  1303. GCSectionsStr:='';
  1304. DynLinkStr:='';
  1305. mapstr:='';
  1306. if (cs_link_staticflag in current_settings.globalswitches) then
  1307. StaticStr:='-static';
  1308. if (cs_link_strip in current_settings.globalswitches) and
  1309. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  1310. StripStr:='-s';
  1311. if (cs_link_map in current_settings.globalswitches) then
  1312. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  1313. if (cs_link_smart in current_settings.globalswitches) and
  1314. create_smartlink_sections then
  1315. GCSectionsStr:='--gc-sections';
  1316. If (cs_profile in current_settings.moduleswitches) or
  1317. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  1318. begin
  1319. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  1320. if cshared then
  1321. DynLinkStr:=DynLinkStr+' --shared ';
  1322. if rlinkpath<>'' then
  1323. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  1324. End;
  1325. { Write used files and libraries }
  1326. WriteResponseFile(false);
  1327. { Call linker }
  1328. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  1329. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  1330. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1331. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1332. Replace(cmdstr,'$STATIC',StaticStr);
  1333. Replace(cmdstr,'$STRIP',StripStr);
  1334. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1335. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  1336. Replace(cmdstr,'$MAP',mapstr);
  1337. { create dynamic symbol table? }
  1338. if HasExports then
  1339. cmdstr:=cmdstr+' -E';
  1340. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1341. { Create external .dbg file with debuginfo }
  1342. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  1343. begin
  1344. for i:=1 to 3 do
  1345. begin
  1346. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  1347. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  1348. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename)));
  1349. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename));
  1350. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1351. if not success then
  1352. break;
  1353. end;
  1354. end;
  1355. { Remove ReponseFile }
  1356. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1357. DeleteFile(outputexedir+Info.ResName);
  1358. MakeExecutable:=success; { otherwise a recursive call to link method }
  1359. end;
  1360. Function TLinkerLinux.MakeSharedLibrary:boolean;
  1361. var
  1362. InitStr,
  1363. FiniStr,
  1364. GCSectionsStr,
  1365. SoNameStr : string[80];
  1366. binstr,
  1367. cmdstr,
  1368. mapstr : TCmdStr;
  1369. success : boolean;
  1370. begin
  1371. MakeSharedLibrary:=false;
  1372. mapstr:='';
  1373. if not(cs_link_nolink in current_settings.globalswitches) then
  1374. Message1(exec_i_linking,current_module.sharedlibfilename);
  1375. if (cs_link_smart in current_settings.globalswitches) and
  1376. create_smartlink_sections then
  1377. GCSectionsStr:='--gc-sections'
  1378. else
  1379. GCSectionsStr:='';
  1380. { Write used files and libraries }
  1381. WriteResponseFile(true);
  1382. { Create some replacements }
  1383. { note: linux does not use exportlib.initname/fininame due to the custom startup code }
  1384. InitStr:='-init FPC_SHARED_LIB_START';
  1385. FiniStr:='-fini FPC_LIB_EXIT';
  1386. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  1387. if (cs_link_map in current_settings.globalswitches) then
  1388. mapstr:='-Map '+maybequoted(ChangeFileExt(current_module.sharedlibfilename,'.map'));
  1389. { Call linker }
  1390. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1391. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1392. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1393. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1394. Replace(cmdstr,'$INIT',InitStr);
  1395. Replace(cmdstr,'$FINI',FiniStr);
  1396. Replace(cmdstr,'$SONAME',SoNameStr);
  1397. Replace(cmdstr,'$MAP',mapstr);
  1398. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  1399. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1400. { Strip the library ? }
  1401. if success and (cs_link_strip in current_settings.globalswitches) then
  1402. begin
  1403. { only remove non global symbols and debugging info for a library }
  1404. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1405. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1406. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  1407. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1408. end;
  1409. { Remove ReponseFile }
  1410. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1411. DeleteFile(outputexedir+Info.ResName);
  1412. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1413. end;
  1414. {*****************************************************************************
  1415. TINTERNALLINKERLINUX
  1416. *****************************************************************************}
  1417. constructor TInternalLinkerLinux.Create;
  1418. begin
  1419. inherited Create;
  1420. SetupLibrarySearchPath;
  1421. SetupDynlinker(dynlinker,libctype);
  1422. CArObjectReader:=TArObjectReader;
  1423. CExeOutput:=ElfExeOutputClass;
  1424. CObjInput:=TElfObjInput;
  1425. end;
  1426. procedure TInternalLinkerLinux.InitSysInitUnitName;
  1427. begin
  1428. linklibc:=ModulesLinkToLibc;
  1429. reorder:=linklibc and ReOrderEntries;
  1430. sysinitunit:=defsinames[current_module.islibrary];
  1431. prtobj:=defprtnames[current_module.islibrary];
  1432. if cs_profile in current_settings.moduleswitches then
  1433. begin
  1434. prtobj:=gprtnames[libctype];
  1435. sysinitunit:=gsinames[libctype];
  1436. linklibc:=true;
  1437. end
  1438. else if linklibc then
  1439. begin
  1440. prtobj:=cprtnames[libctype];
  1441. sysinitunit:=csinames[libctype];
  1442. end;
  1443. end;
  1444. const
  1445. relsec_prefix:array[boolean] of TCmdStr = ('rel','rela');
  1446. procedure TInternalLinkerLinux.DefaultLinkScript;
  1447. var
  1448. s,s1,s2,relprefix:TCmdStr;
  1449. found1,found2:boolean;
  1450. linkToSharedLibs:boolean;
  1451. procedure AddLibraryStatement(const s:TCmdStr);
  1452. var
  1453. i:longint;
  1454. s1,s2:TCmdStr;
  1455. begin
  1456. i:=pos(target_info.sharedClibext+'.',s);
  1457. if (i>0) then
  1458. s1:=target_info.sharedClibprefix+S
  1459. else
  1460. s1:=target_info.sharedClibprefix+S+target_info.sharedClibext;
  1461. { TODO: to be compatible with ld search algorithm, each found file
  1462. must be tested for target compatibility, incompatible ones should be skipped. }
  1463. { TODO: shall we search library without suffix if one with suffix is not found? }
  1464. if (not(cs_link_staticflag in current_settings.globalswitches)) and
  1465. FindLibraryFile(s1,'','',s2) then
  1466. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1467. { TODO: static libraries never have numeric suffix in their names }
  1468. else if FindLibraryFile(s,target_info.staticClibprefix,target_info.staticClibext,s2) then
  1469. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(s2))
  1470. else
  1471. Comment(V_Error,'Import library not found for '+S);
  1472. end;
  1473. begin
  1474. if cs_profile in current_settings.moduleswitches then
  1475. begin
  1476. if not(libctype in [glibc2,glibc21]) then
  1477. AddSharedLibrary('gmon');
  1478. AddSharedLibrary('c');
  1479. end;
  1480. TElfExeOutput(exeoutput).interpreter:=stringdup(dynlinker);
  1481. { add objectfiles, start with prt0 always }
  1482. if not (target_info.system in systems_internal_sysinit) and (prtobj<>'') then
  1483. LinkScript.Concat('READOBJECT '+ maybequoted(FindObjectFile(prtobj,'',false)));
  1484. { try to add crti and crtbegin if linking to C }
  1485. if linklibc and (libctype<>uclibc) then
  1486. begin
  1487. { crti.o must come first }
  1488. if librarysearchpath.FindFile('crti.o',false,s) then
  1489. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1490. { then the crtbegin* }
  1491. if cs_create_pic in current_settings.moduleswitches then
  1492. begin
  1493. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  1494. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1495. end
  1496. else
  1497. if (cs_link_staticflag in current_settings.globalswitches) and
  1498. librarysearchpath.FindFile('crtbeginT.o',false,s) then
  1499. LinkScript.Concat('READOBJECT '+maybequoted(s))
  1500. else if librarysearchpath.FindFile('crtbegin.o',false,s) then
  1501. LinkScript.Concat('READOBJECT '+maybequoted(s));
  1502. end;
  1503. ScriptAddSourceStatements(false);
  1504. { we must reorder here because the result could empty sharedlibfiles }
  1505. if reorder then
  1506. ExpandAndApplyOrder(SharedLibFiles);
  1507. { See tw9089*.pp: if more than one pure-Pascal shared libs are loaded,
  1508. and none have rtld in their DT_NEEDED, then rtld cannot finalize correctly. }
  1509. if IsSharedLibrary then
  1510. LinkScript.Concat('READSTATICLIBRARY '+maybequoted(sysrootpath+dynlinker));
  1511. linkToSharedLibs:=(not SharedLibFiles.Empty);
  1512. { Symbols declared as "external 'libx.so'" are added to ImportLibraryList, library
  1513. prefix/extension *not* stripped. TImportLibLinux copies these to SharedLibFiles,
  1514. stripping prefixes and extensions.
  1515. However extension won't be stripped if library is specified with numeric suffix
  1516. (like "libpango-1.0.so.0")
  1517. Libraries specified with $LINKLIB directive are directly added to SharedLibFiles
  1518. and won't be present in ImportLibraryList. }
  1519. while not SharedLibFiles.Empty do
  1520. begin
  1521. S:=SharedLibFiles.GetFirst;
  1522. if (S<>'c') or reorder then
  1523. AddLibraryStatement(S);
  1524. end;
  1525. if (cs_link_staticflag in current_settings.globalswitches) or
  1526. (linklibc and not reorder) then
  1527. begin
  1528. LinkScript.Concat('GROUP');
  1529. if (cs_link_staticflag in current_settings.globalswitches) then
  1530. begin
  1531. AddLibraryStatement('gcc');
  1532. AddLibraryStatement('gcc_eh');
  1533. end;
  1534. if linklibc and not reorder then
  1535. AddLibraryStatement('c');
  1536. LinkScript.Concat('ENDGROUP');
  1537. end;
  1538. { objects which must be at the end }
  1539. if linklibc and (libctype<>uclibc) then
  1540. begin
  1541. if cs_create_pic in current_settings.moduleswitches then
  1542. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  1543. else
  1544. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  1545. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  1546. if found1 then
  1547. LinkScript.Concat('READOBJECT '+maybequoted(s1));
  1548. if found2 then
  1549. LinkScript.Concat('READOBJECT '+maybequoted(s2));
  1550. end;
  1551. if (not IsSharedLibrary) then
  1552. if (linkToSharedLibs and not linklibc) then
  1553. LinkScript.Concat('ENTRYNAME _dynamic_start')
  1554. else
  1555. LinkScript.Concat('ENTRYNAME _start')
  1556. else
  1557. LinkScript.Concat('ISSHAREDLIBRARY');
  1558. relprefix:=relsec_prefix[ElfTarget.relocs_use_addend];
  1559. with LinkScript do
  1560. begin
  1561. Concat('HEADER');
  1562. Concat('EXESECTION .interp');
  1563. Concat(' OBJSECTION .interp');
  1564. Concat('ENDEXESECTION');
  1565. Concat('EXESECTION .note.ABI-tag');
  1566. Concat(' OBJSECTION .note.ABI-tag');
  1567. Concat('ENDEXESECTION');
  1568. Concat('EXESECTION .note.gnu.build-id');
  1569. Concat(' OBJSECTION .note.gnu.build-id');
  1570. Concat('ENDEXESECTION');
  1571. Concat('EXESECTION .hash');
  1572. Concat(' OBJSECTION .hash');
  1573. Concat('ENDEXESECTION');
  1574. Concat('EXESECTION .dynsym');
  1575. Concat(' OBJSECTION .dynsym');
  1576. Concat('ENDEXESECTION');
  1577. Concat('EXESECTION .dynstr');
  1578. Concat(' OBJSECTION .dynstr');
  1579. Concat('ENDEXESECTION');
  1580. Concat('EXESECTION .gnu.version');
  1581. Concat(' OBJSECTION .gnu.version');
  1582. Concat('ENDEXESECTION');
  1583. Concat('EXESECTION .gnu.version_d');
  1584. Concat(' OBJSECTION .gnu.version_d');
  1585. Concat('ENDEXESECTION');
  1586. Concat('EXESECTION .gnu.version_r');
  1587. Concat(' OBJSECTION .gnu.version_r');
  1588. Concat('ENDEXESECTION');
  1589. Concat('EXESECTION .'+relprefix+'.dyn');
  1590. Concat(' OBJSECTION .'+relprefix+'.dyn');
  1591. Concat('ENDEXESECTION');
  1592. Concat('EXESECTION .'+relprefix+'.plt');
  1593. Concat(' OBJSECTION .'+relprefix+'.plt');
  1594. Concat(' PROVIDE __'+relprefix+'_iplt_start');
  1595. Concat(' OBJSECTION .'+relprefix+'.iplt');
  1596. Concat(' PROVIDE __'+relprefix+'_iplt_end');
  1597. Concat('ENDEXESECTION');
  1598. Concat('EXESECTION .init');
  1599. Concat(' OBJSECTION .init');
  1600. Concat('ENDEXESECTION');
  1601. Concat('EXESECTION .plt');
  1602. Concat(' OBJSECTION .plt');
  1603. Concat('ENDEXESECTION');
  1604. Concat('EXESECTION .text');
  1605. Concat(' OBJSECTION .text*');
  1606. Concat('ENDEXESECTION');
  1607. Concat('EXESECTION .fini');
  1608. Concat(' OBJSECTION .fini');
  1609. Concat(' PROVIDE __etext');
  1610. Concat(' PROVIDE _etext');
  1611. Concat(' PROVIDE etext');
  1612. Concat('ENDEXESECTION');
  1613. Concat('EXESECTION .rodata');
  1614. Concat(' OBJSECTION .rodata*');
  1615. Concat('ENDEXESECTION');
  1616. {$ifdef arm}
  1617. Concat('EXESECTION .ARM.extab');
  1618. Concat(' OBJSECTION .ARM.extab*');
  1619. Concat('ENDEXESECTION');
  1620. Concat('EXESECTION .ARM.exidx');
  1621. Concat(' SYMBOL __exidx_start');
  1622. Concat(' OBJSECTION .ARM.exidx*');
  1623. Concat(' SYMBOL __exidx_end');
  1624. Concat('ENDEXESECTION');
  1625. {$endif}
  1626. Concat('EXESECTION .eh_frame');
  1627. Concat(' OBJSECTION .eh_frame');
  1628. Concat('ENDEXESECTION');
  1629. Concat('EXESECTION .gcc_except_table');
  1630. Concat(' OBJSECTION .gcc_except_table');
  1631. Concat(' OBJSECTION .gcc_except_table.*');
  1632. Concat('ENDEXESECTION');
  1633. Concat('EXESECTION .tdata');
  1634. Concat(' OBJSECTION .tdata');
  1635. Concat(' OBJSECTION .tdata.*');
  1636. Concat('ENDEXESECTION');
  1637. Concat('EXESECTION .tbss');
  1638. Concat(' OBJSECTION .tbss');
  1639. Concat(' OBJSECTION .tbss.*');
  1640. Concat('ENDEXESECTION');
  1641. Concat('EXESECTION .preinit_array');
  1642. Concat(' PROVIDE __preinit_array_start');
  1643. Concat(' OBJSECTION .preinit_array');
  1644. Concat(' PROVIDE __preinit_array_end');
  1645. Concat('ENDEXESECTION');
  1646. Concat('EXESECTION .init_array');
  1647. Concat(' PROVIDE __init_array_start');
  1648. { why the hell .ctors are both here and exesection .ctors below?? }
  1649. // KEEP ( *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
  1650. Concat(' OBJSECTION .init_array');
  1651. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
  1652. Concat('PROVIDE __init_array_end');
  1653. Concat('ENDEXESECTION');
  1654. Concat('EXESECTION .fini_array');
  1655. Concat(' PROVIDE __fini_array_start');
  1656. // KEEP ( *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
  1657. Concat(' OBJSECTION .fini_array');
  1658. // KEEP ( *(EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
  1659. Concat(' PROVIDE __fini_array_end');
  1660. Concat('ENDEXESECTION');
  1661. Concat('EXESECTION .ctors');
  1662. Concat(' OBJSECTION .ctors*');
  1663. Concat('ENDEXESECTION');
  1664. Concat('EXESECTION .dtors');
  1665. Concat(' OBJSECTION .dtors*');
  1666. Concat('ENDEXESECTION');
  1667. Concat('EXESECTION .jcr');
  1668. Concat(' OBJSECTION .jcr');
  1669. Concat('ENDEXESECTION');
  1670. Concat('EXESECTION .dynamic');
  1671. Concat(' OBJSECTION .dynamic');
  1672. Concat('ENDEXESECTION');
  1673. {$ifndef mips}
  1674. Concat('EXESECTION .got');
  1675. {$ifdef arm}
  1676. Concat(' OBJSECTION .got.plt');
  1677. {$endif arm}
  1678. Concat(' OBJSECTION .got');
  1679. Concat('ENDEXESECTION');
  1680. {$endif mips}
  1681. {$ifndef arm}
  1682. Concat('EXESECTION .got.plt');
  1683. Concat(' OBJSECTION .got.plt');
  1684. Concat('ENDEXESECTION');
  1685. {$endif arm}
  1686. Concat('EXESECTION .data');
  1687. Concat(' OBJSECTION .data*');
  1688. Concat(' OBJSECTION .fpc*');
  1689. Concat(' OBJSECTION fpc.resources');
  1690. Concat(' PROVIDE _edata');
  1691. Concat(' PROVIDE edata');
  1692. Concat('ENDEXESECTION');
  1693. {$ifdef mips}
  1694. Concat('EXESECTION .got');
  1695. Concat(' OBJSECTION .got');
  1696. Concat('ENDEXESECTION');
  1697. {$endif mips}
  1698. Concat('EXESECTION .bss');
  1699. Concat(' OBJSECTION .dynbss');
  1700. Concat(' OBJSECTION .bss*');
  1701. Concat(' OBJSECTION fpc.reshandles');
  1702. Concat(' PROVIDE end');
  1703. Concat(' SYMBOL _end');
  1704. Concat('ENDEXESECTION');
  1705. ScriptAddGenericSections('.debug_aranges,.debug_pubnames,.debug_info,'+
  1706. '.debug_abbrev,.debug_line,.debug_frame,.debug_str,.debug_loc,'+
  1707. '.debug_macinfo,.debug_weaknames,.debug_funcnames,.debug_typenames,.debug_varnames,.debug_ranges');
  1708. Concat('EXESECTION .stab');
  1709. Concat(' OBJSECTION .stab');
  1710. Concat('ENDEXESECTION');
  1711. Concat('EXESECTION .stabstr');
  1712. Concat(' OBJSECTION .stabstr');
  1713. Concat('ENDEXESECTION');
  1714. end;
  1715. end;
  1716. {*****************************************************************************
  1717. Initialize
  1718. *****************************************************************************}
  1719. initialization
  1720. RegisterLinker(ld_linux,TLinkerLinux);
  1721. RegisterLinker(ld_int_linux,TInternalLinkerLinux);
  1722. {$ifdef i386}
  1723. RegisterImport(system_i386_linux,timportliblinux);
  1724. RegisterExport(system_i386_linux,texportliblinux);
  1725. RegisterTarget(system_i386_linux_info);
  1726. {$endif i386}
  1727. {$ifdef m68k}
  1728. RegisterImport(system_m68k_linux,timportliblinux);
  1729. RegisterExport(system_m68k_linux,texportliblinux);
  1730. RegisterTarget(system_m68k_linux_info);
  1731. {$endif m68k}
  1732. {$ifdef powerpc}
  1733. RegisterImport(system_powerpc_linux,timportliblinux);
  1734. RegisterExport(system_powerpc_linux,texportliblinux);
  1735. RegisterTarget(system_powerpc_linux_info);
  1736. {$endif powerpc}
  1737. {$ifdef powerpc64}
  1738. { default to little endian either when compiling with -dppc64le, or when
  1739. compiling on a little endian ppc64 platform }
  1740. {$if defined(ppc64le) or (defined(cpupowerpc64) and defined(FPC_LITTLE_ENDIAN))}
  1741. system_powerpc64_linux_info.endian:=endian_little;
  1742. system_powerpc64_linux_info.abi:=abi_powerpc_elfv2;
  1743. {$endif}
  1744. RegisterImport(system_powerpc64_linux,timportliblinux);
  1745. RegisterExport(system_powerpc64_linux,texportliblinux);
  1746. RegisterTarget(system_powerpc64_linux_info);
  1747. {$endif powerpc64}
  1748. {$ifdef x86_64}
  1749. RegisterImport(system_x86_64_linux,timportliblinux);
  1750. RegisterExport(system_x86_64_linux,texportliblinux);
  1751. RegisterTarget(system_x86_64_linux_info);
  1752. RegisterTarget(system_x86_6432_linux_info);
  1753. {$endif x86_64}
  1754. {$ifdef SPARC}
  1755. RegisterImport(system_SPARC_linux,timportliblinux);
  1756. RegisterExport(system_SPARC_linux,texportliblinux);
  1757. RegisterTarget(system_SPARC_linux_info);
  1758. {$endif SPARC}
  1759. {$ifdef SPARC64}
  1760. RegisterImport(system_SPARC64_linux,timportliblinux);
  1761. RegisterExport(system_SPARC64_linux,texportliblinux);
  1762. RegisterTarget(system_SPARC64_linux_info);
  1763. {$endif SPARC64}
  1764. {$ifdef ARM}
  1765. RegisterImport(system_arm_linux,timportliblinux);
  1766. RegisterExport(system_arm_linux,texportliblinux);
  1767. RegisterTarget(system_arm_linux_info);
  1768. {$endif ARM}
  1769. {$ifdef aarch64}
  1770. RegisterImport(system_aarch64_linux,timportliblinux);
  1771. RegisterExport(system_aarch64_linux,texportliblinux);
  1772. RegisterTarget(system_aarch64_linux_info);
  1773. {$endif aarch64}
  1774. {$ifdef MIPS}
  1775. {$ifdef MIPSEL}
  1776. RegisterImport(system_mipsel_linux,timportliblinux);
  1777. RegisterExport(system_mipsel_linux,texportliblinux);
  1778. RegisterTarget(system_mipsel_linux_info);
  1779. {$else MIPS}
  1780. RegisterImport(system_mipseb_linux,timportliblinux);
  1781. RegisterExport(system_mipseb_linux,texportliblinux);
  1782. RegisterTarget(system_mipseb_linux_info);
  1783. {$endif MIPSEL}
  1784. {$endif MIPS}
  1785. {$ifdef riscv32}
  1786. RegisterImport(system_riscv32_linux,timportliblinux);
  1787. RegisterExport(system_riscv32_linux,texportliblinux);
  1788. RegisterTarget(system_riscv32_linux_info);
  1789. {$endif riscv32}
  1790. {$ifdef riscv64}
  1791. RegisterImport(system_riscv64_linux,timportliblinux);
  1792. RegisterExport(system_riscv64_linux,texportliblinux);
  1793. RegisterTarget(system_riscv64_linux_info);
  1794. {$endif riscv64}
  1795. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1796. end.