t_linux.pas 23 KB

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