t_haiku.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. procedure InitSysInitUnitName;override;
  42. function MakeExecutable:boolean;override;
  43. function MakeSharedLibrary:boolean;override;
  44. end;
  45. implementation
  46. uses
  47. SysUtils,
  48. cutils,cfileutl,cclasses,
  49. verbose,systems,globtype,globals,
  50. symconst,cscript,
  51. fmodule,aasmbase,aasmtai,aasmdata,aasmcpu,cpubase,i_haiku,ogbase;
  52. {*****************************************************************************
  53. TIMPORTLIBHAIKU
  54. *****************************************************************************}
  55. procedure timportlibhaiku.generatelib;
  56. var
  57. i : longint;
  58. ImportLibrary : TImportLibrary;
  59. begin
  60. for i:=0 to current_module.ImportLibraryList.Count-1 do
  61. begin
  62. ImportLibrary:=TImportLibrary(current_module.ImportLibraryList[i]);
  63. current_module.linkothersharedlibs.add(ImportLibrary.Name,link_always);
  64. end;
  65. end;
  66. {*****************************************************************************
  67. TEXPORTLIBHAIKU
  68. *****************************************************************************}
  69. procedure texportlibhaiku.preparelib(const s:string);
  70. begin
  71. end;
  72. procedure texportlibhaiku.exportprocedure(hp : texported_item);
  73. var
  74. hp2 : texported_item;
  75. begin
  76. { first test the index value }
  77. if eo_index in hp.options then
  78. begin
  79. Message1(parser_e_no_export_with_index_for_target,'haiku');
  80. exit;
  81. end;
  82. { now place in correct order }
  83. hp2:=texported_item(current_module._exports.first);
  84. while assigned(hp2) and
  85. (hp.name^>hp2.name^) do
  86. hp2:=texported_item(hp2.next);
  87. { insert hp there !! }
  88. if assigned(hp2) and (hp2.name^=hp.name^) then
  89. begin
  90. { this is not allowed !! }
  91. duplicatesymbol(hp.name^);
  92. exit;
  93. end;
  94. if hp2=texported_item(current_module._exports.first) then
  95. current_module._exports.concat(hp)
  96. else if assigned(hp2) then
  97. begin
  98. hp.next:=hp2;
  99. hp.previous:=hp2.previous;
  100. if assigned(hp2.previous) then
  101. hp2.previous.next:=hp;
  102. hp2.previous:=hp;
  103. end
  104. else
  105. current_module._exports.concat(hp);
  106. end;
  107. procedure texportlibhaiku.exportvar(hp : texported_item);
  108. begin
  109. hp.is_var:=true;
  110. exportprocedure(hp);
  111. end;
  112. procedure texportlibhaiku.generatelib;
  113. var
  114. hp2 : texported_item;
  115. pd : tprocdef;
  116. begin
  117. hp2:=texported_item(current_module._exports.first);
  118. while assigned(hp2) do
  119. begin
  120. if (not hp2.is_var) and
  121. (hp2.sym.typ=procsym) then
  122. begin
  123. { the manglednames can already be the same when the procedure
  124. is declared with cdecl }
  125. pd:=tprocdef(tprocsym(hp2.sym).ProcdefList[0]);
  126. if pd.mangledname<>hp2.name^ then
  127. begin
  128. {$ifdef i386}
  129. { place jump in al_procedures }
  130. current_asmdata.asmlists[al_procedures].concat(Tai_align.Create_op(4,$90));
  131. current_asmdata.asmlists[al_procedures].concat(Tai_symbol.Createname_global(hp2.name^,AT_FUNCTION,0,pd));
  132. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(pd.mangledname,AT_FUNCTION)));
  133. current_asmdata.asmlists[al_procedures].concat(Tai_symbol_end.Createname(hp2.name^));
  134. {$endif i386}
  135. end;
  136. end
  137. else
  138. Message1(parser_e_no_export_of_variables_for_target,'haiku');
  139. hp2:=texported_item(hp2.next);
  140. end;
  141. end;
  142. {*****************************************************************************
  143. TLINKERHAIKU
  144. *****************************************************************************}
  145. Constructor TLinkerHaiku.Create;
  146. const
  147. HomeNonPackagedDevLib = '=/boot/home/config/non-packaged/develop/lib';
  148. HomeDevLib = '=/boot/home/config/develop/lib';
  149. CommonNonPackagedDevLib = '=/boot/common/non-packaged/develop/lib';
  150. CommonDevLib = '=/boot/common/develop/lib';
  151. SystemDevLib = '=/boot/system/develop/lib';
  152. var
  153. s : string;
  154. i : integer;
  155. begin
  156. Inherited Create;
  157. s:=GetEnvironmentVariable('BELIBRARIES');
  158. { convert to correct format in case under unix system }
  159. for i:=1 to length(s) do
  160. if s[i] = ':' then
  161. s[i] := ';';
  162. { just in case we have a single path : add the ending ; }
  163. { since that is what the compiler expects. }
  164. if pos(';',s) = 0 then
  165. s:=s+';';
  166. // Under Haiku with package management, BELIBRARIES is empty by default
  167. // We have to look at those system paths, in this order.
  168. // User can still customize BELIBRARIES. That is why it is looked at first.
  169. LibrarySearchPath.AddLibraryPath(sysrootpath,s,true); {format:'path1;path2;...'}
  170. LibrarySearchPath.AddLibraryPath(sysrootpath, HomeNonPackagedDevLib, false);
  171. LibrarySearchPath.AddLibraryPath(sysrootpath, HomeDevLib, false);
  172. LibrarySearchPath.AddLibraryPath(sysrootpath, CommonNonPackagedDevLib, false);
  173. LibrarySearchPath.AddLibraryPath(sysrootpath, CommonDevLib, false);
  174. LibrarySearchPath.AddLibraryPath(sysrootpath, SystemDevLib, false);
  175. end;
  176. procedure TLinkerHaiku.SetDefaultInfo;
  177. begin
  178. with Info do
  179. begin
  180. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $CATRES';
  181. DllCmd[1]:='ld $OPT $INIT $FINI $SONAME -shared -L. -o $EXE $CATRES';
  182. DllCmd[2]:='strip --strip-unneeded $EXE';
  183. (*
  184. ExeCmd[1]:='sh $RES $EXE $OPT $STATIC $STRIP -L.';
  185. { ExeCmd[1]:='sh $RES $EXE $OPT $DYNLINK $STATIC $STRIP -L.';}
  186. DllCmd[1]:='sh $RES $EXE $OPT -L.';
  187. { DllCmd[1]:='sh $RES $EXE $OPT -L. -g -nostart -soname=$EXE';
  188. } DllCmd[2]:='strip --strip-unneeded $EXE';
  189. { DynamicLinker:='/lib/ld-beos.so.2';}
  190. *)
  191. end;
  192. end;
  193. procedure TLinkerHaiku.InitSysInitUnitName;
  194. const
  195. SysInitUnitNames: array[boolean] of string[15] = ( 'si_c', 'si_dllc' );
  196. begin
  197. sysinitunit:=SysInitUnitNames[current_module.islibrary];
  198. end;
  199. function TLinkerHaiku.WriteResponseFile(isdll:boolean;makelib:boolean) : Boolean;
  200. Var
  201. linkres : TLinkRes;
  202. i : integer;
  203. cprtobj,
  204. prtobj : string[80];
  205. HPath : TCmdStrListItem;
  206. s : TCmdStr;
  207. linklibc : boolean;
  208. begin
  209. WriteResponseFile:=False;
  210. { set special options for some targets }
  211. linklibc:=(SharedLibFiles.Find('root')<>nil);
  212. if (cs_profile in current_settings.moduleswitches) or
  213. (not SharedLibFiles.Empty) then
  214. begin
  215. AddSharedLibrary('root');
  216. linklibc:=true;
  217. end;
  218. prtobj:='';
  219. cprtobj:='';
  220. if not (target_info.system in systems_internal_sysinit) then
  221. begin
  222. prtobj:='prt0';
  223. cprtobj:='cprt0';
  224. if (not linklibc) and makelib then
  225. begin
  226. linklibc:=true;
  227. cprtobj:='dllprt.o';
  228. end
  229. else if makelib then
  230. begin
  231. // Making a dll with libc linking. Should be always the case under Haiku.
  232. cprtobj:='dllcprt0';
  233. end;
  234. end;
  235. if linklibc then
  236. prtobj:=cprtobj;
  237. { Open link.res file }
  238. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName,false);
  239. {
  240. if not isdll then
  241. LinkRes.Add('ld -o $1 $2 $3 $4 $5 $6 $7 $8 $9 \')
  242. else
  243. LinkRes.Add('ld -o $1 -e 0 $2 $3 $4 $5 $6 $7 $8 $9\');
  244. }
  245. LinkRes.Add('-m');
  246. {$ifdef i386}
  247. LinkRes.Add('elf_i386_haiku');
  248. {$else i386}
  249. LinkRes.Add('elf_x86_64_haiku');
  250. {$endif i386}
  251. LinkRes.Add('-shared');
  252. LinkRes.Add('-Bsymbolic');
  253. { Write path to search libraries }
  254. HPath:=TCmdStrListItem(current_module.locallibrarysearchpath.First);
  255. while assigned(HPath) do
  256. begin
  257. LinkRes.Add('-L'+HPath.Str);
  258. HPath:=TCmdStrListItem(HPath.Next);
  259. end;
  260. HPath:=TCmdStrListItem(LibrarySearchPath.First);
  261. while assigned(HPath) do
  262. begin
  263. LinkRes.Add('-L'+HPath.Str);
  264. HPath:=TCmdStrListItem(HPath.Next);
  265. end;
  266. { try to add crti and crtbegin if linking to C }
  267. if linklibc then
  268. begin
  269. if librarysearchpath.FindFile('crti.o',false,s) then
  270. LinkRes.AddFileName(s);
  271. if librarysearchpath.FindFile('crtbegin.o',false,s) then
  272. LinkRes.AddFileName(s);
  273. { s:=librarysearchpath.FindFile('start_dyn.o',found)+'start_dyn.o';
  274. if found then LinkRes.AddFileName(s+' \');}
  275. if prtobj<>'' then
  276. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  277. // if isdll then
  278. // LinkRes.AddFileName(FindObjectFile('func.o','',false));
  279. if librarysearchpath.FindFile('init_term_dyn.o',false,s) then
  280. LinkRes.AddFileName(s);
  281. end
  282. else
  283. begin
  284. if prtobj<>'' then
  285. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  286. end;
  287. { main objectfiles }
  288. while not ObjectFiles.Empty do
  289. begin
  290. s:=ObjectFiles.GetFirst;
  291. if s<>'' then
  292. LinkRes.AddFileName(s);
  293. end;
  294. { LinkRes.Add('-lroot \');
  295. LinkRes.Add('/boot/develop/tools/gnupro/lib/gcc-lib/i586-beos/2.9-beos-991026/crtend.o \');
  296. LinkRes.Add('/boot/develop/lib/x86/crtn.o \');}
  297. { Write staticlibraries }
  298. if not StaticLibFiles.Empty then
  299. begin
  300. While not StaticLibFiles.Empty do
  301. begin
  302. S:=StaticLibFiles.GetFirst;
  303. LinkRes.AddFileName(s)
  304. end;
  305. end;
  306. { Write sharedlibraries like -l<lib> }
  307. if not SharedLibFiles.Empty then
  308. begin
  309. While not SharedLibFiles.Empty do
  310. begin
  311. S:=SharedLibFiles.GetFirst;
  312. if s<>'c' then
  313. begin
  314. i:=Pos(target_info.sharedlibext,S);
  315. if i>0 then
  316. Delete(S,i,255);
  317. LinkRes.Add('-l'+s);
  318. end
  319. else
  320. begin
  321. linklibc:=true;
  322. end;
  323. end;
  324. { be sure that libc is the last lib }
  325. { if linklibc then
  326. LinkRes.Add('-lroot');}
  327. { if linkdynamic and (Info.DynamicLinker<>'') then
  328. LinkRes.AddFileName(Info.DynamicLinker);}
  329. end;
  330. if isdll then
  331. LinkRes.Add('-lroot');
  332. { objects which must be at the end }
  333. if linklibc then
  334. begin
  335. if librarysearchpath.FindFile('crtend.o',false,s) then
  336. LinkRes.AddFileName(s);
  337. if librarysearchpath.FindFile('crtn.o',false,s) then
  338. LinkRes.AddFileName(s);
  339. end;
  340. { Write and Close response }
  341. linkres.writetodisk;
  342. linkres.free;
  343. WriteResponseFile:=True;
  344. end;
  345. function TLinkerHaiku.MakeExecutable:boolean;
  346. var
  347. binstr,
  348. cmdstr : TCmdStr;
  349. success,
  350. useshell : boolean;
  351. DynLinkStr : string[60];
  352. GCSectionsStr,
  353. StaticStr,
  354. StripStr : string[40];
  355. begin
  356. if not(cs_link_nolink in current_settings.globalswitches) then
  357. Message1(exec_i_linking,current_module.exefilename);
  358. { Create some replacements }
  359. StaticStr:='';
  360. StripStr:='';
  361. DynLinkStr:='';
  362. GCSectionsStr:='';
  363. if (cs_link_staticflag in current_settings.globalswitches) then
  364. StaticStr:='-static';
  365. if (cs_link_strip in current_settings.globalswitches) then
  366. StripStr:='-s';
  367. if (cs_link_smart in current_settings.globalswitches) and
  368. (tf_smartlink_sections in target_info.flags) then
  369. GCSectionsStr:='--gc-sections';
  370. If (cs_profile in current_settings.moduleswitches) or
  371. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  372. begin
  373. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  374. if cshared Then
  375. DynLinkStr:='--shared ' + DynLinkStr;
  376. if rlinkpath<>'' Then
  377. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  378. End;
  379. { Write used files and libraries }
  380. WriteResponseFile(false,false);
  381. { Call linker }
  382. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  383. Replace(cmdstr,'$EXE',maybequoted(current_module.exefilename));
  384. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  385. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  386. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  387. Replace(cmdstr,'$STATIC',StaticStr);
  388. Replace(cmdstr,'$STRIP',StripStr);
  389. Replace(cmdstr,'$GCSECTIONS',GCSectionsStr);
  390. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  391. useshell:=not (tf_no_backquote_support in source_info.flags);
  392. success:=DoExec(FindUtil(utilsprefix+BinStr),CmdStr,true,useshell);
  393. { Remove ReponseFile }
  394. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  395. DeleteFile(outputexedir+Info.ResName);
  396. MakeExecutable:=success; { otherwise a recursive call to link method }
  397. end;
  398. Function TLinkerHaiku.MakeSharedLibrary:boolean;
  399. var
  400. binstr,
  401. cmdstr,
  402. SoNameStr : TCmdStr;
  403. success : boolean;
  404. DynLinkStr : string[60];
  405. StaticStr,
  406. StripStr : string[40];
  407. begin
  408. MakeSharedLibrary:=false;
  409. if not(cs_link_nolink in current_settings.globalswitches) then
  410. Message1(exec_i_linking,current_module.sharedlibfilename);
  411. { Create some replacements }
  412. StaticStr:='';
  413. StripStr:='';
  414. DynLinkStr:='';
  415. if (cs_link_staticflag in current_settings.globalswitches) then
  416. StaticStr:='-static';
  417. if (cs_link_strip in current_settings.globalswitches) then
  418. StripStr:='-s';
  419. If (cs_profile in current_settings.moduleswitches) or
  420. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  421. begin
  422. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  423. if cshared Then
  424. DynLinkStr:='--shared ' + DynLinkStr;
  425. if rlinkpath<>'' Then
  426. DynLinkStr:='--rpath-link '+rlinkpath + ' '+ DynLinkStr;
  427. End;
  428. { Write used files and libraries }
  429. WriteResponseFile(true,true);
  430. SoNameStr:='-soname '+ExtractFileName(current_module.sharedlibfilename);
  431. { Call linker }
  432. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  433. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  434. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  435. Replace(cmdstr,'$CATRES',CatFileContent(outputexedir+Info.ResName));
  436. Replace(cmdstr,'$RES',maybequoted(outputexedir+Info.ResName));
  437. Replace(cmdstr,'$STATIC',StaticStr);
  438. Replace(cmdstr,'$STRIP',StripStr);
  439. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  440. Replace(cmdstr,'$SONAME',SoNameStr);
  441. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,true);
  442. { Strip the library ? }
  443. if success and (cs_link_strip in current_settings.globalswitches) then
  444. begin
  445. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  446. Replace(cmdstr,'$EXE',maybequoted(current_module.sharedlibfilename));
  447. success:=DoExec(FindUtil(utilsprefix+binstr),cmdstr,true,false);
  448. end;
  449. { Remove ReponseFile }
  450. if (success) and not(cs_link_nolink in current_settings.globalswitches) then
  451. DeleteFile(outputexedir+Info.ResName);
  452. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  453. end;
  454. {*****************************************************************************
  455. Initialize
  456. *****************************************************************************}
  457. initialization
  458. RegisterLinker(ld_haiku,TLinkerhaiku);
  459. {$ifdef i386}
  460. RegisterImport(system_i386_haiku,timportlibhaiku);
  461. RegisterExport(system_i386_haiku,texportlibhaiku);
  462. RegisterTarget(system_i386_haiku_info);
  463. {$endif i386}
  464. {$ifdef x86_64}
  465. RegisterImport(system_x86_64_haiku,timportlibhaiku);
  466. RegisterExport(system_x86_64_haiku,texportlibhaiku);
  467. RegisterTarget(system_x86_64_haiku_info);
  468. {$endif x86_64}
  469. end.