t_linux.pas 25 KB

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