t_beos.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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,
  24. import,export,link;
  25. type
  26. timportlibbeos=class(timportlib)
  27. procedure preparelib(const s:string);override;
  28. procedure importprocedure(const func,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. {$ifdef delphi}
  50. dmisc,
  51. {$else}
  52. dos,
  53. {$endif}
  54. cutils,cclasses,
  55. verbose,systems,globtype,globals,
  56. symconst,script,
  57. fmodule,aasmbase,aasmtai,aasmcpu,cpubase,i_beos;
  58. {*****************************************************************************
  59. TIMPORTLIBBEOS
  60. *****************************************************************************}
  61. procedure timportlibbeos.preparelib(const s : string);
  62. begin
  63. end;
  64. procedure timportlibbeos.importprocedure(const func,module : string;index : longint;const name : string);
  65. begin
  66. { insert sharedlibrary }
  67. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  68. { do nothing with the procedure, only set the mangledname }
  69. if name<>'' then
  70. aktprocdef.setmangledname(name)
  71. else
  72. message(parser_e_empty_import_name);
  73. end;
  74. procedure timportlibbeos.importvariable(vs:tvarsym;const name,module:string);
  75. begin
  76. { insert sharedlibrary }
  77. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  78. { reset the mangledname and turn off the dll_var option }
  79. vs.set_mangledname(name);
  80. exclude(vs.varoptions,vo_is_dll_var);
  81. end;
  82. procedure timportlibbeos.generatelib;
  83. begin
  84. end;
  85. {*****************************************************************************
  86. TEXPORTLIBBEOS
  87. *****************************************************************************}
  88. procedure texportlibbeos.preparelib(const s:string);
  89. begin
  90. end;
  91. procedure texportlibbeos.exportprocedure(hp : texported_item);
  92. var
  93. hp2 : texported_item;
  94. begin
  95. { first test the index value }
  96. if (hp.options and eo_index)<>0 then
  97. begin
  98. Message1(parser_e_no_export_with_index_for_target,'beos');
  99. exit;
  100. end;
  101. { now place in correct order }
  102. hp2:=texported_item(current_module._exports.first);
  103. while assigned(hp2) and
  104. (hp.name^>hp2.name^) do
  105. hp2:=texported_item(hp2.next);
  106. { insert hp there !! }
  107. if assigned(hp2) and (hp2.name^=hp.name^) then
  108. begin
  109. { this is not allowed !! }
  110. Message1(parser_e_export_name_double,hp.name^);
  111. exit;
  112. end;
  113. if hp2=texported_item(current_module._exports.first) then
  114. current_module._exports.concat(hp)
  115. else if assigned(hp2) then
  116. begin
  117. hp.next:=hp2;
  118. hp.previous:=hp2.previous;
  119. if assigned(hp2.previous) then
  120. hp2.previous.next:=hp;
  121. hp2.previous:=hp;
  122. end
  123. else
  124. current_module._exports.concat(hp);
  125. end;
  126. procedure texportlibbeos.exportvar(hp : texported_item);
  127. begin
  128. hp.is_var:=true;
  129. exportprocedure(hp);
  130. end;
  131. procedure texportlibbeos.generatelib;
  132. var
  133. hp2 : texported_item;
  134. begin
  135. hp2:=texported_item(current_module._exports.first);
  136. while assigned(hp2) do
  137. begin
  138. if (not hp2.is_var) and
  139. (hp2.sym.typ=procsym) then
  140. begin
  141. { the manglednames can already be the same when the procedure
  142. is declared with cdecl }
  143. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  144. begin
  145. {$ifdef i386}
  146. { place jump in codesegment }
  147. codesegment.concat(Tai_align.Create_op(4,$90));
  148. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  149. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(tprocsym(hp2.sym).first_procdef.mangledname)));
  150. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  151. {$endif i386}
  152. end;
  153. end
  154. else
  155. Message1(parser_e_no_export_of_variables_for_target,'beos');
  156. hp2:=texported_item(hp2.next);
  157. end;
  158. end;
  159. {*****************************************************************************
  160. TLINKERBEOS
  161. *****************************************************************************}
  162. Constructor TLinkerBeos.Create;
  163. var
  164. s : string;
  165. i : integer;
  166. begin
  167. Inherited Create;
  168. s:=GetEnv('BELIBRARIES');
  169. { convert to correct format in case under unix system }
  170. for i:=1 to length(s) do
  171. if s[i] = ':' then
  172. s[i] := ';';
  173. { just in case we have a single path : add the ending ; }
  174. { since that is what the compiler expects. }
  175. if pos(';',s) = 0 then
  176. s:=s+';';
  177. LibrarySearchPath.AddPath(s,true); {format:'path1;path2;...'}
  178. end;
  179. procedure TLinkerBeOS.SetDefaultInfo;
  180. begin
  181. with Info do
  182. begin
  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. end;
  190. end;
  191. function TLinkerBeOS.WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  192. Var
  193. linkres : TLinkRes;
  194. i : integer;
  195. cprtobj,
  196. prtobj : string[80];
  197. HPath : TStringListItem;
  198. s : string;
  199. linklibc : boolean;
  200. begin
  201. WriteResponseFile:=False;
  202. { set special options for some targets }
  203. linklibc:=(SharedLibFiles.Find('root')<>nil);
  204. prtobj:='prt0';
  205. cprtobj:='cprt0';
  206. if (cs_profile in aktmoduleswitches) or
  207. (not SharedLibFiles.Empty) then
  208. begin
  209. AddSharedLibrary('root');
  210. linklibc:=true;
  211. end;
  212. if (not linklibc) and makelib then
  213. begin
  214. linklibc:=true;
  215. cprtobj:='dllprt.o';
  216. end;
  217. if linklibc then
  218. prtobj:=cprtobj;
  219. { Open link.res file }
  220. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  221. if not isdll then
  222. LinkRes.Add('ld -o $1 $2 $3 $4 $5 $6 $7 $8 $9 \')
  223. else
  224. LinkRes.Add('ld -o $1 -e 0 $2 $3 $4 $5 $6 $7 $8 $9\');
  225. LinkRes.Add('-m elf_i386_be -shared -Bsymbolic \');
  226. { Write path to search libraries }
  227. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  228. while assigned(HPath) do
  229. begin
  230. LinkRes.Add('-L'+HPath.Str+' \');
  231. HPath:=TStringListItem(HPath.Next);
  232. end;
  233. HPath:=TStringListItem(LibrarySearchPath.First);
  234. while assigned(HPath) do
  235. begin
  236. LinkRes.Add('-L'+HPath.Str+' \');
  237. HPath:=TStringListItem(HPath.Next);
  238. end;
  239. { try to add crti and crtbegin if linking to C }
  240. if linklibc then
  241. begin
  242. if librarysearchpath.FindFile('crti.o',s) then
  243. LinkRes.AddFileName(s+' \');
  244. if librarysearchpath.FindFile('crtbegin.o',s) then
  245. LinkRes.AddFileName(s+' \');
  246. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  247. if found then LinkRes.AddFileName(s+' \');}
  248. if prtobj<>'' then
  249. LinkRes.AddFileName(FindObjectFile(prtobj,'')+' \');
  250. if isdll then
  251. LinkRes.AddFileName(FindObjectFile('func.o','')+' \');
  252. if librarysearchpath.FindFile('init_term_dyn.o',s) then
  253. LinkRes.AddFileName(s+' \');
  254. end
  255. else
  256. begin
  257. if prtobj<>'' then
  258. LinkRes.AddFileName(FindObjectFile(prtobj,'')+' \');
  259. end;
  260. { main objectfiles }
  261. while not ObjectFiles.Empty do
  262. begin
  263. s:=ObjectFiles.GetFirst;
  264. if s<>'' then
  265. LinkRes.AddFileName(s+' \');
  266. end;
  267. { LinkRes.Add('-lroot \');
  268. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  269. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  270. { Write staticlibraries }
  271. if not StaticLibFiles.Empty then
  272. begin
  273. While not StaticLibFiles.Empty do
  274. begin
  275. S:=StaticLibFiles.GetFirst;
  276. LinkRes.AddFileName(s+' \')
  277. end;
  278. end;
  279. { Write sharedlibraries like -l<lib> }
  280. if not SharedLibFiles.Empty then
  281. begin
  282. While not SharedLibFiles.Empty do
  283. begin
  284. S:=SharedLibFiles.GetFirst;
  285. if s<>'c' then
  286. begin
  287. i:=Pos(target_info.sharedlibext,S);
  288. if i>0 then
  289. Delete(S,i,255);
  290. LinkRes.Add('-l'+s+' \');
  291. end
  292. else
  293. begin
  294. linklibc:=true;
  295. end;
  296. end;
  297. { be sure that libc is the last lib }
  298. { if linklibc then
  299. LinkRes.Add('-lroot');}
  300. { if linkdynamic and (Info.DynamicLinker<>'') then
  301. LinkRes.AddFileName(Info.DynamicLinker);}
  302. end;
  303. if isdll then
  304. LinkRes.Add('-lroot \');
  305. { objects which must be at the end }
  306. if linklibc then
  307. begin
  308. if librarysearchpath.FindFile('crtend.o',s) then
  309. LinkRes.AddFileName(s+' \');
  310. if librarysearchpath.FindFile('crtn.o',s) then
  311. LinkRes.AddFileName(s+' \');
  312. end;
  313. { Write and Close response }
  314. linkres.Add(' ');
  315. linkres.writetodisk;
  316. linkres.free;
  317. WriteResponseFile:=True;
  318. end;
  319. function TLinkerBeOS.MakeExecutable:boolean;
  320. var
  321. binstr,
  322. cmdstr : string;
  323. success : boolean;
  324. { DynLinkStr : string[60];}
  325. StaticStr,
  326. StripStr : string[40];
  327. begin
  328. if not(cs_link_extern in aktglobalswitches) then
  329. Message1(exec_i_linking,current_module.exefilename^);
  330. { Create some replacements }
  331. StaticStr:='';
  332. StripStr:='';
  333. { DynLinkStr:='';}
  334. if (cs_link_staticflag in aktglobalswitches) then
  335. StaticStr:='-static';
  336. if (cs_link_strip in aktglobalswitches) then
  337. StripStr:='-s';
  338. { If (cs_profile in aktmoduleswitches) or
  339. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  340. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;}
  341. { Write used files and libraries }
  342. WriteResponseFile(false,false);
  343. { Call linker }
  344. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  345. Replace(cmdstr,'$EXE',current_module.exefilename^);
  346. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  347. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  348. Replace(cmdstr,'$STATIC',StaticStr);
  349. Replace(cmdstr,'$STRIP',StripStr);
  350. { Replace(cmdstr,'$DYNLINK',DynLinkStr);}
  351. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  352. { Remove ReponseFile }
  353. if (success) and not(cs_link_extern in aktglobalswitches) then
  354. RemoveFile(outputexedir+Info.ResName);
  355. MakeExecutable:=success; { otherwise a recursive call to link method }
  356. end;
  357. Function TLinkerBeOS.MakeSharedLibrary:boolean;
  358. var
  359. binstr,
  360. cmdstr : string;
  361. success : boolean;
  362. begin
  363. MakeSharedLibrary:=false;
  364. if not(cs_link_extern in aktglobalswitches) then
  365. Message1(exec_i_linking,current_module.sharedlibfilename^);
  366. { Write used files and libraries }
  367. WriteResponseFile(true,true);
  368. { Call linker }
  369. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  370. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  371. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  372. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  373. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  374. { Strip the library ? }
  375. if success and (cs_link_strip in aktglobalswitches) then
  376. begin
  377. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  378. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  379. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  380. end;
  381. { Remove ReponseFile }
  382. if (success) and not(cs_link_extern in aktglobalswitches) then
  383. RemoveFile(outputexedir+Info.ResName);
  384. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  385. end;
  386. {*****************************************************************************
  387. Initialize
  388. *****************************************************************************}
  389. initialization
  390. {$ifdef i386}
  391. RegisterExternalLinker(system_i386_beos_info,TLinkerbeos);
  392. RegisterImport(system_i386_beos,timportlibbeos);
  393. RegisterExport(system_i386_beos,texportlibbeos);
  394. RegisterTarget(system_i386_beos_info);
  395. {$endif i386}
  396. end.
  397. {
  398. $Log$
  399. Revision 1.3 2002-10-05 12:43:29 carl
  400. * fixes for Delphi 6 compilation
  401. (warning : Some features do not work under Delphi)
  402. Revision 1.2 2002/09/09 17:34:17 peter
  403. * tdicationary.replace added to replace and item in a dictionary. This
  404. is only allowed for the same name
  405. * varsyms are inserted in symtable before the types are parsed. This
  406. fixes the long standing "var longint : longint" bug
  407. - consume_idlist and idstringlist removed. The loops are inserted
  408. at the callers place and uses the symtable for duplicate id checking
  409. Revision 1.1 2002/09/06 15:03:51 carl
  410. * moved files to systems directory
  411. Revision 1.24 2002/09/03 16:26:28 daniel
  412. * Make Tprocdef.defs protected
  413. Revision 1.23 2002/08/12 15:08:44 carl
  414. + stab register indexes for powerpc (moved from gdb to cpubase)
  415. + tprocessor enumeration moved to cpuinfo
  416. + linker in target_info is now a class
  417. * many many updates for m68k (will soon start to compile)
  418. - removed some ifdef or correct them for correct cpu
  419. Revision 1.22 2002/08/11 14:32:32 peter
  420. * renamed current_library to objectlibrary
  421. Revision 1.21 2002/08/11 13:24:19 peter
  422. * saving of asmsymbols in ppu supported
  423. * asmsymbollist global is removed and moved into a new class
  424. tasmlibrarydata that will hold the info of a .a file which
  425. corresponds with a single module. Added librarydata to tmodule
  426. to keep the library info stored for the module. In the future the
  427. objectfiles will also be stored to the tasmlibrarydata class
  428. * all getlabel/newasmsymbol and friends are moved to the new class
  429. Revision 1.20 2002/07/26 21:15:45 florian
  430. * rewrote the system handling
  431. Revision 1.19 2002/07/01 18:46:34 peter
  432. * internal linker
  433. * reorganized aasm layer
  434. Revision 1.18 2002/05/18 13:34:26 peter
  435. * readded missing revisions
  436. Revision 1.17 2002/05/16 19:46:53 carl
  437. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  438. + try to fix temp allocation (still in ifdef)
  439. + generic constructor calls
  440. + start of tassembler / tmodulebase class cleanup
  441. Revision 1.15 2002/04/22 18:19:22 carl
  442. - remove use_bound_instruction field
  443. Revision 1.14 2002/04/20 21:43:18 carl
  444. * fix stack size for some targets
  445. + add offset to parameters from frame pointer info.
  446. - remove some unused stuff
  447. Revision 1.13 2002/04/19 15:46:04 peter
  448. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  449. in most cases and not written to the ppu
  450. * add mangeledname_prefix() routine to generate the prefix of
  451. manglednames depending on the current procedure, object and module
  452. * removed static procprefix since the mangledname is now build only
  453. on demand from tprocdef.mangledname
  454. Revision 1.12 2002/04/15 19:16:57 carl
  455. - remove size_of_pointer field
  456. Revision 1.11 2002/01/29 21:27:34 peter
  457. * default alignment changed to 4 bytes for locals and static const,var
  458. }