t_linux.pas 23 KB

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