t_sunos.pas 14 KB

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