t_sunos.pas 14 KB

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