t_linux.pas 32 KB

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