t_linux.pas 25 KB

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