]> git.lizzy.rs Git - rust.git/blob - src/shims/dlsym.rs
introduce platform-specific module hierarchy for dlsym (similar to foreign_items)
[rust.git] / src / shims / dlsym.rs
1 use rustc_middle::mir;
2
3 use crate::*;
4 use shims::posix::dlsym as posix;
5 use shims::windows::dlsym as windows;
6
7 #[derive(Debug, Copy, Clone)]
8 #[allow(non_camel_case_types)]
9 pub enum Dlsym {
10     Posix(posix::Dlsym),
11     Windows(windows::Dlsym),
12 }
13
14 impl Dlsym {
15     // Returns an error for unsupported symbols, and None if this symbol
16     // should become a NULL pointer (pretend it does not exist).
17     pub fn from_str(name: &[u8], target_os: &str) -> InterpResult<'static, Option<Dlsym>> {
18         let name = &*String::from_utf8_lossy(name);
19         Ok(match target_os {
20             "linux" | "macos" => posix::Dlsym::from_str(name, target_os)?.map(Dlsym::Posix),
21             "windows" => windows::Dlsym::from_str(name)?.map(Dlsym::Windows),
22             os => bug!("dlsym not implemented for target_os {}", os),
23         })
24     }
25 }
26
27 impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
28 pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
29     fn call_dlsym(
30         &mut self,
31         dlsym: Dlsym,
32         args: &[OpTy<'tcx, Tag>],
33         ret: Option<(PlaceTy<'tcx, Tag>, mir::BasicBlock)>,
34     ) -> InterpResult<'tcx> {
35         let this = self.eval_context_mut();
36         match dlsym {
37             Dlsym::Posix(dlsym) => posix::EvalContextExt::call_dlsym(this, dlsym, args, ret),
38             Dlsym::Windows(dlsym) => windows::EvalContextExt::call_dlsym(this, dlsym, args, ret),
39         }
40     }
41 }