t_linux.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. {
  2. Copyright (c) 1998-2002 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. symsym,symdef,ppu,
  23. import,export,link;
  24. type
  25. timportliblinux=class(timportlib)
  26. procedure generatelib;override;
  27. end;
  28. texportliblinux=class(texportlib)
  29. procedure preparelib(const s : string);override;
  30. procedure exportprocedure(hp : texported_item);override;
  31. procedure exportvar(hp : texported_item);override;
  32. procedure generatelib;override;
  33. end;
  34. tlinkerlinux=class(texternallinker)
  35. private
  36. libctype:(libc5,glibc2,glibc21,uclibc);
  37. cprtobj,
  38. gprtobj,
  39. prtobj : string[80];
  40. reorder : boolean;
  41. linklibc: boolean;
  42. Function WriteResponseFile(isdll:boolean) : Boolean;
  43. public
  44. constructor Create;override;
  45. procedure SetDefaultInfo;override;
  46. procedure InitSysInitUnitName;override;
  47. function MakeExecutable:boolean;override;
  48. function MakeSharedLibrary:boolean;override;
  49. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  50. procedure LoadPredefinedLibraryOrder; override;
  51. end;
  52. implementation
  53. uses
  54. SysUtils,
  55. cutils,cfileutl,cclasses,
  56. verbose,systems,globtype,globals,
  57. symconst,script,
  58. fmodule,
  59. aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,
  60. cgbase,cgobj,cgutils,ogbase,ncgutil,
  61. comprsrc,
  62. i_linux
  63. ;
  64. {*****************************************************************************
  65. TIMPORTLIBLINUX
  66. *****************************************************************************}
  67. procedure timportliblinux.generatelib;
  68. var
  69. i : longint;
  70. ImportLibrary : TImportLibrary;
  71. begin
  72. for i:=0 to current_module.ImportLibraryList.Count-1 do
  73. begin
  74. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  75. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  76. end;
  77. end;
  78. {*****************************************************************************
  79. TEXPORTLIBLINUX
  80. *****************************************************************************}
  81. procedure texportliblinux.preparelib(const s:string);
  82. begin
  83. end;
  84. procedure texportliblinux.exportprocedure(hp : texported_item);
  85. var
  86. hp2 : texported_item;
  87. begin
  88. { first test the index value }
  89. if (hp.options and eo_index)<>0 then
  90. begin
  91. Message1(parser_e_no_export_with_index_for_target,'linux');
  92. exit;
  93. end;
  94. { now place in correct order }
  95. hp2:=texported_item(current_module._exports.first);
  96. while assigned(hp2) and
  97. (hp.name^>hp2.name^) do
  98. hp2:=texported_item(hp2.next);
  99. { insert hp there !! }
  100. if assigned(hp2) and (hp2.name^=hp.name^) then
  101. begin
  102. { this is not allowed !! }
  103. Message1(parser_e_export_name_double,hp.name^);
  104. exit;
  105. end;
  106. if hp2=texported_item(current_module._exports.first) then
  107. current_module._exports.concat(hp)
  108. else if assigned(hp2) then
  109. begin
  110. hp.next:=hp2;
  111. hp.previous:=hp2.previous;
  112. if assigned(hp2.previous) then
  113. hp2.previous.next:=hp;
  114. hp2.previous:=hp;
  115. end
  116. else
  117. current_module._exports.concat(hp);
  118. end;
  119. procedure texportliblinux.exportvar(hp : texported_item);
  120. begin
  121. hp.is_var:=true;
  122. exportprocedure(hp);
  123. end;
  124. procedure texportliblinux.generatelib;
  125. var
  126. hp2 : texported_item;
  127. pd : tprocdef;
  128. {$ifdef x86}
  129. sym : tasmsymbol;
  130. r : treference;
  131. {$endif x86}
  132. begin
  133. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  134. hp2:=texported_item(current_module._exports.first);
  135. while assigned(hp2) do
  136. begin
  137. if (not hp2.is_var) and
  138. (hp2.sym.typ=procsym) then
  139. begin
  140. { the manglednames can already be the same when the procedure
  141. is declared with cdecl }
  142. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  143. if not has_alias_name(pd,hp2.name^) then
  144. begin
  145. { place jump in al_procedures }
  146. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  147. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  148. if (cs_create_pic in current_settings.moduleswitches) and
  149. { other targets need to be checked how it works }
  150. (target_info.system in [system_x86_64_linux,system_i386_linux]) then
  151. begin
  152. {$ifdef x86}
  153. sym:=current_asmdata.RefAsmSymbol(pd.mangledname);
  154. reference_reset_symbol(r,sym,0);
  155. if cs_create_pic in current_settings.moduleswitches then
  156. r.refaddr:=addr_pic
  157. else
  158. r.refaddr:=addr_full;
  159. current_asmdata.asmlists[al_procedures].concat(taicpu.op_ref(A_JMP,S_NO,r));
  160. {$endif x86}
  161. end
  162. else
  163. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],pd.mangledname);
  164. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  165. end;
  166. end
  167. else
  168. message1(parser_e_no_export_of_variables_for_target,'linux');
  169. hp2:=texported_item(hp2.next);
  170. end;
  171. end;
  172. {*****************************************************************************
  173. TLINKERLINUX
  174. *****************************************************************************}
  175. Constructor TLinkerLinux.Create;
  176. begin
  177. Inherited Create;
  178. if not Dontlinkstdlibpath Then
  179. {$ifdef x86_64}
  180. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  181. {$else}
  182. {$ifdef powerpc64}
  183. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  184. {$else powerpc64}
  185. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  186. {$endif powerpc64}
  187. {$endif x86_64}
  188. end;
  189. procedure TLinkerLinux.SetDefaultInfo;
  190. {
  191. This will also detect which libc version will be used
  192. }
  193. const
  194. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  195. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  196. {$ifdef powerpc}platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  197. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  198. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  199. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  200. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  201. var
  202. defdynlinker: string;
  203. begin
  204. with Info do
  205. begin
  206. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  207. { when we want to cross-link we need to override default library paths }
  208. if length(sysrootpath) > 0 then
  209. ExeCmd[1]:=ExeCmd[1]+' -T';
  210. ExeCmd[1]:=ExeCmd[1]+' $RES';
  211. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES -E';
  212. DllCmd[2]:='strip --strip-unneeded $EXE';
  213. {$ifdef m68k}
  214. { experimental, is this correct? }
  215. defdynlinker:='/lib/ld-linux.so.2';
  216. {$endif m68k}
  217. {$ifdef i386}
  218. defdynlinker:='/lib/ld-linux.so.1';
  219. {$endif}
  220. {$ifdef x86_64}
  221. defdynlinker:='/lib64/ld-linux-x86-64.so.2';
  222. {$endif x86_64}
  223. {$ifdef sparc}
  224. defdynlinker:='/lib/ld-linux.so.2';
  225. {$endif sparc}
  226. {$ifdef powerpc}
  227. defdynlinker:='/lib/ld.so.1';
  228. {$endif powerpc}
  229. {$ifdef powerpc64}
  230. defdynlinker:='/lib64/ld64.so.1';
  231. {$endif powerpc64}
  232. {$ifdef arm}
  233. defdynlinker:='/lib/ld-linux.so.2';
  234. {$endif arm}
  235. {
  236. Search order:
  237. glibc 2.1+
  238. uclibc
  239. glibc 2.0
  240. If none is found (e.g. when cross compiling) glibc21 is assumed
  241. }
  242. {$ifdef i386}
  243. if FileExists(sysrootpath+'/lib/ld-linux.so.2',false) then
  244. begin
  245. DynamicLinker:='/lib/ld-linux.so.2';
  246. libctype:=glibc21;
  247. end
  248. else
  249. {$endif i386}
  250. if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  251. begin
  252. dynamiclinker:='/lib/ld-uClibc.so.0';
  253. libctype:=uclibc;
  254. end
  255. else if fileexists(sysrootpath+defdynlinker,false) then
  256. begin
  257. DynamicLinker:=defdynlinker;
  258. libctype:=glibc2;
  259. end
  260. else
  261. begin
  262. { default is glibc 2.1+ compatible }
  263. DynamicLinker:='/lib/ld-linux.so.2';
  264. libctype:=glibc21;
  265. end;
  266. end;
  267. end;
  268. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  269. // put your linkorder/linkalias overrides here.
  270. // Note: assumes only called when reordering/aliasing is used.
  271. Begin
  272. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  273. Begin
  274. LinkLibraryOrder.add('gcc','',15);
  275. LinkLibraryOrder.add('c','',100);
  276. LinkLibraryOrder.add('gmon','',120);
  277. LinkLibraryOrder.add('dl','',140);
  278. LinkLibraryOrder.add('pthread','',160);
  279. end;
  280. End;
  281. Procedure TLinkerLinux.InitSysInitUnitName;
  282. var
  283. csysinitunit,
  284. gsysinitunit : string[20];
  285. hp : tmodule;
  286. begin
  287. hp:=tmodule(loaded_units.first);
  288. while assigned(hp) do
  289. begin
  290. linklibc := hp.linkothersharedlibs.find('c');
  291. if linklibc then break;
  292. hp:=tmodule(hp.next);
  293. end;
  294. reorder := linklibc and ReOrderEntries;
  295. if current_module.islibrary then
  296. begin
  297. sysinitunit:='dll';
  298. csysinitunit:='dll';
  299. gsysinitunit:='dll';
  300. prtobj:='dllprt0';
  301. cprtobj:='dllprt0';
  302. gprtobj:='dllprt0';
  303. end
  304. else
  305. begin
  306. prtobj:='prt0';
  307. sysinitunit:='prc';
  308. case libctype of
  309. glibc21:
  310. begin
  311. cprtobj:='cprt21';
  312. gprtobj:='gprt21';
  313. csysinitunit:='c21';
  314. gsysinitunit:='c21g';
  315. end;
  316. uclibc:
  317. begin
  318. cprtobj:='ucprt0';
  319. gprtobj:='ugprt0';
  320. csysinitunit:='uc';
  321. gsysinitunit:='ucg';
  322. end
  323. else
  324. cprtobj:='cprt0';
  325. gprtobj:='gprt0';
  326. csysinitunit:='c';
  327. gsysinitunit:='g';
  328. end;
  329. end;
  330. if cs_profile in current_settings.moduleswitches then
  331. begin
  332. prtobj:=gprtobj;
  333. sysinitunit:=gsysinitunit;
  334. linklibc:=true;
  335. end
  336. else
  337. begin
  338. if linklibc then
  339. begin
  340. prtobj:=cprtobj;
  341. sysinitunit:=csysinitunit;
  342. end;
  343. end;
  344. sysinitunit:='si_'+sysinitunit;
  345. end;
  346. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  347. Var
  348. linkres : TLinkRes;
  349. i : longint;
  350. HPath : TCmdStrListItem;
  351. s,s1,s2 : TCmdStr;
  352. found1,
  353. found2 : boolean;
  354. begin
  355. result:=False;
  356. { set special options for some targets }
  357. if cs_profile in current_settings.moduleswitches then
  358. begin
  359. if not(libctype in [glibc2,glibc21]) then
  360. AddSharedLibrary('gmon');
  361. AddSharedLibrary('c');
  362. end;
  363. { Open link.res file }
  364. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  365. with linkres do
  366. begin
  367. { Write path to search libraries }
  368. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  369. while assigned(HPath) do
  370. begin
  371. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  372. HPath:=TCmdStrListItem(HPath.Next);
  373. end;
  374. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  375. while assigned(HPath) do
  376. begin
  377. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  378. HPath:=TCmdStrListItem(HPath.Next);
  379. end;
  380. StartSection('INPUT(');
  381. { add objectfiles, start with prt0 always }
  382. if not (target_info.system in system_internal_sysinit) and (prtobj<>'') then
  383. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  384. { try to add crti and crtbegin if linking to C }
  385. if linklibc and (libctype<>uclibc) then
  386. begin
  387. { x86_64 requires this to use entry/exit code with pic,
  388. see also issue #8210 regarding a discussion
  389. no idea about the other non i386 CPUs (FK)
  390. }
  391. {$ifdef x86_64}
  392. if current_module.islibrary then
  393. begin
  394. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  395. AddFileName(s);
  396. end
  397. else
  398. {$endif x86_64}
  399. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  400. AddFileName(s);
  401. if librarysearchpath.FindFile('crti.o',false,s) then
  402. AddFileName(s);
  403. end;
  404. { main objectfiles }
  405. while not ObjectFiles.Empty do
  406. begin
  407. s:=ObjectFiles.GetFirst;
  408. if s<>'' then
  409. AddFileName(maybequoted(s));
  410. end;
  411. EndSection(')');
  412. { Write staticlibraries }
  413. if not StaticLibFiles.Empty then
  414. begin
  415. Add('GROUP(');
  416. While not StaticLibFiles.Empty do
  417. begin
  418. S:=StaticLibFiles.GetFirst;
  419. AddFileName(maybequoted(s))
  420. end;
  421. Add(')');
  422. end;
  423. // we must reorder here because the result could empty sharedlibfiles
  424. if reorder Then
  425. ExpandAndApplyOrder(SharedLibFiles);
  426. // after this point addition of shared libs not allowed.
  427. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  428. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  429. if not SharedLibFiles.Empty then
  430. begin
  431. Add('INPUT(');
  432. While not SharedLibFiles.Empty do
  433. begin
  434. S:=SharedLibFiles.GetFirst;
  435. if (s<>'c') or reorder then
  436. begin
  437. i:=Pos(target_info.sharedlibext,S);
  438. if i>0 then
  439. Delete(S,i,255);
  440. Add('-l'+s);
  441. end
  442. else
  443. begin
  444. linklibc:=true;
  445. end;
  446. end;
  447. { be sure that libc is the last lib }
  448. if linklibc and not reorder then
  449. Add('-lc');
  450. { when we have -static for the linker the we also need libgcc }
  451. if (cs_link_staticflag in current_settings.globalswitches) then
  452. Add('-lgcc');
  453. Add(')');
  454. end;
  455. { objects which must be at the end }
  456. if linklibc and (libctype<>uclibc) then
  457. begin
  458. { x86_64 requires this to use entry/exit code with pic,
  459. see also issue #8210 regarding a discussion
  460. no idea about the other non i386 CPUs (FK)
  461. }
  462. {$ifdef x86_64}
  463. if current_module.islibrary then
  464. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  465. else
  466. {$endif x86_64}
  467. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  468. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  469. if found1 or found2 then
  470. begin
  471. Add('INPUT(');
  472. if found1 then
  473. AddFileName(s1);
  474. if found2 then
  475. AddFileName(s2);
  476. Add(')');
  477. end;
  478. end;
  479. {Entry point.}
  480. add('ENTRY(_start)');
  481. {$ifdef x86_64}
  482. {$define LINKERSCRIPT_INCLUDED}
  483. add('SECTIONS');
  484. add('{');
  485. {Read-only sections, merged into text segment:}
  486. if current_module.islibrary then
  487. add(' . = 0 + SIZEOF_HEADERS;')
  488. else
  489. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  490. add(' . = 0 + SIZEOF_HEADERS;');
  491. add(' .interp : { *(.interp) }');
  492. add(' .hash : { *(.hash) }');
  493. add(' .dynsym : { *(.dynsym) }');
  494. add(' .dynstr : { *(.dynstr) }');
  495. add(' .gnu.version : { *(.gnu.version) }');
  496. add(' .gnu.version_d : { *(.gnu.version_d) }');
  497. add(' .gnu.version_r : { *(.gnu.version_r) }');
  498. add(' .rel.dyn :');
  499. add(' {');
  500. add(' *(.rel.init)');
  501. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  502. add(' *(.rel.fini)');
  503. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  504. add(' *(.rel.data.rel.ro*)');
  505. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  506. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  507. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  508. add(' *(.rel.got)');
  509. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  510. add(' }');
  511. add(' .rela.dyn :');
  512. add(' {');
  513. add(' *(.rela.init)');
  514. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  515. add(' *(.rela.fini)');
  516. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  517. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  518. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  519. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  520. add(' *(.rela.got)');
  521. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  522. add(' }');
  523. add(' .rel.plt : { *(.rel.plt) }');
  524. add(' .rela.plt : { *(.rela.plt) }');
  525. add(' .init :');
  526. add(' {');
  527. add(' KEEP (*(.init))');
  528. add(' } =0x90909090');
  529. add(' .plt : { *(.plt) }');
  530. add(' .text :');
  531. add(' {');
  532. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  533. add(' KEEP (*(.text.*personality*))');
  534. {.gnu.warning sections are handled specially by elf32.em.}
  535. add(' *(.gnu.warning)');
  536. add(' } =0x90909090');
  537. add(' .fini :');
  538. add(' {');
  539. add(' KEEP (*(.fini))');
  540. add(' } =0x90909090');
  541. add(' PROVIDE (_etext = .);');
  542. add(' .rodata :');
  543. add(' {');
  544. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  545. add(' }');
  546. {Adjust the address for the data segment. We want to adjust up to
  547. the same address within the page on the next page up.}
  548. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  549. add(' .dynamic : { *(.dynamic) }');
  550. add(' .got : { *(.got .toc) }');
  551. add(' .got.plt : { *(.got.plt .toc.plt) }');
  552. add(' .data :');
  553. add(' {');
  554. add(' *(.data .data.* .gnu.linkonce.d.*)');
  555. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  556. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  557. add(' }');
  558. add(' PROVIDE (_edata = .);');
  559. add(' PROVIDE (edata = .);');
  560. {$ifdef zsegment_threadvars}
  561. add(' _z = .;');
  562. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  563. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  564. add(' . = _z + SIZEOF (.threadvar);');
  565. {$else}
  566. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  567. {$endif}
  568. add(' __bss_start = .;');
  569. add(' .bss :');
  570. add(' {');
  571. add(' *(.dynbss)');
  572. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  573. add(' *(COMMON)');
  574. {Align here to ensure that the .bss section occupies space up to
  575. _end. Align after .bss to ensure correct alignment even if the
  576. .bss section disappears because there are no input sections.}
  577. add(' . = ALIGN(32 / 8);');
  578. add(' }');
  579. add(' . = ALIGN(32 / 8);');
  580. add(' PROVIDE (_end = .);');
  581. add(' PROVIDE (end = .);');
  582. {Stabs debugging sections.}
  583. add(' .stab 0 : { *(.stab) }');
  584. add(' .stabstr 0 : { *(.stabstr) }');
  585. add(' /* DWARF debug sections.');
  586. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  587. add(' of the section so we begin them at 0. */');
  588. add(' /* DWARF 1 */');
  589. add(' .debug 0 : { *(.debug) }');
  590. add(' .line 0 : { *(.line) }');
  591. add(' /* GNU DWARF 1 extensions */');
  592. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  593. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  594. add(' /* DWARF 1.1 and DWARF 2 */');
  595. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  596. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  597. add(' /* DWARF 2 */');
  598. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  599. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  600. add(' .debug_line 0 : { *(.debug_line) }');
  601. add(' .debug_frame 0 : { *(.debug_frame) }');
  602. add(' .debug_str 0 : { *(.debug_str) }');
  603. add(' .debug_loc 0 : { *(.debug_loc) }');
  604. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  605. add(' /* SGI/MIPS DWARF 2 extensions */');
  606. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  607. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  608. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  609. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  610. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  611. add('}');
  612. {$endif x86_64}
  613. {$ifndef LINKERSCRIPT_INCLUDED}
  614. {Sections.}
  615. add('SECTIONS');
  616. add('{');
  617. {Read-only sections, merged into text segment:}
  618. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 +0x100;');
  619. add(' .interp : { *(.interp) }');
  620. add(' .hash : { *(.hash) }');
  621. add(' .dynsym : { *(.dynsym) }');
  622. add(' .dynstr : { *(.dynstr) }');
  623. add(' .gnu.version : { *(.gnu.version) }');
  624. add(' .gnu.version_d : { *(.gnu.version_d) }');
  625. add(' .gnu.version_r : { *(.gnu.version_r) }');
  626. add(' .rel.dyn :');
  627. add(' {');
  628. add(' *(.rel.init)');
  629. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  630. add(' *(.rel.fini)');
  631. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  632. add(' *(.rel.data.rel.ro*)');
  633. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  634. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  635. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  636. add(' *(.rel.got)');
  637. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  638. add(' }');
  639. add(' .rela.dyn :');
  640. add(' {');
  641. add(' *(.rela.init)');
  642. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  643. add(' *(.rela.fini)');
  644. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  645. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  646. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  647. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  648. add(' *(.rela.got)');
  649. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  650. add(' }');
  651. add(' .rel.plt : { *(.rel.plt) }');
  652. add(' .rela.plt : { *(.rela.plt) }');
  653. add(' .init :');
  654. add(' {');
  655. add(' KEEP (*(.init))');
  656. add(' } =0x90909090');
  657. add(' .plt : { *(.plt) }');
  658. add(' .text :');
  659. add(' {');
  660. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  661. add(' KEEP (*(.text.*personality*))');
  662. {.gnu.warning sections are handled specially by elf32.em.}
  663. add(' *(.gnu.warning)');
  664. add(' } =0x90909090');
  665. add(' .fini :');
  666. add(' {');
  667. add(' KEEP (*(.fini))');
  668. add(' } =0x90909090');
  669. add(' PROVIDE (_etext = .);');
  670. add(' .rodata :');
  671. add(' {');
  672. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  673. add(' }');
  674. {Adjust the address for the data segment. We want to adjust up to
  675. the same address within the page on the next page up.}
  676. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  677. add(' .dynamic : { *(.dynamic) }');
  678. add(' .got : { *(.got) }');
  679. add(' .got.plt : { *(.got.plt) }');
  680. add(' .data :');
  681. add(' {');
  682. add(' *(.data .data.* .gnu.linkonce.d.*)');
  683. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  684. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  685. add(' }');
  686. add(' PROVIDE (_edata = .);');
  687. add(' PROVIDE (edata = .);');
  688. {$ifdef zsegment_threadvars}
  689. add(' _z = .;');
  690. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  691. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  692. add(' . = _z + SIZEOF (.threadvar);');
  693. {$else}
  694. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  695. {$endif}
  696. add(' __bss_start = .;');
  697. add(' .bss :');
  698. add(' {');
  699. add(' *(.dynbss)');
  700. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  701. add(' *(COMMON)');
  702. {Align here to ensure that the .bss section occupies space up to
  703. _end. Align after .bss to ensure correct alignment even if the
  704. .bss section disappears because there are no input sections.}
  705. add(' . = ALIGN(32 / 8);');
  706. add(' }');
  707. add(' . = ALIGN(32 / 8);');
  708. add(' PROVIDE (_end = .);');
  709. add(' PROVIDE (end = .);');
  710. {Stabs debugging sections.}
  711. add(' .stab 0 : { *(.stab) }');
  712. add(' .stabstr 0 : { *(.stabstr) }');
  713. add('}');
  714. {$endif LINKERSCRIPT_INCLUDED}
  715. { Write and Close response }
  716. writetodisk;
  717. Free;
  718. end;
  719. WriteResponseFile:=True;
  720. end;
  721. function TLinkerLinux.MakeExecutable:boolean;
  722. var
  723. binstr,
  724. cmdstr : TCmdStr;
  725. success : boolean;
  726. DynLinkStr : string;
  727. GCSectionsStr,
  728. StaticStr,
  729. StripStr : string[40];
  730. begin
  731. if not(cs_link_nolink in current_settings.globalswitches) then
  732. Message1(exec_i_linking,current_module.exefilename^);
  733. { Create some replacements }
  734. StaticStr:='';
  735. StripStr:='';
  736. GCSectionsStr:='';
  737. DynLinkStr:='';
  738. if (cs_link_staticflag in current_settings.globalswitches) then
  739. StaticStr:='-static';
  740. if (cs_link_strip in current_settings.globalswitches) then
  741. StripStr:='-s';
  742. if (cs_link_map in current_settings.globalswitches) then
  743. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  744. if create_smartlink_sections then
  745. GCSectionsStr:='--gc-sections';
  746. If (cs_profile in current_settings.moduleswitches) or
  747. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  748. begin
  749. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  750. if cshared then
  751. DynLinkStr:=DynLinkStr+' --shared ';
  752. if rlinkpath<>'' then
  753. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  754. End;
  755. { Write used files and libraries }
  756. WriteResponseFile(false);
  757. { Call linker }
  758. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  759. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  760. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  761. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  762. Replace(cmdstr,'$STATIC',StaticStr);
  763. Replace(cmdstr,'$STRIP',StripStr);
  764. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  765. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  766. { create dynamic symbol table? }
  767. if HasExports then
  768. cmdstr:=cmdstr+' -E';
  769. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  770. { Remove ReponseFile }
  771. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  772. DeleteFile(outputexedir+Info.ResName);
  773. if (success) then
  774. success:=PostProcessExecutable(current_module.exefilename^,false);
  775. MakeExecutable:=success; { otherwise a recursive call to link method }
  776. end;
  777. Function TLinkerLinux.MakeSharedLibrary:boolean;
  778. var
  779. InitStr,
  780. FiniStr,
  781. SoNameStr : string[80];
  782. binstr,
  783. cmdstr : TCmdStr;
  784. success : boolean;
  785. begin
  786. MakeSharedLibrary:=false;
  787. if not(cs_link_nolink in current_settings.globalswitches) then
  788. Message1(exec_i_linking,current_module.sharedlibfilename^);
  789. { Write used files and libraries }
  790. WriteResponseFile(true);
  791. { Create some replacements }
  792. InitStr:='-init FPC_LIB_START';
  793. FiniStr:='-fini FPC_LIB_EXIT';
  794. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  795. { Call linker }
  796. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  797. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  798. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  799. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  800. Replace(cmdstr,'$INIT',InitStr);
  801. Replace(cmdstr,'$FINI',FiniStr);
  802. Replace(cmdstr,'$SONAME',SoNameStr);
  803. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  804. { Strip the library ? }
  805. if success and (cs_link_strip in current_settings.globalswitches) then
  806. begin
  807. { only remove non global symbols and debugging info for a library }
  808. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  809. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  810. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  811. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  812. end;
  813. { Remove ReponseFile }
  814. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  815. DeleteFile(outputexedir+Info.ResName);
  816. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  817. end;
  818. function tlinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  819. var
  820. cmdstr: string;
  821. begin
  822. result:=True;
  823. if HasResources and
  824. (target_res.id=res_elf) then
  825. begin
  826. cmdstr:=' -f -i '+maybequoted(fn);
  827. result:=DoExec(FindUtil(utilsprefix+'fpcres'),cmdstr,false,false);
  828. end;
  829. end;
  830. {*****************************************************************************
  831. Initialize
  832. *****************************************************************************}
  833. initialization
  834. {$ifdef i386}
  835. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  836. RegisterImport(system_i386_linux,timportliblinux);
  837. RegisterExport(system_i386_linux,texportliblinux);
  838. RegisterTarget(system_i386_linux_info);
  839. RegisterRes(res_elf32_info,TWinLikeResourceFile);
  840. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  841. RegisterImport(system_x86_6432_linux,timportliblinux);
  842. RegisterExport(system_x86_6432_linux,texportliblinux);
  843. RegisterTarget(system_x86_6432_linux_info);
  844. {$endif i386}
  845. {$ifdef m68k}
  846. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  847. RegisterImport(system_m68k_linux,timportliblinux);
  848. RegisterExport(system_m68k_linux,texportliblinux);
  849. RegisterTarget(system_m68k_linux_info);
  850. {$endif m68k}
  851. {$ifdef powerpc}
  852. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  853. RegisterImport(system_powerpc_linux,timportliblinux);
  854. RegisterExport(system_powerpc_linux,texportliblinux);
  855. RegisterTarget(system_powerpc_linux_info);
  856. {$endif powerpc}
  857. {$ifdef powerpc64}
  858. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  859. RegisterImport(system_powerpc64_linux,timportliblinux);
  860. RegisterExport(system_powerpc64_linux,texportliblinux);
  861. RegisterTarget(system_powerpc64_linux_info);
  862. {$endif powerpc64}
  863. {$ifdef alpha}
  864. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  865. RegisterImport(system_alpha_linux,timportliblinux);
  866. RegisterExport(system_alpha_linux,texportliblinux);
  867. RegisterTarget(system_alpha_linux_info);
  868. {$endif alpha}
  869. {$ifdef x86_64}
  870. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  871. RegisterImport(system_x86_64_linux,timportliblinux);
  872. RegisterExport(system_x86_64_linux,texportliblinux);
  873. RegisterTarget(system_x86_64_linux_info);
  874. RegisterRes(res_elf64_info,TWinLikeResourceFile);
  875. {$endif x86_64}
  876. {$ifdef SPARC}
  877. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  878. RegisterImport(system_SPARC_linux,timportliblinux);
  879. RegisterExport(system_SPARC_linux,texportliblinux);
  880. RegisterTarget(system_SPARC_linux_info);
  881. {$endif SPARC}
  882. {$ifdef ARM}
  883. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  884. RegisterImport(system_arm_linux,timportliblinux);
  885. RegisterExport(system_arm_linux,texportliblinux);
  886. RegisterTarget(system_arm_linux_info);
  887. {$endif ARM}
  888. end.