t_linux.pas 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. {
  2. Copyright (c) 1998-2008 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. procedure LoadPredefinedLibraryOrder; override;
  50. end;
  51. implementation
  52. uses
  53. SysUtils,
  54. cutils,cfileutl,cclasses,
  55. verbose,systems,globtype,globals,
  56. symconst,script,
  57. fmodule,
  58. aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,
  59. cgbase,cgobj,cgutils,ogbase,ncgutil,
  60. comprsrc,
  61. rescmn, i_linux
  62. ;
  63. {*****************************************************************************
  64. TIMPORTLIBLINUX
  65. *****************************************************************************}
  66. procedure timportliblinux.generatelib;
  67. var
  68. i : longint;
  69. ImportLibrary : TImportLibrary;
  70. begin
  71. for i:=0 to current_module.ImportLibraryList.Count-1 do
  72. begin
  73. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  74. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  75. end;
  76. end;
  77. {*****************************************************************************
  78. TEXPORTLIBLINUX
  79. *****************************************************************************}
  80. procedure texportliblinux.preparelib(const s:string);
  81. begin
  82. end;
  83. procedure texportliblinux.exportprocedure(hp : texported_item);
  84. var
  85. hp2 : texported_item;
  86. begin
  87. { first test the index value }
  88. if (hp.options and eo_index)<>0 then
  89. begin
  90. Message1(parser_e_no_export_with_index_for_target,'linux');
  91. exit;
  92. end;
  93. { now place in correct order }
  94. hp2:=texported_item(current_module._exports.first);
  95. while assigned(hp2) and
  96. (hp.name^>hp2.name^) do
  97. hp2:=texported_item(hp2.next);
  98. { insert hp there !! }
  99. if assigned(hp2) and (hp2.name^=hp.name^) then
  100. begin
  101. { this is not allowed !! }
  102. Message1(parser_e_export_name_double,hp.name^);
  103. exit;
  104. end;
  105. if hp2=texported_item(current_module._exports.first) then
  106. current_module._exports.concat(hp)
  107. else if assigned(hp2) then
  108. begin
  109. hp.next:=hp2;
  110. hp.previous:=hp2.previous;
  111. if assigned(hp2.previous) then
  112. hp2.previous.next:=hp;
  113. hp2.previous:=hp;
  114. end
  115. else
  116. current_module._exports.concat(hp);
  117. end;
  118. procedure texportliblinux.exportvar(hp : texported_item);
  119. begin
  120. hp.is_var:=true;
  121. exportprocedure(hp);
  122. end;
  123. procedure texportliblinux.generatelib;
  124. var
  125. hp2 : texported_item;
  126. pd : tprocdef;
  127. {$ifdef x86}
  128. sym : tasmsymbol;
  129. r : treference;
  130. {$endif x86}
  131. begin
  132. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  133. hp2:=texported_item(current_module._exports.first);
  134. while assigned(hp2) do
  135. begin
  136. if (not hp2.is_var) and
  137. (hp2.sym.typ=procsym) then
  138. begin
  139. { the manglednames can already be the same when the procedure
  140. is declared with cdecl }
  141. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  142. if not has_alias_name(pd,hp2.name^) then
  143. begin
  144. { place jump in al_procedures }
  145. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  146. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  147. if (cs_create_pic in current_settings.moduleswitches) and
  148. { other targets need to be checked how it works }
  149. (target_info.system in [system_x86_64_linux,system_i386_linux]) then
  150. begin
  151. {$ifdef x86}
  152. sym:=current_asmdata.RefAsmSymbol(pd.mangledname);
  153. reference_reset_symbol(r,sym,0);
  154. if cs_create_pic in current_settings.moduleswitches then
  155. r.refaddr:=addr_pic
  156. else
  157. r.refaddr:=addr_full;
  158. current_asmdata.asmlists[al_procedures].concat(taicpu.op_ref(A_JMP,S_NO,r));
  159. {$endif x86}
  160. end
  161. else
  162. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],pd.mangledname);
  163. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  164. end;
  165. end
  166. else
  167. message1(parser_e_no_export_of_variables_for_target,'linux');
  168. hp2:=texported_item(hp2.next);
  169. end;
  170. end;
  171. {*****************************************************************************
  172. TLINKERLINUX
  173. *****************************************************************************}
  174. Constructor TLinkerLinux.Create;
  175. begin
  176. Inherited Create;
  177. if not Dontlinkstdlibpath Then
  178. {$ifdef x86_64}
  179. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  180. {$else}
  181. {$ifdef powerpc64}
  182. LibrarySearchPath.AddPath(sysrootpath,'/lib64;/usr/lib64;/usr/X11R6/lib64',true);
  183. {$else powerpc64}
  184. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib',true);
  185. {$endif powerpc64}
  186. {$endif x86_64}
  187. end;
  188. procedure TLinkerLinux.SetDefaultInfo;
  189. {
  190. This will also detect which libc version will be used
  191. }
  192. const
  193. {$ifdef i386} platform_select='-b elf32-i386 -m elf_i386';{$endif}
  194. {$ifdef x86_64} platform_select='-b elf64-x86-64 -m elf_x86_64';{$endif}
  195. {$ifdef powerpc}platform_select='-b elf32-powerpc -m elf32ppclinux';{$endif}
  196. {$ifdef POWERPC64} platform_select='-b elf64-powerpc -m elf64ppc';{$endif}
  197. {$ifdef sparc} platform_select='-b elf32-sparc -m elf32_sparc';{$endif}
  198. {$ifdef arm} platform_select='';{$endif} {unknown :( }
  199. {$ifdef m68k} platform_select='';{$endif} {unknown :( }
  200. var
  201. defdynlinker: string;
  202. begin
  203. with Info do
  204. begin
  205. ExeCmd[1]:='ld '+platform_select+' $OPT $DYNLINK $STATIC $GCSECTIONS $STRIP -L. -o $EXE';
  206. { when we want to cross-link we need to override default library paths }
  207. if length(sysrootpath) > 0 then
  208. ExeCmd[1]:=ExeCmd[1]+' -T';
  209. ExeCmd[1]:=ExeCmd[1]+' $RES';
  210. DllCmd[1]:='ld '+platform_select+' $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $RES -E';
  211. DllCmd[2]:='strip --strip-unneeded $EXE';
  212. ExtDbgCmd[1]:='objcopy --only-keep-debug $EXE $DBG';
  213. ExtDbgCmd[2]:='objcopy --add-gnu-debuglink=$DBG $EXE';
  214. ExtDbgCmd[3]:='strip --strip-unneeded $EXE';
  215. {$ifdef m68k}
  216. { experimental, is this correct? }
  217. defdynlinker:='/lib/ld-linux.so.2';
  218. {$endif m68k}
  219. {$ifdef i386}
  220. defdynlinker:='/lib/ld-linux.so.1';
  221. {$endif}
  222. {$ifdef x86_64}
  223. defdynlinker:='/lib64/ld-linux-x86-64.so.2';
  224. {$endif x86_64}
  225. {$ifdef sparc}
  226. defdynlinker:='/lib/ld-linux.so.2';
  227. {$endif sparc}
  228. {$ifdef powerpc}
  229. defdynlinker:='/lib/ld.so.1';
  230. {$endif powerpc}
  231. {$ifdef powerpc64}
  232. defdynlinker:='/lib64/ld64.so.1';
  233. {$endif powerpc64}
  234. {$ifdef arm}
  235. defdynlinker:='/lib/ld-linux.so.2';
  236. {$endif arm}
  237. {
  238. Search order:
  239. glibc 2.1+
  240. uclibc
  241. glibc 2.0
  242. If none is found (e.g. when cross compiling) glibc21 is assumed
  243. }
  244. {$ifdef i386}
  245. if FileExists(sysrootpath+'/lib/ld-linux.so.2',false) then
  246. begin
  247. DynamicLinker:='/lib/ld-linux.so.2';
  248. libctype:=glibc21;
  249. end
  250. else
  251. {$endif i386}
  252. if fileexists(sysrootpath+'/lib/ld-uClibc.so.0',false) then
  253. begin
  254. dynamiclinker:='/lib/ld-uClibc.so.0';
  255. libctype:=uclibc;
  256. end
  257. else if fileexists(sysrootpath+defdynlinker,false) then
  258. begin
  259. DynamicLinker:=defdynlinker;
  260. libctype:=glibc2;
  261. end
  262. else
  263. begin
  264. { default is glibc 2.1+ compatible }
  265. DynamicLinker:='/lib/ld-linux.so.2';
  266. libctype:=glibc21;
  267. end;
  268. end;
  269. end;
  270. procedure TLinkerLinux.LoadPredefinedLibraryOrder;
  271. // put your linkorder/linkalias overrides here.
  272. // Note: assumes only called when reordering/aliasing is used.
  273. Begin
  274. if not (cs_link_no_default_lib_order in current_settings.globalswitches) Then
  275. Begin
  276. LinkLibraryOrder.add('gcc','',15);
  277. LinkLibraryOrder.add('c','',100);
  278. LinkLibraryOrder.add('gmon','',120);
  279. LinkLibraryOrder.add('dl','',140);
  280. LinkLibraryOrder.add('pthread','',160);
  281. end;
  282. End;
  283. Procedure TLinkerLinux.InitSysInitUnitName;
  284. var
  285. csysinitunit,
  286. gsysinitunit : string[20];
  287. hp : tmodule;
  288. begin
  289. hp:=tmodule(loaded_units.first);
  290. while assigned(hp) do
  291. begin
  292. linklibc := hp.linkothersharedlibs.find('c');
  293. if linklibc then break;
  294. hp:=tmodule(hp.next);
  295. end;
  296. reorder := linklibc and ReOrderEntries;
  297. if current_module.islibrary then
  298. begin
  299. sysinitunit:='dll';
  300. csysinitunit:='dll';
  301. gsysinitunit:='dll';
  302. prtobj:='dllprt0';
  303. cprtobj:='dllprt0';
  304. gprtobj:='dllprt0';
  305. end
  306. else
  307. begin
  308. prtobj:='prt0';
  309. sysinitunit:='prc';
  310. case libctype of
  311. glibc21:
  312. begin
  313. cprtobj:='cprt21';
  314. gprtobj:='gprt21';
  315. csysinitunit:='c21';
  316. gsysinitunit:='c21g';
  317. end;
  318. uclibc:
  319. begin
  320. cprtobj:='ucprt0';
  321. gprtobj:='ugprt0';
  322. csysinitunit:='uc';
  323. gsysinitunit:='ucg';
  324. end
  325. else
  326. cprtobj:='cprt0';
  327. gprtobj:='gprt0';
  328. csysinitunit:='c';
  329. gsysinitunit:='g';
  330. end;
  331. end;
  332. if cs_profile in current_settings.moduleswitches then
  333. begin
  334. prtobj:=gprtobj;
  335. sysinitunit:=gsysinitunit;
  336. linklibc:=true;
  337. end
  338. else
  339. begin
  340. if linklibc then
  341. begin
  342. prtobj:=cprtobj;
  343. sysinitunit:=csysinitunit;
  344. end;
  345. end;
  346. sysinitunit:='si_'+sysinitunit;
  347. end;
  348. Function TLinkerLinux.WriteResponseFile(isdll:boolean) : Boolean;
  349. Var
  350. linkres : TLinkRes;
  351. i : longint;
  352. HPath : TCmdStrListItem;
  353. s,s1,s2 : TCmdStr;
  354. found1,
  355. found2 : boolean;
  356. begin
  357. result:=False;
  358. { set special options for some targets }
  359. if cs_profile in current_settings.moduleswitches then
  360. begin
  361. if not(libctype in [glibc2,glibc21]) then
  362. AddSharedLibrary('gmon');
  363. AddSharedLibrary('c');
  364. end;
  365. { Open link.res file }
  366. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  367. with linkres do
  368. begin
  369. { Write path to search libraries }
  370. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  371. while assigned(HPath) do
  372. begin
  373. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  374. HPath:=TCmdStrListItem(HPath.Next);
  375. end;
  376. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  377. while assigned(HPath) do
  378. begin
  379. Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  380. HPath:=TCmdStrListItem(HPath.Next);
  381. end;
  382. StartSection('INPUT(');
  383. { add objectfiles, start with prt0 always }
  384. if not (target_info.system in system_internal_sysinit) and (prtobj<>'') then
  385. AddFileName(maybequoted(FindObjectFile(prtobj,'',false)));
  386. { try to add crti and crtbegin if linking to C }
  387. if linklibc and (libctype<>uclibc) then
  388. begin
  389. { x86_64 requires this to use entry/exit code with pic,
  390. see also issue #8210 regarding a discussion
  391. no idea about the other non i386 CPUs (FK)
  392. }
  393. {$ifdef x86_64}
  394. if current_module.islibrary then
  395. begin
  396. if librarysearchpath.FindFile('crtbeginS.o',false,s) then
  397. AddFileName(s);
  398. end
  399. else
  400. {$endif x86_64}
  401. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  402. AddFileName(s);
  403. if librarysearchpath.FindFile('crti.o',false,s) then
  404. AddFileName(s);
  405. end;
  406. { main objectfiles }
  407. while not ObjectFiles.Empty do
  408. begin
  409. s:=ObjectFiles.GetFirst;
  410. if s<>'' then
  411. AddFileName(maybequoted(s));
  412. end;
  413. EndSection(')');
  414. { Write staticlibraries }
  415. if not StaticLibFiles.Empty then
  416. begin
  417. Add('GROUP(');
  418. While not StaticLibFiles.Empty do
  419. begin
  420. S:=StaticLibFiles.GetFirst;
  421. AddFileName(maybequoted(s))
  422. end;
  423. Add(')');
  424. end;
  425. // we must reorder here because the result could empty sharedlibfiles
  426. if reorder Then
  427. ExpandAndApplyOrder(SharedLibFiles);
  428. // after this point addition of shared libs not allowed.
  429. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  430. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  431. if not SharedLibFiles.Empty then
  432. begin
  433. Add('INPUT(');
  434. While not SharedLibFiles.Empty do
  435. begin
  436. S:=SharedLibFiles.GetFirst;
  437. if (s<>'c') or reorder then
  438. begin
  439. i:=Pos(target_info.sharedlibext,S);
  440. if i>0 then
  441. Delete(S,i,255);
  442. Add('-l'+s);
  443. end
  444. else
  445. begin
  446. linklibc:=true;
  447. end;
  448. end;
  449. { be sure that libc is the last lib }
  450. if linklibc and not reorder then
  451. Add('-lc');
  452. { when we have -static for the linker the we also need libgcc }
  453. if (cs_link_staticflag in current_settings.globalswitches) then
  454. Add('-lgcc');
  455. Add(')');
  456. end;
  457. { objects which must be at the end }
  458. if linklibc and (libctype<>uclibc) then
  459. begin
  460. { x86_64 requires this to use entry/exit code with pic,
  461. see also issue #8210 regarding a discussion
  462. no idea about the other non i386 CPUs (FK)
  463. }
  464. {$ifdef x86_64}
  465. if current_module.islibrary then
  466. found1:=librarysearchpath.FindFile('crtendS.o',false,s1)
  467. else
  468. {$endif x86_64}
  469. found1:=librarysearchpath.FindFile('crtend.o',false,s1);
  470. found2:=librarysearchpath.FindFile('crtn.o',false,s2);
  471. if found1 or found2 then
  472. begin
  473. Add('INPUT(');
  474. if found1 then
  475. AddFileName(s1);
  476. if found2 then
  477. AddFileName(s2);
  478. Add(')');
  479. end;
  480. end;
  481. {Entry point.}
  482. add('ENTRY(_start)');
  483. {$ifdef x86_64}
  484. {$define LINKERSCRIPT_INCLUDED}
  485. add('SECTIONS');
  486. add('{');
  487. {Read-only sections, merged into text segment:}
  488. if current_module.islibrary then
  489. add(' . = 0 + SIZEOF_HEADERS;')
  490. else
  491. add(' PROVIDE (__executable_start = 0x0400000); . = 0x0400000 + SIZEOF_HEADERS;');
  492. add(' . = 0 + SIZEOF_HEADERS;');
  493. add(' .interp : { *(.interp) }');
  494. add(' .hash : { *(.hash) }');
  495. add(' .dynsym : { *(.dynsym) }');
  496. add(' .dynstr : { *(.dynstr) }');
  497. add(' .gnu.version : { *(.gnu.version) }');
  498. add(' .gnu.version_d : { *(.gnu.version_d) }');
  499. add(' .gnu.version_r : { *(.gnu.version_r) }');
  500. add(' .rel.dyn :');
  501. add(' {');
  502. add(' *(.rel.init)');
  503. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  504. add(' *(.rel.fini)');
  505. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  506. add(' *(.rel.data.rel.ro*)');
  507. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  508. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  509. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  510. add(' *(.rel.got)');
  511. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  512. add(' }');
  513. add(' .rela.dyn :');
  514. add(' {');
  515. add(' *(.rela.init)');
  516. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  517. add(' *(.rela.fini)');
  518. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  519. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  520. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  521. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  522. add(' *(.rela.got)');
  523. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  524. add(' }');
  525. add(' .rel.plt : { *(.rel.plt) }');
  526. add(' .rela.plt : { *(.rela.plt) }');
  527. add(' .init :');
  528. add(' {');
  529. add(' KEEP (*(.init))');
  530. add(' } =0x90909090');
  531. add(' .plt : { *(.plt) }');
  532. add(' .text :');
  533. add(' {');
  534. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  535. add(' KEEP (*(.text.*personality*))');
  536. {.gnu.warning sections are handled specially by elf32.em.}
  537. add(' *(.gnu.warning)');
  538. add(' } =0x90909090');
  539. add(' .fini :');
  540. add(' {');
  541. add(' KEEP (*(.fini))');
  542. add(' } =0x90909090');
  543. add(' PROVIDE (_etext = .);');
  544. add(' .rodata :');
  545. add(' {');
  546. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  547. add(' }');
  548. {Adjust the address for the data segment. We want to adjust up to
  549. the same address within the page on the next page up.}
  550. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  551. add(' .dynamic : { *(.dynamic) }');
  552. add(' .got : { *(.got .toc) }');
  553. add(' .got.plt : { *(.got.plt .toc.plt) }');
  554. add(' .data :');
  555. add(' {');
  556. add(' *(.data .data.* .gnu.linkonce.d.*)');
  557. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  558. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  559. add(' }');
  560. add(' PROVIDE (_edata = .);');
  561. add(' PROVIDE (edata = .);');
  562. {$ifdef zsegment_threadvars}
  563. add(' _z = .;');
  564. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  565. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  566. add(' . = _z + SIZEOF (.threadvar);');
  567. {$else}
  568. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  569. {$endif}
  570. add(' __bss_start = .;');
  571. add(' .bss :');
  572. add(' {');
  573. add(' *(.dynbss)');
  574. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  575. add(' *(COMMON)');
  576. {Align here to ensure that the .bss section occupies space up to
  577. _end. Align after .bss to ensure correct alignment even if the
  578. .bss section disappears because there are no input sections.}
  579. add(' . = ALIGN(32 / 8);');
  580. add(' }');
  581. add(' . = ALIGN(32 / 8);');
  582. add(' PROVIDE (_end = .);');
  583. add(' PROVIDE (end = .);');
  584. {Stabs debugging sections.}
  585. add(' .stab 0 : { *(.stab) }');
  586. add(' .stabstr 0 : { *(.stabstr) }');
  587. add(' /* DWARF debug sections.');
  588. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  589. add(' of the section so we begin them at 0. */');
  590. add(' /* DWARF 1 */');
  591. add(' .debug 0 : { *(.debug) }');
  592. add(' .line 0 : { *(.line) }');
  593. add(' /* GNU DWARF 1 extensions */');
  594. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  595. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  596. add(' /* DWARF 1.1 and DWARF 2 */');
  597. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  598. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  599. add(' /* DWARF 2 */');
  600. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  601. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  602. add(' .debug_line 0 : { *(.debug_line) }');
  603. add(' .debug_frame 0 : { *(.debug_frame) }');
  604. add(' .debug_str 0 : { *(.debug_str) }');
  605. add(' .debug_loc 0 : { *(.debug_loc) }');
  606. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  607. add(' /* SGI/MIPS DWARF 2 extensions */');
  608. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  609. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  610. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  611. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  612. add(' /DISCARD/ : { *(.note.GNU-stack) }');
  613. add('}');
  614. {$endif x86_64}
  615. {$ifdef ARM}
  616. if target_info.abi=abi_eabi then
  617. begin
  618. { from GNU ld (CodeSourcery Sourcery G++ Lite 2007q3-53) 2.18.50.20070820 }
  619. add('/* Script for -z combreloc: combine and sort reloc sections */');
  620. add('OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm",');
  621. add(' "elf32-littlearm")');
  622. add('OUTPUT_ARCH(arm)');
  623. add('ENTRY(_start)');
  624. add('SEARCH_DIR("=/usr/local/lib"); SEARCH_DIR("=/lib"); SEARCH_DIR("=/usr/lib");');
  625. add('SECTIONS');
  626. add('{');
  627. add(' /* Read-only sections, merged into text segment: */');
  628. add(' PROVIDE (__executable_start = 0x8000); . = 0x8000;');
  629. add(' .interp : { *(.interp) }');
  630. add(' .note.gnu.build-id : { *(.note.gnu.build-id) }');
  631. add(' .hash : { *(.hash) }');
  632. add(' .gnu.hash : { *(.gnu.hash) }');
  633. add(' .dynsym : { *(.dynsym) }');
  634. add(' .dynstr : { *(.dynstr) }');
  635. add(' .gnu.version : { *(.gnu.version) }');
  636. add(' .gnu.version_d : { *(.gnu.version_d) }');
  637. add(' .gnu.version_r : { *(.gnu.version_r) }');
  638. add(' .rel.dyn :');
  639. add(' {');
  640. add(' *(.rel.init)');
  641. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  642. add(' *(.rel.fini)');
  643. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  644. add(' *(.rel.data.rel.ro* .rel.gnu.linkonce.d.rel.ro.*)');
  645. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  646. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  647. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  648. add(' *(.rel.ctors)');
  649. add(' *(.rel.dtors)');
  650. add(' *(.rel.got)');
  651. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  652. add(' }');
  653. add(' .rela.dyn :');
  654. add(' {');
  655. add(' *(.rela.init)');
  656. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  657. add(' *(.rela.fini)');
  658. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  659. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  660. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  661. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  662. add(' *(.rela.ctors)');
  663. add(' *(.rela.dtors)');
  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(' } =0');
  673. add(' .plt : { *(.plt) }');
  674. add(' .text :');
  675. add(' {');
  676. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  677. add(' KEEP (*(.text.*personality*))');
  678. add(' /* .gnu.warning sections are handled specially by elf32.em. */');
  679. add(' *(.gnu.warning)');
  680. add(' *(.glue_7t) *(.glue_7) *(.vfp11_veneer)');
  681. add(' } =0');
  682. add(' .fini :');
  683. add(' {');
  684. add(' KEEP (*(.fini))');
  685. add(' } =0');
  686. add(' PROVIDE (__etext = .);');
  687. add(' PROVIDE (_etext = .);');
  688. add(' PROVIDE (etext = .);');
  689. add(' .rodata : { *(.rodata .rodata.* .gnu.linkonce.r.*) }');
  690. add(' .rodata1 : { *(.rodata1) }');
  691. add(' .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }');
  692. add(' __exidx_start = .;');
  693. add(' .ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }');
  694. add(' __exidx_end = .;');
  695. add(' .eh_frame_hdr : { *(.eh_frame_hdr) }');
  696. add(' .eh_frame : ONLY_IF_RO { KEEP (*(.eh_frame)) }');
  697. add(' .gcc_except_table : ONLY_IF_RO { *(.gcc_except_table .gcc_except_table.*) }');
  698. add(' /* Adjust the address for the data segment. We want to adjust up to');
  699. add(' the same address within the page on the next page up. */');
  700. add(' . = ALIGN(CONSTANT (MAXPAGESIZE)) + (. & (CONSTANT (MAXPAGESIZE) - 1));');
  701. add(' /* Exception handling */');
  702. add(' .eh_frame : ONLY_IF_RW { KEEP (*(.eh_frame)) }');
  703. add(' .gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }');
  704. add(' /* Thread Local Storage sections */');
  705. add(' .tdata : { *(.tdata .tdata.* .gnu.linkonce.td.*) }');
  706. add(' .tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }');
  707. add(' .preinit_array :');
  708. add(' {');
  709. add(' PROVIDE_HIDDEN (__preinit_array_start = .);');
  710. add(' KEEP (*(.preinit_array))');
  711. add(' PROVIDE_HIDDEN (__preinit_array_end = .);');
  712. add(' }');
  713. add(' .init_array :');
  714. add(' {');
  715. add(' PROVIDE_HIDDEN (__init_array_start = .);');
  716. add(' KEEP (*(SORT(.init_array.*)))');
  717. add(' KEEP (*(.init_array))');
  718. add(' PROVIDE_HIDDEN (__init_array_end = .);');
  719. add(' }');
  720. add(' .fini_array :');
  721. add(' {');
  722. add(' PROVIDE_HIDDEN (__fini_array_start = .);');
  723. add(' KEEP (*(.fini_array))');
  724. add(' KEEP (*(SORT(.fini_array.*)))');
  725. add(' PROVIDE_HIDDEN (__fini_array_end = .);');
  726. add(' }');
  727. add(' .ctors :');
  728. add(' {');
  729. add(' /* gcc uses crtbegin.o to find the start of');
  730. add(' the constructors, so we make sure it is');
  731. add(' first. Because this is a wildcard, it');
  732. add(' doesn''t matter if the user does not');
  733. add(' actually link against crtbegin.o; the');
  734. add(' linker won''t look for a file to match a');
  735. add(' wildcard. The wildcard also means that it');
  736. add(' doesn''t matter which directory crtbegin.o');
  737. add(' is in. */');
  738. add(' KEEP (*crtbegin.o(.ctors))');
  739. add(' KEEP (*crtbegin?.o(.ctors))');
  740. add(' /* We don''t want to include the .ctor section from');
  741. add(' the crtend.o file until after the sorted ctors.');
  742. add(' The .ctor section from the crtend file contains the');
  743. add(' end of ctors marker and it must be last */');
  744. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))');
  745. add(' KEEP (*(SORT(.ctors.*)))');
  746. add(' KEEP (*(.ctors))');
  747. add(' }');
  748. add(' .dtors :');
  749. add(' {');
  750. add(' KEEP (*crtbegin.o(.dtors))');
  751. add(' KEEP (*crtbegin?.o(.dtors))');
  752. add(' KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))');
  753. add(' KEEP (*(SORT(.dtors.*)))');
  754. add(' KEEP (*(.dtors))');
  755. add(' }');
  756. add(' .jcr : { KEEP (*(.jcr)) }');
  757. add(' .data.rel.ro : { *(.data.rel.ro.local* .gnu.linkonce.d.rel.ro.local.*) *(.data.rel.ro* .gnu.linkonce.d.rel.ro.*) }');
  758. add(' .dynamic : { *(.dynamic) }');
  759. add(' .got : { *(.got.plt) *(.got) }');
  760. add(' .data :');
  761. add(' {');
  762. add(' __data_start = . ;');
  763. add(' *(.data .data.* .gnu.linkonce.d.*)');
  764. { extra by FPC }
  765. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  766. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  767. add(' SORT(CONSTRUCTORS)');
  768. add(' }');
  769. add(' .data1 : { *(.data1) }');
  770. add(' _edata = .; PROVIDE (edata = .);');
  771. add(' __bss_start = .;');
  772. add(' __bss_start__ = .;');
  773. add(' .bss :');
  774. add(' {');
  775. add(' *(.dynbss)');
  776. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  777. add(' *(COMMON)');
  778. add(' /* Align here to ensure that the .bss section occupies space up to');
  779. add(' _end. Align after .bss to ensure correct alignment even if the');
  780. add(' .bss section disappears because there are no input sections.');
  781. add(' FIXME: Why do we need it? When there is no .bss section, we don''t');
  782. add(' pad the .data section. */');
  783. add(' . = ALIGN(. != 0 ? 32 / 8 : 1);');
  784. add(' }');
  785. add(' _bss_end__ = . ; __bss_end__ = . ;');
  786. add(' . = ALIGN(32 / 8);');
  787. add(' . = ALIGN(32 / 8);');
  788. add(' __end__ = . ;');
  789. add(' _end = .; PROVIDE (end = .);');
  790. add(' /* Stabs debugging sections. */');
  791. add(' .stab 0 : { *(.stab) }');
  792. add(' .stabstr 0 : { *(.stabstr) }');
  793. add(' .stab.excl 0 : { *(.stab.excl) }');
  794. add(' .stab.exclstr 0 : { *(.stab.exclstr) }');
  795. add(' .stab.index 0 : { *(.stab.index) }');
  796. add(' .stab.indexstr 0 : { *(.stab.indexstr) }');
  797. add(' .comment 0 : { *(.comment) }');
  798. add(' /* DWARF debug sections.');
  799. add(' Symbols in the DWARF debugging sections are relative to the beginning');
  800. add(' of the section so we begin them at 0. */');
  801. add(' /* DWARF 1 */');
  802. add(' .debug 0 : { *(.debug) }');
  803. add(' .line 0 : { *(.line) }');
  804. add(' /* GNU DWARF 1 extensions */');
  805. add(' .debug_srcinfo 0 : { *(.debug_srcinfo) }');
  806. add(' .debug_sfnames 0 : { *(.debug_sfnames) }');
  807. add(' /* DWARF 1.1 and DWARF 2 */');
  808. add(' .debug_aranges 0 : { *(.debug_aranges) }');
  809. add(' .debug_pubnames 0 : { *(.debug_pubnames) }');
  810. add(' /* DWARF 2 */');
  811. add(' .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }');
  812. add(' .debug_abbrev 0 : { *(.debug_abbrev) }');
  813. add(' .debug_line 0 : { *(.debug_line) }');
  814. add(' .debug_frame 0 : { *(.debug_frame) }');
  815. add(' .debug_str 0 : { *(.debug_str) }');
  816. add(' .debug_loc 0 : { *(.debug_loc) }');
  817. add(' .debug_macinfo 0 : { *(.debug_macinfo) }');
  818. add(' /* SGI/MIPS DWARF 2 extensions */');
  819. add(' .debug_weaknames 0 : { *(.debug_weaknames) }');
  820. add(' .debug_funcnames 0 : { *(.debug_funcnames) }');
  821. add(' .debug_typenames 0 : { *(.debug_typenames) }');
  822. add(' .debug_varnames 0 : { *(.debug_varnames) }');
  823. add(' /* DWARF 3 */');
  824. add(' .debug_pubtypes 0 : { *(.debug_pubtypes) }');
  825. add(' .debug_ranges 0 : { *(.debug_ranges) }');
  826. add(' .stack 0x80000 :');
  827. add(' {');
  828. add(' _stack = .;');
  829. add(' *(.stack)');
  830. add(' }');
  831. add(' .ARM.attributes 0 : { KEEP (*(.ARM.attributes)) KEEP (*(.gnu.attributes)) }');
  832. add(' .note.gnu.arm.ident 0 : { KEEP (*(.note.gnu.arm.ident)) }');
  833. add(' /DISCARD/ : { *(.note.GNU-stack) *(.gnu_debuglink) }');
  834. add('}');
  835. end
  836. else
  837. {$endif ARM}
  838. {$ifndef LINKERSCRIPT_INCLUDED}
  839. begin
  840. {Sections.}
  841. add('SECTIONS');
  842. add('{');
  843. {Read-only sections, merged into text segment:}
  844. add(' PROVIDE (__executable_start = 0x010000); . = 0x010000 +0x100;');
  845. add(' .interp : { *(.interp) }');
  846. add(' .hash : { *(.hash) }');
  847. add(' .dynsym : { *(.dynsym) }');
  848. add(' .dynstr : { *(.dynstr) }');
  849. add(' .gnu.version : { *(.gnu.version) }');
  850. add(' .gnu.version_d : { *(.gnu.version_d) }');
  851. add(' .gnu.version_r : { *(.gnu.version_r) }');
  852. add(' .rel.dyn :');
  853. add(' {');
  854. add(' *(.rel.init)');
  855. add(' *(.rel.text .rel.text.* .rel.gnu.linkonce.t.*)');
  856. add(' *(.rel.fini)');
  857. add(' *(.rel.rodata .rel.rodata.* .rel.gnu.linkonce.r.*)');
  858. add(' *(.rel.data.rel.ro*)');
  859. add(' *(.rel.data .rel.data.* .rel.gnu.linkonce.d.*)');
  860. add(' *(.rel.tdata .rel.tdata.* .rel.gnu.linkonce.td.*)');
  861. add(' *(.rel.tbss .rel.tbss.* .rel.gnu.linkonce.tb.*)');
  862. add(' *(.rel.got)');
  863. add(' *(.rel.bss .rel.bss.* .rel.gnu.linkonce.b.*)');
  864. add(' }');
  865. add(' .rela.dyn :');
  866. add(' {');
  867. add(' *(.rela.init)');
  868. add(' *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)');
  869. add(' *(.rela.fini)');
  870. add(' *(.rela.rodata .rela.rodata.* .rela.gnu.linkonce.r.*)');
  871. add(' *(.rela.data .rela.data.* .rela.gnu.linkonce.d.*)');
  872. add(' *(.rela.tdata .rela.tdata.* .rela.gnu.linkonce.td.*)');
  873. add(' *(.rela.tbss .rela.tbss.* .rela.gnu.linkonce.tb.*)');
  874. add(' *(.rela.got)');
  875. add(' *(.rela.bss .rela.bss.* .rela.gnu.linkonce.b.*)');
  876. add(' }');
  877. add(' .rel.plt : { *(.rel.plt) }');
  878. add(' .rela.plt : { *(.rela.plt) }');
  879. add(' .init :');
  880. add(' {');
  881. add(' KEEP (*(.init))');
  882. add(' } =0x90909090');
  883. add(' .plt : { *(.plt) }');
  884. add(' .text :');
  885. add(' {');
  886. add(' *(.text .stub .text.* .gnu.linkonce.t.*)');
  887. add(' KEEP (*(.text.*personality*))');
  888. {.gnu.warning sections are handled specially by elf32.em.}
  889. add(' *(.gnu.warning)');
  890. add(' } =0x90909090');
  891. add(' .fini :');
  892. add(' {');
  893. add(' KEEP (*(.fini))');
  894. add(' } =0x90909090');
  895. add(' PROVIDE (_etext = .);');
  896. add(' .rodata :');
  897. add(' {');
  898. add(' *(.rodata .rodata.* .gnu.linkonce.r.*)');
  899. add(' }');
  900. {Adjust the address for the data segment. We want to adjust up to
  901. the same address within the page on the next page up.}
  902. add(' . = ALIGN (0x1000) - ((0x1000 - .) & (0x1000 - 1));');
  903. add(' .dynamic : { *(.dynamic) }');
  904. add(' .got : { *(.got) }');
  905. add(' .got.plt : { *(.got.plt) }');
  906. add(' .data :');
  907. add(' {');
  908. add(' *(.data .data.* .gnu.linkonce.d.*)');
  909. add(' KEEP (*(.fpc .fpc.n_version .fpc.n_links))');
  910. add(' KEEP (*(.gnu.linkonce.d.*personality*))');
  911. add(' }');
  912. add(' PROVIDE (_edata = .);');
  913. add(' PROVIDE (edata = .);');
  914. {$ifdef zsegment_threadvars}
  915. add(' _z = .;');
  916. add(' .threadvar 0 : AT (_z) { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  917. add(' PROVIDE (_threadvar_size = SIZEOF(.threadvar));');
  918. add(' . = _z + SIZEOF (.threadvar);');
  919. {$else}
  920. add(' .threadvar : { *(.threadvar .threadvar.* .gnu.linkonce.tv.*) }');
  921. {$endif}
  922. add(' __bss_start = .;');
  923. add(' .bss :');
  924. add(' {');
  925. add(' *(.dynbss)');
  926. add(' *(.bss .bss.* .gnu.linkonce.b.*)');
  927. add(' *(COMMON)');
  928. {Align here to ensure that the .bss section occupies space up to
  929. _end. Align after .bss to ensure correct alignment even if the
  930. .bss section disappears because there are no input sections.}
  931. add(' . = ALIGN(32 / 8);');
  932. add(' }');
  933. add(' . = ALIGN(32 / 8);');
  934. add(' PROVIDE (_end = .);');
  935. add(' PROVIDE (end = .);');
  936. {Stabs debugging sections.}
  937. add(' .stab 0 : { *(.stab) }');
  938. add(' .stabstr 0 : { *(.stabstr) }');
  939. add('}');
  940. end;
  941. {$endif LINKERSCRIPT_INCLUDED}
  942. { Write and Close response }
  943. writetodisk;
  944. Free;
  945. end;
  946. WriteResponseFile:=True;
  947. end;
  948. function TLinkerLinux.MakeExecutable:boolean;
  949. var
  950. i : longint;
  951. binstr,
  952. cmdstr : TCmdStr;
  953. success : boolean;
  954. DynLinkStr : string;
  955. GCSectionsStr,
  956. StaticStr,
  957. StripStr : string[40];
  958. begin
  959. if not(cs_link_nolink in current_settings.globalswitches) then
  960. Message1(exec_i_linking,current_module.exefilename^);
  961. { Create some replacements }
  962. StaticStr:='';
  963. StripStr:='';
  964. GCSectionsStr:='';
  965. DynLinkStr:='';
  966. if (cs_link_staticflag in current_settings.globalswitches) then
  967. StaticStr:='-static';
  968. if (cs_link_strip in current_settings.globalswitches) and
  969. not(cs_link_separate_dbg_file in current_settings.globalswitches) then
  970. StripStr:='-s';
  971. if (cs_link_map in current_settings.globalswitches) then
  972. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename^,'.map'));
  973. if create_smartlink_sections then
  974. GCSectionsStr:='--gc-sections';
  975. If (cs_profile in current_settings.moduleswitches) or
  976. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  977. begin
  978. DynLinkStr:='--dynamic-linker='+Info.DynamicLinker;
  979. if cshared then
  980. DynLinkStr:=DynLinkStr+' --shared ';
  981. if rlinkpath<>'' then
  982. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath;
  983. End;
  984. { Write used files and libraries }
  985. WriteResponseFile(false);
  986. { Call linker }
  987. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  988. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  989. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  990. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  991. Replace(cmdstr,'$STATIC',StaticStr);
  992. Replace(cmdstr,'$STRIP',StripStr);
  993. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  994. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  995. { create dynamic symbol table? }
  996. if HasExports then
  997. cmdstr:=cmdstr+' -E';
  998. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  999. { Create external .dbg file with debuginfo }
  1000. if success and (cs_link_separate_dbg_file in current_settings.globalswitches) then
  1001. begin
  1002. for i:=1 to 3 do
  1003. begin
  1004. SplitBinCmd(Info.ExtDbgCmd[i],binstr,cmdstr);
  1005. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  1006. Replace(cmdstr,'$DBGFN',maybequoted(extractfilename(current_module.dbgfilename^)));
  1007. Replace(cmdstr,'$DBG',maybequoted(current_module.dbgfilename^));
  1008. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  1009. if not success then
  1010. break;
  1011. end;
  1012. end;
  1013. { Remove ReponseFile }
  1014. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1015. DeleteFile(outputexedir+Info.ResName);
  1016. MakeExecutable:=success; { otherwise a recursive call to link method }
  1017. end;
  1018. Function TLinkerLinux.MakeSharedLibrary:boolean;
  1019. var
  1020. InitStr,
  1021. FiniStr,
  1022. SoNameStr : string[80];
  1023. binstr,
  1024. cmdstr : TCmdStr;
  1025. success : boolean;
  1026. begin
  1027. MakeSharedLibrary:=false;
  1028. if not(cs_link_nolink in current_settings.globalswitches) then
  1029. Message1(exec_i_linking,current_module.sharedlibfilename^);
  1030. { Write used files and libraries }
  1031. WriteResponseFile(true);
  1032. { Create some replacements }
  1033. InitStr:='-init FPC_LIB_START';
  1034. FiniStr:='-fini FPC_LIB_EXIT';
  1035. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  1036. { Call linker }
  1037. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  1038. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1039. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  1040. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  1041. Replace(cmdstr,'$INIT',InitStr);
  1042. Replace(cmdstr,'$FINI',FiniStr);
  1043. Replace(cmdstr,'$SONAME',SoNameStr);
  1044. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1045. { Strip the library ? }
  1046. if success and (cs_link_strip in current_settings.globalswitches) then
  1047. begin
  1048. { only remove non global symbols and debugging info for a library }
  1049. Info.DllCmd[2]:='strip --discard-all --strip-debug $EXE';
  1050. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  1051. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  1052. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  1053. end;
  1054. { Remove ReponseFile }
  1055. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  1056. DeleteFile(outputexedir+Info.ResName);
  1057. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  1058. end;
  1059. {*****************************************************************************
  1060. Initialize
  1061. *****************************************************************************}
  1062. initialization
  1063. {$ifdef i386}
  1064. RegisterExternalLinker(system_i386_linux_info,TLinkerLinux);
  1065. RegisterImport(system_i386_linux,timportliblinux);
  1066. RegisterExport(system_i386_linux,texportliblinux);
  1067. RegisterTarget(system_i386_linux_info);
  1068. RegisterExternalLinker(system_x86_6432_linux_info,TLinkerLinux);
  1069. RegisterImport(system_x86_6432_linux,timportliblinux);
  1070. RegisterExport(system_x86_6432_linux,texportliblinux);
  1071. RegisterTarget(system_x86_6432_linux_info);
  1072. {$endif i386}
  1073. {$ifdef m68k}
  1074. RegisterExternalLinker(system_m68k_linux_info,TLinkerLinux);
  1075. RegisterImport(system_m68k_linux,timportliblinux);
  1076. RegisterExport(system_m68k_linux,texportliblinux);
  1077. RegisterTarget(system_m68k_linux_info);
  1078. {$endif m68k}
  1079. {$ifdef powerpc}
  1080. RegisterExternalLinker(system_powerpc_linux_info,TLinkerLinux);
  1081. RegisterImport(system_powerpc_linux,timportliblinux);
  1082. RegisterExport(system_powerpc_linux,texportliblinux);
  1083. RegisterTarget(system_powerpc_linux_info);
  1084. {$endif powerpc}
  1085. {$ifdef powerpc64}
  1086. RegisterExternalLinker(system_powerpc64_linux_info,TLinkerLinux);
  1087. RegisterImport(system_powerpc64_linux,timportliblinux);
  1088. RegisterExport(system_powerpc64_linux,texportliblinux);
  1089. RegisterTarget(system_powerpc64_linux_info);
  1090. {$endif powerpc64}
  1091. {$ifdef alpha}
  1092. RegisterExternalLinker(system_alpha_linux_info,TLinkerLinux);
  1093. RegisterImport(system_alpha_linux,timportliblinux);
  1094. RegisterExport(system_alpha_linux,texportliblinux);
  1095. RegisterTarget(system_alpha_linux_info);
  1096. {$endif alpha}
  1097. {$ifdef x86_64}
  1098. RegisterExternalLinker(system_x86_64_linux_info,TLinkerLinux);
  1099. RegisterImport(system_x86_64_linux,timportliblinux);
  1100. RegisterExport(system_x86_64_linux,texportliblinux);
  1101. RegisterTarget(system_x86_64_linux_info);
  1102. {$endif x86_64}
  1103. {$ifdef SPARC}
  1104. RegisterExternalLinker(system_sparc_linux_info,TLinkerLinux);
  1105. RegisterImport(system_SPARC_linux,timportliblinux);
  1106. RegisterExport(system_SPARC_linux,texportliblinux);
  1107. RegisterTarget(system_SPARC_linux_info);
  1108. {$endif SPARC}
  1109. {$ifdef ARM}
  1110. RegisterExternalLinker(system_arm_linux_info,TLinkerLinux);
  1111. RegisterImport(system_arm_linux,timportliblinux);
  1112. RegisterExport(system_arm_linux,texportliblinux);
  1113. RegisterTarget(system_arm_linux_info);
  1114. {$endif ARM}
  1115. RegisterRes(res_elf_info,TWinLikeResourceFile);
  1116. end.