#[cfg(feature = "sys-syscall")]
pub mod syscall;
#[inline(never)]
pub fn resolve<T: Sized + Copy>(dylib: &str, name: &str) -> Option<T> {
use core::ffi::c_void;
use core::mem;
unsafe extern "C" {
fn dlopen(filename: *const i8, flag: i32) -> *mut c_void;
fn dlsym(handle: *mut c_void, symbol: *const i8) -> *mut c_void;
}
const RTLD_LAZY: i32 = 1;
const MAX_LEN: usize = 256;
if dylib.len() >= MAX_LEN || name.len() >= MAX_LEN {
return None;
}
let mut dylib_buf = [0u8; MAX_LEN];
let mut name_buf = [0u8; MAX_LEN];
dylib_buf[..dylib.len()].copy_from_slice(dylib.as_bytes());
name_buf[..name.len()].copy_from_slice(name.as_bytes());
if dylib.as_bytes().contains(&0) || name.as_bytes().contains(&0) {
return None;
}
let handle = unsafe { dlopen(dylib_buf.as_ptr() as *const i8, RTLD_LAZY) };
if handle.is_null() {
return None;
}
let sym = unsafe { dlsym(handle, name_buf.as_ptr() as *const i8) };
if sym.is_null() {
return None;
}
Some(unsafe { mem::transmute_copy(&sym) })
}
#[cfg(test)]
mod tests {
#[test]
fn test_() {
let _x = c"2222222";
let _y = r"2222222";
}
}