t_beos.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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,cfileutl,cclasses,
  47. verbose,systems,globtype,globals,
  48. symconst,cscript,
  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 eo_index in hp.options 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. duplicatesymbol(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. pd : tprocdef;
  114. begin
  115. hp2:=texported_item(current_module._exports.first);
  116. while assigned(hp2) do
  117. begin
  118. if (not hp2.is_var) and
  119. (hp2.sym.typ=procsym) then
  120. begin
  121. { the manglednames can already be the same when the procedure
  122. is declared with cdecl }
  123. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  124. if pd.mangledname<>hp2.name^ then
  125. begin
  126. {$ifdef i386}
  127. { place jump in al_procedures }
  128. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  129. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0,pd));
  130. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(pd.mangledname,AT_FUNCTION)));
  131. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  132. {$endif i386}
  133. end;
  134. end
  135. else
  136. Message1(parser_e_no_export_of_variables_for_target,'beos');
  137. hp2:=texported_item(hp2.next);
  138. end;
  139. end;
  140. {*****************************************************************************
  141. TLINKERBEOS
  142. *****************************************************************************}
  143. Constructor TLinkerBeos.Create;
  144. var
  145. s : string;
  146. i : integer;
  147. begin
  148. Inherited Create;
  149. s:=GetEnvironmentVariable('BELIBRARIES');
  150. { convert to correct format in case under unix system }
  151. Replace(s,':',';=');
  152. Insert('=',s,1);
  153. if s[length(s)]='=' then
  154. setlength(s,length(s)-1);
  155. { just in case we have a single path : add the ending ; }
  156. { since that is what the compiler expects. }
  157. if pos(';',s) = 0 then
  158. s:=s+';';
  159. LibrarySearchPath.AddLibraryPath(sysrootpath,s,true); {format:'path1;path2;...'}
  160. end;
  161. procedure TLinkerBeOS.SetDefaultInfo;
  162. begin
  163. with Info do
  164. begin
  165. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $CATRES';
  166. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $CATRES';
  167. DllCmd[2]:='strip --strip-unneeded $EXE';
  168. (*
  169. ExeCmd[1]:='sh $RES $EXE $OPT $STATIC $STRIP -L.';
  170. { ExeCmd[1]:='sh $RES $EXE $OPT $DYNLINK $STATIC $STRIP -L.';}
  171. DllCmd[1]:='sh $RES $EXE $OPT -L.';
  172. { DllCmd[1]:='sh $RES $EXE $OPT -L. -g -nostart -soname=$EXE';
  173. } DllCmd[2]:='strip --strip-unneeded $EXE';
  174. { DynamicLinker:='/lib/ld-beos.so.2';}
  175. *)
  176. end;
  177. end;
  178. function TLinkerBeOS.WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  179. Var
  180. linkres : TLinkRes;
  181. i : integer;
  182. cprtobj,
  183. prtobj : string[80];
  184. HPath : TCmdStrListItem;
  185. s : TCmdStr;
  186. linklibc : boolean;
  187. begin
  188. WriteResponseFile:=False;
  189. { set special options for some targets }
  190. linklibc:=(SharedLibFiles.Find('root')<>nil);
  191. prtobj:='prt0';
  192. cprtobj:='cprt0';
  193. if (cs_profile in current_settings.moduleswitches) or
  194. (not SharedLibFiles.Empty) then
  195. begin
  196. AddSharedLibrary('root');
  197. linklibc:=true;
  198. end;
  199. if (not linklibc) and makelib then
  200. begin
  201. linklibc:=true;
  202. cprtobj:='dllprt.o';
  203. end;
  204. if linklibc then
  205. prtobj:=cprtobj;
  206. { Open link.res file }
  207. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,false);
  208. {
  209. if not isdll then
  210. LinkRes.Add('ld -o $1 $2 $3 $4 $5 $6 $7 $8 $9 \')
  211. else
  212. LinkRes.Add('ld -o $1 -e 0 $2 $3 $4 $5 $6 $7 $8 $9\');
  213. }
  214. LinkRes.Add('-m');
  215. // LinkRes.Add('elf_i386_be');
  216. LinkRes.Add('elf_i386_haiku');
  217. LinkRes.Add('-shared');
  218. LinkRes.Add('-Bsymbolic');
  219. { Write path to search libraries }
  220. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  221. while assigned(HPath) do
  222. begin
  223. LinkRes.Add('-L'+HPath.Str);
  224. HPath:=TCmdStrListItem(HPath.Next);
  225. end;
  226. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  227. while assigned(HPath) do
  228. begin
  229. LinkRes.Add('-L'+HPath.Str);
  230. HPath:=TCmdStrListItem(HPath.Next);
  231. end;
  232. { try to add crti and crtbegin if linking to C }
  233. if linklibc then
  234. begin
  235. if librarysearchpath.FindFile('crti.o',false,s) then
  236. LinkRes.AddFileName(s);
  237. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  238. LinkRes.AddFileName(s);
  239. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  240. if found then LinkRes.AddFileName(s+' \');}
  241. if prtobj<>'' then
  242. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  243. // if isdll then
  244. // LinkRes.AddFileName(FindObjectFile('func.o','',false));
  245. if librarysearchpath.FindFile('init_term_dyn.o',false,s) then
  246. LinkRes.AddFileName(s);
  247. end
  248. else
  249. begin
  250. if prtobj<>'' then
  251. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  252. end;
  253. { main objectfiles }
  254. while not ObjectFiles.Empty do
  255. begin
  256. s:=ObjectFiles.GetFirst;
  257. if s<>'' then
  258. LinkRes.AddFileName(s);
  259. end;
  260. { LinkRes.Add('-lroot \');
  261. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  262. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  263. { Write staticlibraries }
  264. if not StaticLibFiles.Empty then
  265. begin
  266. While not StaticLibFiles.Empty do
  267. begin
  268. S:=StaticLibFiles.GetFirst;
  269. LinkRes.AddFileName(s)
  270. end;
  271. end;
  272. { Write sharedlibraries like -l<lib> }
  273. if not SharedLibFiles.Empty then
  274. begin
  275. While not SharedLibFiles.Empty do
  276. begin
  277. S:=SharedLibFiles.GetFirst;
  278. if s<>'c' then
  279. begin
  280. i:=Pos(target_info.sharedlibext,S);
  281. if i>0 then
  282. Delete(S,i,255);
  283. LinkRes.Add('-l'+s);
  284. end
  285. else
  286. begin
  287. linklibc:=true;
  288. end;
  289. end;
  290. { be sure that libc is the last lib }
  291. { if linklibc then
  292. LinkRes.Add('-lroot');}
  293. { if linkdynamic and (Info.DynamicLinker<>'') then
  294. LinkRes.AddFileName(Info.DynamicLinker);}
  295. end;
  296. if isdll then
  297. LinkRes.Add('-lroot');
  298. { objects which must be at the end }
  299. if linklibc then
  300. begin
  301. if librarysearchpath.FindFile('crtend.o',false,s) then
  302. LinkRes.AddFileName(s);
  303. if librarysearchpath.FindFile('crtn.o',false,s) then
  304. LinkRes.AddFileName(s);
  305. end;
  306. { Write and Close response }
  307. linkres.writetodisk;
  308. linkres.free;
  309. WriteResponseFile:=True;
  310. end;
  311. function TLinkerBeOS.MakeExecutable:boolean;
  312. var
  313. binstr,
  314. cmdstr : TCmdStr;
  315. success,
  316. useshell : boolean;
  317. DynLinkStr : string[60];
  318. GCSectionsStr,
  319. StaticStr,
  320. StripStr : string[40];
  321. begin
  322. if not(cs_link_nolink in current_settings.globalswitches) then
  323. Message1(exec_i_linking,current_module.exefilename);
  324. { Create some replacements }
  325. StaticStr:='';
  326. StripStr:='';
  327. DynLinkStr:='';
  328. GCSectionsStr:='';
  329. if (cs_link_staticflag in current_settings.globalswitches) then
  330. StaticStr:='-static';
  331. if (cs_link_strip in current_settings.globalswitches) then
  332. StripStr:='-s';
  333. if (cs_link_smart in current_settings.globalswitches) and
  334. (tf_smartlink_sections in target_info.flags) then
  335. GCSectionsStr:='--gc-sections';
  336. If (cs_profile in current_settings.moduleswitches) 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,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  352. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  353. Replace(cmdstr,'$STATIC',StaticStr);
  354. Replace(cmdstr,'$STRIP',StripStr);
  355. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  356. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  357. useshell:=not (tf_no_backquote_support in source_info.flags);
  358. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,useshell);
  359. { Remove ReponseFile }
  360. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  361. DeleteFile(outputexedir+Info.ResName);
  362. MakeExecutable:=success; { otherwise a recursive call to link method }
  363. end;
  364. Function TLinkerBeOS.MakeSharedLibrary:boolean;
  365. var
  366. binstr,
  367. cmdstr,
  368. SoNameStr : TCmdStr;
  369. success : boolean;
  370. DynLinkStr : string[60];
  371. StaticStr,
  372. StripStr : string[40];
  373. begin
  374. MakeSharedLibrary:=false;
  375. if not(cs_link_nolink in current_settings.globalswitches) then
  376. Message1(exec_i_linking,current_module.sharedlibfilename);
  377. { Create some replacements }
  378. StaticStr:='';
  379. StripStr:='';
  380. DynLinkStr:='';
  381. if (cs_link_staticflag in current_settings.globalswitches) then
  382. StaticStr:='-static';
  383. if (cs_link_strip in current_settings.globalswitches) then
  384. StripStr:='-s';
  385. If (cs_profile in current_settings.moduleswitches) or
  386. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  387. begin
  388. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  389. if cshared Then
  390. DynLinkStr:='--shared ' + DynLinkStr;
  391. if rlinkpath<>'' Then
  392. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  393. End;
  394. { Write used files and libraries }
  395. WriteResponseFile(true,true);
  396. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  397. { Call linker }
  398. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  399. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  400. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  401. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  402. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  403. Replace(cmdstr,'$STATIC',StaticStr);
  404. Replace(cmdstr,'$STRIP',StripStr);
  405. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  406. Replace(cmdstr,'$SONAME',SoNameStr);
  407. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  408. { Strip the library ? }
  409. if success and (cs_link_strip in current_settings.globalswitches) then
  410. begin
  411. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  412. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  413. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  414. end;
  415. { Remove ReponseFile }
  416. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  417. DeleteFile(outputexedir+Info.ResName);
  418. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  419. end;
  420. {*****************************************************************************
  421. Initialize
  422. *****************************************************************************}
  423. initialization
  424. RegisterLinker(ld_beos,TLinkerbeos);
  425. {$ifdef i386}
  426. RegisterImport(system_i386_beos,timportlibbeos);
  427. RegisterExport(system_i386_beos,texportlibbeos);
  428. RegisterTarget(system_i386_beos_info);
  429. {$endif i386}
  430. end.