t_linux.pas 32 KB

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