t_sunos.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. {
  2. Copyright (c) 1998-2008 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) solaris 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_sunos;
  19. {$i fpcdefs.inc}
  20. interface
  21. { copy from t_linux
  22. // Up to now we use gld since the solaris ld seems not support .res-files}
  23. {-$DEFINE LinkTest} { DON't del link.res and write Info }
  24. {$DEFINE GnuLd}{The other is not implemented }
  25. implementation
  26. uses
  27. sysutils,
  28. cutils,cfileutl,cclasses,
  29. verbose,systems,globtype,globals,
  30. symconst,script,
  31. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,symsym,symdef,
  32. cgobj,
  33. import,export,expunix,link,comprsrc,rescmn,i_sunos,ogbase;
  34. type
  35. timportlibsolaris=class(timportlib)
  36. procedure generatelib;override;
  37. end;
  38. texportlibsolaris=class(texportlibunix)
  39. (*
  40. procedure setinitname(list: TAsmList; const s: string); override;
  41. procedure setfininame(list: TAsmList; const s: string); override;
  42. *)
  43. end;
  44. tlinkersolaris=class(texternallinker)
  45. private
  46. Glibc2,
  47. Glibc21 : boolean;
  48. use_gnu_ld : boolean;
  49. linkres : TLinkRes;
  50. Function WriteResponseFile(isdll:boolean) : Boolean;
  51. public
  52. constructor Create;override;
  53. procedure SetDefaultInfo;override;
  54. function MakeExecutable:boolean;override;
  55. function MakeSharedLibrary:boolean;override;
  56. end;
  57. {*****************************************************************************
  58. TIMPORTLIBsolaris
  59. *****************************************************************************}
  60. procedure timportlibsolaris.generatelib;
  61. var
  62. i : longint;
  63. ImportLibrary : TImportLibrary;
  64. begin
  65. for i:=0 to current_module.ImportLibraryList.Count-1 do
  66. begin
  67. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  68. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  69. end;
  70. end;
  71. {*****************************************************************************
  72. TEXPORTLIBsolaris
  73. *****************************************************************************}
  74. (*
  75. procedure texportlibsolaris.setinitname(list: TAsmList; const s: string);
  76. begin
  77. inherited setinitname(list,s);
  78. {$ifdef sparc}
  79. new_section(list,sec_init,'',4);
  80. list.concat(tai_symbol.createname_global('_init',AT_FUNCTION,0));
  81. list.concat(taicpu.op_reg_const_reg(A_SAVE,NR_STACK_POINTER_REG,-96,NR_STACK_POINTER_REG));
  82. {$endif sparc}
  83. end;
  84. procedure texportlibsolaris.setfininame(list: TAsmList; const s: string);
  85. begin
  86. inherited setfininame(list,s);
  87. {$ifdef sparc}
  88. new_section(list,sec_fini,'',4);
  89. list.concat(tai_symbol.createname_global('_fini',AT_FUNCTION,0));
  90. list.concat(taicpu.op_reg_const_reg(A_SAVE,NR_STACK_POINTER_REG,-96,NR_STACK_POINTER_REG));
  91. {$endif sparc}
  92. end;
  93. *)
  94. {*****************************************************************************
  95. TLINKERsolaris
  96. *****************************************************************************}
  97. {$ifdef x86_64}
  98. const
  99. new_gnu_emul = '-m elf_x86_64_sol2';
  100. old_gnu_emul = '-m elf_x86_64';
  101. {$endif}
  102. {$ifdef i386}
  103. const
  104. new_gnu_emul = '-m elf_i386_sol2';
  105. old_gnu_emul = '-m elf_i386';
  106. {$endif }
  107. {$ifdef sparc}
  108. const
  109. { no emulation specification needed, as long as only 32-bit is supported }
  110. new_gnu_emul = '';
  111. old_gnu_emul = '';
  112. {$endif}
  113. Constructor TLinkersolaris.Create;
  114. begin
  115. Inherited Create;
  116. if cs_link_native in init_settings.globalswitches then
  117. use_gnu_ld:=false
  118. else
  119. use_gnu_ld:=true;
  120. if NOT Dontlinkstdlibpath Then
  121. {$ifdef x86_64}
  122. LibrarySearchPath.AddPath(sysrootpath,'/lib/64;/usr/lib/64;/usr/X11R6/lib/64;/opt/sfw/lib/64',true);
  123. {$else not x86_64}
  124. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib;/opt/sfw/lib',true);
  125. {$endif not x86_64}
  126. {$ifdef LinkTest}
  127. if (cs_link_staticflag in current_settings.globalswitches) then WriteLN('ForceLinkStaticFlag');
  128. if (cs_link_static in current_settings.globalswitches) then WriteLN('LinkStatic-Flag');
  129. if (cs_link_shared in current_settings.globalswitches) then WriteLN('LinkSynamicFlag');
  130. {$EndIf}
  131. end;
  132. procedure TLinkersolaris.SetDefaultInfo;
  133. {
  134. This will also detect which libc version will be used
  135. }
  136. {$ifdef x86_64}
  137. const
  138. gld = 'gld $EMUL ';
  139. solaris_ld = '/usr/bin/ld -64 ';
  140. {$endif}
  141. {$ifdef i386}
  142. const
  143. gld = 'gld $EMUL';
  144. solaris_ld = '/usr/bin/ld ';
  145. {$endif }
  146. {$ifdef sparc}
  147. const
  148. gld = 'gld ';
  149. solaris_ld = 'ld ';
  150. {$endif}
  151. begin
  152. Glibc2:=false;
  153. Glibc21:=false;
  154. with Info do
  155. begin
  156. {$IFDEF GnuLd}
  157. ExeCmd[1]:=gld + '$OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  158. ExeCmd[2]:=solaris_ld + '$OPT $DYNLINK $STATIC $STRIP -L . -o $EXE $RESDATA $REDIRECT';
  159. DllCmd[1]:=gld + '$OPT $INITFINI -shared -L. $MAP -o $EXE $RES';
  160. DllCmd[2]:='gstrip --strip-unneeded $EXE';
  161. DllCmd[3]:=solaris_ld + '$OPT $INITFINI -M $VERSIONFILE $MAP -G -Bdynamic -L. -o $EXE $RESDATA $REDIRECT';
  162. DynamicLinker:=''; { Gnu uses the default }
  163. Glibc21:=false;
  164. {$ELSE}
  165. Not Implememted
  166. {$ENDIF}
  167. end;
  168. end;
  169. Function TLinkersolaris.WriteResponseFile(isdll:boolean) : Boolean;
  170. Var
  171. i : longint;
  172. { cprtobj,
  173. gprtobj,
  174. prtobj : string[80];}
  175. HPath : TCmdStrListItem;
  176. s,s2 : TCmdStr;
  177. linkdynamic,
  178. linklibc : boolean;
  179. LinkRes2 : TLinkRes;
  180. begin
  181. WriteResponseFile:=False;
  182. { set special options for some targets }
  183. linkdynamic:=not(SharedLibFiles.empty);
  184. { linkdynamic:=false; // da nicht getestet }
  185. linklibc:=(SharedLibFiles.Find('c')<>nil);
  186. { prtobj:='prt0';
  187. cprtobj:='cprt0';
  188. gprtobj:='gprt0';}
  189. if cs_profile in current_settings.moduleswitches then
  190. begin
  191. { prtobj:=gprtobj;}
  192. if not glibc2 then
  193. AddSharedLibrary('gmon');
  194. AddSharedLibrary('c');
  195. linklibc:=true;
  196. end
  197. else
  198. begin
  199. if linklibc then
  200. begin
  201. { prtobj:=cprtobj;}
  202. end
  203. else
  204. AddSharedLibrary('c'); { quick hack: this solaris implementation needs alwys libc }
  205. end;
  206. if use_gnu_ld then
  207. begin
  208. { Open link.res file }
  209. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,true);
  210. { Write path to search libraries }
  211. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  212. while assigned(HPath) do
  213. begin
  214. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  215. HPath:=TCmdStrListItem(HPath.Next);
  216. end;
  217. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  218. while assigned(HPath) do
  219. begin
  220. LinkRes.Add('SEARCH_DIR("'+HPath.Str+'")');
  221. HPath:=TCmdStrListItem(HPath.Next);
  222. end;
  223. { force local symbol resolution (i.e., inside the shared }
  224. { library itself) for all non-exorted symbols, otherwise }
  225. { several RTL symbols of FPC-compiled shared libraries }
  226. { will be bound to those of a single shared library or }
  227. { to the main program }
  228. if (isdll) then
  229. begin
  230. LinkRes.add('VERSION');
  231. LinkRes.add('{ DEFAULT'); { gld 2.25 does not support anonymous version }
  232. LinkRes.add(' {');
  233. if not texportlibunix(exportlib).exportedsymnames.empty then
  234. begin
  235. LinkRes.add(' global:');
  236. repeat
  237. LinkRes.add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  238. until texportlibunix(exportlib).exportedsymnames.empty;
  239. end;
  240. LinkRes.add(' local:');
  241. LinkRes.add(' *;');
  242. LinkRes.add(' };');
  243. LinkRes.add('}');
  244. end;
  245. LinkRes.Add('INPUT(');
  246. { add objectfiles, start with prt0 always }
  247. { solaris port contains _start inside the system unit, it
  248. needs only one entry because it is linked always against libc
  249. if prtobj<>'' then
  250. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  251. }
  252. { try to add crti and crtbegin if linking to C }
  253. if linklibc then { Needed in solaris? }
  254. begin
  255. { if librarysearchpath.FindFile('crtbegin.o',s) then
  256. LinkRes.AddFileName(s);}
  257. if librarysearchpath.FindFile('crti.o',false,s) then
  258. LinkRes.AddFileName(s);
  259. end;
  260. { main objectfiles }
  261. while not ObjectFiles.Empty do
  262. begin
  263. s:=ObjectFiles.GetFirst;
  264. if s<>'' then
  265. LinkRes.AddFileName(maybequoted(s));
  266. end;
  267. LinkRes.Add(')');
  268. { Write staticlibraries }
  269. if not StaticLibFiles.Empty then
  270. begin
  271. LinkRes.Add('GROUP(');
  272. While not StaticLibFiles.Empty do
  273. begin
  274. S:=StaticLibFiles.GetFirst;
  275. LinkRes.AddFileName(maybequoted(s))
  276. end;
  277. LinkRes.Add(')');
  278. end;
  279. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  280. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  281. if not SharedLibFiles.Empty then
  282. begin
  283. LinkRes.Add('INPUT(');
  284. While not SharedLibFiles.Empty do
  285. begin
  286. S:=SharedLibFiles.GetFirst;
  287. if s<>'c' then
  288. begin
  289. i:=Pos(target_info.sharedlibext,S);
  290. if i>0 then
  291. Delete(S,i,255);
  292. LinkRes.Add('-l'+s);
  293. end
  294. else
  295. begin
  296. LinkRes.Add('-lc');
  297. linklibc:=true;
  298. linkdynamic:=false; { libc will include the ld-solaris (war ld-linux) for us }
  299. end;
  300. end;
  301. { be sure that libc is the last lib }
  302. if linklibc then
  303. LinkRes.Add('-lc');
  304. { when we have -static for the linker the we also need libgcc }
  305. if (cs_link_staticflag in current_settings.globalswitches) then begin
  306. LinkRes.Add('-lgcc');
  307. end;
  308. if linkdynamic and (Info.DynamicLinker<>'') then { gld has a default, DynamicLinker is not set in solaris }
  309. LinkRes.AddFileName(Info.DynamicLinker);
  310. LinkRes.Add(')');
  311. end;
  312. { objects which must be at the end }
  313. if linklibc then {needed in solaris ? }
  314. begin
  315. if {librarysearchpath.FindFile('crtend.o',s1) or}
  316. librarysearchpath.FindFile('crtn.o',false,s2) then
  317. begin
  318. LinkRes.Add('INPUT(');
  319. { LinkRes.AddFileName(s1);}
  320. LinkRes.AddFileName(s2);
  321. LinkRes.Add(')');
  322. end;
  323. end;
  324. { Write and Close response }
  325. linkres.writetodisk;
  326. LinkRes.Free;
  327. end
  328. else { not use_gnu_ld }
  329. begin
  330. { Open TlinkRes, will not be written to disk }
  331. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName+'2',false);
  332. { Write path to search libraries }
  333. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  334. while assigned(HPath) do
  335. begin
  336. LinkRes.Add('-L '+maybequoted(HPath.Str));
  337. HPath:=TCmdStrListItem(HPath.Next);
  338. end;
  339. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  340. while assigned(HPath) do
  341. begin
  342. LinkRes.Add('-L '+maybequoted(HPath.Str));
  343. HPath:=TCmdStrListItem(HPath.Next);
  344. end;
  345. { force local symbol resolution (i.e., inside the shared }
  346. { library itself) for all non-exorted symbols, otherwise }
  347. { several RTL symbols of FPC-compiled shared libraries }
  348. { will be bound to those of a single shared library or }
  349. { to the main program }
  350. if (isdll) then
  351. begin
  352. LinkRes2:=TLinkRes.Create(outputexedir+Info.ResName,true);
  353. // LinkRes2.add('VERSION'); not needed for now
  354. LinkRes2.add(' {');
  355. if not texportlibunix(exportlib).exportedsymnames.empty then
  356. begin
  357. LinkRes2.add(' global:');
  358. repeat
  359. LinkRes2.add(' '+texportlibunix(exportlib).exportedsymnames.getfirst+';');
  360. until texportlibunix(exportlib).exportedsymnames.empty;
  361. end;
  362. LinkRes2.add(' local:');
  363. LinkRes2.add(' *;');
  364. LinkRes2.add(' };');
  365. LinkRes2.writetodisk;
  366. LinkRes2.Free;
  367. end;
  368. { add objectfiles, start with prt0 always }
  369. { solaris port contains _start inside the system unit, it
  370. needs only one entry because it is linked always against libc
  371. if prtobj<>'' then
  372. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  373. }
  374. { try to add crti and crtbegin if linking to C }
  375. if linklibc then { Needed in solaris? }
  376. begin
  377. { if librarysearchpath.FindFile('crtbegin.o',s) then
  378. LinkRes.AddFileName(s);}
  379. if librarysearchpath.FindFile('crti.o',false,s) then
  380. LinkRes.AddFileName(s);
  381. end;
  382. { main objectfiles }
  383. while not ObjectFiles.Empty do
  384. begin
  385. s:=ObjectFiles.GetFirst;
  386. if s<>'' then
  387. LinkRes.AddFileName(maybequoted(s));
  388. end;
  389. { Write staticlibraries }
  390. if not StaticLibFiles.Empty then
  391. begin
  392. linkres.add('-(');
  393. While not StaticLibFiles.Empty do
  394. begin
  395. S:=StaticLibFiles.GetFirst;
  396. LinkRes.AddFileName(maybequoted(s))
  397. end;
  398. linkres.add('-)');
  399. end;
  400. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  401. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  402. if not SharedLibFiles.Empty then
  403. begin
  404. While not SharedLibFiles.Empty do
  405. begin
  406. S:=SharedLibFiles.GetFirst;
  407. if s<>'c' then
  408. begin
  409. i:=Pos(target_info.sharedlibext,S);
  410. if i>0 then
  411. Delete(S,i,255);
  412. LinkRes.Add('-l'+s);
  413. end
  414. else
  415. begin
  416. linklibc:=true;
  417. linkdynamic:=false; { libc will include the ld-solaris (war ld-linux) for us }
  418. end;
  419. end;
  420. { be sure that libc is the last lib }
  421. if linklibc then
  422. LinkRes.Add('-lc');
  423. { when we have -static for the linker the we also need libgcc }
  424. if (cs_link_staticflag in current_settings.globalswitches) then begin
  425. LinkRes.Add('-lgcc');
  426. end;
  427. if linkdynamic and (Info.DynamicLinker<>'') then { gld has a default, DynamicLinker is not set in solaris }
  428. LinkRes.AddFileName(Info.DynamicLinker);
  429. end;
  430. { objects which must be at the end }
  431. if linklibc then {needed in solaris ? }
  432. begin
  433. if {librarysearchpath.FindFile('crtend.o',s1) or}
  434. librarysearchpath.FindFile('crtn.o',false,s2) then
  435. begin
  436. { LinkRes.AddFileName(s1);}
  437. LinkRes.AddFileName(s2);
  438. end;
  439. end;
  440. { Write and Close response }
  441. //linkres.writetodisk;
  442. //LinkRes.Free;
  443. end;
  444. WriteResponseFile:=True;
  445. end;
  446. function TLinkersolaris.MakeExecutable:boolean;
  447. var
  448. binstr,
  449. s, linkstr,
  450. cmdstr : TCmdStr;
  451. success : boolean;
  452. DynLinkStr : string[60];
  453. StaticStr, RedirectStr,
  454. StripStr : string[40];
  455. begin
  456. success:=false;
  457. if not(cs_link_nolink in current_settings.globalswitches) then
  458. Message1(exec_i_linking,current_module.exefilename);
  459. { Create some replacements }
  460. StaticStr:='';
  461. StripStr:='';
  462. RedirectStr:='';
  463. DynLinkStr:='';
  464. if (cs_link_staticflag in current_settings.globalswitches) then
  465. StaticStr:='-Bstatic';
  466. if (cs_link_strip in current_settings.globalswitches) then
  467. StripStr:='-s';
  468. if (cs_link_map in current_settings.globalswitches) then
  469. begin
  470. if use_gnu_ld then
  471. StripStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'))
  472. else
  473. begin
  474. StripStr:='-m';
  475. RedirectStr:=' > '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  476. end;
  477. end;
  478. If (cs_profile in current_settings.moduleswitches) or
  479. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  480. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  481. if rlinkpath<>'' then
  482. if use_gnu_ld then
  483. DynLinkStr:=DynLinkStr+' --rpath-link '+rlinkpath
  484. else
  485. DynLinkStr:=DynLinkStr+' -R '+rlinkpath;
  486. { solaris sets DynamicLinker, but gld will (hopefully) defaults to -Bdynamic and add the default-linker }
  487. { Write used files and libraries }
  488. WriteResponseFile(false);
  489. { Call linker }
  490. if use_gnu_ld then
  491. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr)
  492. else
  493. SplitBinCmd(Info.ExeCmd[2],binstr,cmdstr);
  494. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  495. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  496. if use_gnu_ld then
  497. begin
  498. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  499. Replace(cmdstr,'$EMUL',new_gnu_emul);
  500. end
  501. else
  502. begin
  503. linkstr:='';
  504. while not linkres.data.Empty do
  505. begin
  506. s:=linkres.data.GetFirst;
  507. if s<>'' then
  508. linkstr:=linkstr+' '+s;
  509. end;
  510. linkres.free;
  511. Replace(cmdstr,'$RESDATA',linkstr);
  512. end;
  513. Replace(cmdstr,'$STATIC',StaticStr);
  514. Replace(cmdstr,'$STRIP',StripStr);
  515. Replace(cmdstr,'$REDIRECT',RedirectStr);
  516. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  517. if BinStr[1]<>'/' then
  518. BinStr:=FindUtil(utilsprefix+BinStr);
  519. if (cs_link_nolink in current_settings.globalswitches) then
  520. begin
  521. AsmRes.Add('echo Linking '+ScriptFixFileName(current_module.exefilename));
  522. AsmRes.Add('OFS=$IFS');
  523. AsmRes.Add('IFS="');
  524. AsmRes.Add('"');
  525. AsmRes.Add(maybequoted(BinStr)+' '+Trim(cmdstr));
  526. AsmRes.Add('if [ $? != 0 ] ; then');
  527. AsmRes.Add('echo "Retrying with old emulation"');
  528. Replace(cmdstr,new_gnu_emul,old_gnu_emul);
  529. AsmRes.Add(maybequoted(BinStr)+' '+Trim(cmdstr));
  530. AsmRes.Add('fi');
  531. AsmRes.Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(current_module.exefilename)+'; fi');
  532. AsmRes.Add('IFS=$OFS');
  533. end
  534. else
  535. begin
  536. { We need shell if output is redirected }
  537. success:=DoExec(BinStr,Trim(CmdStr),true,RedirectStr<>'');
  538. { Try old emulation name if new failed }
  539. if use_gnu_ld and (not success) and (new_gnu_emul <> '') then
  540. begin
  541. Replace(cmdstr,new_gnu_emul,old_gnu_emul);
  542. success:=DoExec(BinStr,Trim(CmdStr),true,RedirectStr<>'');
  543. end;
  544. end;
  545. { Remove ReponseFile }
  546. {$IFNDEF LinkTest}
  547. if (success) and use_gnu_ld and
  548. not(cs_link_nolink in current_settings.globalswitches) then
  549. DeleteFile(outputexedir+Info.ResName);
  550. {$ENDIF}
  551. MakeExecutable:=success; { otherwise a recursive call to link method }
  552. end;
  553. Function TLinkersolaris.MakeSharedLibrary:boolean;
  554. var
  555. InitFiniStr : string;
  556. binstr, RedirectStr,
  557. s, linkstr, MapStr,
  558. cmdstr : TCmdStr;
  559. need_quotes, success : boolean;
  560. begin
  561. success:=false;
  562. MakeSharedLibrary:=false;
  563. if not(cs_link_nolink in current_settings.globalswitches) then
  564. Message1(exec_i_linking,current_module.sharedlibfilename);
  565. { Write used files and libraries }
  566. WriteResponseFile(true);
  567. RedirectStr:='';
  568. MapStr:='';
  569. { Create some replacements }
  570. if (cs_link_map in current_settings.globalswitches) then
  571. begin
  572. if use_gnu_ld then
  573. MapStr:='-Map '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'))
  574. else
  575. begin
  576. MapStr:='-m';
  577. RedirectStr:=' > '+maybequoted(ChangeFileExt(current_module.exefilename,'.map'));
  578. end;
  579. end;
  580. need_quotes:= (cs_link_nolink in current_settings.globalswitches) or
  581. (RedirectStr<>'');
  582. { initname and fininame may contain $, which can be wrongly interpreted
  583. in a link script, thus we surround them with single quotes
  584. in cs_link_nolink is in globalswitches }
  585. if use_gnu_ld then
  586. begin
  587. InitFiniStr:='-init ';
  588. if need_quotes then
  589. InitFiniStr:=InitFiniStr+''''+exportlib.initname+''''
  590. else
  591. InitFiniStr:=InitFiniStr+exportlib.initname;
  592. if (exportlib.fininame<>'') then
  593. begin
  594. if need_quotes then
  595. InitFiniStr:=InitFiniStr+' -fini '''+exportlib.initname+''''
  596. else
  597. InitFiniStr:=InitFiniStr+' -fini '+exportlib.fininame;
  598. end;
  599. end
  600. else
  601. begin
  602. InitFiniStr:='-z initarray=';
  603. if need_quotes then
  604. InitFiniStr:=InitFiniStr+''''+exportlib.initname+''''
  605. else
  606. InitFiniStr:=InitFiniStr+exportlib.initname;
  607. if (exportlib.fininame<>'') then
  608. begin
  609. if need_quotes then
  610. InitFiniStr:=InitFiniStr+' -z finiarray='''+exportlib.initname+''''
  611. else
  612. InitFiniStr:=InitFiniStr+' -z finiarray='+exportlib.fininame;
  613. end;
  614. end;
  615. { Call linker }
  616. if use_gnu_ld then
  617. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr)
  618. else
  619. SplitBinCmd(Info.DllCmd[3],binstr,cmdstr);
  620. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  621. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  622. Replace(cmdstr,'$INITFINI',InitFiniStr);
  623. if use_gnu_ld then
  624. begin
  625. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  626. Replace(cmdstr,'$EMUL',new_gnu_emul);
  627. end
  628. else
  629. begin
  630. Replace(cmdstr,'$VERSIONFILE',maybequoted(outputexedir+Info.ResName));
  631. linkstr:='';
  632. while not linkres.data.Empty do
  633. begin
  634. s:=linkres.data.GetFirst;
  635. if s<>'' then
  636. linkstr:=linkstr+' '+s;
  637. end;
  638. linkres.free;
  639. Replace(cmdstr,'$RESDATA',linkstr);
  640. end;
  641. Replace(cmdstr,'$REDIRECT',RedirectStr);
  642. Replace(cmdstr,'$MAP',MapStr);
  643. if BinStr[1]<>'/' then
  644. BinStr:=FindUtil(utilsprefix+BinStr);
  645. if (cs_link_nolink in current_settings.globalswitches) then
  646. begin
  647. AsmRes.Add('echo Linking '+ScriptFixFileName(current_module.exefilename));
  648. AsmRes.Add('OFS=$IFS');
  649. AsmRes.Add('IFS="');
  650. AsmRes.Add('"');
  651. AsmRes.Add(maybequoted(BinStr)+' '+Trim(cmdstr));
  652. AsmRes.Add('if [ $? != 0 ] ; then');
  653. AsmRes.Add('echo "Retrying with old emulation"');
  654. Replace(cmdstr,new_gnu_emul,old_gnu_emul);
  655. AsmRes.Add(maybequoted(BinStr)+' '+Trim(cmdstr));
  656. AsmRes.Add('fi');
  657. AsmRes.Add('if [ $? != 0 ]; then DoExitLink '+ScriptFixFileName(current_module.exefilename)+'; fi');
  658. AsmRes.Add('IFS=$OFS');
  659. end
  660. else
  661. begin
  662. { We need shell if output is redirected }
  663. success:=DoExec(BinStr,Trim(CmdStr),true,RedirectStr<>'');
  664. { Try old emulation name if new failed }
  665. if use_gnu_ld and (not success) and (new_gnu_emul <> '') then
  666. begin
  667. Replace(cmdstr,new_gnu_emul,old_gnu_emul);
  668. success:=DoExec(BinStr,Trim(CmdStr),true,RedirectStr<>'');
  669. end;
  670. end;
  671. { Strip the library ? }
  672. if success and (cs_link_strip in current_settings.globalswitches) then
  673. begin
  674. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  675. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  676. success:=DoExec(utilsprefix+FindUtil(binstr),cmdstr,true,false);
  677. end;
  678. { Remove ReponseFile }
  679. {$IFNDEF LinkTest}
  680. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  681. DeleteFile(outputexedir+Info.ResName);
  682. {$ENDIF}
  683. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  684. end;
  685. {*****************************************************************************
  686. Initialize
  687. *****************************************************************************}
  688. initialization
  689. RegisterLinker(ld_solaris,TLinkerSolaris);
  690. {$ifdef i386}
  691. RegisterImport(system_i386_solaris,TImportLibsolaris);
  692. RegisterExport(system_i386_solaris,TExportLibsolaris);
  693. RegisterTarget(system_i386_solaris_info);
  694. {$endif i386}
  695. {$ifdef x86_64}
  696. RegisterImport(system_x86_64_solaris,TImportLibsolaris);
  697. RegisterExport(system_x86_64_solaris,TExportLibsolaris);
  698. RegisterTarget(system_x86_64_solaris_info);
  699. {$endif x86_64}
  700. {$ifdef sparc}
  701. RegisterImport(system_sparc_solaris,TImportLibsolaris);
  702. RegisterExport(system_sparc_solaris,TExportLibsolaris);
  703. RegisterTarget(system_sparc_solaris_info);
  704. {$endif sparc}
  705. RegisterRes(res_elf_info,TWinLikeResourceFile);
  706. end.