t_fbsd.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Peter Vreman (original Linux)
  4. (c) 2000 by Marco van de Voort (FreeBSD mods)
  5. This unit implements support import,export,link routines
  6. for the (i386)FreeBSD target
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit t_fbsd;
  21. {$i fpcdefs.inc}
  22. interface
  23. implementation
  24. uses
  25. cutils,cclasses,
  26. verbose,systems,globtype,globals,
  27. symconst,script,
  28. fmodule,aasmbase,aasmtai,aasmcpu,cpubase,symsym,symdef,
  29. import,export,link,i_fbsd;
  30. type
  31. timportlibfreebsd=class(timportlib)
  32. procedure preparelib(const s:string);override;
  33. procedure importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);override;
  34. procedure importvariable(vs:tvarsym;const name,module:string);override;
  35. procedure generatelib;override;
  36. end;
  37. texportlibfreebsd=class(texportlib)
  38. procedure preparelib(const s : string);override;
  39. procedure exportprocedure(hp : texported_item);override;
  40. procedure exportvar(hp : texported_item);override;
  41. procedure generatelib;override;
  42. end;
  43. tlinkerfreebsd=class(texternallinker)
  44. private
  45. Glibc2,
  46. Glibc21,
  47. LdSupportsNoResponseFile : boolean;
  48. Function WriteResponseFile(isdll:boolean) : Boolean;
  49. public
  50. constructor Create;override;
  51. procedure SetDefaultInfo;override;
  52. function MakeExecutable:boolean;override;
  53. function MakeSharedLibrary:boolean;override;
  54. end;
  55. {*****************************************************************************
  56. TIMPORTLIBLINUX
  57. *****************************************************************************}
  58. procedure timportlibfreebsd.preparelib(const s : string);
  59. begin
  60. end;
  61. procedure timportlibfreebsd.importprocedure(aprocdef:tprocdef;const module:string;index:longint;const name:string);
  62. begin
  63. { insert sharedlibrary }
  64. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  65. { do nothing with the procedure, only set the mangledname }
  66. if name<>'' then
  67. begin
  68. aprocdef.setmangledname(name);
  69. end
  70. else
  71. message(parser_e_empty_import_name);
  72. end;
  73. procedure timportlibfreebsd.importvariable(vs:tvarsym;const name,module:string);
  74. begin
  75. { insert sharedlibrary }
  76. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  77. { reset the mangledname and turn off the dll_var option }
  78. vs.set_mangledname(name);
  79. exclude(vs.varoptions,vo_is_dll_var);
  80. end;
  81. procedure timportlibfreebsd.generatelib;
  82. begin
  83. end;
  84. {*****************************************************************************
  85. TEXPORTLIBLINUX
  86. *****************************************************************************}
  87. procedure texportlibfreebsd.preparelib(const s:string);
  88. begin
  89. end;
  90. procedure texportlibfreebsd.exportprocedure(hp : texported_item);
  91. var
  92. hp2 : texported_item;
  93. begin
  94. { first test the index value }
  95. if (hp.options and eo_index)<>0 then
  96. begin
  97. Message1(parser_e_no_export_with_index_for_target,'freebsd');
  98. exit;
  99. end;
  100. { now place in correct order }
  101. hp2:=texported_item(current_module._exports.first);
  102. while assigned(hp2) and
  103. (hp.name^>hp2.name^) do
  104. hp2:=texported_item(hp2.next);
  105. { insert hp there !! }
  106. if assigned(hp2) and (hp2.name^=hp.name^) then
  107. begin
  108. { this is not allowed !! }
  109. Message1(parser_e_export_name_double,hp.name^);
  110. exit;
  111. end;
  112. if hp2=texported_item(current_module._exports.first) then
  113. current_module._exports.concat(hp)
  114. else if assigned(hp2) then
  115. begin
  116. hp.next:=hp2;
  117. hp.previous:=hp2.previous;
  118. if assigned(hp2.previous) then
  119. hp2.previous.next:=hp;
  120. hp2.previous:=hp;
  121. end
  122. else
  123. current_module._exports.concat(hp);
  124. end;
  125. procedure texportlibfreebsd.exportvar(hp : texported_item);
  126. begin
  127. hp.is_var:=true;
  128. exportprocedure(hp);
  129. end;
  130. procedure texportlibfreebsd.generatelib;
  131. var
  132. hp2 : texported_item;
  133. begin
  134. hp2:=texported_item(current_module._exports.first);
  135. while assigned(hp2) do
  136. begin
  137. if (not hp2.is_var) and
  138. (hp2.sym.typ=procsym) then
  139. begin
  140. { the manglednames can already be the same when the procedure
  141. is declared with cdecl }
  142. if tprocsym(hp2.sym).first_procdef.mangledname<>hp2.name^ then
  143. begin
  144. {$ifdef i386}
  145. { place jump in codesegment }
  146. codesegment.concat(Tai_align.Create_op(4,$90));
  147. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  148. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(tprocsym(hp2.sym).first_procdef.mangledname)));
  149. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  150. {$endif i386}
  151. end;
  152. end
  153. else
  154. Message1(parser_e_no_export_of_variables_for_target,'freebsd');
  155. hp2:=texported_item(hp2.next);
  156. end;
  157. end;
  158. {*****************************************************************************
  159. TLINKERLINUX
  160. *****************************************************************************}
  161. Constructor TLinkerFreeBSD.Create;
  162. begin
  163. Inherited Create;
  164. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  165. end;
  166. procedure TLinkerFreeBSD.SetDefaultInfo;
  167. {
  168. This will also detect which libc version will be used
  169. }
  170. begin
  171. Glibc2:=false;
  172. Glibc21:=false;
  173. {$ifdef NETBSD}
  174. {$ifdef M68K}
  175. LdSupportsNoResponseFile:=true;
  176. {$else : not M68K}
  177. LdSupportsNoResponseFile:=false;
  178. {$endif M68K}
  179. {$else : not NETBSD}
  180. LdSupportsNoResponseFile:=false;
  181. {$endif NETBSD}
  182. with Info do
  183. begin
  184. if LdSupportsNoResponseFile then
  185. begin
  186. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE `cat $RES`';
  187. { We need external linking to interpret the `cat $RES` PM }
  188. include(aktglobalswitches,cs_link_extern);
  189. end
  190. else
  191. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  192. DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';
  193. DllCmd[2]:='strip --strip-unneeded $EXE';
  194. { first try glibc2 }
  195. {$ifdef GLIBC2} {Keep linux code in place. FBSD might go to a different
  196. glibc too once}
  197. DynamicLinker:='/lib/ld-linux.so.2';
  198. if FileExists(DynamicLinker) then
  199. begin
  200. Glibc2:=true;
  201. { Check for 2.0 files, else use the glibc 2.1 stub }
  202. if FileExists('/lib/ld-2.0.*') then
  203. Glibc21:=false
  204. else
  205. Glibc21:=true;
  206. end
  207. else
  208. DynamicLinker:='/lib/ld-linux.so.1';
  209. {$else}
  210. DynamicLinker:='';
  211. {$endif}
  212. end;
  213. end;
  214. Function TLinkerFreeBSD.WriteResponseFile(isdll:boolean) : Boolean;
  215. Var
  216. linkres : TLinkRes;
  217. i : longint;
  218. cprtobj,
  219. gprtobj,
  220. prtobj : string[80];
  221. HPath : TStringListItem;
  222. s,s1,s2 : string;
  223. linkdynamic,
  224. linklibc : boolean;
  225. Fl1,Fl2 : Boolean;
  226. begin
  227. WriteResponseFile:=False;
  228. { set special options for some targets }
  229. linkdynamic:=not(SharedLibFiles.empty);
  230. linklibc:=(SharedLibFiles.Find('c')<>nil);
  231. prtobj:='prt0';
  232. cprtobj:='cprt0';
  233. gprtobj:='gprt0';
  234. if glibc21 then
  235. begin
  236. cprtobj:='cprt21';
  237. gprtobj:='gprt21';
  238. end;
  239. if cs_profile in aktmoduleswitches then
  240. begin
  241. prtobj:=gprtobj;
  242. if not glibc2 then
  243. AddSharedLibrary('gmon');
  244. AddSharedLibrary('c');
  245. linklibc:=true;
  246. end
  247. else
  248. begin
  249. if linklibc then
  250. prtobj:=cprtobj;
  251. end;
  252. { Open link.res file }
  253. LinkRes:=TLinkRes.Create(outputexedir+Info.ResName);
  254. { Write path to search libraries }
  255. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  256. while assigned(HPath) do
  257. begin
  258. if LdSupportsNoResponseFile then
  259. LinkRes.Add('-L'+HPath.Str)
  260. else
  261. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  262. HPath:=TStringListItem(HPath.Next);
  263. end;
  264. HPath:=TStringListItem(LibrarySearchPath.First);
  265. while assigned(HPath) do
  266. begin
  267. if LdSupportsNoResponseFile then
  268. LinkRes.Add('-L'+HPath.Str)
  269. else
  270. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  271. HPath:=TStringListItem(HPath.Next);
  272. end;
  273. if not LdSupportsNoResponseFile then
  274. LinkRes.Add('INPUT(');
  275. { add objectfiles, start with prt0 always }
  276. if prtobj<>'' then
  277. LinkRes.AddFileName(FindObjectFile(prtobj,'',false));
  278. { try to add crti and crtbegin if linking to C }
  279. if linklibc then
  280. begin
  281. if librarysearchpath.FindFile('crtbegin.o',s) then
  282. LinkRes.AddFileName(s);
  283. if librarysearchpath.FindFile('crti.o',s) then
  284. LinkRes.AddFileName(s);
  285. end;
  286. { main objectfiles }
  287. while not ObjectFiles.Empty do
  288. begin
  289. s:=ObjectFiles.GetFirst;
  290. if s<>'' then
  291. LinkRes.AddFileName(s);
  292. end;
  293. if not LdSupportsNoResponseFile then
  294. LinkRes.Add(')');
  295. { Write staticlibraries }
  296. if not StaticLibFiles.Empty then
  297. begin
  298. if not LdSupportsNoResponseFile then
  299. LinkRes.Add('GROUP(');
  300. While not StaticLibFiles.Empty do
  301. begin
  302. S:=StaticLibFiles.GetFirst;
  303. LinkRes.AddFileName(s)
  304. end;
  305. if not LdSupportsNoResponseFile then
  306. LinkRes.Add(')');
  307. end;
  308. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  309. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  310. if not SharedLibFiles.Empty then
  311. begin
  312. if not LdSupportsNoResponseFile then
  313. LinkRes.Add('INPUT(');
  314. While not SharedLibFiles.Empty do
  315. begin
  316. S:=SharedLibFiles.GetFirst;
  317. if s<>'c' then
  318. begin
  319. i:=Pos(target_info.sharedlibext,S);
  320. if i>0 then
  321. Delete(S,i,255);
  322. LinkRes.Add('-l'+s);
  323. end
  324. else
  325. begin
  326. linklibc:=true;
  327. linkdynamic:=false; { libc will include the ld-linux for us }
  328. end;
  329. end;
  330. { be sure that libc is the last lib }
  331. if linklibc then
  332. LinkRes.Add('-lc');
  333. { when we have -static for the linker the we also need libgcc }
  334. if (cs_link_staticflag in aktglobalswitches) then
  335. LinkRes.Add('-lgcc');
  336. if linkdynamic and (Info.DynamicLinker<>'') then
  337. LinkRes.AddFileName(Info.DynamicLinker);
  338. if not LdSupportsNoResponseFile then
  339. LinkRes.Add(')');
  340. end;
  341. { objects which must be at the end }
  342. if linklibc then
  343. begin
  344. Fl1:=librarysearchpath.FindFile('crtend.o',s1);
  345. Fl2:=librarysearchpath.FindFile('crtn.o',s2);
  346. if Fl1 or Fl2 then
  347. begin
  348. LinkRes.Add('INPUT(');
  349. If Fl1 Then
  350. LinkRes.AddFileName(s1);
  351. If Fl2 Then
  352. LinkRes.AddFileName(s2);
  353. LinkRes.Add(')');
  354. end;
  355. end;
  356. { Write and Close response }
  357. linkres.writetodisk;
  358. linkres.Free;
  359. WriteResponseFile:=True;
  360. end;
  361. function TLinkerFreeBSD.MakeExecutable:boolean;
  362. var
  363. binstr,
  364. cmdstr : string;
  365. success : boolean;
  366. DynLinkStr : string[60];
  367. StaticStr,
  368. StripStr : string[40];
  369. begin
  370. if not(cs_link_extern in aktglobalswitches) then
  371. Message1(exec_i_linking,current_module.exefilename^);
  372. { Create some replacements }
  373. StaticStr:='';
  374. StripStr:='';
  375. DynLinkStr:='';
  376. if (cs_link_staticflag in aktglobalswitches) then
  377. begin
  378. if (target_info.system=system_m68k_netbsd) and
  379. ((cs_link_on_target in aktglobalswitches) or
  380. (target_info.system=source_info.system)) then
  381. StaticStr:='-Bstatic'
  382. else
  383. StaticStr:='-static';
  384. end;
  385. if (cs_link_strip in aktglobalswitches) then
  386. StripStr:='-s';
  387. If (cs_profile in aktmoduleswitches) or
  388. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  389. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  390. { Write used files and libraries }
  391. WriteResponseFile(false);
  392. { Call linker }
  393. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  394. Replace(cmdstr,'$EXE',current_module.exefilename^);
  395. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  396. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  397. Replace(cmdstr,'$STATIC',StaticStr);
  398. Replace(cmdstr,'$STRIP',StripStr);
  399. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  400. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  401. { Remove ReponseFile }
  402. if (success) and not(cs_link_extern in aktglobalswitches) then
  403. RemoveFile(outputexedir+Info.ResName);
  404. MakeExecutable:=success; { otherwise a recursive call to link method }
  405. end;
  406. Function TLinkerFreeBSD.MakeSharedLibrary:boolean;
  407. var
  408. binstr,
  409. cmdstr : string;
  410. success : boolean;
  411. begin
  412. MakeSharedLibrary:=false;
  413. if not(cs_link_extern in aktglobalswitches) then
  414. Message1(exec_i_linking,current_module.sharedlibfilename^);
  415. { Write used files and libraries }
  416. WriteResponseFile(true);
  417. { Call linker }
  418. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  419. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  420. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  421. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  422. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  423. { Strip the library ? }
  424. if success and (cs_link_strip in aktglobalswitches) then
  425. begin
  426. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  427. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  428. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  429. end;
  430. { Remove ReponseFile }
  431. if (success) and not(cs_link_extern in aktglobalswitches) then
  432. RemoveFile(outputexedir+Info.ResName);
  433. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  434. end;
  435. {*****************************************************************************
  436. Initialize
  437. *****************************************************************************}
  438. initialization
  439. {$ifdef i386}
  440. RegisterExternalLinker(system_i386_FreeBSD_info,TLinkerFreeBSD);
  441. RegisterExternalLinker(system_i386_NetBSD_info,TLinkerFreeBSD);
  442. RegisterImport(system_i386_freebsd,timportlibfreebsd);
  443. RegisterExport(system_i386_freebsd,texportlibfreebsd);
  444. RegisterTarget(system_i386_freebsd_info);
  445. RegisterImport(system_i386_netbsd,timportlibfreebsd);
  446. RegisterExport(system_i386_netbsd,texportlibfreebsd);
  447. RegisterTarget(system_i386_netbsd_info);
  448. {$endif i386}
  449. {$ifdef m68k}
  450. // RegisterExternalLinker(system_m68k_FreeBSD_info,TLinkerFreeBSD);
  451. RegisterExternalLinker(system_m68k_NetBSD_info,TLinkerFreeBSD);
  452. RegisterImport(system_m68k_netbsd,timportlibfreebsd);
  453. RegisterExport(system_m68k_netbsd,texportlibfreebsd);
  454. RegisterTarget(system_m68k_netbsd_info);
  455. {$endif m68k}
  456. end.
  457. {
  458. $Log$
  459. Revision 1.5 2003-04-27 07:29:52 peter
  460. * aktprocdef cleanup, aktprocdef is now always nil when parsing
  461. a new procdef declaration
  462. * aktprocsym removed
  463. * lexlevel removed, use symtable.symtablelevel instead
  464. * implicit init/final code uses the normal genentry/genexit
  465. * funcret state checking updated for new funcret handling
  466. Revision 1.4 2003/04/26 09:16:08 peter
  467. * .o files belonging to the unit are first searched in the same dir
  468. as the .ppu
  469. Revision 1.3 2003/01/18 16:16:13 marco
  470. * Small fix for netbsd
  471. Revision 1.2 2002/09/09 17:34:17 peter
  472. * tdicationary.replace added to replace and item in a dictionary. This
  473. is only allowed for the same name
  474. * varsyms are inserted in symtable before the types are parsed. This
  475. fixes the long standing "var longint : longint" bug
  476. - consume_idlist and idstringlist removed. The loops are inserted
  477. at the callers place and uses the symtable for duplicate id checking
  478. Revision 1.1 2002/09/06 15:03:51 carl
  479. * moved files to systems directory
  480. Revision 1.29 2002/09/03 16:26:28 daniel
  481. * Make Tprocdef.defs protected
  482. Revision 1.28 2002/08/12 15:08:44 carl
  483. + stab register indexes for powerpc (moved from gdb to cpubase)
  484. + tprocessor enumeration moved to cpuinfo
  485. + linker in target_info is now a class
  486. * many many updates for m68k (will soon start to compile)
  487. - removed some ifdef or correct them for correct cpu
  488. Revision 1.27 2002/08/11 14:32:32 peter
  489. * renamed current_library to objectlibrary
  490. Revision 1.26 2002/08/11 13:24:19 peter
  491. * saving of asmsymbols in ppu supported
  492. * asmsymbollist global is removed and moved into a new class
  493. tasmlibrarydata that will hold the info of a .a file which
  494. corresponds with a single module. Added librarydata to tmodule
  495. to keep the library info stored for the module. In the future the
  496. objectfiles will also be stored to the tasmlibrarydata class
  497. * all getlabel/newasmsymbol and friends are moved to the new class
  498. Revision 1.25 2002/07/26 21:15:45 florian
  499. * rewrote the system handling
  500. Revision 1.24 2002/07/24 13:51:34 marco
  501. * Fixed small error
  502. Revision 1.23 2002/07/24 13:10:22 marco
  503. * urgent fix.
  504. Revision 1.22 2002/07/01 18:46:34 peter
  505. * internal linker
  506. * reorganized aasm layer
  507. Revision 1.21 2002/05/18 13:34:26 peter
  508. * readded missing revisions
  509. Revision 1.20 2002/05/16 19:46:53 carl
  510. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  511. + try to fix temp allocation (still in ifdef)
  512. + generic constructor calls
  513. + start of tassembler / tmodulebase class cleanup
  514. Revision 1.18 2002/04/22 18:19:22 carl
  515. - remove use_bound_instruction field
  516. Revision 1.17 2002/04/20 21:43:18 carl
  517. * fix stack size for some targets
  518. + add offset to parameters from frame pointer info.
  519. - remove some unused stuff
  520. Revision 1.16 2002/04/19 15:46:04 peter
  521. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  522. in most cases and not written to the ppu
  523. * add mangeledname_prefix() routine to generate the prefix of
  524. manglednames depending on the current procedure, object and module
  525. * removed static procprefix since the mangledname is now build only
  526. on demand from tprocdef.mangledname
  527. Revision 1.15 2002/04/15 19:16:57 carl
  528. - remove size_of_pointer field
  529. Revision 1.14 2002/01/29 21:27:34 peter
  530. * default alignment changed to 4 bytes for locals and static const,var
  531. }