t_haiku.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. Copyright (c) 2008-2008 by Olivier Coursiere
  4. This unit implements support import,export,link routines
  5. for the (i386) Haiku 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_haiku;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. symsym,symdef,
  24. import,export,link;
  25. type
  26. timportlibhaiku=class(timportlib)
  27. procedure generatelib;override;
  28. end;
  29. texportlibhaiku=class(texportlib)
  30. procedure preparelib(const s : string);override;
  31. procedure exportprocedure(hp : texported_item);override;
  32. procedure exportvar(hp : texported_item);override;
  33. procedure generatelib;override;
  34. end;
  35. tlinkerhaiku=class(texternallinker)
  36. private
  37. Function WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  38. public
  39. constructor Create;override;
  40. procedure SetDefaultInfo;override;
  41. function MakeExecutable:boolean;override;
  42. function MakeSharedLibrary:boolean;override;
  43. end;
  44. implementation
  45. uses
  46. SysUtils,
  47. cutils,cfileutl,cclasses,
  48. verbose,systems,globtype,globals,
  49. symconst,script,
  50. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,i_haiku,ogbase;
  51. {*****************************************************************************
  52. TIMPORTLIBHAIKU
  53. *****************************************************************************}
  54. procedure timportlibhaiku.generatelib;
  55. var
  56. i : longint;
  57. ImportLibrary : TImportLibrary;
  58. begin
  59. for i:=0 to current_module.ImportLibraryList.Count-1 do
  60. begin
  61. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  62. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  63. end;
  64. end;
  65. {*****************************************************************************
  66. TEXPORTLIBHAIKU
  67. *****************************************************************************}
  68. procedure texportlibhaiku.preparelib(const s:string);
  69. begin
  70. end;
  71. procedure texportlibhaiku.exportprocedure(hp : texported_item);
  72. var
  73. hp2 : texported_item;
  74. begin
  75. { first test the index value }
  76. if (hp.options and eo_index)<>0 then
  77. begin
  78. Message1(parser_e_no_export_with_index_for_target,'haiku');
  79. exit;
  80. end;
  81. { now place in correct order }
  82. hp2:=texported_item(current_module._exports.first);
  83. while assigned(hp2) and
  84. (hp.name^>hp2.name^) do
  85. hp2:=texported_item(hp2.next);
  86. { insert hp there !! }
  87. if assigned(hp2) and (hp2.name^=hp.name^) then
  88. begin
  89. { this is not allowed !! }
  90. Message1(parser_e_export_name_double,hp.name^);
  91. exit;
  92. end;
  93. if hp2=texported_item(current_module._exports.first) then
  94. current_module._exports.concat(hp)
  95. else if assigned(hp2) then
  96. begin
  97. hp.next:=hp2;
  98. hp.previous:=hp2.previous;
  99. if assigned(hp2.previous) then
  100. hp2.previous.next:=hp;
  101. hp2.previous:=hp;
  102. end
  103. else
  104. current_module._exports.concat(hp);
  105. end;
  106. procedure texportlibhaiku.exportvar(hp : texported_item);
  107. begin
  108. hp.is_var:=true;
  109. exportprocedure(hp);
  110. end;
  111. procedure texportlibhaiku.generatelib;
  112. var
  113. hp2 : texported_item;
  114. pd : tprocdef;
  115. begin
  116. hp2:=texported_item(current_module._exports.first);
  117. while assigned(hp2) do
  118. begin
  119. if (not hp2.is_var) and
  120. (hp2.sym.typ=procsym) then
  121. begin
  122. { the manglednames can already be the same when the procedure
  123. is declared with cdecl }
  124. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  125. if pd.mangledname<>hp2.name^ then
  126. begin
  127. {$ifdef i386}
  128. { place jump in al_procedures }
  129. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  130. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0));
  131. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(pd.mangledname)));
  132. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  133. {$endif i386}
  134. end;
  135. end
  136. else
  137. Message1(parser_e_no_export_of_variables_for_target,'haiku');
  138. hp2:=texported_item(hp2.next);
  139. end;
  140. end;
  141. {*****************************************************************************
  142. TLINKERHAIKU
  143. *****************************************************************************}
  144. Constructor TLinkerHaiku.Create;
  145. var
  146. s : string;
  147. i : integer;
  148. begin
  149. Inherited Create;
  150. s:=GetEnvironmentVariable('BELIBRARIES');
  151. { convert to correct format in case under unix system }
  152. for i:=1 to length(s) do
  153. if s[i] = ':' then
  154. s[i] := ';';
  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.AddPath(sysrootpath,s,true); {format:'path1;path2;...'}
  160. end;
  161. procedure TLinkerHaiku.SetDefaultInfo;
  162. begin
  163. with Info do
  164. begin
  165. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE `cat $RES`';
  166. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE `cat $RES`';
  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 TLinkerHaiku.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);
  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_haiku');
  216. LinkRes.Add('-shared');
  217. LinkRes.Add('-Bsymbolic');
  218. { Write path to search libraries }
  219. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  220. while assigned(HPath) do
  221. begin
  222. LinkRes.Add('-L'+HPath.Str);
  223. HPath:=TCmdStrListItem(HPath.Next);
  224. end;
  225. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  226. while assigned(HPath) do
  227. begin
  228. LinkRes.Add('-L'+HPath.Str);
  229. HPath:=TCmdStrListItem(HPath.Next);
  230. end;
  231. { try to add crti and crtbegin if linking to C }
  232. if linklibc then
  233. begin
  234. if librarysearchpath.FindFile('crti.o',false,s) then
  235. LinkRes.AddFileName(s);
  236. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  237. LinkRes.AddFileName(s);
  238. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  239. if found then LinkRes.AddFileName(s+' \');}
  240. if prtobj<>'' then
  241. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  242. // if isdll then
  243. // LinkRes.AddFileName(FindObjectFile('func.o','',false));
  244. if librarysearchpath.FindFile('init_term_dyn.o',false,s) then
  245. LinkRes.AddFileName(s);
  246. end
  247. else
  248. begin
  249. if prtobj<>'' then
  250. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  251. end;
  252. { main objectfiles }
  253. while not ObjectFiles.Empty do
  254. begin
  255. s:=ObjectFiles.GetFirst;
  256. if s<>'' then
  257. LinkRes.AddFileName(s);
  258. end;
  259. { LinkRes.Add('-lroot \');
  260. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  261. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  262. { Write staticlibraries }
  263. if not StaticLibFiles.Empty then
  264. begin
  265. While not StaticLibFiles.Empty do
  266. begin
  267. S:=StaticLibFiles.GetFirst;
  268. LinkRes.AddFileName(s)
  269. end;
  270. end;
  271. { Write sharedlibraries like -l<lib> }
  272. if not SharedLibFiles.Empty then
  273. begin
  274. While not SharedLibFiles.Empty do
  275. begin
  276. S:=SharedLibFiles.GetFirst;
  277. if s<>'c' then
  278. begin
  279. i:=Pos(target_info.sharedlibext,S);
  280. if i>0 then
  281. Delete(S,i,255);
  282. LinkRes.Add('-l'+s);
  283. end
  284. else
  285. begin
  286. linklibc:=true;
  287. end;
  288. end;
  289. { be sure that libc is the last lib }
  290. { if linklibc then
  291. LinkRes.Add('-lroot');}
  292. { if linkdynamic and (Info.DynamicLinker<>'') then
  293. LinkRes.AddFileName(Info.DynamicLinker);}
  294. end;
  295. if isdll then
  296. LinkRes.Add('-lroot');
  297. { objects which must be at the end }
  298. if linklibc then
  299. begin
  300. if librarysearchpath.FindFile('crtend.o',false,s) then
  301. LinkRes.AddFileName(s);
  302. if librarysearchpath.FindFile('crtn.o',false,s) then
  303. LinkRes.AddFileName(s);
  304. end;
  305. { Write and Close response }
  306. linkres.Add(' ');
  307. linkres.writetodisk;
  308. linkres.free;
  309. WriteResponseFile:=True;
  310. end;
  311. function TLinkerHaiku.MakeExecutable:boolean;
  312. var
  313. binstr,
  314. cmdstr : TCmdStr;
  315. success : boolean;
  316. DynLinkStr : string[60];
  317. GCSectionsStr,
  318. StaticStr,
  319. StripStr : string[40];
  320. begin
  321. if not(cs_link_nolink in current_settings.globalswitches) then
  322. Message1(exec_i_linking,current_module.exefilename^);
  323. { Create some replacements }
  324. StaticStr:='';
  325. StripStr:='';
  326. DynLinkStr:='';
  327. GCSectionsStr:='';
  328. if (cs_link_staticflag in current_settings.globalswitches) then
  329. StaticStr:='-static';
  330. if (cs_link_strip in current_settings.globalswitches) then
  331. StripStr:='-s';
  332. if (cs_link_smart in current_settings.globalswitches) and
  333. (tf_smartlink_sections in target_info.flags) then
  334. GCSectionsStr:='--gc-sections';
  335. If (cs_profile in current_settings.moduleswitches) 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,'$GCSECTIONS',GCSectionsStr);
  354. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  355. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,true);
  356. { Remove ReponseFile }
  357. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  358. DeleteFile(outputexedir+Info.ResName);
  359. MakeExecutable:=success; { otherwise a recursive call to link method }
  360. end;
  361. Function TLinkerHaiku.MakeSharedLibrary:boolean;
  362. var
  363. binstr,
  364. cmdstr,
  365. SoNameStr : TCmdStr;
  366. success : boolean;
  367. DynLinkStr : string[60];
  368. StaticStr,
  369. StripStr : string[40];
  370. begin
  371. MakeSharedLibrary:=false;
  372. if not(cs_link_nolink in current_settings.globalswitches) then
  373. Message1(exec_i_linking,current_module.sharedlibfilename^);
  374. { Create some replacements }
  375. StaticStr:='';
  376. StripStr:='';
  377. DynLinkStr:='';
  378. if (cs_link_staticflag in current_settings.globalswitches) then
  379. StaticStr:='-static';
  380. if (cs_link_strip in current_settings.globalswitches) then
  381. StripStr:='-s';
  382. If (cs_profile in current_settings.moduleswitches) or
  383. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  384. begin
  385. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  386. if cshared Then
  387. DynLinkStr:='--shared ' + DynLinkStr;
  388. if rlinkpath<>'' Then
  389. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  390. End;
  391. { Write used files and libraries }
  392. WriteResponseFile(true,true);
  393. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename^);
  394. { Call linker }
  395. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  396. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  397. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  398. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  399. Replace(cmdstr,'$STATIC',StaticStr);
  400. Replace(cmdstr,'$STRIP',StripStr);
  401. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  402. Replace(cmdstr,'$SONAME',SoNameStr);
  403. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  404. { Strip the library ? }
  405. if success and (cs_link_strip in current_settings.globalswitches) then
  406. begin
  407. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  408. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename^));
  409. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  410. end;
  411. { Remove ReponseFile }
  412. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  413. DeleteFile(outputexedir+Info.ResName);
  414. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  415. end;
  416. {*****************************************************************************
  417. Initialize
  418. *****************************************************************************}
  419. initialization
  420. {$ifdef i386}
  421. RegisterExternalLinker(system_i386_haiku_info,TLinkerhaiku);
  422. RegisterImport(system_i386_haiku,timportlibhaiku);
  423. RegisterExport(system_i386_haiku,texportlibhaiku);
  424. RegisterTarget(system_i386_haiku_info);
  425. {$endif i386}
  426. end.