t_linux.pas 25 KB

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