t_linux.pas 32 KB

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