t_beos.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements support import,export,link routines
  4. for the (i386) BeOS 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_beos;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,symdef,
  23. import,export,link;
  24. type
  25. timportlibbeos=class(timportlib)
  26. procedure generatelib;override;
  27. end;
  28. texportlibbeos=class(texportlib)
  29. procedure preparelib(const s : string);override;
  30. procedure exportprocedure(hp : texported_item);override;
  31. procedure exportvar(hp : texported_item);override;
  32. procedure generatelib;override;
  33. end;
  34. tlinkerbeos=class(texternallinker)
  35. private
  36. Function WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  37. public
  38. constructor Create;override;
  39. procedure SetDefaultInfo;override;
  40. function MakeExecutable:boolean;override;
  41. function MakeSharedLibrary:boolean;override;
  42. end;
  43. implementation
  44. uses
  45. SysUtils,
  46. cutils,cfileutils,cclasses,
  47. verbose,systems,globtype,globals,
  48. symconst,script,
  49. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,i_beos,ogbase;
  50. {*****************************************************************************
  51. TIMPORTLIBBEOS
  52. *****************************************************************************}
  53. procedure timportlibbeos.generatelib;
  54. var
  55. i : longint;
  56. ImportLibrary : TImportLibrary;
  57. begin
  58. for i:=0 to current_module.ImportLibraryList.Count-1 do
  59. begin
  60. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  61. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  62. end;
  63. end;
  64. {*****************************************************************************
  65. TEXPORTLIBBEOS
  66. *****************************************************************************}
  67. procedure texportlibbeos.preparelib(const s:string);
  68. begin
  69. end;
  70. procedure texportlibbeos.exportprocedure(hp : texported_item);
  71. var
  72. hp2 : texported_item;
  73. begin
  74. { first test the index value }
  75. if (hp.options and eo_index)<>0 then
  76. begin
  77. Message1(parser_e_no_export_with_index_for_target,'beos');
  78. exit;
  79. end;
  80. { now place in correct order }
  81. hp2:=texported_item(current_module._exports.first);
  82. while assigned(hp2) and
  83. (hp.name^>hp2.name^) do
  84. hp2:=texported_item(hp2.next);
  85. { insert hp there !! }
  86. if assigned(hp2) and (hp2.name^=hp.name^) then
  87. begin
  88. { this is not allowed !! }
  89. Message1(parser_e_export_name_double,hp.name^);
  90. exit;
  91. end;
  92. if hp2=texported_item(current_module._exports.first) then
  93. current_module._exports.concat(hp)
  94. else if assigned(hp2) then
  95. begin
  96. hp.next:=hp2;
  97. hp.previous:=hp2.previous;
  98. if assigned(hp2.previous) then
  99. hp2.previous.next:=hp;
  100. hp2.previous:=hp;
  101. end
  102. else
  103. current_module._exports.concat(hp);
  104. end;
  105. procedure texportlibbeos.exportvar(hp : texported_item);
  106. begin
  107. hp.is_var:=true;
  108. exportprocedure(hp);
  109. end;
  110. procedure texportlibbeos.generatelib;
  111. var
  112. hp2 : texported_item;
  113. begin
  114. hp2:=texported_item(current_module._exports.first);
  115. while assigned(hp2) do
  116. begin
  117. if (not hp2.is_var) and
  118. (hp2.sym.typ=procsym) then
  119. begin
  120. { the manglednames can already be the same when the procedure
  121. is declared with cdecl }
  122. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  123. begin
  124. {$ifdef i386}
  125. { place jump in al_procedures }
  126. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  127. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  128. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(tprocsym(hp2.sym).first_procdef.mangledname)));
  129. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  130. {$endif i386}
  131. end;
  132. end
  133. else
  134. Message1(parser_e_no_export_of_variables_for_target,'beos');
  135. hp2:=texported_item(hp2.next);
  136. end;
  137. end;
  138. {*****************************************************************************
  139. TLINKERBEOS
  140. *****************************************************************************}
  141. Constructor TLinkerBeos.Create;
  142. var
  143. s : string;
  144. i : integer;
  145. begin
  146. Inherited Create;
  147. s:=GetEnvironmentVariable('BELIBRARIES');
  148. { convert to correct format in case under unix system }
  149. for i:=1 to length(s) do
  150. if s[i] = ':' then
  151. s[i] := ';';
  152. { just in case we have a single path : add the ending ; }
  153. { since that is what the compiler expects. }
  154. if pos(';',s) = 0 then
  155. s:=s+';';
  156. LibrarySearchPath.AddPath(s,true); {format:'path1;path2;...'}
  157. end;
  158. procedure TLinkerBeOS.SetDefaultInfo;
  159. begin
  160. with Info do
  161. begin
  162. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE `cat $RES`';
  163. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE `cat $RES`';
  164. DllCmd[2]:='strip --strip-unneeded $EXE';
  165. (*
  166. ExeCmd[1]:='sh $RES $EXE $OPT $STATIC $STRIP -L.';
  167. { ExeCmd[1]:='sh $RES $EXE $OPT $DYNLINK $STATIC $STRIP -L.';}
  168. DllCmd[1]:='sh $RES $EXE $OPT -L.';
  169. { DllCmd[1]:='sh $RES $EXE $OPT -L. -g -nostart -soname=$EXE';
  170. } DllCmd[2]:='strip --strip-unneeded $EXE';
  171. { DynamicLinker:='/lib/ld-beos.so.2';}
  172. *)
  173. end;
  174. end;
  175. function TLinkerBeOS.WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  176. Var
  177. linkres : TLinkRes;
  178. i : integer;
  179. cprtobj,
  180. prtobj : string[80];
  181. HPath : TStringListItem;
  182. s : string;
  183. linklibc : boolean;
  184. begin
  185. WriteResponseFile:=False;
  186. { set special options for some targets }
  187. linklibc:=(SharedLibFiles.Find('root')<>nil);
  188. prtobj:='prt0';
  189. cprtobj:='cprt0';
  190. if (cs_profile in current_settings.moduleswitches) or
  191. (not SharedLibFiles.Empty) then
  192. begin
  193. AddSharedLibrary('root');
  194. linklibc:=true;
  195. end;
  196. if (not linklibc) and makelib then
  197. begin
  198. linklibc:=true;
  199. cprtobj:='dllprt.o';
  200. end;
  201. if linklibc then
  202. prtobj:=cprtobj;
  203. { Open link.res file }
  204. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  205. {
  206. if not isdll then
  207. LinkRes.Add('ld -o $1 $2 $3 $4 $5 $6 $7 $8 $9 \')
  208. else
  209. LinkRes.Add('ld -o $1 -e 0 $2 $3 $4 $5 $6 $7 $8 $9\');
  210. }
  211. LinkRes.Add('-m elf_i386_be -shared -Bsymbolic');
  212. { Write path to search libraries }
  213. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  214. while assigned(HPath) do
  215. begin
  216. LinkRes.Add(maybequoted('-L'+HPath.Str));
  217. HPath:=TStringListItem(HPath.Next);
  218. end;
  219. HPath:=TStringListItem(LibrarySearchPath.First);
  220. while assigned(HPath) do
  221. begin
  222. LinkRes.Add(maybequoted('-L'+HPath.Str));
  223. HPath:=TStringListItem(HPath.Next);
  224. end;
  225. { try to add crti and crtbegin if linking to C }
  226. if linklibc then
  227. begin
  228. if librarysearchpath.FindFile('crti.o',s) then
  229. LinkRes.AddFileName(s);
  230. if librarysearchpath.FindFile('crtbegin.o',s) then
  231. LinkRes.AddFileName(s);
  232. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  233. if found then LinkRes.AddFileName(s+' \');}
  234. if prtobj<>'' then
  235. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  236. if isdll then
  237. LinkRes.AddFileName(FindObjectFile('func.o','',false));
  238. if librarysearchpath.FindFile('init_term_dyn.o',s) then
  239. LinkRes.AddFileName(s);
  240. end
  241. else
  242. begin
  243. if prtobj<>'' then
  244. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  245. end;
  246. { main objectfiles }
  247. while not ObjectFiles.Empty do
  248. begin
  249. s:=ObjectFiles.GetFirst;
  250. if s<>'' then
  251. LinkRes.AddFileName(maybequoted(s));
  252. end;
  253. { LinkRes.Add('-lroot \');
  254. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  255. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  256. { Write staticlibraries }
  257. if not StaticLibFiles.Empty then
  258. begin
  259. While not StaticLibFiles.Empty do
  260. begin
  261. S:=StaticLibFiles.GetFirst;
  262. LinkRes.AddFileName(maybequoted(s))
  263. end;
  264. end;
  265. { Write sharedlibraries like -l<lib> }
  266. if not SharedLibFiles.Empty then
  267. begin
  268. While not SharedLibFiles.Empty do
  269. begin
  270. S:=SharedLibFiles.GetFirst;
  271. if s<>'c' then
  272. begin
  273. i:=Pos(target_info.sharedlibext,S);
  274. if i>0 then
  275. Delete(S,i,255);
  276. LinkRes.Add('-l'+s);
  277. end
  278. else
  279. begin
  280. linklibc:=true;
  281. end;
  282. end;
  283. { be sure that libc is the last lib }
  284. { if linklibc then
  285. LinkRes.Add('-lroot');}
  286. { if linkdynamic and (Info.DynamicLinker<>'') then
  287. LinkRes.AddFileName(Info.DynamicLinker);}
  288. end;
  289. if isdll then
  290. LinkRes.Add('-lroot');
  291. { objects which must be at the end }
  292. if linklibc then
  293. begin
  294. if librarysearchpath.FindFile('crtend.o',s) then
  295. LinkRes.AddFileName(s);
  296. if librarysearchpath.FindFile('crtn.o',s) then
  297. LinkRes.AddFileName(s);
  298. end;
  299. { Write and Close response }
  300. linkres.Add(' ');
  301. linkres.writetodisk;
  302. linkres.free;
  303. WriteResponseFile:=True;
  304. end;
  305. function TLinkerBeOS.MakeExecutable:boolean;
  306. var
  307. binstr : String;
  308. cmdstr : TcmdStr;
  309. success : boolean;
  310. DynLinkStr : string[60];
  311. StaticStr,
  312. StripStr : string[40];
  313. begin
  314. if not(cs_link_nolink in current_settings.globalswitches) then
  315. Message1(exec_i_linking,current_module.exefilename^);
  316. { Create some replacements }
  317. StaticStr:='';
  318. StripStr:='';
  319. DynLinkStr:='';
  320. if (cs_link_staticflag in current_settings.globalswitches) then
  321. StaticStr:='-static';
  322. if (cs_link_strip in current_settings.globalswitches) then
  323. StripStr:='-s';
  324. If (cs_profile in current_settings.moduleswitches) or
  325. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  326. begin
  327. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  328. if cshared Then
  329. DynLinkStr:='--shared ' + DynLinkStr;
  330. if rlinkpath<>'' Then
  331. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  332. End;
  333. { Write used files and libraries }
  334. WriteResponseFile(false,false);
  335. { Call linker }
  336. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  337. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  338. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  339. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  340. Replace(cmdstr,'$STATIC',StaticStr);
  341. Replace(cmdstr,'$STRIP',StripStr);
  342. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  343. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,true);
  344. { Remove ReponseFile }
  345. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  346. RemoveFile(outputexedir+Info.ResName);
  347. MakeExecutable:=success; { otherwise a recursive call to link method }
  348. end;
  349. Function TLinkerBeOS.MakeSharedLibrary:boolean;
  350. var
  351. binstr : String;
  352. cmdstr : TCmdStr;
  353. success : boolean;
  354. DynLinkStr : string[60];
  355. StaticStr,
  356. StripStr : string[40];
  357. begin
  358. MakeSharedLibrary:=false;
  359. if not(cs_link_nolink in current_settings.globalswitches) then
  360. Message1(exec_i_linking,current_module.sharedlibfilename^);
  361. { Create some replacements }
  362. StaticStr:='';
  363. StripStr:='';
  364. DynLinkStr:='';
  365. if (cs_link_staticflag in current_settings.globalswitches) then
  366. StaticStr:='-static';
  367. if (cs_link_strip in current_settings.globalswitches) then
  368. StripStr:='-s';
  369. If (cs_profile in current_settings.moduleswitches) or
  370. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  371. begin
  372. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  373. if cshared Then
  374. DynLinkStr:='--shared ' + DynLinkStr;
  375. if rlinkpath<>'' Then
  376. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  377. End;
  378. { Write used files and libraries }
  379. WriteResponseFile(true,true);
  380. { Call linker }
  381. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  382. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename^));
  383. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  384. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  385. Replace(cmdstr,'$STATIC',StaticStr);
  386. Replace(cmdstr,'$STRIP',StripStr);
  387. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  388. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  389. { Strip the library ? }
  390. if success and (cs_link_strip in current_settings.globalswitches) then
  391. begin
  392. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  393. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  394. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  395. end;
  396. { Remove ReponseFile }
  397. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  398. RemoveFile(outputexedir+Info.ResName);
  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_beos_info,TLinkerbeos);
  407. RegisterImport(system_i386_beos,timportlibbeos);
  408. RegisterExport(system_i386_beos,texportlibbeos);
  409. RegisterTarget(system_i386_beos_info);
  410. {$endif i386}
  411. end.