t_linux.pas 31 KB

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