t_beos.pas 15 KB

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