t_linux.pas 73 KB

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