t_sunos.pas 14 KB

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