pub struct ElfLibrary { /* private fields */ }Implementations§
Source§impl ElfLibrary
impl ElfLibrary
Sourcepub fn dlopen(path: impl AsRef<OsStr>) -> Result<RelocatedDylib>
pub fn dlopen(path: impl AsRef<OsStr>) -> Result<RelocatedDylib>
Load a shared library from a specified path
§Note
Please set the RUST_LD_LIBRARY_PATH environment variable before calling this function.
dlopen-rs will look for dependent dynamic libraries in the paths saved in RUST_LD_LIBRARY_PATH.
The way to set RUST_LD_LIBRARY_PATH is the same as LD_LIBRARY_PATH.
§Example
use std::path::Path;
use dlopen_rs::ELFLibrary;
let path = Path::new("/path/to/library.so");
let lib = ELFLibrary::dlopen(path).expect("Failed to load library");Source§impl ElfLibrary
impl ElfLibrary
Sourcepub unsafe fn sys_load_from_raw(
handle: *mut c_void,
name: &str,
) -> Result<RelocatedDylib>
pub unsafe fn sys_load_from_raw( handle: *mut c_void, name: &str, ) -> Result<RelocatedDylib>
Convert a raw handle returned by dlopen-family of calls to a RelocatedLibrary.
§Safety
The pointer shall be a result of a successful call of the dlopen-family of functions. It must be valid to call dlclose
with this pointer as an argument.
Source§impl ElfLibrary
impl ElfLibrary
Sourcepub fn from_file(path: impl AsRef<OsStr>) -> Result<Self>
pub fn from_file(path: impl AsRef<OsStr>) -> Result<Self>
Find and load a elf dynamic library from path.
The filename argument may be either:
- A library filename;
- The absolute path to the library;
- A relative (to the current working directory) path to the library.
§Examples
let lib = ELFLibrary::from_file("/path/to/awesome.module")
.unwrap();Sourcepub fn from_open_file(file: File, name: impl AsRef<str>) -> Result<ElfLibrary>
pub fn from_open_file(file: File, name: impl AsRef<str>) -> Result<ElfLibrary>
Creates a new ELFLibrary instance from an open file handle.
§Examples
let file = File::open("path_to_elf").unwrap();
let lib = ELFLibrary::from_open_file(file, "my_elf_library").unwrap();Sourcepub fn from_binary(bytes: &[u8], name: impl AsRef<str>) -> Result<Self>
pub fn from_binary(bytes: &[u8], name: impl AsRef<str>) -> Result<Self>
load a elf dynamic library from bytes
§Examples
let path = Path::new("/path/to/awesome.module");
let bytes = std::fs::read(path).unwrap();
let lib = ELFLibrary::from_binary(&bytes).unwarp();Sourcepub fn needed_libs(&self) -> &Vec<&str>
pub fn needed_libs(&self) -> &Vec<&str>
get the name of the dependent libraries
pub fn is_finished(&self) -> bool
pub fn name(&self) -> &str
Sourcepub fn relocate(self, libs: impl AsRef<[RelocatedDylib]>) -> Self
pub fn relocate(self, libs: impl AsRef<[RelocatedDylib]>) -> Self
use internal libraries to relocate the current library
§Examples
let libc = ELFLibrary::load_self("libc").unwrap();
let libgcc = ELFLibrary::load_self("libgcc").unwrap();
let lib = ELFLibrary::from_file("/path/to/awesome.module")
.unwrap()
.relocate(&[libgcc, libc])
.finish()
.unwrap();Sourcepub fn relocate_with<F>(
self,
libs: impl AsRef<[RelocatedDylib]>,
func: F,
) -> Self
pub fn relocate_with<F>( self, libs: impl AsRef<[RelocatedDylib]>, func: F, ) -> Self
use internal libraries and function closure to relocate the current library
§Examples
extern "C" fn mymalloc(size: size_t) -> *mut c_void {
println!("malloc:{}bytes", size);
unsafe { nix::libc::malloc(size) }
}
let libc = ELFLibrary::load_self("libc").unwrap();
let libgcc = ELFLibrary::load_self("libgcc").unwrap();
let lib = ELFLibrary::from_file("/path/to/awesome.module")
.unwrap()
.relocate_with(&[libc, libgcc], |name| {
if name == "malloc" {
return Some(mymalloc as _);
} else {
return None;
}
})
.finish()
.unwrap();§Note
It will use function closure to relocate current lib firstly