t_linux.pas 25 KB

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