t_linux.pas 33 KB

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