t_haiku.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. const
  146. HomeNonPackagedDevLib = '/boot/home/config/non-packaged/develop/lib';
  147. HomeDevLib = '/boot/home/config/develop/lib';
  148. CommonNonPackagedDevLib = '/boot/common/non-packaged/develop/lib';
  149. CommonDevLib = '/boot/common/develop/lib';
  150. SystemDevLib = '/boot/system/develop/lib';
  151. var
  152. s : string;
  153. i : integer;
  154. begin
  155. Inherited Create;
  156. s:=GetEnvironmentVariable('BELIBRARIES');
  157. { convert to correct format in case under unix system }
  158. for i:=1 to length(s) do
  159. if s[i] = ':' then
  160. s[i] := ';';
  161. { just in case we have a single path : add the ending ; }
  162. { since that is what the compiler expects. }
  163. if pos(';',s) = 0 then
  164. s:=s+';';
  165. // Under Haiku with package management, BELIBRARIES is empty by default
  166. // We have to look at those system paths, in this order.
  167. // User can still customize BELIBRARIES. That is why it is looked at first.
  168. LibrarySearchPath.AddPath(sysrootpath,s,true); {format:'path1;path2;...'}
  169. LibrarySearchPath.AddPath(sysrootpath, HomeNonPackagedDevLib, false);
  170. LibrarySearchPath.AddPath(sysrootpath, HomeDevLib, false);
  171. LibrarySearchPath.AddPath(sysrootpath, CommonNonPackagedDevLib, false);
  172. LibrarySearchPath.AddPath(sysrootpath, CommonDevLib, false);
  173. LibrarySearchPath.AddPath(sysrootpath, SystemDevLib, false);
  174. end;
  175. procedure TLinkerHaiku.SetDefaultInfo;
  176. begin
  177. with Info do
  178. begin
  179. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $CATRES';
  180. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $CATRES';
  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 TLinkerHaiku.WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  193. Var
  194. linkres : TLinkRes;
  195. i : integer;
  196. cprtobj,
  197. prtobj : string[80];
  198. HPath : TCmdStrListItem;
  199. s : TCmdStr;
  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 current_settings.moduleswitches) 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,false);
  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');
  229. LinkRes.Add('elf_i386_haiku');
  230. LinkRes.Add('-shared');
  231. LinkRes.Add('-Bsymbolic');
  232. { Write path to search libraries }
  233. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  234. while assigned(HPath) do
  235. begin
  236. LinkRes.Add('-L'+HPath.Str);
  237. HPath:=TCmdStrListItem(HPath.Next);
  238. end;
  239. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  240. while assigned(HPath) do
  241. begin
  242. LinkRes.Add('-L'+HPath.Str);
  243. HPath:=TCmdStrListItem(HPath.Next);
  244. end;
  245. { try to add crti and crtbegin if linking to C }
  246. if linklibc then
  247. begin
  248. if librarysearchpath.FindFile('crti.o',false,s) then
  249. LinkRes.AddFileName(s);
  250. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  251. LinkRes.AddFileName(s);
  252. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  253. if found then LinkRes.AddFileName(s+' \');}
  254. if prtobj<>'' then
  255. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  256. // if isdll then
  257. // LinkRes.AddFileName(FindObjectFile('func.o','',false));
  258. if librarysearchpath.FindFile('init_term_dyn.o',false,s) then
  259. LinkRes.AddFileName(s);
  260. end
  261. else
  262. begin
  263. if prtobj<>'' then
  264. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  265. end;
  266. { main objectfiles }
  267. while not ObjectFiles.Empty do
  268. begin
  269. s:=ObjectFiles.GetFirst;
  270. if s<>'' then
  271. LinkRes.AddFileName(s);
  272. end;
  273. { LinkRes.Add('-lroot \');
  274. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  275. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  276. { Write staticlibraries }
  277. if not StaticLibFiles.Empty then
  278. begin
  279. While not StaticLibFiles.Empty do
  280. begin
  281. S:=StaticLibFiles.GetFirst;
  282. LinkRes.AddFileName(s)
  283. end;
  284. end;
  285. { Write sharedlibraries like -l<lib> }
  286. if not SharedLibFiles.Empty then
  287. begin
  288. While not SharedLibFiles.Empty do
  289. begin
  290. S:=SharedLibFiles.GetFirst;
  291. if s<>'c' then
  292. begin
  293. i:=Pos(target_info.sharedlibext,S);
  294. if i>0 then
  295. Delete(S,i,255);
  296. LinkRes.Add('-l'+s);
  297. end
  298. else
  299. begin
  300. linklibc:=true;
  301. end;
  302. end;
  303. { be sure that libc is the last lib }
  304. { if linklibc then
  305. LinkRes.Add('-lroot');}
  306. { if linkdynamic and (Info.DynamicLinker<>'') then
  307. LinkRes.AddFileName(Info.DynamicLinker);}
  308. end;
  309. if isdll then
  310. LinkRes.Add('-lroot');
  311. { objects which must be at the end }
  312. if linklibc then
  313. begin
  314. if librarysearchpath.FindFile('crtend.o',false,s) then
  315. LinkRes.AddFileName(s);
  316. if librarysearchpath.FindFile('crtn.o',false,s) then
  317. LinkRes.AddFileName(s);
  318. end;
  319. { Write and Close response }
  320. linkres.writetodisk;
  321. linkres.free;
  322. WriteResponseFile:=True;
  323. end;
  324. function TLinkerHaiku.MakeExecutable:boolean;
  325. var
  326. binstr,
  327. cmdstr : TCmdStr;
  328. success,
  329. useshell : boolean;
  330. DynLinkStr : string[60];
  331. GCSectionsStr,
  332. StaticStr,
  333. StripStr : string[40];
  334. begin
  335. if not(cs_link_nolink in current_settings.globalswitches) then
  336. Message1(exec_i_linking,current_module.exefilename);
  337. { Create some replacements }
  338. StaticStr:='';
  339. StripStr:='';
  340. DynLinkStr:='';
  341. GCSectionsStr:='';
  342. if (cs_link_staticflag in current_settings.globalswitches) then
  343. StaticStr:='-static';
  344. if (cs_link_strip in current_settings.globalswitches) then
  345. StripStr:='-s';
  346. if (cs_link_smart in current_settings.globalswitches) and
  347. (tf_smartlink_sections in target_info.flags) then
  348. GCSectionsStr:='--gc-sections';
  349. If (cs_profile in current_settings.moduleswitches) or
  350. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  351. begin
  352. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  353. if cshared Then
  354. DynLinkStr:='--shared ' + DynLinkStr;
  355. if rlinkpath<>'' Then
  356. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  357. End;
  358. { Write used files and libraries }
  359. WriteResponseFile(false,false);
  360. { Call linker }
  361. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  362. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  363. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  364. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  365. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  366. Replace(cmdstr,'$STATIC',StaticStr);
  367. Replace(cmdstr,'$STRIP',StripStr);
  368. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  369. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  370. useshell:=not (tf_no_backquote_support in source_info.flags);
  371. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,useshell);
  372. { Remove ReponseFile }
  373. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  374. DeleteFile(outputexedir+Info.ResName);
  375. MakeExecutable:=success; { otherwise a recursive call to link method }
  376. end;
  377. Function TLinkerHaiku.MakeSharedLibrary:boolean;
  378. var
  379. binstr,
  380. cmdstr,
  381. SoNameStr : TCmdStr;
  382. success : boolean;
  383. DynLinkStr : string[60];
  384. StaticStr,
  385. StripStr : string[40];
  386. begin
  387. MakeSharedLibrary:=false;
  388. if not(cs_link_nolink in current_settings.globalswitches) then
  389. Message1(exec_i_linking,current_module.sharedlibfilename);
  390. { Create some replacements }
  391. StaticStr:='';
  392. StripStr:='';
  393. DynLinkStr:='';
  394. if (cs_link_staticflag in current_settings.globalswitches) then
  395. StaticStr:='-static';
  396. if (cs_link_strip in current_settings.globalswitches) then
  397. StripStr:='-s';
  398. If (cs_profile in current_settings.moduleswitches) or
  399. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  400. begin
  401. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  402. if cshared Then
  403. DynLinkStr:='--shared ' + DynLinkStr;
  404. if rlinkpath<>'' Then
  405. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  406. End;
  407. { Write used files and libraries }
  408. WriteResponseFile(true,true);
  409. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  410. { Call linker }
  411. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  412. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  413. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  414. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  415. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  416. Replace(cmdstr,'$STATIC',StaticStr);
  417. Replace(cmdstr,'$STRIP',StripStr);
  418. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  419. Replace(cmdstr,'$SONAME',SoNameStr);
  420. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  421. { Strip the library ? }
  422. if success and (cs_link_strip in current_settings.globalswitches) then
  423. begin
  424. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  425. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  426. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  427. end;
  428. { Remove ReponseFile }
  429. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  430. DeleteFile(outputexedir+Info.ResName);
  431. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  432. end;
  433. {*****************************************************************************
  434. Initialize
  435. *****************************************************************************}
  436. initialization
  437. RegisterLinker(ld_haiku,TLinkerhaiku);
  438. {$ifdef i386}
  439. RegisterImport(system_i386_haiku,timportlibhaiku);
  440. RegisterExport(system_i386_haiku,texportlibhaiku);
  441. RegisterTarget(system_i386_haiku_info);
  442. {$endif i386}
  443. end.