t_linux.pas 73 KB

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