libloading_mini/
lib.rs

1#[cfg(unix)] mod internal_impl {
2    // Platform specific things
3    use std::os::raw;
4    use std::ffi::OsStr;
5    use super::cstr_cow_from_bytes;
6
7    extern { // syscalls
8        fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void;
9        fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void;
10        fn dlclose(handle: *mut raw::c_void) -> raw::c_int;
11        fn dlerror() -> *mut raw::c_char;
12    }
13
14    #[cfg(not(target_os="android"))]
15    const RTLD_NOW: raw::c_int = 2;
16    #[cfg(target_os="android")]
17    const RTLD_NOW: raw::c_int = 0;
18
19    /// A platform-specific equivalent of the cross-platform `Library`.
20    pub struct Library(*mut raw::c_void);
21
22    unsafe impl Send for Library {}
23    unsafe impl Sync for Library {}
24
25    impl Library {
26        pub fn new<P: AsRef<OsStr>>(path: P) -> Option<Self> {
27            use std::os::unix::ffi::OsStrExt;
28            let file_path = cstr_cow_from_bytes(path.as_ref().as_bytes())?;
29            let result = unsafe { dlopen(file_path.as_ptr(), RTLD_NOW) };
30            if result.is_null() {
31                #[cfg(feature = "debug_symbols")] {
32                    println!("failed to dlopen library in path {}", path.as_ref().to_string_lossy());
33                }
34                None
35            } else {
36                #[cfg(feature = "debug_symbols")] {
37                    println!("dlopen library in path {} @ 0x{:x}", path.as_ref().to_string_lossy(), result as usize);
38                }
39                Some(Library(result))
40            }
41        }
42
43        pub fn get(&self, symbol: &[u8]) -> Option<*mut raw::c_void> {
44            let symbol_name_new = cstr_cow_from_bytes(symbol)?;
45            let symbol_new = unsafe { dlsym(self.0, symbol_name_new.as_ptr()) };
46            let error = unsafe { dlerror() };
47            if error.is_null() {
48                #[cfg(feature = "debug_symbols")] {
49                    println!("ok loaded symbol {} @ 0x{:x}", unsafe { std::str::from_utf8_unchecked(symbol) }, symbol_new as usize);
50                }
51                Some(symbol_new)
52            } else {
53                #[cfg(feature = "debug_symbols")] {
54                    println!("missing symbol {} @ 0x{:x}", unsafe { std::str::from_utf8_unchecked(symbol) }, symbol_new as usize);
55                }
56                None
57            }
58        }
59    }
60
61    impl Drop for Library {
62        fn drop(&mut self) {
63            unsafe { dlclose(self.0) };
64        }
65    }
66}
67
68#[cfg(windows)] mod internal_impl {
69
70    extern crate winapi;
71
72    use self::winapi::shared::minwindef::{HMODULE, FARPROC};
73    use self::winapi::um::libloaderapi;
74    use super::cstr_cow_from_bytes;
75    use std::ffi::OsStr;
76
77    pub struct Library(HMODULE);
78
79    unsafe impl Send for Library {}
80    unsafe impl Sync for Library {}
81
82    impl Library {
83        pub fn new<P: AsRef<OsStr>>(path: P) -> Option<Self> {
84            use std::os::windows::ffi::OsStrExt;
85            let wide_filename: Vec<u16> = path.as_ref().encode_wide().chain(Some(0)).collect();
86            let handle = unsafe { libloaderapi::LoadLibraryExW(wide_filename.as_ptr(), std::ptr::null_mut(), 0) };
87            if handle.is_null()  { None } else { Some(Library(handle)) }
88        }
89        pub fn get(&self, symbol: &[u8]) -> Option<FARPROC> {
90            let symbol = cstr_cow_from_bytes(symbol)?;
91            let symbol = unsafe { libloaderapi::GetProcAddress(self.0, symbol.as_ptr()) };
92            if symbol.is_null() { None } else { Some(symbol) }
93        }
94    }
95
96    impl Drop for Library {
97        fn drop(&mut self) {
98            unsafe { libloaderapi::FreeLibrary(self.0); }
99        }
100    }
101}
102
103use std::ffi::{CStr, CString};
104use std::borrow::Cow;
105use std::os::raw;
106
107pub(crate) fn cstr_cow_from_bytes<'a>(slice: &'a [u8]) -> Option<Cow<'a, CStr>> {
108    static ZERO: raw::c_char = 0;
109    Some(match slice.last() {
110        // Slice out of 0 elements
111        None => unsafe { Cow::Borrowed(CStr::from_ptr(&ZERO)) },
112        // Slice with trailing 0
113        Some(&0) => {
114            Cow::Borrowed(CStr::from_bytes_with_nul(slice).ok()?)
115        },
116        // Slice with no trailing 0
117        Some(_) => Cow::Owned(CString::new(slice).ok()?),
118    })
119}
120
121pub use internal_impl::Library;