t_linux.pas 22 KB

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