t_fbsd.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  22. interface
  23. uses
  24. import,export,link;
  25. type
  26. timportlibfreebsd=class(timportlib)
  27. procedure preparelib(const s:string);override;
  28. procedure importprocedure(const func,module:string;index:longint;const name:string);override;
  29. procedure importvariable(const varname,module:string;const name:string);override;
  30. procedure generatelib;override;
  31. end;
  32. texportlibfreebsd=class(texportlib)
  33. procedure preparelib(const s : string);override;
  34. procedure exportprocedure(hp : texported_item);override;
  35. procedure exportvar(hp : texported_item);override;
  36. procedure generatelib;override;
  37. end;
  38. tlinkerfreebsd=class(tlinker)
  39. private
  40. Glibc2,
  41. Glibc21 : boolean;
  42. Function WriteResponseFile(isdll:boolean) : Boolean;
  43. public
  44. constructor Create;
  45. procedure SetDefaultInfo;override;
  46. function MakeExecutable:boolean;override;
  47. function MakeSharedLibrary:boolean;override;
  48. end;
  49. implementation
  50. uses
  51. cutils,cclasses,
  52. verbose,systems,globtype,globals,
  53. symconst,script,
  54. fmodule,aasm,cpuasm,cpubase,symsym;
  55. {*****************************************************************************
  56. TIMPORTLIBLINUX
  57. *****************************************************************************}
  58. procedure timportlibfreebsd.preparelib(const s : string);
  59. begin
  60. end;
  61. procedure timportlibfreebsd.importprocedure(const func,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. aktprocsym^.definition^.setmangledname(name)
  68. else
  69. message(parser_e_empty_import_name);
  70. end;
  71. procedure timportlibfreebsd.importvariable(const varname,module:string;const name:string);
  72. begin
  73. { insert sharedlibrary }
  74. current_module.linkothersharedlibs.add(SplitName(module),link_allways);
  75. { reset the mangledname and turn off the dll_var option }
  76. aktvarsym^.setmangledname(name);
  77. exclude(aktvarsym^.varoptions,vo_is_dll_var);
  78. end;
  79. procedure timportlibfreebsd.generatelib;
  80. begin
  81. end;
  82. {*****************************************************************************
  83. TEXPORTLIBLINUX
  84. *****************************************************************************}
  85. procedure texportlibfreebsd.preparelib(const s:string);
  86. begin
  87. end;
  88. procedure texportlibfreebsd.exportprocedure(hp : texported_item);
  89. var
  90. hp2 : texported_item;
  91. begin
  92. { first test the index value }
  93. if (hp.options and eo_index)<>0 then
  94. begin
  95. Message1(parser_e_no_export_with_index_for_target,'freebsd');
  96. exit;
  97. end;
  98. { now place in correct order }
  99. hp2:=texported_item(current_module._exports.first);
  100. while assigned(hp2) and
  101. (hp.name^>hp2.name^) do
  102. hp2:=texported_item(hp2.next);
  103. { insert hp there !! }
  104. if assigned(hp2) and (hp2.name^=hp.name^) then
  105. begin
  106. { this is not allowed !! }
  107. Message1(parser_e_export_name_double,hp.name^);
  108. exit;
  109. end;
  110. if hp2=texported_item(current_module._exports.first) then
  111. current_module._exports.concat(hp)
  112. else if assigned(hp2) then
  113. begin
  114. hp.next:=hp2;
  115. hp.previous:=hp2.previous;
  116. if assigned(hp2.previous) then
  117. hp2.previous.next:=hp;
  118. hp2.previous:=hp;
  119. end
  120. else
  121. current_module._exports.concat(hp);
  122. end;
  123. procedure texportlibfreebsd.exportvar(hp : texported_item);
  124. begin
  125. hp.is_var:=true;
  126. exportprocedure(hp);
  127. end;
  128. procedure texportlibfreebsd.generatelib;
  129. var
  130. hp2 : texported_item;
  131. begin
  132. hp2:=texported_item(current_module._exports.first);
  133. while assigned(hp2) do
  134. begin
  135. if not hp2.is_var then
  136. begin
  137. {$ifdef i386}
  138. { place jump in codesegment }
  139. codeSegment.concat(Tai_align.Create_op(4,$90));
  140. codeSegment.concat(Tai_symbol.Createname_global(hp2.name^,0));
  141. codeSegment.concat(Taicpu.Op_sym(A_JMP,S_NO,newasmsymbol(hp2.sym^.mangledname)));
  142. codeSegment.concat(Tai_symbol_end.Createname(hp2.name^));
  143. {$endif i386}
  144. end
  145. else
  146. Message1(parser_e_no_export_of_variables_for_target,'freebsd');
  147. hp2:=texported_item(hp2.next);
  148. end;
  149. end;
  150. {*****************************************************************************
  151. TLINKERLINUX
  152. *****************************************************************************}
  153. Constructor TLinkerFreeBSD.Create;
  154. begin
  155. Inherited Create;
  156. LibrarySearchPath.AddPath('/lib;/usr/lib;/usr/X11R6/lib',true);
  157. end;
  158. procedure TLinkerFreeBSD.SetDefaultInfo;
  159. {
  160. This will also detect which libc version will be used
  161. }
  162. begin
  163. Glibc2:=false;
  164. Glibc21:=false;
  165. with Info do
  166. begin
  167. ExeCmd[1]:='ld $OPT $DYNLINK $STATIC $STRIP -L. -o $EXE $RES';
  168. DllCmd[1]:='ld $OPT -shared -L. -o $EXE $RES';
  169. DllCmd[2]:='strip --strip-unneeded $EXE';
  170. { first try glibc2 }
  171. {$ifndef BSD} {Keep linux code in place. FBSD might go to a different
  172. glibc too once}
  173. DynamicLinker:='/lib/ld-linux.so.2';
  174. if FileExists(DynamicLinker) then
  175. begin
  176. Glibc2:=true;
  177. { Check for 2.0 files, else use the glibc 2.1 stub }
  178. if FileExists('/lib/ld-2.0.*') then
  179. Glibc21:=false
  180. else
  181. Glibc21:=true;
  182. end
  183. else
  184. DynamicLinker:='/lib/ld-linux.so.1';
  185. {$ELSE}
  186. DynamicLinker:='';
  187. {$endif}
  188. end;
  189. end;
  190. Function TLinkerFreeBSD.WriteResponseFile(isdll:boolean) : Boolean;
  191. Var
  192. linkres : TLinkRes;
  193. i : longint;
  194. cprtobj,
  195. gprtobj,
  196. prtobj : string[80];
  197. HPath : TStringListItem;
  198. s : string;
  199. found,
  200. linkdynamic,
  201. linklibc : boolean;
  202. begin
  203. WriteResponseFile:=False;
  204. { set special options for some targets }
  205. linkdynamic:=not(SharedLibFiles.empty);
  206. linklibc:=(SharedLibFiles.Find('c')<>nil);
  207. prtobj:='prt0';
  208. cprtobj:='cprt0';
  209. gprtobj:='gprt0';
  210. if glibc21 then
  211. begin
  212. cprtobj:='cprt21';
  213. gprtobj:='gprt21';
  214. end;
  215. if cs_profile in aktmoduleswitches then
  216. begin
  217. prtobj:=gprtobj;
  218. if not glibc2 then
  219. AddSharedLibrary('gmon');
  220. AddSharedLibrary('c');
  221. linklibc:=true;
  222. end
  223. else
  224. begin
  225. if linklibc then
  226. prtobj:=cprtobj;
  227. end;
  228. { Open link.res file }
  229. LinkRes.Init(outputexedir+Info.ResName);
  230. { Write path to search libraries }
  231. HPath:=TStringListItem(current_module.locallibrarysearchpath.First);
  232. while assigned(HPath) do
  233. begin
  234. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  235. HPath:=TStringListItem(HPath.Next);
  236. end;
  237. HPath:=TStringListItem(LibrarySearchPath.First);
  238. while assigned(HPath) do
  239. begin
  240. LinkRes.Add('SEARCH_DIR('+HPath.Str+')');
  241. HPath:=TStringListItem(HPath.Next);
  242. end;
  243. LinkRes.Add('INPUT(');
  244. { add objectfiles, start with prt0 always }
  245. if prtobj<>'' then
  246. LinkRes.AddFileName(FindObjectFile(prtobj,''));
  247. { try to add crti and crtbegin if linking to C }
  248. if linklibc then
  249. begin
  250. s:=librarysearchpath.FindFile('crtbegin.o',found)+'crtbegin.o';
  251. if found then
  252. LinkRes.AddFileName(s);
  253. s:=librarysearchpath.FindFile('crti.o',found)+'crti.o';
  254. if found then
  255. LinkRes.AddFileName(s);
  256. end;
  257. { main objectfiles }
  258. while not ObjectFiles.Empty do
  259. begin
  260. s:=ObjectFiles.GetFirst;
  261. if s<>'' then
  262. LinkRes.AddFileName(s);
  263. end;
  264. { objects which must be at the end }
  265. if linklibc then
  266. begin
  267. s:=librarysearchpath.FindFile('crtend.o',found)+'crtend.o';
  268. if found then
  269. LinkRes.AddFileName(s);
  270. s:=librarysearchpath.FindFile('crtn.o',found)+'crtn.o';
  271. if found then
  272. LinkRes.AddFileName(s);
  273. end;
  274. LinkRes.Add(')');
  275. { Write staticlibraries }
  276. if not StaticLibFiles.Empty then
  277. begin
  278. LinkRes.Add('GROUP(');
  279. While not StaticLibFiles.Empty do
  280. begin
  281. S:=StaticLibFiles.GetFirst;
  282. LinkRes.AddFileName(s)
  283. end;
  284. LinkRes.Add(')');
  285. end;
  286. { Write sharedlibraries like -l<lib>, also add the needed dynamic linker
  287. here to be sure that it gets linked this is needed for glibc2 systems (PFV) }
  288. if not SharedLibFiles.Empty then
  289. begin
  290. LinkRes.Add('INPUT(');
  291. While not SharedLibFiles.Empty do
  292. begin
  293. S:=SharedLibFiles.GetFirst;
  294. if s<>'c' then
  295. begin
  296. i:=Pos(target_os.sharedlibext,S);
  297. if i>0 then
  298. Delete(S,i,255);
  299. LinkRes.Add('-l'+s);
  300. end
  301. else
  302. begin
  303. linklibc:=true;
  304. linkdynamic:=false; { libc will include the ld-linux for us }
  305. end;
  306. end;
  307. { be sure that libc is the last lib }
  308. if linklibc then
  309. LinkRes.Add('-lc');
  310. { when we have -static for the linker the we also need libgcc }
  311. if (cs_link_staticflag in aktglobalswitches) then
  312. LinkRes.Add('-lgcc');
  313. if linkdynamic and (Info.DynamicLinker<>'') then
  314. LinkRes.AddFileName(Info.DynamicLinker);
  315. LinkRes.Add(')');
  316. end;
  317. { Write and Close response }
  318. linkres.writetodisk;
  319. linkres.done;
  320. WriteResponseFile:=True;
  321. end;
  322. function TLinkerFreeBSD.MakeExecutable:boolean;
  323. var
  324. binstr,
  325. cmdstr : string;
  326. success : boolean;
  327. DynLinkStr : string[60];
  328. StaticStr,
  329. StripStr : string[40];
  330. begin
  331. if not(cs_link_extern in aktglobalswitches) then
  332. Message1(exec_i_linking,current_module.exefilename^);
  333. { Create some replacements }
  334. StaticStr:='';
  335. StripStr:='';
  336. DynLinkStr:='';
  337. if (cs_link_staticflag in aktglobalswitches) then
  338. StaticStr:='-static';
  339. if (cs_link_strip in aktglobalswitches) then
  340. StripStr:='-s';
  341. If (cs_profile in aktmoduleswitches) or
  342. ((Info.DynamicLinker<>'') and (not SharedLibFiles.Empty)) then
  343. DynLinkStr:='-dynamic-linker='+Info.DynamicLinker;
  344. { Write used files and libraries }
  345. WriteResponseFile(false);
  346. { Call linker }
  347. SplitBinCmd(Info.ExeCmd[1],binstr,cmdstr);
  348. Replace(cmdstr,'$EXE',current_module.exefilename^);
  349. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  350. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  351. Replace(cmdstr,'$STATIC',StaticStr);
  352. Replace(cmdstr,'$STRIP',StripStr);
  353. Replace(cmdstr,'$DYNLINK',DynLinkStr);
  354. success:=DoExec(FindUtil(BinStr),CmdStr,true,false);
  355. { Remove ReponseFile }
  356. if (success) and not(cs_link_extern in aktglobalswitches) then
  357. RemoveFile(outputexedir+Info.ResName);
  358. MakeExecutable:=success; { otherwise a recursive call to link method }
  359. end;
  360. Function TLinkerFreeBSD.MakeSharedLibrary:boolean;
  361. var
  362. binstr,
  363. cmdstr : string;
  364. success : boolean;
  365. begin
  366. MakeSharedLibrary:=false;
  367. if not(cs_link_extern in aktglobalswitches) then
  368. Message1(exec_i_linking,current_module.sharedlibfilename^);
  369. { Write used files and libraries }
  370. WriteResponseFile(true);
  371. { Call linker }
  372. SplitBinCmd(Info.DllCmd[1],binstr,cmdstr);
  373. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  374. Replace(cmdstr,'$OPT',Info.ExtraOptions);
  375. Replace(cmdstr,'$RES',outputexedir+Info.ResName);
  376. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  377. { Strip the library ? }
  378. if success and (cs_link_strip in aktglobalswitches) then
  379. begin
  380. SplitBinCmd(Info.DllCmd[2],binstr,cmdstr);
  381. Replace(cmdstr,'$EXE',current_module.sharedlibfilename^);
  382. success:=DoExec(FindUtil(binstr),cmdstr,true,false);
  383. end;
  384. { Remove ReponseFile }
  385. if (success) and not(cs_link_extern in aktglobalswitches) then
  386. RemoveFile(outputexedir+Info.ResName);
  387. MakeSharedLibrary:=success; { otherwise a recursive call to link method }
  388. end;
  389. end.
  390. {
  391. $Log$
  392. Revision 1.6 2000-12-30 22:53:25 peter
  393. * export with the case provided in the exports section
  394. Revision 1.5 2000/12/25 00:07:30 peter
  395. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  396. tlinkedlist objects)
  397. Revision 1.4 2000/10/31 22:02:53 peter
  398. * symtable splitted, no real code changes
  399. Revision 1.3 2000/09/24 21:33:47 peter
  400. * message updates merges
  401. Revision 1.2 2000/09/24 15:12:12 peter
  402. * renamed to be 8.3
  403. Revision 1.2 2000/09/16 12:24:00 peter
  404. * freebsd support routines
  405. }