t_sunos.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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. uses
  27. import,export,link;
  28. type
  29. timportlibsunos=class(timportlib)
  30. procedure preparelib(const s:string);override;
  31. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  32. procedure importvariable(const varname,module:string;const name:string);override;
  33. procedure generatelib;override;
  34. end;
  35. texportlibsunos=class(texportlib)
  36. procedure preparelib(const s : string);override;
  37. procedure exportprocedure(hp : texported_item);override;
  38. procedure exportvar(hp : texported_item);override;
  39. procedure generatelib;override;
  40. end;
  41. tlinkersunos=class(tlinker)
  42. private
  43. Glibc2,
  44. Glibc21 : boolean;
  45. Function WriteResponseFile(isdll:boolean) : Boolean;
  46. public
  47. constructor Create;
  48. procedure SetDefaultInfo;override;
  49. function MakeExecutable:boolean;override;
  50. function MakeSharedLibrary:boolean;override;
  51. end;
  52. implementation
  53. uses
  54. cutils,cclasses,
  55. verbose,systems,globtype,globals,
  56. symconst,script,
  57. fmodule,aasm,cpuasm,cpubase,symsym;
  58. {*****************************************************************************
  59. TIMPORTLIBsunos
  60. *****************************************************************************}
  61. procedure timportlibsunos.preparelib(const s : string);
  62. begin
  63. {$ifDef LinkTest}
  64. WriteLN('Prepare import: ',s);
  65. {$EndIf}
  66. end;
  67. procedure timportlibsunos.importprocedure(const func,module : string;index : longint;const name : string);
  68. begin
  69. { insert sharedlibrary }
  70. {$ifDef LinkTest}
  71. WriteLN('Import: f:',func,' m:',module,' n:',name);
  72. {$EndIf}
  73. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  74. { do nothing with the procedure, only set the mangledname }
  75. if name<>'' then
  76. aktprocsym^.definition^.setmangledname(name)
  77. else
  78. message(parser_e_empty_import_name);
  79. end;
  80. procedure timportlibsunos.importvariable(const varname,module:string;const name:string);
  81. begin
  82. { insert sharedlibrary }
  83. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  84. { reset the mangledname and turn off the dll_var option }
  85. aktvarsym^.setmangledname(name);
  86. exclude(aktvarsym^.varoptions,vo_is_dll_var);
  87. end;
  88. procedure timportlibsunos.generatelib;
  89. begin
  90. end;
  91. {*****************************************************************************
  92. TEXPORTLIBsunos
  93. *****************************************************************************}
  94. procedure texportlibsunos.preparelib(const s:string);
  95. begin
  96. end;
  97. procedure texportlibsunos.exportprocedure(hp : texported_item);
  98. var
  99. hp2 : texported_item;
  100. begin
  101. { first test the index value }
  102. if (hp.options and eo_index)<>0 then
  103. begin
  104. Message1(parser_e_no_export_with_index_for_target,'SunOS');
  105. exit;
  106. end;
  107. { use pascal name is none specified }
  108. if (hp.options and eo_name)=0 then
  109. begin
  110. hp.name:=stringdup(hp.sym^.name);
  111. hp.options:=hp.options or eo_name;
  112. end;
  113. { now place in correct order }
  114. hp2:=texported_item(current_module._exports.first);
  115. while assigned(hp2) and
  116. (hp.name^>hp2.name^) do
  117. hp2:=texported_item(hp2.next);
  118. { insert hp there !! }
  119. if assigned(hp2) and (hp2.name^=hp.name^) then
  120. begin
  121. { this is not allowed !! }
  122. Message1(parser_e_export_name_double,hp.name^);
  123. exit;
  124. end;
  125. if hp2=texported_item(current_module._exports.first) then
  126. current_module._exports.insert(hp)
  127. else if assigned(hp2) then
  128. begin
  129. hp.next:=hp2;
  130. hp.previous:=hp2.previous;
  131. if assigned(hp2.previous) then
  132. hp2.previous.next:=hp;
  133. hp2.previous:=hp;
  134. end
  135. else
  136. current_module._exports.concat(hp);
  137. end;
  138. procedure texportlibsunos.exportvar(hp : texported_item);
  139. begin
  140. hp.is_var:=true;
  141. exportprocedure(hp);
  142. end;
  143. procedure texportlibsunos.generatelib;
  144. var
  145. hp2 : texported_item;
  146. begin
  147. hp2:=texported_item(current_module._exports.first);
  148. while assigned(hp2) do
  149. begin
  150. if not hp2.is_var then
  151. begin
  152. {$ifdef i386}
  153. { place jump in codesegment }
  154. codesegment.concat(Tai_align.Create_op(4,$90));
  155. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  156. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,newasmsymbol(hp2.sym^.mangledname)));
  157. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  158. {$endif i386}
  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. 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 TLinkersunos.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 TLinkersunos.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 : 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 glibc21 then
  233. begin
  234. cprtobj:='cprt21';
  235. gprtobj:='gprt21';
  236. end;
  237. *)
  238. if cs_profile in aktmoduleswitches then
  239. begin
  240. prtobj:=gprtobj;
  241. if not glibc2 then
  242. AddSharedLibrary('gmon');
  243. AddSharedLibrary('c');
  244. linklibc:=true;
  245. end
  246. else
  247. begin
  248. if linklibc then
  249. prtobj:=cprtobj
  250. else
  251. AddSharedLibrary('c'); { quick hack: this sunos implementation needs alwys libc }
  252. end;
  253. { Open link.res file }
  254. LinkRes.Init(outputexedir+Info.ResName);
  255. { Write path to search libraries }
  256. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  257. while assigned(HPath) do
  258. begin
  259. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  260. HPath:=TStringListItem(HPath.Next);
  261. end;
  262. HPath:=TStringListItem(LibrarySearchPath.First);
  263. while assigned(HPath) do
  264. begin
  265. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  266. HPath:=TStringListItem(HPath.Next);
  267. end;
  268. LinkRes.Add('INPUT(');
  269. { add objectfiles, start with prt0 always }
  270. if prtobj<>'' then
  271. LinkRes.AddFileName(FindObjectFile(prtobj,''));
  272. { try to add crti and crtbegin if linking to C }
  273. if linklibc then { Needed in sunos? }
  274. begin
  275. if librarysearchpath.FindFile('crtbegin.o',s) then
  276. LinkRes.AddFileName(s);
  277. if librarysearchpath.FindFile('crti.o',s) then
  278. LinkRes.AddFileName(s);
  279. end;
  280. { main objectfiles }
  281. while not ObjectFiles.Empty do
  282. begin
  283. s:=ObjectFiles.GetFirst;
  284. if s<>'' then
  285. LinkRes.AddFileName(s);
  286. end;
  287. { objects which must be at the end }
  288. if linklibc then { Needed in sunos? }
  289. begin
  290. if librarysearchpath.FindFile('crtend.o',s) then
  291. LinkRes.AddFileName(s);
  292. if librarysearchpath.FindFile('crtn.o',s) then
  293. LinkRes.AddFileName(s);
  294. end;
  295. LinkRes.Add(')');
  296. { Write staticlibraries }
  297. if not StaticLibFiles.Empty then
  298. begin
  299. LinkRes.Add('GROUP(');
  300. While not StaticLibFiles.Empty do
  301. begin
  302. S:=StaticLibFiles.GetFirst;
  303. LinkRes.AddFileName(s)
  304. end;
  305. LinkRes.Add(')');
  306. end;
  307. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  308. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  309. if not SharedLibFiles.Empty then
  310. begin
  311. LinkRes.Add('INPUT(');
  312. While not SharedLibFiles.Empty do
  313. begin
  314. S:=SharedLibFiles.GetFirst;
  315. if s<>'c' then
  316. begin
  317. i:=Pos(target_os.sharedlibext,S);
  318. if i>0 then
  319. Delete(S,i,255);
  320. LinkRes.Add('-l'+s);
  321. end
  322. else
  323. begin
  324. linklibc:=true;
  325. linkdynamic:=false; { libc will include the ld-sunos (war ld-linux) for us }
  326. end;
  327. end;
  328. { be sure that libc is the last lib }
  329. if linklibc then
  330. LinkRes.Add('-lc');
  331. { when we have -static for the linker the we also need libgcc }
  332. if (cs_link_staticflag in aktglobalswitches) then begin
  333. LinkRes.Add('-lgcc');
  334. end;
  335. if linkdynamic and (Info.DynamicLinker<>'') then { gld has a default, DynamicLinker is not set in sunos }
  336. LinkRes.AddFileName(Info.DynamicLinker);
  337. LinkRes.Add(')');
  338. end;
  339. { Write and Close response }
  340. linkres.writetodisk;
  341. linkres.done;
  342. WriteResponseFile:=True;
  343. end;
  344. function TLinkersunos.MakeExecutable:boolean;
  345. var
  346. binstr,
  347. cmdstr : string;
  348. success : boolean;
  349. DynLinkStr : string[60];
  350. StaticStr,
  351. StripStr : string[40];
  352. begin
  353. if not(cs_link_extern in aktglobalswitches) then
  354. Message1(exec_i_linking,current_module.exefilename^);
  355. { Create some replacements }
  356. StaticStr:='';
  357. StripStr:='';
  358. DynLinkStr:='';
  359. if (cs_link_staticflag in aktglobalswitches) then
  360. StaticStr:='-Bstatic';
  361. if (cs_link_strip in aktglobalswitches) then
  362. StripStr:='-s';
  363. If (cs_profile in aktmoduleswitches) or
  364. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  365. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  366. { sunos sets DynamicLinker, but gld will (hopefully) defaults to -Bdynamic and add the default-linker }
  367. { Write used files and libraries }
  368. WriteResponseFile(false);
  369. { Call linker }
  370. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  371. Replace(cmdstr,'$EXE',current_module.exefilename^);
  372. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  373. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  374. Replace(cmdstr,'$STATIC',StaticStr);
  375. Replace(cmdstr,'$STRIP',StripStr);
  376. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  377. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  378. { Remove ReponseFile }
  379. {$IFNDEF LinkTest}
  380. if (success) and not(cs_link_extern in aktglobalswitches) then
  381. RemoveFile(outputexedir+Info.ResName);
  382. {$ENDIF}
  383. MakeExecutable:=success; { otherwise a recursive call to link method }
  384. end;
  385. Function TLinkersunos.MakeSharedLibrary:boolean;
  386. var
  387. binstr,
  388. cmdstr : string;
  389. success : boolean;
  390. begin
  391. MakeSharedLibrary:=false;
  392. if not(cs_link_extern in aktglobalswitches) then
  393. Message1(exec_i_linking,current_module.sharedlibfilename^);
  394. { Write used files and libraries }
  395. WriteResponseFile(true);
  396. { Call linker }
  397. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  398. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  399. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  400. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  401. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  402. { Strip the library ? }
  403. if success and (cs_link_strip in aktglobalswitches) then
  404. begin
  405. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  406. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  407. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  408. end;
  409. { Remove ReponseFile }
  410. {$IFNDEF LinkTest}
  411. if (success) and not(cs_link_extern in aktglobalswitches) then
  412. RemoveFile(outputexedir+Info.ResName);
  413. {$ENDIF}
  414. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  415. end;
  416. end.
  417. {
  418. $Log$
  419. Revision 1.1 2001-02-26 19:43:11 peter
  420. * moved target units to subdir
  421. }