2
0

t_linux.pas 75 KB

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