t_linux.pas 69 KB

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