t_sunos.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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,link,comprsrc,rescmn,i_sunos,ogbase;
  34. type
  35. timportlibsolaris=class(timportlib)
  36. procedure generatelib;override;
  37. end;
  38. texportlibsolaris=class(texportlib)
  39. procedure preparelib(const s : string);override;
  40. procedure exportprocedure(hp : texported_item);override;
  41. procedure exportvar(hp : texported_item);override;
  42. procedure generatelib;override;
  43. end;
  44. tlinkersolaris=class(texternallinker)
  45. private
  46. Glibc2,
  47. Glibc21 : boolean;
  48. Function WriteResponseFile(isdll:boolean) : Boolean;
  49. public
  50. constructor Create;override;
  51. procedure SetDefaultInfo;override;
  52. function MakeExecutable:boolean;override;
  53. function MakeSharedLibrary:boolean;override;
  54. end;
  55. {*****************************************************************************
  56. TIMPORTLIBsolaris
  57. *****************************************************************************}
  58. procedure timportlibsolaris.generatelib;
  59. var
  60. i : longint;
  61. ImportLibrary : TImportLibrary;
  62. begin
  63. for i:=0 to current_module.ImportLibraryList.Count-1 do
  64. begin
  65. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  66. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  67. end;
  68. end;
  69. {*****************************************************************************
  70. TEXPORTLIBsolaris
  71. *****************************************************************************}
  72. procedure texportlibsolaris.preparelib(const s:string);
  73. begin
  74. end;
  75. procedure texportlibsolaris.exportprocedure(hp : texported_item);
  76. var
  77. hp2 : texported_item;
  78. begin
  79. { first test the index value }
  80. if (hp.options and eo_index)<>0 then
  81. begin
  82. Message1(parser_e_no_export_with_index_for_target,'solaris');
  83. exit;
  84. end;
  85. { use pascal name is none specified }
  86. if (hp.options and eo_name)=0 then
  87. begin
  88. hp.name:=stringdup(hp.sym.name);
  89. hp.options:=hp.options or eo_name;
  90. end;
  91. { now place in correct order }
  92. hp2:=texported_item(current_module._exports.first);
  93. while assigned(hp2) and
  94. (hp.name^>hp2.name^) do
  95. hp2:=texported_item(hp2.next);
  96. { insert hp there !! }
  97. if assigned(hp2) and (hp2.name^=hp.name^) then
  98. begin
  99. { this is not allowed !! }
  100. Message1(parser_e_export_name_double,hp.name^);
  101. exit;
  102. end;
  103. if hp2=texported_item(current_module._exports.first) then
  104. current_module._exports.insert(hp)
  105. else if assigned(hp2) then
  106. begin
  107. hp.next:=hp2;
  108. hp.previous:=hp2.previous;
  109. if assigned(hp2.previous) then
  110. hp2.previous.next:=hp;
  111. hp2.previous:=hp;
  112. end
  113. else
  114. current_module._exports.concat(hp);
  115. end;
  116. procedure texportlibsolaris.exportvar(hp : texported_item);
  117. begin
  118. hp.is_var:=true;
  119. exportprocedure(hp);
  120. end;
  121. procedure texportlibsolaris.generatelib;
  122. var
  123. hp2 : texported_item;
  124. pd : tprocdef;
  125. begin
  126. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  127. hp2:=texported_item(current_module._exports.first);
  128. while assigned(hp2) do
  129. begin
  130. if (not hp2.is_var) and
  131. (hp2.sym.typ=procsym) then
  132. begin
  133. { the manglednames can already be the same when the procedure
  134. is declared with cdecl }
  135. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  136. if pd.mangledname<>hp2.name^ then
  137. begin
  138. { place jump in al_procedures }
  139. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  140. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  141. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],pd.mangledname);
  142. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  143. end;
  144. end
  145. else
  146. Message1(parser_e_no_export_of_variables_for_target,'linux');
  147. hp2:=texported_item(hp2.next);
  148. end;
  149. end;
  150. {*****************************************************************************
  151. TLINKERsolaris
  152. *****************************************************************************}
  153. Constructor TLinkersolaris.Create;
  154. begin
  155. Inherited Create;
  156. if NOT Dontlinkstdlibpath Then
  157. LibrarySearchPath.AddPath(sysrootpath,'/lib;/usr/lib;/usr/X11R6/lib;/opt/sfw/lib',true);
  158. {$ifdef LinkTest}
  159. if (cs_link_staticflag in current_settings.globalswitches) then WriteLN('ForceLinkStaticFlag');
  160. if (cs_link_static in current_settings.globalswitches) then WriteLN('LinkStatic-Flag');
  161. if (cs_link_shared in current_settings.globalswitches) then WriteLN('LinkSynamicFlag');
  162. {$EndIf}
  163. end;
  164. procedure TLinkersolaris.SetDefaultInfo;
  165. {
  166. This will also detect which libc version will be used
  167. }
  168. begin
  169. Glibc2:=false;
  170. Glibc21:=false;
  171. with Info do
  172. begin
  173. {$IFDEF GnuLd}
  174. ExeCmd[1]:='gld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  175. DllCmd[1]:='gld $OPT -shared -L. -o $EXE $RES';
  176. DllCmd[2]:='gstrip --strip-unneeded $EXE';
  177. DynamicLinker:=''; { Gnu uses the default }
  178. Glibc21:=false;
  179. {$ELSE}
  180. Not Implememted
  181. {$ENDIF}
  182. (* Linux Stuff not needed?
  183. { first try glibc2 } // muss noch gendert werden
  184. if FileExists(DynamicLinker) then
  185. begin
  186. Glibc2:=true;
  187. { Check for 2.0 files, else use the glibc 2.1 stub }
  188. if FileExists('/lib/ld-2.0.*') then
  189. Glibc21:=false
  190. else
  191. Glibc21:=true;
  192. end
  193. else
  194. DynamicLinker:='/lib/ld-linux.so.1';
  195. *)
  196. end;
  197. end;
  198. Function TLinkersolaris.WriteResponseFile(isdll:boolean) : Boolean;
  199. Var
  200. linkres : TLinkRes;
  201. i : longint;
  202. { cprtobj,
  203. gprtobj,
  204. prtobj : string[80];}
  205. HPath : TCmdStrListItem;
  206. s,s2 : TCmdStr;
  207. linkdynamic,
  208. linklibc : boolean;
  209. begin
  210. WriteResponseFile:=False;
  211. { set special options for some targets }
  212. linkdynamic:=not(SharedLibFiles.empty);
  213. { linkdynamic:=false; // da nicht getestet }
  214. linklibc:=(SharedLibFiles.Find('c')<>nil);
  215. { prtobj:='prt0';
  216. cprtobj:='cprt0';
  217. gprtobj:='gprt0';}
  218. if cs_profile in current_settings.moduleswitches then
  219. begin
  220. { prtobj:=gprtobj;}
  221. if not glibc2 then
  222. AddSharedLibrary('gmon');
  223. AddSharedLibrary('c');
  224. linklibc:=true;
  225. end
  226. else
  227. begin
  228. if linklibc then
  229. begin
  230. { prtobj:=cprtobj;}
  231. end
  232. else
  233. AddSharedLibrary('c'); { quick hack: this solaris implementation needs alwys libc }
  234. end;
  235. { Open link.res file }
  236. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  237. { Write path to search libraries }
  238. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  239. while assigned(HPath) do
  240. begin
  241. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  242. HPath:=TCmdStrListItem(HPath.Next);
  243. end;
  244. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  245. while assigned(HPath) do
  246. begin
  247. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  248. HPath:=TCmdStrListItem(HPath.Next);
  249. end;
  250. LinkRes.Add('INPUT(');
  251. { add objectfiles, start with prt0 always }
  252. { solaris port contains _start inside the system unit, it
  253. needs only one entry because it is linked always against libc
  254. if prtobj<>'' then
  255. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  256. }
  257. { try to add crti and crtbegin if linking to C }
  258. if linklibc then { Needed in solaris? }
  259. begin
  260. { if librarysearchpath.FindFile('crtbegin.o',s) then
  261. LinkRes.AddFileName(s);}
  262. if librarysearchpath.FindFile('crti.o',false,s) then
  263. LinkRes.AddFileName(s);
  264. end;
  265. { main objectfiles }
  266. while not ObjectFiles.Empty do
  267. begin
  268. s:=ObjectFiles.GetFirst;
  269. if s<>'' then
  270. LinkRes.AddFileName(maybequoted(s));
  271. end;
  272. LinkRes.Add(')');
  273. { Write staticlibraries }
  274. if not StaticLibFiles.Empty then
  275. begin
  276. LinkRes.Add('GROUP(');
  277. While not StaticLibFiles.Empty do
  278. begin
  279. S:=StaticLibFiles.GetFirst;
  280. LinkRes.AddFileName(maybequoted(s))
  281. end;
  282. LinkRes.Add(')');
  283. end;
  284. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  285. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  286. if not SharedLibFiles.Empty then
  287. begin
  288. LinkRes.Add('INPUT(');
  289. While not SharedLibFiles.Empty do
  290. begin
  291. S:=SharedLibFiles.GetFirst;
  292. if s<>'c' then
  293. begin
  294. i:=Pos(target_info.sharedlibext,S);
  295. if i>0 then
  296. Delete(S,i,255);
  297. LinkRes.Add('-l'+s);
  298. end
  299. else
  300. begin
  301. linklibc:=true;
  302. linkdynamic:=false; { libc will include the ld-solaris (war ld-linux) for us }
  303. end;
  304. end;
  305. { be sure that libc is the last lib }
  306. if linklibc then
  307. LinkRes.Add('-lc');
  308. { when we have -static for the linker the we also need libgcc }
  309. if (cs_link_staticflag in current_settings.globalswitches) then begin
  310. LinkRes.Add('-lgcc');
  311. end;
  312. if linkdynamic and (Info.DynamicLinker<>'') then { gld has a default, DynamicLinker is not set in solaris }
  313. LinkRes.AddFileName(Info.DynamicLinker);
  314. LinkRes.Add(')');
  315. end;
  316. { objects which must be at the end }
  317. if linklibc then {needed in solaris ? }
  318. begin
  319. if {librarysearchpath.FindFile('crtend.o',s1) or}
  320. librarysearchpath.FindFile('crtn.o',false,s2) then
  321. begin
  322. LinkRes.Add('INPUT(');
  323. { LinkRes.AddFileName(s1);}
  324. LinkRes.AddFileName(s2);
  325. LinkRes.Add(')');
  326. end;
  327. end;
  328. { Write and Close response }
  329. linkres.writetodisk;
  330. LinkRes.Free;
  331. WriteResponseFile:=True;
  332. end;
  333. function TLinkersolaris.MakeExecutable:boolean;
  334. var
  335. binstr,
  336. cmdstr : TCmdStr;
  337. success : boolean;
  338. DynLinkStr : string[60];
  339. StaticStr,
  340. StripStr : string[40];
  341. begin
  342. if not(cs_link_nolink in current_settings.globalswitches) then
  343. Message1(exec_i_linking,current_module.exefilename^);
  344. { Create some replacements }
  345. StaticStr:='';
  346. StripStr:='';
  347. DynLinkStr:='';
  348. if (cs_link_staticflag in current_settings.globalswitches) then
  349. StaticStr:='-Bstatic';
  350. if (cs_link_strip in current_settings.globalswitches) then
  351. StripStr:='-s';
  352. If (cs_profile in current_settings.moduleswitches) or
  353. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  354. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  355. { solaris sets DynamicLinker, but gld will (hopefully) defaults to -Bdynamic and add the default-linker }
  356. { Write used files and libraries }
  357. WriteResponseFile(false);
  358. { Call linker }
  359. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  360. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  361. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  362. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  363. Replace(cmdstr,'$STATIC',StaticStr);
  364. Replace(cmdstr,'$STRIP',StripStr);
  365. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  366. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  367. { Remove ReponseFile }
  368. {$IFNDEF LinkTest}
  369. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  370. DeleteFile(outputexedir+Info.ResName);
  371. {$ENDIF}
  372. MakeExecutable:=success; { otherwise a recursive call to link method }
  373. end;
  374. Function TLinkersolaris.MakeSharedLibrary:boolean;
  375. var
  376. binstr,
  377. cmdstr : TCmdStr;
  378. success : boolean;
  379. begin
  380. MakeSharedLibrary:=false;
  381. if not(cs_link_nolink in current_settings.globalswitches) then
  382. Message1(exec_i_linking,current_module.sharedlibfilename^);
  383. { Write used files and libraries }
  384. WriteResponseFile(true);
  385. { Call linker }
  386. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  387. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  388. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  389. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  390. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  391. { Strip the library ? }
  392. if success and (cs_link_strip in current_settings.globalswitches) then
  393. begin
  394. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  395. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  396. success:=DoExec(utilsprefix+FindUtil(binstr),cmdstr,true,false);
  397. end;
  398. { Remove ReponseFile }
  399. {$IFNDEF LinkTest}
  400. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  401. DeleteFile(outputexedir+Info.ResName);
  402. {$ENDIF}
  403. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  404. end;
  405. {*****************************************************************************
  406. Initialize
  407. *****************************************************************************}
  408. initialization
  409. {$ifdef i386}
  410. RegisterExternalLinker(system_i386_solaris_info,TLinkersolaris);
  411. RegisterImport(system_i386_solaris,TImportLibsolaris);
  412. RegisterExport(system_i386_solaris,TExportLibsolaris);
  413. RegisterTarget(system_i386_solaris_info);
  414. {$endif i386}
  415. {$ifdef sparc}
  416. RegisterExternalLinker(system_sparc_solaris_info,TLinkersolaris);
  417. RegisterImport(system_sparc_solaris,TImportLibsolaris);
  418. RegisterExport(system_sparc_solaris,TExportLibsolaris);
  419. RegisterTarget(system_sparc_solaris_info);
  420. {$endif sparc}
  421. RegisterRes(res_elf_info,TWinLikeResourceFile);
  422. end.