t_beos.pas 15 KB

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