t_sunos.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman
  4. This unit implements support import,export,link routines
  5. for the (i386) sunos target
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit t_sunos;
  20. {$i fpcdefs.inc}
  21. interface
  22. { copy from t_linux
  23. // Up to now we use gld since the solaris ld seems not support .res-files}
  24. {-$DEFINE LinkTest} { DON't del link.res and write Info }
  25. {$DEFINE GnuLd} {The other is not implemented }
  26. implementation
  27. uses
  28. cutils,cclasses,
  29. verbose,systems,globtype,globals,
  30. symconst,script,
  31. fmodule,aasmbase,aasmtai,aasmcpu,cpubase,symsym,symdef,
  32. import,export,link,i_sunos;
  33. type
  34. timportlibsunos=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. texportlibsunos=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. tlinkersunos=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. TIMPORTLIBsunos
  59. *****************************************************************************}
  60. procedure timportlibsunos.preparelib(const s : string);
  61. begin
  62. {$ifDef LinkTest}
  63. WriteLN('Prepare import: ',s);
  64. {$EndIf}
  65. end;
  66. procedure timportlibsunos.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 timportlibsunos.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 timportlibsunos.generatelib;
  83. begin
  84. end;
  85. {*****************************************************************************
  86. TEXPORTLIBsunos
  87. *****************************************************************************}
  88. procedure texportlibsunos.preparelib(const s:string);
  89. begin
  90. end;
  91. procedure texportlibsunos.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,'SunOS');
  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 texportlibsunos.exportvar(hp : texported_item);
  133. begin
  134. hp.is_var:=true;
  135. exportprocedure(hp);
  136. end;
  137. procedure texportlibsunos.generatelib;
  138. var
  139. hp2 : texported_item;
  140. begin
  141. hp2:=texported_item(current_module._exports.first);
  142. while assigned(hp2) do
  143. begin
  144. if (not hp2.is_var) and
  145. (hp2.sym.typ=procsym) then
  146. begin
  147. { the manglednames can already be the same when the procedure
  148. is declared with cdecl }
  149. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  150. begin
  151. {$ifdef i386}
  152. { place jump in codesegment }
  153. codesegment.concat(Tai_align.Create_op(4,$90));
  154. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  155. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(tprocsym(hp2.sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION)));
  156. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  157. {$endif i386}
  158. end;
  159. end
  160. else
  161. Message1(parser_e_no_export_of_variables_for_target,'SunOS');
  162. hp2:=texported_item(hp2.next);
  163. end;
  164. end;
  165. {*****************************************************************************
  166. TLINKERSUNOS
  167. *****************************************************************************}
  168. Constructor TLinkersunos.Create;
  169. begin
  170. Inherited Create;
  171. if NOT Dontlinkstdlibpath Then
  172. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib;/opt/sfw/lib',true);
  173. {$ifdef LinkTest}
  174. if (cs_link_staticflag in aktglobalswitches) then WriteLN('ForceLinkStaticFlag');
  175. if (cs_link_static in aktglobalswitches) then WriteLN('LinkStatic-Flag');
  176. if (cs_link_shared in aktglobalswitches) then WriteLN('LinkSynamicFlag');
  177. {$EndIf}
  178. end;
  179. procedure TLinkersunos.SetDefaultInfo;
  180. {
  181. This will also detect which libc version will be used
  182. }
  183. begin
  184. Glibc2:=false;
  185. Glibc21:=false;
  186. with Info do
  187. begin
  188. {$IFDEF GnuLd}
  189. ExeCmd[1]:='gld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  190. DllCmd[1]:='gld $OPT -shared -L. -o $EXE $RES';
  191. DllCmd[2]:='strip --strip-unneeded $EXE';
  192. DynamicLinker:=''; { Gnu uses the default }
  193. Glibc21:=false;
  194. {$ELSE}
  195. Not Implememted
  196. {$ENDIF}
  197. (* Linux Stuff not needed?
  198. { first try glibc2 } // muss noch gendert werden
  199. if FileExists(DynamicLinker) then
  200. begin
  201. Glibc2:=true;
  202. { Check for 2.0 files, else use the glibc 2.1 stub }
  203. if FileExists('/lib/ld-2.0.*') then
  204. Glibc21:=false
  205. else
  206. Glibc21:=true;
  207. end
  208. else
  209. DynamicLinker:='/lib/ld-linux.so.1';
  210. *)
  211. end;
  212. end;
  213. Function TLinkersunos.WriteResponseFile(isdll:boolean) : Boolean;
  214. Var
  215. linkres : TLinkRes;
  216. i : longint;
  217. cprtobj,
  218. gprtobj,
  219. prtobj : string[80];
  220. HPath : TStringListItem;
  221. s,s2 : string;
  222. linkdynamic,
  223. linklibc : boolean;
  224. begin
  225. WriteResponseFile:=False;
  226. { set special options for some targets }
  227. linkdynamic:=not(SharedLibFiles.empty);
  228. { linkdynamic:=false; // da nicht getestet }
  229. linklibc:=(SharedLibFiles.Find('c')<>nil);
  230. prtobj:='prt0';
  231. cprtobj:='cprt0';
  232. gprtobj:='gprt0';
  233. if cs_profile in aktmoduleswitches then
  234. begin
  235. prtobj:=gprtobj;
  236. if not glibc2 then
  237. AddSharedLibrary('gmon');
  238. AddSharedLibrary('c');
  239. linklibc:=true;
  240. end
  241. else
  242. begin
  243. if linklibc then
  244. prtobj:=cprtobj
  245. else
  246. AddSharedLibrary('c'); { quick hack: this sunos implementation needs alwys libc }
  247. end;
  248. { Open link.res file }
  249. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  250. { Write path to search libraries }
  251. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  252. while assigned(HPath) do
  253. begin
  254. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  255. HPath:=TStringListItem(HPath.Next);
  256. end;
  257. HPath:=TStringListItem(LibrarySearchPath.First);
  258. while assigned(HPath) do
  259. begin
  260. LinkRes.Add('SEARCH_DIR('+maybequoted(HPath.Str)+')');
  261. HPath:=TStringListItem(HPath.Next);
  262. end;
  263. LinkRes.Add('INPUT(');
  264. { add objectfiles, start with prt0 always }
  265. if prtobj<>'' then
  266. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  267. { try to add crti and crtbegin if linking to C }
  268. if linklibc then { Needed in sunos? }
  269. begin
  270. { if librarysearchpath.FindFile('crtbegin.o',s) then
  271. LinkRes.AddFileName(s);}
  272. if librarysearchpath.FindFile('crti.o',s) then
  273. LinkRes.AddFileName(s);
  274. end;
  275. { main objectfiles }
  276. while not ObjectFiles.Empty do
  277. begin
  278. s:=ObjectFiles.GetFirst;
  279. if s<>'' then
  280. LinkRes.AddFileName(maybequoted(s));
  281. end;
  282. LinkRes.Add(')');
  283. { Write staticlibraries }
  284. if not StaticLibFiles.Empty then
  285. begin
  286. LinkRes.Add('GROUP(');
  287. While not StaticLibFiles.Empty do
  288. begin
  289. S:=StaticLibFiles.GetFirst;
  290. LinkRes.AddFileName(maybequoted(s))
  291. end;
  292. LinkRes.Add(')');
  293. end;
  294. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  295. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  296. if not SharedLibFiles.Empty then
  297. begin
  298. LinkRes.Add('INPUT(');
  299. While not SharedLibFiles.Empty do
  300. begin
  301. S:=SharedLibFiles.GetFirst;
  302. if s<>'c' then
  303. begin
  304. i:=Pos(target_info.sharedlibext,S);
  305. if i>0 then
  306. Delete(S,i,255);
  307. LinkRes.Add('-l'+s);
  308. end
  309. else
  310. begin
  311. linklibc:=true;
  312. linkdynamic:=false; { libc will include the ld-sunos (war ld-linux) for us }
  313. end;
  314. end;
  315. { be sure that libc is the last lib }
  316. if linklibc then
  317. LinkRes.Add('-lc');
  318. { when we have -static for the linker the we also need libgcc }
  319. if (cs_link_staticflag in aktglobalswitches) then begin
  320. LinkRes.Add('-lgcc');
  321. end;
  322. if linkdynamic and (Info.DynamicLinker<>'') then { gld has a default, DynamicLinker is not set in sunos }
  323. LinkRes.AddFileName(Info.DynamicLinker);
  324. LinkRes.Add(')');
  325. end;
  326. { objects which must be at the end }
  327. if linklibc then {needed in sunos ? }
  328. begin
  329. if {librarysearchpath.FindFile('crtend.o',s1) or}
  330. librarysearchpath.FindFile('crtn.o',s2) then
  331. begin
  332. LinkRes.Add('INPUT(');
  333. { LinkRes.AddFileName(s1);}
  334. LinkRes.AddFileName(s2);
  335. LinkRes.Add(')');
  336. end;
  337. end;
  338. { Write and Close response }
  339. linkres.writetodisk;
  340. LinkRes.Free;
  341. WriteResponseFile:=True;
  342. end;
  343. function TLinkersunos.MakeExecutable:boolean;
  344. var
  345. binstr : String;
  346. cmdstr : TCmdStr;
  347. success : boolean;
  348. DynLinkStr : string[60];
  349. StaticStr,
  350. StripStr : string[40];
  351. begin
  352. if not(cs_link_extern in aktglobalswitches) then
  353. Message1(exec_i_linking,current_module.exefilename^);
  354. { Create some replacements }
  355. StaticStr:='';
  356. StripStr:='';
  357. DynLinkStr:='';
  358. if (cs_link_staticflag in aktglobalswitches) then
  359. StaticStr:='-Bstatic';
  360. if (cs_link_strip in aktglobalswitches) then
  361. StripStr:='-s';
  362. If (cs_profile in aktmoduleswitches) or
  363. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  364. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  365. { sunos sets DynamicLinker, but gld will (hopefully) defaults to -Bdynamic and add the default-linker }
  366. { Write used files and libraries }
  367. WriteResponseFile(false);
  368. { Call linker }
  369. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  370. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  371. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  372. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  373. Replace(cmdstr,'$STATIC',StaticStr);
  374. Replace(cmdstr,'$STRIP',StripStr);
  375. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  376. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,false);
  377. { Remove ReponseFile }
  378. {$IFNDEF LinkTest}
  379. if (success) and not(cs_link_extern in aktglobalswitches) then
  380. RemoveFile(outputexedir+Info.ResName);
  381. {$ENDIF}
  382. MakeExecutable:=success; { otherwise a recursive call to link method }
  383. end;
  384. Function TLinkersunos.MakeSharedLibrary:boolean;
  385. var
  386. binstr : String;
  387. cmdstr : TCmdStr;
  388. success : boolean;
  389. begin
  390. MakeSharedLibrary:=false;
  391. if not(cs_link_extern in aktglobalswitches) then
  392. Message1(exec_i_linking,current_module.sharedlibfilename^);
  393. { Write used files and libraries }
  394. WriteResponseFile(true);
  395. { Call linker }
  396. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  397. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  398. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  399. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  400. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  401. { Strip the library ? }
  402. if success and (cs_link_strip in aktglobalswitches) then
  403. begin
  404. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  405. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  406. success:=DoExec(utilsprefix+FindUtil(binstr),cmdstr,true,false);
  407. end;
  408. { Remove ReponseFile }
  409. {$IFNDEF LinkTest}
  410. if (success) and not(cs_link_extern in aktglobalswitches) then
  411. RemoveFile(outputexedir+Info.ResName);
  412. {$ENDIF}
  413. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  414. end;
  415. {*****************************************************************************
  416. Initialize
  417. *****************************************************************************}
  418. initialization
  419. {$ifdef i386}
  420. RegisterExternalLinker(system_i386_sunos_info,TLinkerSunos);
  421. RegisterImport(system_i386_sunos,TImportLibSunos);
  422. RegisterExport(system_i386_sunos,TExportLibSunos);
  423. RegisterTarget(system_i386_sunos_info);
  424. {$endif i386}
  425. {$ifdef sparc}
  426. RegisterExternalLinker(system_sparc_sunos_info,TLinkerSunos);
  427. RegisterImport(system_sparc_sunos,TImportLibSunos);
  428. RegisterExport(system_sparc_sunos,TExportLibSunos);
  429. RegisterTarget(system_sparc_sunos_info);
  430. {$endif sparc}
  431. end.
  432. {
  433. $Log$
  434. Revision 1.16 2004-12-22 16:32:46 peter
  435. * maybequoted() added
  436. Revision 1.15 2004/11/19 16:30:24 peter
  437. * fixed setting of mangledname when importing
  438. Revision 1.14 2004/11/08 22:09:59 peter
  439. * tvarsym splitted
  440. Revision 1.13 2004/11/03 12:04:03 florian
  441. * fixed sparc <-> i386 mixture
  442. Revision 1.12 2004/10/14 18:16:17 mazen
  443. * USE_SYSUTILS merged successfully : cycles with and without defines
  444. * Need to be optimized in performance
  445. Revision 1.11 2004/10/01 17:41:21 marco
  446. * small updates to make playing with sparc/sunos easier
  447. Revision 1.10 2004/09/22 15:25:14 mazen
  448. * Fix error committing : previous version must be in branch USE_SYSUTILS
  449. Revision 1.8 2004/06/20 08:55:32 florian
  450. * logs truncated
  451. Revision 1.7 2004/03/02 00:36:33 olle
  452. * big transformation of Tai_[const_]Symbol.Create[data]name*
  453. }