t_haiku.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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,cscript,
  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 eo_index in hp.options 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. duplicatesymbol(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,pd));
  131. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(pd.mangledname,AT_FUNCTION)));
  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. else if makelib then
  219. begin
  220. // Making a dll with libc linking. Should be always the case under Haiku.
  221. cprtobj:='dllcprt0';
  222. end;
  223. if linklibc then
  224. prtobj:=cprtobj;
  225. { Open link.res file }
  226. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,false);
  227. {
  228. if not isdll then
  229. LinkRes.Add('ld -o $1 $2 $3 $4 $5 $6 $7 $8 $9 \')
  230. else
  231. LinkRes.Add('ld -o $1 -e 0 $2 $3 $4 $5 $6 $7 $8 $9\');
  232. }
  233. LinkRes.Add('-m');
  234. {$ifdef i386}
  235. LinkRes.Add('elf_i386_haiku');
  236. {$else i386}
  237. LinkRes.Add('elf_x86_64_haiku');
  238. {$endif i386}
  239. LinkRes.Add('-shared');
  240. LinkRes.Add('-Bsymbolic');
  241. { Write path to search libraries }
  242. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  243. while assigned(HPath) do
  244. begin
  245. LinkRes.Add('-L'+HPath.Str);
  246. HPath:=TCmdStrListItem(HPath.Next);
  247. end;
  248. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  249. while assigned(HPath) do
  250. begin
  251. LinkRes.Add('-L'+HPath.Str);
  252. HPath:=TCmdStrListItem(HPath.Next);
  253. end;
  254. { try to add crti and crtbegin if linking to C }
  255. if linklibc then
  256. begin
  257. if librarysearchpath.FindFile('crti.o',false,s) then
  258. LinkRes.AddFileName(s);
  259. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  260. LinkRes.AddFileName(s);
  261. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  262. if found then LinkRes.AddFileName(s+' \');}
  263. if prtobj<>'' then
  264. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  265. // if isdll then
  266. // LinkRes.AddFileName(FindObjectFile('func.o','',false));
  267. if librarysearchpath.FindFile('init_term_dyn.o',false,s) then
  268. LinkRes.AddFileName(s);
  269. end
  270. else
  271. begin
  272. if prtobj<>'' then
  273. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  274. end;
  275. { main objectfiles }
  276. while not ObjectFiles.Empty do
  277. begin
  278. s:=ObjectFiles.GetFirst;
  279. if s<>'' then
  280. LinkRes.AddFileName(s);
  281. end;
  282. { LinkRes.Add('-lroot \');
  283. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  284. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  285. { Write staticlibraries }
  286. if not StaticLibFiles.Empty then
  287. begin
  288. While not StaticLibFiles.Empty do
  289. begin
  290. S:=StaticLibFiles.GetFirst;
  291. LinkRes.AddFileName(s)
  292. end;
  293. end;
  294. { Write sharedlibraries like -l<lib> }
  295. if not SharedLibFiles.Empty then
  296. begin
  297. While not SharedLibFiles.Empty do
  298. begin
  299. S:=SharedLibFiles.GetFirst;
  300. if s<>'c' then
  301. begin
  302. i:=Pos(target_info.sharedlibext,S);
  303. if i>0 then
  304. Delete(S,i,255);
  305. LinkRes.Add('-l'+s);
  306. end
  307. else
  308. begin
  309. linklibc:=true;
  310. end;
  311. end;
  312. { be sure that libc is the last lib }
  313. { if linklibc then
  314. LinkRes.Add('-lroot');}
  315. { if linkdynamic and (Info.DynamicLinker<>'') then
  316. LinkRes.AddFileName(Info.DynamicLinker);}
  317. end;
  318. if isdll then
  319. LinkRes.Add('-lroot');
  320. { objects which must be at the end }
  321. if linklibc then
  322. begin
  323. if librarysearchpath.FindFile('crtend.o',false,s) then
  324. LinkRes.AddFileName(s);
  325. if librarysearchpath.FindFile('crtn.o',false,s) then
  326. LinkRes.AddFileName(s);
  327. end;
  328. { Write and Close response }
  329. linkres.writetodisk;
  330. linkres.free;
  331. WriteResponseFile:=True;
  332. end;
  333. function TLinkerHaiku.MakeExecutable:boolean;
  334. var
  335. binstr,
  336. cmdstr : TCmdStr;
  337. success,
  338. useshell : boolean;
  339. DynLinkStr : string[60];
  340. GCSectionsStr,
  341. StaticStr,
  342. StripStr : string[40];
  343. begin
  344. if not(cs_link_nolink in current_settings.globalswitches) then
  345. Message1(exec_i_linking,current_module.exefilename);
  346. { Create some replacements }
  347. StaticStr:='';
  348. StripStr:='';
  349. DynLinkStr:='';
  350. GCSectionsStr:='';
  351. if (cs_link_staticflag in current_settings.globalswitches) then
  352. StaticStr:='-static';
  353. if (cs_link_strip in current_settings.globalswitches) then
  354. StripStr:='-s';
  355. if (cs_link_smart in current_settings.globalswitches) and
  356. (tf_smartlink_sections in target_info.flags) then
  357. GCSectionsStr:='--gc-sections';
  358. If (cs_profile in current_settings.moduleswitches) or
  359. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  360. begin
  361. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  362. if cshared Then
  363. DynLinkStr:='--shared ' + DynLinkStr;
  364. if rlinkpath<>'' Then
  365. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  366. End;
  367. { Write used files and libraries }
  368. WriteResponseFile(false,false);
  369. { Call linker }
  370. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  371. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  372. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  373. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  374. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  375. Replace(cmdstr,'$STATIC',StaticStr);
  376. Replace(cmdstr,'$STRIP',StripStr);
  377. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  378. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  379. useshell:=not (tf_no_backquote_support in source_info.flags);
  380. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,useshell);
  381. { Remove ReponseFile }
  382. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  383. DeleteFile(outputexedir+Info.ResName);
  384. MakeExecutable:=success; { otherwise a recursive call to link method }
  385. end;
  386. Function TLinkerHaiku.MakeSharedLibrary:boolean;
  387. var
  388. binstr,
  389. cmdstr,
  390. SoNameStr : TCmdStr;
  391. success : boolean;
  392. DynLinkStr : string[60];
  393. StaticStr,
  394. StripStr : string[40];
  395. begin
  396. MakeSharedLibrary:=false;
  397. if not(cs_link_nolink in current_settings.globalswitches) then
  398. Message1(exec_i_linking,current_module.sharedlibfilename);
  399. { Create some replacements }
  400. StaticStr:='';
  401. StripStr:='';
  402. DynLinkStr:='';
  403. if (cs_link_staticflag in current_settings.globalswitches) then
  404. StaticStr:='-static';
  405. if (cs_link_strip in current_settings.globalswitches) then
  406. StripStr:='-s';
  407. If (cs_profile in current_settings.moduleswitches) or
  408. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  409. begin
  410. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  411. if cshared Then
  412. DynLinkStr:='--shared ' + DynLinkStr;
  413. if rlinkpath<>'' Then
  414. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  415. End;
  416. { Write used files and libraries }
  417. WriteResponseFile(true,true);
  418. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  419. { Call linker }
  420. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  421. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  422. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  423. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  424. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  425. Replace(cmdstr,'$STATIC',StaticStr);
  426. Replace(cmdstr,'$STRIP',StripStr);
  427. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  428. Replace(cmdstr,'$SONAME',SoNameStr);
  429. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  430. { Strip the library ? }
  431. if success and (cs_link_strip in current_settings.globalswitches) then
  432. begin
  433. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  434. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  435. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  436. end;
  437. { Remove ReponseFile }
  438. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  439. DeleteFile(outputexedir+Info.ResName);
  440. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  441. end;
  442. {*****************************************************************************
  443. Initialize
  444. *****************************************************************************}
  445. initialization
  446. RegisterLinker(ld_haiku,TLinkerhaiku);
  447. {$ifdef i386}
  448. RegisterImport(system_i386_haiku,timportlibhaiku);
  449. RegisterExport(system_i386_haiku,texportlibhaiku);
  450. RegisterTarget(system_i386_haiku_info);
  451. {$endif i386}
  452. {$ifdef x86_64}
  453. RegisterImport(system_x86_64_haiku,timportlibhaiku);
  454. RegisterExport(system_x86_64_haiku,texportlibhaiku);
  455. RegisterTarget(system_x86_64_haiku_info);
  456. {$endif x86_64}
  457. end.