t_linux.pas 24 KB

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