t_linux.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. {$ifdef has_internal_sysinit}
  38. reorder : boolean;
  39. linklibc: boolean;
  40. {$endif}
  41. Function WriteResponseFile(isdll:boolean) : Boolean;
  42. public
  43. constructor Create;override;
  44. procedure SetDefaultInfo;override;
  45. {$ifdef has_internal_sysinit}
  46. procedure InitSysInitUnitName;override;
  47. {$endif}
  48. function MakeExecutable:boolean;override;
  49. function MakeSharedLibrary:boolean;override;
  50. function postprocessexecutable(const fn : string;isdll:boolean):boolean;
  51. procedure LoadPredefinedLibraryOrder; override;
  52. end;
  53. implementation
  54. uses
  55. SysUtils,
  56. cutils,cfileutils,cclasses,
  57. verbose,systems,globtype,globals,
  58. symconst,script,
  59. fmodule,
  60. aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,
  61. cgbase,cgobj,cgutils,ogbase,
  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. {$ifdef x86}
  128. sym : tasmsymbol;
  129. r : treference;
  130. {$endif x86}
  131. begin
  132. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  133. hp2:=texported_item(current_module._exports.first);
  134. while assigned(hp2) do
  135. begin
  136. if (not hp2.is_var) and
  137. (hp2.sym.typ=procsym) then
  138. begin
  139. { the manglednames can already be the same when the procedure
  140. is declared with cdecl }
  141. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  142. begin
  143. { place jump in al_procedures }
  144. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  145. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  146. if (cs_create_pic in current_settings.moduleswitches) and
  147. { other targets need to be checked how it works }
  148. (target_info.system in [system_x86_64_linux,system_i386_linux]) then
  149. begin
  150. {$ifdef x86}
  151. sym:=current_asmdata.RefAsmSymbol(tprocsym(hp2.sym).first_procdef.mangledname);
  152. reference_reset_symbol(r,sym,0);
  153. if cs_create_pic in current_settings.moduleswitches then
  154. r.refaddr:=addr_pic
  155. else
  156. r.refaddr:=addr_full;
  157. current_asmdata.asmlists[al_procedures].concat(taicpu.op_ref(A_JMP,S_NO,r));
  158. {$endif x86}
  159. end
  160. else
  161. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],tprocsym(hp2.sym).first_procdef.mangledname);
  162. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  163. end;
  164. end
  165. else
  166. message1(parser_e_no_export_of_variables_for_target,'linux');
  167. hp2:=texported_item(hp2.next);
  168. end;
  169. end;
  170. {*****************************************************************************
  171. TLINKERLINUX
  172. *****************************************************************************}
  173. Constructor TLinkerLinux.Create;
  174. begin
  175. Inherited Create;
  176. if not Dontlinkstdlibpath Then
  177. {$ifdef x86_64}
  178. LibrarySearchPath.AddPath('/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  179. {$else}
  180. {$ifdef powerpc64}
  181. LibrarySearchPath.AddPath('/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  182. {$else powerpc64}
  183. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  184. {$endif powerpc64}
  185. {$endif x86_64}
  186. end;
  187. procedure TLinkerLinux.SetDefaultInfo;
  188. {
  189. This will also detect which libc version will be used
  190. }
  191. const
  192. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  193. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  194. {$ifdef powerpc}platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  195. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  196. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  197. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  198. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  199. {$ifdef m68k}
  200. var
  201. St : TSearchRec;
  202. {$endif m68k}
  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. libctype:=glibc2;
  211. if FindFirst('/lib/ld*',faAnyFile+faSymLink,st)<>0 then
  212. begin
  213. repeat
  214. if copy(st.name,1,5)='ld-2.' then
  215. begin
  216. DynamicLinker:='/lib/'+St.name;
  217. if st.name[6]<>'0' then
  218. libctype:=glibc21;
  219. break;
  220. end;
  221. until FindNext(St)<>0;
  222. end;
  223. FindClose(St);
  224. {$endif m68k}
  225. {$ifdef i386}
  226. { default is glibc 2.1+ compatible }
  227. libctype:=glibc21;
  228. if FileExists('/lib/ld-linux.so.2',false) then
  229. DynamicLinker:='/lib/ld-linux.so.2'
  230. else if fileexists('/lib/ld-uClibc.so.0',false) then
  231. begin
  232. dynamiclinker:='/lib/ld-uClibc.so.0';
  233. libctype:=uclibc;
  234. end
  235. else if fileexists('/lib/ld-linux.so.1',false) then
  236. begin
  237. DynamicLinker:='/lib/ld-linux.so.1';
  238. libctype:=glibc2;
  239. end;
  240. {$endif i386}
  241. {$ifdef x86_64}
  242. DynamicLinker:='/lib64/ld-linux-x86-64.so.2';
  243. libctype:=glibc2;
  244. {$endif x86_64}
  245. {$ifdef sparc}
  246. DynamicLinker:='/lib/ld-linux.so.2';
  247. libctype:=glibc2;
  248. {$endif sparc}
  249. {$ifdef powerpc}
  250. DynamicLinker:='/lib/ld.so.1';
  251. libctype:=glibc2;
  252. {$endif powerpc}
  253. {$ifdef powerpc64}
  254. DynamicLinker:='/lib64/ld64.so.1';
  255. libctype:=glibc2;
  256. {$endif powerpc64}
  257. {$ifdef arm}
  258. DynamicLinker:='/lib/ld-linux.so.2';
  259. libctype:=glibc2;
  260. {$endif arm}
  261. end;
  262. end;
  263. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  264. // put your linkorder/linkalias overrides here.
  265. // Note: assumes only called when reordering/aliasing is used.
  266. Begin
  267. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  268. Begin
  269. LinkLibraryOrder.add('gcc','',15);
  270. LinkLibraryOrder.add('c','',100);
  271. LinkLibraryOrder.add('gmon','',120);
  272. LinkLibraryOrder.add('dl','',140);
  273. LinkLibraryOrder.add('pthread','',160);
  274. end;
  275. End;
  276. {$ifdef has_internal_sysinit}
  277. Procedure TLinkerLinux.InitSysInitUnitName;
  278. var
  279. csysinitunit,
  280. gsysinitunit : string[20];
  281. hp : tmodule;
  282. begin
  283. hp:=tmodule(loaded_units.first);
  284. while assigned(hp) do
  285. begin
  286. linklibc := hp.linkunitsharedlibs.find('c');
  287. if linklibc then break;
  288. hp:=tmodule(hp.next);
  289. end;
  290. reorder := linklibc and ReOrderEntries;
  291. if islibrary then
  292. begin
  293. sysinitunit:='dll';
  294. csysinitunit:='dll';
  295. gsysinitunit:='dll';
  296. end
  297. else
  298. begin
  299. sysinitunit:='prc';
  300. case libctype of
  301. glibc21:
  302. begin
  303. csysinitunit:='c21';
  304. gsysinitunit:='c21g';
  305. end;
  306. uclibc:
  307. begin
  308. csysinitunit:='uc';
  309. gsysinitunit:='ucg';
  310. end
  311. else
  312. csysinitunit:='c';
  313. gsysinitunit:='g';
  314. end;
  315. end;
  316. if cs_profile in current_settings.moduleswitches then
  317. begin
  318. sysinitunit:=gsysinitunit;
  319. linklibc:=true;
  320. end
  321. else
  322. begin
  323. if linklibc then
  324. sysinitunit:=csysinitunit;
  325. end;
  326. end;
  327. {$endif}
  328. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  329. Var
  330. linkres : TLinkRes;
  331. i : longint;
  332. {$ifndef has_internal_sysinit}
  333. cprtobj,
  334. gprtobj,
  335. prtobj : string[80];
  336. reorder,
  337. linklibc : boolean;
  338. {$endif}
  339. HPath : TStringListItem;
  340. s,s1,s2 : string;
  341. found1,
  342. found2 : boolean;
  343. begin
  344. result:=False;
  345. { set special options for some targets }
  346. {$ifndef has_internal_sysinit}
  347. linklibc:=(SharedLibFiles.Find('c')<>nil);
  348. reorder := linklibc and ReOrderEntries;
  349. if isdll then
  350. begin
  351. prtobj:='dllprt0';
  352. cprtobj:='dllprt0';
  353. gprtobj:='dllprt0';
  354. end
  355. else
  356. begin
  357. prtobj:='prt0';
  358. case libctype of
  359. glibc21:
  360. begin
  361. cprtobj:='cprt21';
  362. gprtobj:='gprt21';
  363. end;
  364. uclibc:
  365. begin
  366. cprtobj:='ucprt0';
  367. gprtobj:='ugprt0';
  368. end
  369. else
  370. cprtobj:='cprt0';
  371. gprtobj:='gprt0';
  372. end;
  373. end;
  374. {$endif}
  375. if cs_profile in current_settings.moduleswitches then
  376. begin
  377. {$ifndef has_internal_sysinit}
  378. prtobj:=gprtobj;
  379. {$endif}
  380. if not(libctype in [glibc2,glibc21]) then
  381. AddSharedLibrary('gmon');
  382. AddSharedLibrary('c');
  383. linklibc:=true;
  384. end
  385. else
  386. begin
  387. {$ifndef has_internal_sysinit}
  388. if linklibc then
  389. prtobj:=cprtobj;
  390. {$endif}
  391. end;
  392. { Open link.res file }
  393. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  394. with linkres do
  395. begin
  396. { Write path to search libraries }
  397. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  398. while assigned(HPath) do
  399. begin
  400. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  401. HPath:=TStringListItem(HPath.Next);
  402. end;
  403. HPath:=TStringListItem(LibrarySearchPath.First);
  404. while assigned(HPath) do
  405. begin
  406. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  407. HPath:=TStringListItem(HPath.Next);
  408. end;
  409. StartSection('INPUT(');
  410. {$ifndef has_internal_sysinit}
  411. { add objectfiles, start with prt0 always }
  412. if prtobj<>'' then
  413. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  414. {$endif}
  415. { try to add crti and crtbegin if linking to C }
  416. if linklibc then
  417. begin
  418. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  419. AddFileName(s);
  420. if librarysearchpath.FindFile('crti.o',false,s) then
  421. AddFileName(s);
  422. end;
  423. { main objectfiles }
  424. while not ObjectFiles.Empty do
  425. begin
  426. s:=ObjectFiles.GetFirst;
  427. if s<>'' then
  428. AddFileName(maybequoted(s));
  429. end;
  430. EndSection(')');
  431. { Write staticlibraries }
  432. if not StaticLibFiles.Empty then
  433. begin
  434. Add('GROUP(');
  435. While not StaticLibFiles.Empty do
  436. begin
  437. S:=StaticLibFiles.GetFirst;
  438. AddFileName(maybequoted(s))
  439. end;
  440. Add(')');
  441. end;
  442. // we must reorder here because the result could empty sharedlibfiles
  443. if reorder Then
  444. ExpandAndApplyOrder(SharedLibFiles);
  445. // after this point addition of shared libs not allowed.
  446. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  447. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  448. if not SharedLibFiles.Empty then
  449. begin
  450. Add('INPUT(');
  451. While not SharedLibFiles.Empty do
  452. begin
  453. S:=SharedLibFiles.GetFirst;
  454. if (s<>'c') or reorder then
  455. begin
  456. i:=Pos(target_info.sharedlibext,S);
  457. if i>0 then
  458. Delete(S,i,255);
  459. Add('-l'+s);
  460. end
  461. else
  462. begin
  463. linklibc:=true;
  464. end;
  465. end;
  466. { be sure that libc is the last lib }
  467. if linklibc and not reorder then
  468. Add('-lc');
  469. { when we have -static for the linker the we also need libgcc }
  470. if (cs_link_staticflag in current_settings.globalswitches) then
  471. Add('-lgcc');
  472. Add(')');
  473. end;
  474. { objects which must be at the end }
  475. if linklibc and (libctype<>uclibc) then
  476. begin
  477. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  478. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  479. if found1 or found2 then
  480. begin
  481. Add('INPUT(');
  482. if found1 then
  483. AddFileName(s1);
  484. if found2 then
  485. AddFileName(s2);
  486. Add(')');
  487. end;
  488. end;
  489. {Entry point.}
  490. add('ENTRY(_start)');
  491. {Sections.}
  492. add('SECTIONS');
  493. add('{');
  494. {Read-only sections, merged into text segment:}
  495. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 +0x100;');
  496. add(' .interp : { *(.interp) }');
  497. add(' .hash : { *(.hash) }');
  498. add(' .dynsym : { *(.dynsym) }');
  499. add(' .dynstr : { *(.dynstr) }');
  500. add(' .gnu.version : { *(.gnu.version) }');
  501. add(' .gnu.version_d : { *(.gnu.version_d) }');
  502. add(' .gnu.version_r : { *(.gnu.version_r) }');
  503. add(' .rel.dyn :');
  504. add(' {');
  505. add(' *(.rel.init)');
  506. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  507. add(' *(.rel.fini)');
  508. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  509. add(' *(.rel.data.rel.ro*)');
  510. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  511. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  512. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  513. add(' *(.rel.got)');
  514. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  515. add(' }');
  516. add(' .rela.dyn :');
  517. add(' {');
  518. add(' *(.rela.init)');
  519. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  520. add(' *(.rela.fini)');
  521. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  522. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  523. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  524. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  525. add(' *(.rela.got)');
  526. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  527. add(' }');
  528. add(' .rel.plt : { *(.rel.plt) }');
  529. add(' .rela.plt : { *(.rela.plt) }');
  530. add(' .init :');
  531. add(' {');
  532. add(' KEEP (*(.init))');
  533. add(' } =0x90909090');
  534. add(' .plt : { *(.plt) }');
  535. add(' .text :');
  536. add(' {');
  537. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  538. add(' KEEP (*(.text.*personality*))');
  539. {.gnu.warning sections are handled specially by elf32.em.}
  540. add(' *(.gnu.warning)');
  541. add(' } =0x90909090');
  542. add(' .fini :');
  543. add(' {');
  544. add(' KEEP (*(.fini))');
  545. add(' } =0x90909090');
  546. add(' PROVIDE (_etext = .);');
  547. add(' .rodata :');
  548. add(' {');
  549. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  550. add(' }');
  551. {Adjust the address for the data segment. We want to adjust up to
  552. the same address within the page on the next page up.}
  553. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  554. add(' .dynamic : { *(.dynamic) }');
  555. add(' .got : { *(.got) }');
  556. add(' .got.plt : { *(.got.plt) }');
  557. add(' .data :');
  558. add(' {');
  559. add(' *(.data .data.* .gnu.linkonce.d.*)');
  560. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  561. add(' }');
  562. add(' _edata = .;');
  563. add(' PROVIDE (edata = .);');
  564. {$ifdef zsegment_threadvars}
  565. add(' _z = .;');
  566. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  567. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  568. add(' . = _z + SIZEOF (.threadvar);');
  569. {$else}
  570. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  571. {$endif}
  572. add(' __bss_start = .;');
  573. add(' .bss :');
  574. add(' {');
  575. add(' *(.dynbss)');
  576. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  577. add(' *(COMMON)');
  578. {Align here to ensure that the .bss section occupies space up to
  579. _end. Align after .bss to ensure correct alignment even if the
  580. .bss section disappears because there are no input sections.}
  581. add(' . = ALIGN(32 / 8);');
  582. add(' }');
  583. add(' . = ALIGN(32 / 8);');
  584. add(' _end = .;');
  585. add(' PROVIDE (end = .);');
  586. {Stabs debugging sections.}
  587. add(' .stab 0 : { *(.stab) }');
  588. add(' .stabstr 0 : { *(.stabstr) }');
  589. add('}');
  590. { Write and Close response }
  591. writetodisk;
  592. Free;
  593. end;
  594. WriteResponseFile:=True;
  595. end;
  596. function TLinkerLinux.MakeExecutable:boolean;
  597. var
  598. binstr : String;
  599. cmdstr : TCmdStr;
  600. success : boolean;
  601. DynLinkStr : string[60];
  602. GCSectionsStr,
  603. StaticStr,
  604. StripStr : string[40];
  605. begin
  606. if not(cs_link_nolink in current_settings.globalswitches) then
  607. Message1(exec_i_linking,current_module.exefilename^);
  608. { Create some replacements }
  609. StaticStr:='';
  610. StripStr:='';
  611. GCSectionsStr:='';
  612. DynLinkStr:='';
  613. if (cs_link_staticflag in current_settings.globalswitches) then
  614. StaticStr:='-static';
  615. if (cs_link_strip in current_settings.globalswitches) then
  616. StripStr:='-s';
  617. if (cs_link_map in current_settings.globalswitches) then
  618. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  619. if use_smartlink_section then
  620. GCSectionsStr:='--gc-sections';
  621. If (cs_profile in current_settings.moduleswitches) or
  622. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  623. begin
  624. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  625. if cshared Then
  626. DynLinkStr:='--shared ' + DynLinkStr;
  627. if rlinkpath<>'' Then
  628. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  629. End;
  630. { Write used files and libraries }
  631. WriteResponseFile(false);
  632. { Call linker }
  633. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  634. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  635. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  636. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  637. Replace(cmdstr,'$STATIC',StaticStr);
  638. Replace(cmdstr,'$STRIP',StripStr);
  639. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  640. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  641. { create dynamic symbol table? }
  642. if HasExports then
  643. cmdstr:=cmdstr+' -E';
  644. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  645. { Remove ReponseFile }
  646. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  647. DeleteFile(outputexedir+Info.ResName);
  648. if (success) then
  649. success:=PostProcessExecutable(current_module.exefilename^,false);
  650. MakeExecutable:=success; { otherwise a recursive call to link method }
  651. end;
  652. Function TLinkerLinux.MakeSharedLibrary:boolean;
  653. var
  654. InitStr,
  655. FiniStr,
  656. SoNameStr : string[80];
  657. binstr : String;
  658. cmdstr : TCmdStr;
  659. success : boolean;
  660. begin
  661. MakeSharedLibrary:=false;
  662. if not(cs_link_nolink in current_settings.globalswitches) then
  663. Message1(exec_i_linking,current_module.sharedlibfilename^);
  664. { Write used files and libraries }
  665. WriteResponseFile(true);
  666. { Create some replacements }
  667. InitStr:='-init FPC_LIB_START';
  668. FiniStr:='-fini FPC_LIB_EXIT';
  669. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  670. { Call linker }
  671. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  672. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  673. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  674. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  675. Replace(cmdstr,'$INIT',InitStr);
  676. Replace(cmdstr,'$FINI',FiniStr);
  677. Replace(cmdstr,'$SONAME',SoNameStr);
  678. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  679. { Strip the library ? }
  680. if success and (cs_link_strip in current_settings.globalswitches) then
  681. begin
  682. { only remove non global symbols and debugging info for a library }
  683. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  684. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  685. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  686. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  687. end;
  688. { Remove ReponseFile }
  689. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  690. DeleteFile(outputexedir+Info.ResName);
  691. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  692. end;
  693. function tlinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  694. var
  695. cmdstr: string;
  696. begin
  697. result:=True;
  698. if HasResources and
  699. (target_res.id=res_elf) then
  700. begin
  701. cmdstr:=' -f -i '+maybequoted(fn);
  702. result:=DoExec(FindUtil(utilsprefix+'fpcres'),cmdstr,false,false);
  703. end;
  704. end;
  705. {*****************************************************************************
  706. Initialize
  707. *****************************************************************************}
  708. initialization
  709. {$ifdef i386}
  710. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  711. RegisterImport(system_i386_linux,timportliblinux);
  712. RegisterExport(system_i386_linux,texportliblinux);
  713. RegisterTarget(system_i386_linux_info);
  714. RegisterRes(res_elf32_info);
  715. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  716. RegisterImport(system_x86_6432_linux,timportliblinux);
  717. RegisterExport(system_x86_6432_linux,texportliblinux);
  718. RegisterTarget(system_x86_6432_linux_info);
  719. {$endif i386}
  720. {$ifdef m68k}
  721. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  722. RegisterImport(system_m68k_linux,timportliblinux);
  723. RegisterExport(system_m68k_linux,texportliblinux);
  724. RegisterTarget(system_m68k_linux_info);
  725. {$endif m68k}
  726. {$ifdef powerpc}
  727. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  728. RegisterImport(system_powerpc_linux,timportliblinux);
  729. RegisterExport(system_powerpc_linux,texportliblinux);
  730. RegisterTarget(system_powerpc_linux_info);
  731. {$endif powerpc}
  732. {$ifdef powerpc64}
  733. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  734. RegisterImport(system_powerpc64_linux,timportliblinux);
  735. RegisterExport(system_powerpc64_linux,texportliblinux);
  736. RegisterTarget(system_powerpc64_linux_info);
  737. {$endif powerpc64}
  738. {$ifdef alpha}
  739. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  740. RegisterImport(system_alpha_linux,timportliblinux);
  741. RegisterExport(system_alpha_linux,texportliblinux);
  742. RegisterTarget(system_alpha_linux_info);
  743. {$endif alpha}
  744. {$ifdef x86_64}
  745. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  746. RegisterImport(system_x86_64_linux,timportliblinux);
  747. RegisterExport(system_x86_64_linux,texportliblinux);
  748. RegisterTarget(system_x86_64_linux_info);
  749. RegisterRes(res_elf64_info);
  750. {$endif x86_64}
  751. {$ifdef SPARC}
  752. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  753. RegisterImport(system_SPARC_linux,timportliblinux);
  754. RegisterExport(system_SPARC_linux,texportliblinux);
  755. RegisterTarget(system_SPARC_linux_info);
  756. {$endif SPARC}
  757. {$ifdef ARM}
  758. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  759. RegisterImport(system_arm_linux,timportliblinux);
  760. RegisterExport(system_arm_linux,texportliblinux);
  761. RegisterTarget(system_arm_linux_info);
  762. {$endif ARM}
  763. end.