1#[cfg(feature = "sys-syscall")]
5pub mod syscall;
6
7#[inline(never)]
20pub fn resolve<T: Sized + Copy>(dylib: &str, name: &str) -> Option<T> {
21 use core::ffi::c_void;
22 use core::mem;
23
24 unsafe extern "C" {
25 fn dlopen(filename: *const i8, flag: i32) -> *mut c_void;
26 fn dlsym(handle: *mut c_void, symbol: *const i8) -> *mut c_void;
27 }
28 const RTLD_LAZY: i32 = 1;
29 const MAX_LEN: usize = 256;
30
31 if dylib.len() >= MAX_LEN || name.len() >= MAX_LEN {
32 return None;
33 }
34
35 let mut dylib_buf = [0u8; MAX_LEN];
36 let mut name_buf = [0u8; MAX_LEN];
37
38 dylib_buf[..dylib.len()].copy_from_slice(dylib.as_bytes());
39 name_buf[..name.len()].copy_from_slice(name.as_bytes());
40
41 if dylib.as_bytes().contains(&0) || name.as_bytes().contains(&0) {
42 return None;
43 }
44
45 let handle = unsafe { dlopen(dylib_buf.as_ptr() as *const i8, RTLD_LAZY) };
48 if handle.is_null() {
49 return None;
50 }
51
52 let sym = unsafe { dlsym(handle, name_buf.as_ptr() as *const i8) };
55 if sym.is_null() {
56 return None;
57 }
58
59 Some(unsafe { mem::transmute_copy(&sym) })
62}
63
64#[cfg(test)]
65mod tests {
66 #[test]
67 fn test_() {
68 let _x = c"2222222";
69 let _y = r"2222222";
70 }
71}