t_linux.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 : SearchRec;
  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. FindFirst('/lib/ld*',AnyFile,st);
  205. while DosError=0 do
  206. begin
  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. FindNext(St);
  215. end;
  216. FindClose(St);
  217. {$endif m68k}
  218. {$ifdef i386}
  219. { first try glibc2 }
  220. DynamicLinker:='/lib/ld-linux.so.2';
  221. if FileExists(DynamicLinker) then
  222. { Check for 2.0 files, else use the glibc 2.1 stub }
  223. if FileExists('/lib/ld-2.0.*') then
  224. libctype:=glibc2
  225. else
  226. libctype:=glibc21
  227. else
  228. if fileexists('/lib/ld-uClibc.so.0') then
  229. begin
  230. libctype:=uclibc;
  231. dynamiclinker:='/lib/ld-uClibc.so.0';
  232. end
  233. else if fileexists('/lib/ld-linux.so.1') then
  234. DynamicLinker:='/lib/ld-linux.so.1'
  235. else
  236. libctype:=glibc21;
  237. {$endif i386}
  238. {$ifdef x86_64}
  239. DynamicLinker:='/lib64/ld-linux-x86-64.so.2';
  240. libctype:=glibc2;
  241. {$endif x86_64}
  242. {$ifdef sparc}
  243. DynamicLinker:='/lib/ld-linux.so.2';
  244. libctype:=glibc2;
  245. {$endif sparc}
  246. {$ifdef powerpc}
  247. DynamicLinker:='/lib/ld.so.1';
  248. libctype:=glibc2;
  249. {$endif powerpc}
  250. {$ifdef powerpc64}
  251. DynamicLinker:='/lib64/ld64.so.1';
  252. libctype:=glibc2;
  253. {$endif powerpc64}
  254. {$ifdef arm}
  255. DynamicLinker:='/lib/ld-linux.so.2';
  256. libctype:=glibc2;
  257. {$endif arm}
  258. end;
  259. end;
  260. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  261. // put your linkorder/linkalias overrides here.
  262. // Note: assumes only called when reordering/aliasing is used.
  263. Begin
  264. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  265. Begin
  266. LinkLibraryOrder.add('gcc','',15);
  267. LinkLibraryOrder.add('c','',100);
  268. LinkLibraryOrder.add('gmon','',120);
  269. LinkLibraryOrder.add('dl','',140);
  270. LinkLibraryOrder.add('pthread','',160);
  271. end;
  272. End;
  273. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  274. Var
  275. linkres : TLinkRes;
  276. i : longint;
  277. cprtobj,
  278. gprtobj,
  279. prtobj : string[80];
  280. HPath : TStringListItem;
  281. s,s1,s2 : string;
  282. found1,
  283. found2,
  284. Reorder,
  285. linklibc : boolean;
  286. begin
  287. result:=False;
  288. { set special options for some targets }
  289. linklibc:=(SharedLibFiles.Find('c')<>nil);
  290. reorder := linklibc and ReOrderEntries;
  291. if isdll then
  292. begin
  293. prtobj:='dllprt0';
  294. cprtobj:='dllprt0';
  295. gprtobj:='dllprt0';
  296. end
  297. else
  298. begin
  299. prtobj:='prt0';
  300. case libctype of
  301. glibc21:
  302. begin
  303. cprtobj:='cprt21';
  304. gprtobj:='gprt21';
  305. end;
  306. uclibc:
  307. begin
  308. cprtobj:='ucprt0';
  309. gprtobj:='ugprt0';
  310. end
  311. else
  312. cprtobj:='cprt0';
  313. gprtobj:='gprt0';
  314. end;
  315. end;
  316. if cs_profile in current_settings.moduleswitches then
  317. begin
  318. prtobj:=gprtobj;
  319. if not(libctype in [glibc2,glibc21]) then
  320. AddSharedLibrary('gmon');
  321. AddSharedLibrary('c');
  322. linklibc:=true;
  323. end
  324. else
  325. begin
  326. if linklibc then
  327. prtobj:=cprtobj;
  328. end;
  329. { Open link.res file }
  330. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  331. with linkres do
  332. begin
  333. { Write path to search libraries }
  334. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  335. while assigned(HPath) do
  336. begin
  337. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  338. HPath:=TStringListItem(HPath.Next);
  339. end;
  340. HPath:=TStringListItem(LibrarySearchPath.First);
  341. while assigned(HPath) do
  342. begin
  343. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  344. HPath:=TStringListItem(HPath.Next);
  345. end;
  346. Add('INPUT(');
  347. { add objectfiles, start with prt0 always }
  348. if prtobj<>'' then
  349. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  350. { try to add crti and crtbegin if linking to C }
  351. if linklibc then
  352. begin
  353. if librarysearchpath.FindFile('crtbegin.o',s) then
  354. AddFileName(s);
  355. if librarysearchpath.FindFile('crti.o',s) then
  356. AddFileName(s);
  357. end;
  358. { main objectfiles }
  359. while not ObjectFiles.Empty do
  360. begin
  361. s:=ObjectFiles.GetFirst;
  362. if s<>'' then
  363. AddFileName(maybequoted(s));
  364. end;
  365. Add(')');
  366. { Write staticlibraries }
  367. if not StaticLibFiles.Empty then
  368. begin
  369. Add('GROUP(');
  370. While not StaticLibFiles.Empty do
  371. begin
  372. S:=StaticLibFiles.GetFirst;
  373. AddFileName(maybequoted(s))
  374. end;
  375. Add(')');
  376. end;
  377. // we must reorder here because the result could empty sharedlibfiles
  378. if reorder Then
  379. ExpandAndApplyOrder(SharedLibFiles);
  380. // after this point addition of shared libs not allowed.
  381. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  382. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  383. if not SharedLibFiles.Empty then
  384. begin
  385. Add('INPUT(');
  386. While not SharedLibFiles.Empty do
  387. begin
  388. S:=SharedLibFiles.GetFirst;
  389. if (s<>'c') or reorder then
  390. begin
  391. i:=Pos(target_info.sharedlibext,S);
  392. if i>0 then
  393. Delete(S,i,255);
  394. Add('-l'+s);
  395. end
  396. else
  397. begin
  398. linklibc:=true;
  399. end;
  400. end;
  401. { be sure that libc is the last lib }
  402. if linklibc and not reorder then
  403. Add('-lc');
  404. { when we have -static for the linker the we also need libgcc }
  405. if (cs_link_staticflag in current_settings.globalswitches) then
  406. Add('-lgcc');
  407. Add(')');
  408. end;
  409. { objects which must be at the end }
  410. if linklibc and (libctype<>uclibc) then
  411. begin
  412. found1:=librarysearchpath.FindFile('crtend.o',s1);
  413. found2:=librarysearchpath.FindFile('crtn.o',s2);
  414. if found1 or found2 then
  415. begin
  416. Add('INPUT(');
  417. if found1 then
  418. AddFileName(s1);
  419. if found2 then
  420. AddFileName(s2);
  421. Add(')');
  422. end;
  423. end;
  424. {Entry point.}
  425. add('ENTRY(_start)');
  426. {Sections.}
  427. add('SECTIONS');
  428. add('{');
  429. {Read-only sections, merged into text segment:}
  430. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 +0x100;');
  431. add(' .interp : { *(.interp) }');
  432. add(' .hash : { *(.hash) }');
  433. add(' .dynsym : { *(.dynsym) }');
  434. add(' .dynstr : { *(.dynstr) }');
  435. add(' .gnu.version : { *(.gnu.version) }');
  436. add(' .gnu.version_d : { *(.gnu.version_d) }');
  437. add(' .gnu.version_r : { *(.gnu.version_r) }');
  438. add(' .rel.dyn :');
  439. add(' {');
  440. add(' *(.rel.init)');
  441. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  442. add(' *(.rel.fini)');
  443. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  444. add(' *(.rel.data.rel.ro*)');
  445. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  446. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  447. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  448. add(' *(.rel.got)');
  449. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  450. add(' }');
  451. add(' .rela.dyn :');
  452. add(' {');
  453. add(' *(.rela.init)');
  454. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  455. add(' *(.rela.fini)');
  456. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  457. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  458. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  459. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  460. add(' *(.rela.got)');
  461. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  462. add(' }');
  463. add(' .rel.plt : { *(.rel.plt) }');
  464. add(' .rela.plt : { *(.rela.plt) }');
  465. add(' .init :');
  466. add(' {');
  467. add(' KEEP (*(.init))');
  468. add(' } =0x90909090');
  469. add(' .plt : { *(.plt) }');
  470. add(' .text :');
  471. add(' {');
  472. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  473. add(' KEEP (*(.text.*personality*))');
  474. {.gnu.warning sections are handled specially by elf32.em.}
  475. add(' *(.gnu.warning)');
  476. add(' } =0x90909090');
  477. add(' .fini :');
  478. add(' {');
  479. add(' KEEP (*(.fini))');
  480. add(' } =0x90909090');
  481. add(' PROVIDE (_etext = .);');
  482. add(' .rodata :');
  483. add(' {');
  484. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  485. add(' }');
  486. {Adjust the address for the data segment. We want to adjust up to
  487. the same address within the page on the next page up.}
  488. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  489. add(' .dynamic : { *(.dynamic) }');
  490. add(' .got : { *(.got) }');
  491. add(' .got.plt : { *(.got.plt) }');
  492. add(' .data :');
  493. add(' {');
  494. add(' *(.data .data.* .gnu.linkonce.d.*)');
  495. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  496. add(' }');
  497. add(' _edata = .;');
  498. add(' PROVIDE (edata = .);');
  499. {$ifdef zsegment_threadvars}
  500. add(' _z = .;');
  501. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  502. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  503. add(' . = _z + SIZEOF (.threadvar);');
  504. {$else}
  505. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  506. {$endif}
  507. add(' __bss_start = .;');
  508. add(' .bss :');
  509. add(' {');
  510. add(' *(.dynbss)');
  511. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  512. add(' *(COMMON)');
  513. {Align here to ensure that the .bss section occupies space up to
  514. _end. Align after .bss to ensure correct alignment even if the
  515. .bss section disappears because there are no input sections.}
  516. add(' . = ALIGN(32 / 8);');
  517. add(' }');
  518. add(' . = ALIGN(32 / 8);');
  519. add(' _end = .;');
  520. add(' PROVIDE (end = .);');
  521. {Stabs debugging sections.}
  522. add(' .stab 0 : { *(.stab) }');
  523. add(' .stabstr 0 : { *(.stabstr) }');
  524. add('}');
  525. { Write and Close response }
  526. writetodisk;
  527. Free;
  528. end;
  529. WriteResponseFile:=True;
  530. end;
  531. function TLinkerLinux.MakeExecutable:boolean;
  532. var
  533. binstr : String;
  534. cmdstr : TCmdStr;
  535. success : boolean;
  536. DynLinkStr : string[60];
  537. GCSectionsStr,
  538. StaticStr,
  539. StripStr : string[40];
  540. begin
  541. if not(cs_link_nolink in current_settings.globalswitches) then
  542. Message1(exec_i_linking,current_module.exefilename^);
  543. { Create some replacements }
  544. StaticStr:='';
  545. StripStr:='';
  546. GCSectionsStr:='';
  547. DynLinkStr:='';
  548. if (cs_link_staticflag in current_settings.globalswitches) then
  549. StaticStr:='-static';
  550. if (cs_link_strip in current_settings.globalswitches) then
  551. StripStr:='-s';
  552. if (cs_link_map in current_settings.globalswitches) then
  553. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  554. if use_smartlink_section then
  555. GCSectionsStr:='--gc-sections';
  556. If (cs_profile in current_settings.moduleswitches) or
  557. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  558. begin
  559. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  560. if cshared Then
  561. DynLinkStr:='--shared ' + DynLinkStr;
  562. if rlinkpath<>'' Then
  563. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  564. End;
  565. { Write used files and libraries }
  566. WriteResponseFile(false);
  567. { Call linker }
  568. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  569. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  570. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  571. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  572. Replace(cmdstr,'$STATIC',StaticStr);
  573. Replace(cmdstr,'$STRIP',StripStr);
  574. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  575. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  576. { create dynamic symbol table? }
  577. if HasExports then
  578. cmdstr:=cmdstr+' -E';
  579. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  580. { Remove ReponseFile }
  581. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  582. DeleteFile(outputexedir+Info.ResName);
  583. if (success) then
  584. success:=PostProcessExecutable(current_module.exefilename^,false);
  585. MakeExecutable:=success; { otherwise a recursive call to link method }
  586. end;
  587. Function TLinkerLinux.MakeSharedLibrary:boolean;
  588. var
  589. InitStr,
  590. FiniStr,
  591. SoNameStr : string[80];
  592. binstr : String;
  593. cmdstr : TCmdStr;
  594. success : boolean;
  595. begin
  596. MakeSharedLibrary:=false;
  597. if not(cs_link_nolink in current_settings.globalswitches) then
  598. Message1(exec_i_linking,current_module.sharedlibfilename^);
  599. { Write used files and libraries }
  600. WriteResponseFile(true);
  601. { Create some replacements }
  602. InitStr:='-init FPC_LIB_START';
  603. FiniStr:='-fini FPC_LIB_EXIT';
  604. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  605. { Call linker }
  606. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  607. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  608. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  609. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  610. Replace(cmdstr,'$INIT',InitStr);
  611. Replace(cmdstr,'$FINI',FiniStr);
  612. Replace(cmdstr,'$SONAME',SoNameStr);
  613. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  614. { Strip the library ? }
  615. if success and (cs_link_strip in current_settings.globalswitches) then
  616. begin
  617. { only remove non global symbols and debugging info for a library }
  618. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  619. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  620. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  621. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  622. end;
  623. { Remove ReponseFile }
  624. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  625. DeleteFile(outputexedir+Info.ResName);
  626. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  627. end;
  628. function tlinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  629. var
  630. cmdstr: string;
  631. begin
  632. result:=True;
  633. if HasResources and
  634. (target_res.id=res_elf) then
  635. begin
  636. cmdstr:=' -f -i '+maybequoted(fn);
  637. result:=DoExec(FindUtil(utilsprefix+'fpcres'),cmdstr,false,false);
  638. end;
  639. end;
  640. {*****************************************************************************
  641. Initialize
  642. *****************************************************************************}
  643. initialization
  644. {$ifdef i386}
  645. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  646. RegisterImport(system_i386_linux,timportliblinux);
  647. RegisterExport(system_i386_linux,texportliblinux);
  648. RegisterTarget(system_i386_linux_info);
  649. RegisterRes(res_elf32_info);
  650. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  651. RegisterImport(system_x86_6432_linux,timportliblinux);
  652. RegisterExport(system_x86_6432_linux,texportliblinux);
  653. RegisterTarget(system_x86_6432_linux_info);
  654. {$endif i386}
  655. {$ifdef m68k}
  656. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  657. RegisterImport(system_m68k_linux,timportliblinux);
  658. RegisterExport(system_m68k_linux,texportliblinux);
  659. RegisterTarget(system_m68k_linux_info);
  660. {$endif m68k}
  661. {$ifdef powerpc}
  662. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  663. RegisterImport(system_powerpc_linux,timportliblinux);
  664. RegisterExport(system_powerpc_linux,texportliblinux);
  665. RegisterTarget(system_powerpc_linux_info);
  666. {$endif powerpc}
  667. {$ifdef powerpc64}
  668. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  669. RegisterImport(system_powerpc64_linux,timportliblinux);
  670. RegisterExport(system_powerpc64_linux,texportliblinux);
  671. RegisterTarget(system_powerpc64_linux_info);
  672. {$endif powerpc64}
  673. {$ifdef alpha}
  674. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  675. RegisterImport(system_alpha_linux,timportliblinux);
  676. RegisterExport(system_alpha_linux,texportliblinux);
  677. RegisterTarget(system_alpha_linux_info);
  678. {$endif alpha}
  679. {$ifdef x86_64}
  680. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  681. RegisterImport(system_x86_64_linux,timportliblinux);
  682. RegisterExport(system_x86_64_linux,texportliblinux);
  683. RegisterTarget(system_x86_64_linux_info);
  684. RegisterRes(res_elf64_info);
  685. {$endif x86_64}
  686. {$ifdef SPARC}
  687. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  688. RegisterImport(system_SPARC_linux,timportliblinux);
  689. RegisterExport(system_SPARC_linux,texportliblinux);
  690. RegisterTarget(system_SPARC_linux_info);
  691. {$endif SPARC}
  692. {$ifdef ARM}
  693. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  694. RegisterImport(system_arm_linux,timportliblinux);
  695. RegisterExport(system_arm_linux,texportliblinux);
  696. RegisterTarget(system_arm_linux_info);
  697. {$endif ARM}
  698. end.