t_linux.pas 31 KB

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