t_linux.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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. 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. {$ifdef ARM}
  617. if target_info.abi=abi_eabi then
  618. begin
  619. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  620. add('/* Script for -z combreloc: combine and sort reloc sections */');
  621. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  622. add(' "elf32-littlearm")');
  623. add('OUTPUT_ARCH(arm)');
  624. add('ENTRY(_start)');
  625. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  626. add('SECTIONS');
  627. add('{');
  628. add(' /* Read-only sections, merged into text segment: */');
  629. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000;');
  630. add(' .interp : { *(.interp) }');
  631. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  632. add(' .hash : { *(.hash) }');
  633. add(' .gnu.hash : { *(.gnu.hash) }');
  634. add(' .dynsym : { *(.dynsym) }');
  635. add(' .dynstr : { *(.dynstr) }');
  636. add(' .gnu.version : { *(.gnu.version) }');
  637. add(' .gnu.version_d : { *(.gnu.version_d) }');
  638. add(' .gnu.version_r : { *(.gnu.version_r) }');
  639. add(' .rel.dyn :');
  640. add(' {');
  641. add(' *(.rel.init)');
  642. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  643. add(' *(.rel.fini)');
  644. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  645. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  646. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  647. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  648. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  649. add(' *(.rel.ctors)');
  650. add(' *(.rel.dtors)');
  651. add(' *(.rel.got)');
  652. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  653. add(' }');
  654. add(' .rela.dyn :');
  655. add(' {');
  656. add(' *(.rela.init)');
  657. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  658. add(' *(.rela.fini)');
  659. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  660. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  661. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  662. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  663. add(' *(.rela.ctors)');
  664. add(' *(.rela.dtors)');
  665. add(' *(.rela.got)');
  666. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  667. add(' }');
  668. add(' .rel.plt : { *(.rel.plt) }');
  669. add(' .rela.plt : { *(.rela.plt) }');
  670. add(' .init :');
  671. add(' {');
  672. add(' KEEP (*(.init))');
  673. add(' } =0');
  674. add(' .plt : { *(.plt) }');
  675. add(' .text :');
  676. add(' {');
  677. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  678. add(' KEEP (*(.text.*personality*))');
  679. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  680. add(' *(.gnu.warning)');
  681. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  682. add(' } =0');
  683. add(' .fini :');
  684. add(' {');
  685. add(' KEEP (*(.fini))');
  686. add(' } =0');
  687. add(' PROVIDE (__etext = .);');
  688. add(' PROVIDE (_etext = .);');
  689. add(' PROVIDE (etext = .);');
  690. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  691. add(' .rodata1 : { *(.rodata1) }');
  692. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  693. add(' __exidx_start = .;');
  694. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  695. add(' __exidx_end = .;');
  696. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  697. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  698. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  699. add(' /* Adjust the address for the data segment. We want to adjust up to');
  700. add(' the same address within the page on the next page up. */');
  701. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  702. add(' /* Exception handling */');
  703. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  704. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  705. add(' /* Thread Local Storage sections */');
  706. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  707. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  708. add(' .preinit_array :');
  709. add(' {');
  710. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  711. add(' KEEP (*(.preinit_array))');
  712. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  713. add(' }');
  714. add(' .init_array :');
  715. add(' {');
  716. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  717. add(' KEEP (*(SORT(.init_array.*)))');
  718. add(' KEEP (*(.init_array))');
  719. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  720. add(' }');
  721. add(' .fini_array :');
  722. add(' {');
  723. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  724. add(' KEEP (*(.fini_array))');
  725. add(' KEEP (*(SORT(.fini_array.*)))');
  726. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  727. add(' }');
  728. add(' .ctors :');
  729. add(' {');
  730. add(' /* gcc uses crtbegin.o to find the start of');
  731. add(' the constructors, so we make sure it is');
  732. add(' first. Because this is a wildcard, it');
  733. add(' doesn''t matter if the user does not');
  734. add(' actually link against crtbegin.o; the');
  735. add(' linker won''t look for a file to match a');
  736. add(' wildcard. The wildcard also means that it');
  737. add(' doesn''t matter which directory crtbegin.o');
  738. add(' is in. */');
  739. add(' KEEP (*crtbegin.o(.ctors))');
  740. add(' KEEP (*crtbegin?.o(.ctors))');
  741. add(' /* We don''t want to include the .ctor section from');
  742. add(' the crtend.o file until after the sorted ctors.');
  743. add(' The .ctor section from the crtend file contains the');
  744. add(' end of ctors marker and it must be last */');
  745. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  746. add(' KEEP (*(SORT(.ctors.*)))');
  747. add(' KEEP (*(.ctors))');
  748. add(' }');
  749. add(' .dtors :');
  750. add(' {');
  751. add(' KEEP (*crtbegin.o(.dtors))');
  752. add(' KEEP (*crtbegin?.o(.dtors))');
  753. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  754. add(' KEEP (*(SORT(.dtors.*)))');
  755. add(' KEEP (*(.dtors))');
  756. add(' }');
  757. add(' .jcr : { KEEP (*(.jcr)) }');
  758. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  759. add(' .dynamic : { *(.dynamic) }');
  760. add(' .got : { *(.got.plt) *(.got) }');
  761. add(' .data :');
  762. add(' {');
  763. add(' __data_start = . ;');
  764. add(' *(.data .data.* .gnu.linkonce.d.*)');
  765. { extra by FPC }
  766. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  767. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  768. add(' SORT(CONSTRUCTORS)');
  769. add(' }');
  770. add(' .data1 : { *(.data1) }');
  771. add(' _edata = .; PROVIDE (edata = .);');
  772. add(' __bss_start = .;');
  773. add(' __bss_start__ = .;');
  774. add(' .bss :');
  775. add(' {');
  776. add(' *(.dynbss)');
  777. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  778. add(' *(COMMON)');
  779. add(' /* Align here to ensure that the .bss section occupies space up to');
  780. add(' _end. Align after .bss to ensure correct alignment even if the');
  781. add(' .bss section disappears because there are no input sections.');
  782. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  783. add(' pad the .data section. */');
  784. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  785. add(' }');
  786. add(' _bss_end__ = . ; __bss_end__ = . ;');
  787. add(' . = ALIGN(32 / 8);');
  788. add(' . = ALIGN(32 / 8);');
  789. add(' __end__ = . ;');
  790. add(' _end = .; PROVIDE (end = .);');
  791. add(' /* Stabs debugging sections. */');
  792. add(' .stab 0 : { *(.stab) }');
  793. add(' .stabstr 0 : { *(.stabstr) }');
  794. add(' .stab.excl 0 : { *(.stab.excl) }');
  795. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  796. add(' .stab.index 0 : { *(.stab.index) }');
  797. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  798. add(' .comment 0 : { *(.comment) }');
  799. add(' /* DWARF debug sections.');
  800. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  801. add(' of the section so we begin them at 0. */');
  802. add(' /* DWARF 1 */');
  803. add(' .debug 0 : { *(.debug) }');
  804. add(' .line 0 : { *(.line) }');
  805. add(' /* GNU DWARF 1 extensions */');
  806. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  807. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  808. add(' /* DWARF 1.1 and DWARF 2 */');
  809. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  810. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  811. add(' /* DWARF 2 */');
  812. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  813. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  814. add(' .debug_line 0 : { *(.debug_line) }');
  815. add(' .debug_frame 0 : { *(.debug_frame) }');
  816. add(' .debug_str 0 : { *(.debug_str) }');
  817. add(' .debug_loc 0 : { *(.debug_loc) }');
  818. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  819. add(' /* SGI/MIPS DWARF 2 extensions */');
  820. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  821. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  822. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  823. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  824. add(' /* DWARF 3 */');
  825. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  826. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  827. add(' .stack 0x80000 :');
  828. add(' {');
  829. add(' _stack = .;');
  830. add(' *(.stack)');
  831. add(' }');
  832. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  833. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  834. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  835. add('}');
  836. end
  837. else
  838. {$endif ARM}
  839. {$ifndef LINKERSCRIPT_INCLUDED}
  840. begin
  841. {Sections.}
  842. add('SECTIONS');
  843. add('{');
  844. {Read-only sections, merged into text segment:}
  845. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 +0x100;');
  846. add(' .interp : { *(.interp) }');
  847. add(' .hash : { *(.hash) }');
  848. add(' .dynsym : { *(.dynsym) }');
  849. add(' .dynstr : { *(.dynstr) }');
  850. add(' .gnu.version : { *(.gnu.version) }');
  851. add(' .gnu.version_d : { *(.gnu.version_d) }');
  852. add(' .gnu.version_r : { *(.gnu.version_r) }');
  853. add(' .rel.dyn :');
  854. add(' {');
  855. add(' *(.rel.init)');
  856. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  857. add(' *(.rel.fini)');
  858. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  859. add(' *(.rel.data.rel.ro*)');
  860. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  861. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  862. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  863. add(' *(.rel.got)');
  864. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  865. add(' }');
  866. add(' .rela.dyn :');
  867. add(' {');
  868. add(' *(.rela.init)');
  869. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  870. add(' *(.rela.fini)');
  871. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  872. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  873. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  874. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  875. add(' *(.rela.got)');
  876. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  877. add(' }');
  878. add(' .rel.plt : { *(.rel.plt) }');
  879. add(' .rela.plt : { *(.rela.plt) }');
  880. add(' .init :');
  881. add(' {');
  882. add(' KEEP (*(.init))');
  883. add(' } =0x90909090');
  884. add(' .plt : { *(.plt) }');
  885. add(' .text :');
  886. add(' {');
  887. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  888. add(' KEEP (*(.text.*personality*))');
  889. {.gnu.warning sections are handled specially by elf32.em.}
  890. add(' *(.gnu.warning)');
  891. add(' } =0x90909090');
  892. add(' .fini :');
  893. add(' {');
  894. add(' KEEP (*(.fini))');
  895. add(' } =0x90909090');
  896. add(' PROVIDE (_etext = .);');
  897. add(' .rodata :');
  898. add(' {');
  899. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  900. add(' }');
  901. {Adjust the address for the data segment. We want to adjust up to
  902. the same address within the page on the next page up.}
  903. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  904. add(' .dynamic : { *(.dynamic) }');
  905. add(' .got : { *(.got) }');
  906. add(' .got.plt : { *(.got.plt) }');
  907. add(' .data :');
  908. add(' {');
  909. add(' *(.data .data.* .gnu.linkonce.d.*)');
  910. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  911. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  912. add(' }');
  913. add(' PROVIDE (_edata = .);');
  914. add(' PROVIDE (edata = .);');
  915. {$ifdef zsegment_threadvars}
  916. add(' _z = .;');
  917. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  918. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  919. add(' . = _z + SIZEOF (.threadvar);');
  920. {$else}
  921. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  922. {$endif}
  923. add(' __bss_start = .;');
  924. add(' .bss :');
  925. add(' {');
  926. add(' *(.dynbss)');
  927. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  928. add(' *(COMMON)');
  929. {Align here to ensure that the .bss section occupies space up to
  930. _end. Align after .bss to ensure correct alignment even if the
  931. .bss section disappears because there are no input sections.}
  932. add(' . = ALIGN(32 / 8);');
  933. add(' }');
  934. add(' . = ALIGN(32 / 8);');
  935. add(' PROVIDE (_end = .);');
  936. add(' PROVIDE (end = .);');
  937. {Stabs debugging sections.}
  938. add(' .stab 0 : { *(.stab) }');
  939. add(' .stabstr 0 : { *(.stabstr) }');
  940. add('}');
  941. end;
  942. {$endif LINKERSCRIPT_INCLUDED}
  943. { Write and Close response }
  944. writetodisk;
  945. Free;
  946. end;
  947. WriteResponseFile:=True;
  948. end;
  949. function TLinkerLinux.MakeExecutable:boolean;
  950. var
  951. i : longint;
  952. binstr,
  953. cmdstr : TCmdStr;
  954. success : boolean;
  955. DynLinkStr : string;
  956. GCSectionsStr,
  957. StaticStr,
  958. StripStr : string[40];
  959. begin
  960. if not(cs_link_nolink in current_settings.globalswitches) then
  961. Message1(exec_i_linking,current_module.exefilename^);
  962. { Create some replacements }
  963. StaticStr:='';
  964. StripStr:='';
  965. GCSectionsStr:='';
  966. DynLinkStr:='';
  967. if (cs_link_staticflag in current_settings.globalswitches) then
  968. StaticStr:='-static';
  969. if (cs_link_strip in current_settings.globalswitches) and
  970. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  971. StripStr:='-s';
  972. if (cs_link_map in current_settings.globalswitches) then
  973. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  974. if create_smartlink_sections then
  975. GCSectionsStr:='--gc-sections';
  976. If (cs_profile in current_settings.moduleswitches) or
  977. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  978. begin
  979. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  980. if cshared then
  981. DynLinkStr:=DynLinkStr+' --shared ';
  982. if rlinkpath<>'' then
  983. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  984. End;
  985. { Write used files and libraries }
  986. WriteResponseFile(false);
  987. { Call linker }
  988. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  989. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  990. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  991. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  992. Replace(cmdstr,'$STATIC',StaticStr);
  993. Replace(cmdstr,'$STRIP',StripStr);
  994. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  995. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  996. { create dynamic symbol table? }
  997. if HasExports then
  998. cmdstr:=cmdstr+' -E';
  999. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1000. { Create external .dbg file with debuginfo }
  1001. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  1002. begin
  1003. for i:=1 to 3 do
  1004. begin
  1005. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  1006. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  1007. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename^)));
  1008. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename^));
  1009. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1010. if not success then
  1011. break;
  1012. end;
  1013. end;
  1014. { Remove ReponseFile }
  1015. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1016. DeleteFile(outputexedir+Info.ResName);
  1017. if (success) then
  1018. success:=PostProcessExecutable(current_module.exefilename^,false);
  1019. MakeExecutable:=success; { otherwise a recursive call to link method }
  1020. end;
  1021. Function TLinkerLinux.MakeSharedLibrary:boolean;
  1022. var
  1023. InitStr,
  1024. FiniStr,
  1025. SoNameStr : string[80];
  1026. binstr,
  1027. cmdstr : TCmdStr;
  1028. success : boolean;
  1029. begin
  1030. MakeSharedLibrary:=false;
  1031. if not(cs_link_nolink in current_settings.globalswitches) then
  1032. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1033. { Write used files and libraries }
  1034. WriteResponseFile(true);
  1035. { Create some replacements }
  1036. InitStr:='-init FPC_LIB_START';
  1037. FiniStr:='-fini FPC_LIB_EXIT';
  1038. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  1039. { Call linker }
  1040. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1041. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1042. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1043. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1044. Replace(cmdstr,'$INIT',InitStr);
  1045. Replace(cmdstr,'$FINI',FiniStr);
  1046. Replace(cmdstr,'$SONAME',SoNameStr);
  1047. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1048. { Strip the library ? }
  1049. if success and (cs_link_strip in current_settings.globalswitches) then
  1050. begin
  1051. { only remove non global symbols and debugging info for a library }
  1052. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1053. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1054. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1055. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1056. end;
  1057. { Remove ReponseFile }
  1058. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1059. DeleteFile(outputexedir+Info.ResName);
  1060. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1061. end;
  1062. function tlinkerLinux.postprocessexecutable(const fn : string;isdll:boolean):boolean;
  1063. var
  1064. cmdstr: string;
  1065. begin
  1066. result:=True;
  1067. if HasResources and
  1068. (target_res.id=res_elf) then
  1069. begin
  1070. cmdstr:=' -f -i '+maybequoted(fn);
  1071. result:=DoExec(FindUtil(utilsprefix+'fpcres'),cmdstr,false,false);
  1072. end;
  1073. end;
  1074. {*****************************************************************************
  1075. Initialize
  1076. *****************************************************************************}
  1077. initialization
  1078. {$ifdef i386}
  1079. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  1080. RegisterImport(system_i386_linux,timportliblinux);
  1081. RegisterExport(system_i386_linux,texportliblinux);
  1082. RegisterTarget(system_i386_linux_info);
  1083. RegisterRes(res_elf32_info,TWinLikeResourceFile);
  1084. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  1085. RegisterImport(system_x86_6432_linux,timportliblinux);
  1086. RegisterExport(system_x86_6432_linux,texportliblinux);
  1087. RegisterTarget(system_x86_6432_linux_info);
  1088. {$endif i386}
  1089. {$ifdef m68k}
  1090. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  1091. RegisterImport(system_m68k_linux,timportliblinux);
  1092. RegisterExport(system_m68k_linux,texportliblinux);
  1093. RegisterTarget(system_m68k_linux_info);
  1094. {$endif m68k}
  1095. {$ifdef powerpc}
  1096. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  1097. RegisterImport(system_powerpc_linux,timportliblinux);
  1098. RegisterExport(system_powerpc_linux,texportliblinux);
  1099. RegisterTarget(system_powerpc_linux_info);
  1100. {$endif powerpc}
  1101. {$ifdef powerpc64}
  1102. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  1103. RegisterImport(system_powerpc64_linux,timportliblinux);
  1104. RegisterExport(system_powerpc64_linux,texportliblinux);
  1105. RegisterTarget(system_powerpc64_linux_info);
  1106. {$endif powerpc64}
  1107. {$ifdef alpha}
  1108. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  1109. RegisterImport(system_alpha_linux,timportliblinux);
  1110. RegisterExport(system_alpha_linux,texportliblinux);
  1111. RegisterTarget(system_alpha_linux_info);
  1112. {$endif alpha}
  1113. {$ifdef x86_64}
  1114. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  1115. RegisterImport(system_x86_64_linux,timportliblinux);
  1116. RegisterExport(system_x86_64_linux,texportliblinux);
  1117. RegisterTarget(system_x86_64_linux_info);
  1118. RegisterRes(res_elf64_info,TWinLikeResourceFile);
  1119. {$endif x86_64}
  1120. {$ifdef SPARC}
  1121. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  1122. RegisterImport(system_SPARC_linux,timportliblinux);
  1123. RegisterExport(system_SPARC_linux,texportliblinux);
  1124. RegisterTarget(system_SPARC_linux_info);
  1125. {$endif SPARC}
  1126. {$ifdef ARM}
  1127. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  1128. RegisterImport(system_arm_linux,timportliblinux);
  1129. RegisterExport(system_arm_linux,texportliblinux);
  1130. RegisterTarget(system_arm_linux_info);
  1131. {$endif ARM}
  1132. end.