t_linux.pas 25 KB

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