t_linux.pas 23 KB

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