t_linux.pas 23 KB

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