t_sunos.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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;
  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_always);
  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_always);
  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(current_asmdata.asmlists[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. current_asmdata.asmlists[al_procedures].concat(tai_align.create(target_info.alignment.procalign));
  154. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  155. cg.a_jmp_name(current_asmdata.asmlists[al_procedures],tprocsym(hp2.sym).first_procdef.mangledname);
  156. current_asmdata.asmlists[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. { solaris port contains _start inside the system unit, it
  265. needs only one entry because it is linked always against libc
  266. if prtobj<>'' then
  267. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  268. }
  269. { try to add crti and crtbegin if linking to C }
  270. if linklibc then { Needed in solaris? }
  271. begin
  272. { if librarysearchpath.FindFile('crtbegin.o',s) then
  273. LinkRes.AddFileName(s);}
  274. if librarysearchpath.FindFile('crti.o',s) then
  275. LinkRes.AddFileName(s);
  276. end;
  277. { main objectfiles }
  278. while not ObjectFiles.Empty do
  279. begin
  280. s:=ObjectFiles.GetFirst;
  281. if s<>'' then
  282. LinkRes.AddFileName(maybequoted(s));
  283. end;
  284. LinkRes.Add(')');
  285. { Write staticlibraries }
  286. if not StaticLibFiles.Empty then
  287. begin
  288. LinkRes.Add('GROUP(');
  289. While not StaticLibFiles.Empty do
  290. begin
  291. S:=StaticLibFiles.GetFirst;
  292. LinkRes.AddFileName(maybequoted(s))
  293. end;
  294. LinkRes.Add(')');
  295. end;
  296. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  297. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  298. if not SharedLibFiles.Empty then
  299. begin
  300. LinkRes.Add('INPUT(');
  301. While not SharedLibFiles.Empty do
  302. begin
  303. S:=SharedLibFiles.GetFirst;
  304. if s<>'c' then
  305. begin
  306. i:=Pos(target_info.sharedlibext,S);
  307. if i>0 then
  308. Delete(S,i,255);
  309. LinkRes.Add('-l'+s);
  310. end
  311. else
  312. begin
  313. linklibc:=true;
  314. linkdynamic:=false; { libc will include the ld-solaris (war ld-linux) for us }
  315. end;
  316. end;
  317. { be sure that libc is the last lib }
  318. if linklibc then
  319. LinkRes.Add('-lc');
  320. { when we have -static for the linker the we also need libgcc }
  321. if (cs_link_staticflag in aktglobalswitches) then begin
  322. LinkRes.Add('-lgcc');
  323. end;
  324. if linkdynamic and (Info.DynamicLinker<>'') then { gld has a default, DynamicLinker is not set in solaris }
  325. LinkRes.AddFileName(Info.DynamicLinker);
  326. LinkRes.Add(')');
  327. end;
  328. { objects which must be at the end }
  329. if linklibc then {needed in solaris ? }
  330. begin
  331. if {librarysearchpath.FindFile('crtend.o',s1) or}
  332. librarysearchpath.FindFile('crtn.o',s2) then
  333. begin
  334. LinkRes.Add('INPUT(');
  335. { LinkRes.AddFileName(s1);}
  336. LinkRes.AddFileName(s2);
  337. LinkRes.Add(')');
  338. end;
  339. end;
  340. { Write and Close response }
  341. linkres.writetodisk;
  342. LinkRes.Free;
  343. WriteResponseFile:=True;
  344. end;
  345. function TLinkersolaris.MakeExecutable:boolean;
  346. var
  347. binstr : String;
  348. cmdstr : TCmdStr;
  349. success : boolean;
  350. DynLinkStr : string[60];
  351. StaticStr,
  352. StripStr : string[40];
  353. begin
  354. if not(cs_link_extern in aktglobalswitches) then
  355. Message1(exec_i_linking,current_module.exefilename^);
  356. { Create some replacements }
  357. StaticStr:='';
  358. StripStr:='';
  359. DynLinkStr:='';
  360. if (cs_link_staticflag in aktglobalswitches) then
  361. StaticStr:='-Bstatic';
  362. if (cs_link_strip in aktglobalswitches) then
  363. StripStr:='-s';
  364. If (cs_profile in aktmoduleswitches) or
  365. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  366. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  367. { solaris sets DynamicLinker, but gld will (hopefully) defaults to -Bdynamic and add the default-linker }
  368. { Write used files and libraries }
  369. WriteResponseFile(false);
  370. { Call linker }
  371. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  372. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  373. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  374. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  375. Replace(cmdstr,'$STATIC',StaticStr);
  376. Replace(cmdstr,'$STRIP',StripStr);
  377. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  378. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  379. { Remove ReponseFile }
  380. {$IFNDEF LinkTest}
  381. if (success) and not(cs_link_extern in aktglobalswitches) then
  382. RemoveFile(outputexedir+Info.ResName);
  383. {$ENDIF}
  384. MakeExecutable:=success; { otherwise a recursive call to link method }
  385. end;
  386. Function TLinkersolaris.MakeSharedLibrary:boolean;
  387. var
  388. binstr : String;
  389. cmdstr : TCmdStr;
  390. success : boolean;
  391. begin
  392. MakeSharedLibrary:=false;
  393. if not(cs_link_extern in aktglobalswitches) then
  394. Message1(exec_i_linking,current_module.sharedlibfilename^);
  395. { Write used files and libraries }
  396. WriteResponseFile(true);
  397. { Call linker }
  398. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  399. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  400. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  401. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  402. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  403. { Strip the library ? }
  404. if success and (cs_link_strip in aktglobalswitches) then
  405. begin
  406. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  407. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  408. success:=DoExec(utilsprefix+FindUtil(binstr),cmdstr,true,false);
  409. end;
  410. { Remove ReponseFile }
  411. {$IFNDEF LinkTest}
  412. if (success) and not(cs_link_extern in aktglobalswitches) then
  413. RemoveFile(outputexedir+Info.ResName);
  414. {$ENDIF}
  415. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  416. end;
  417. {*****************************************************************************
  418. Initialize
  419. *****************************************************************************}
  420. initialization
  421. {$ifdef i386}
  422. RegisterExternalLinker(system_i386_solaris_info,TLinkersolaris);
  423. RegisterImport(system_i386_solaris,TImportLibsolaris);
  424. RegisterExport(system_i386_solaris,TExportLibsolaris);
  425. RegisterTarget(system_i386_solaris_info);
  426. {$endif i386}
  427. {$ifdef sparc}
  428. RegisterExternalLinker(system_sparc_solaris_info,TLinkersolaris);
  429. RegisterImport(system_sparc_solaris,TImportLibsolaris);
  430. RegisterExport(system_sparc_solaris,TExportLibsolaris);
  431. RegisterTarget(system_sparc_solaris_info);
  432. {$endif sparc}
  433. end.