t_linux.pas 72 KB

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