t_linux.pas 75 KB

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