t_linux.pas 23 KB

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