t_beos.pas 14 KB

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