t_linux.pas 24 KB

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