pub struct ElfLibrary { /* private fields */ }
Expand description
An unrelocated dynamic library
Implementations§
Source§impl ElfLibrary
impl ElfLibrary
Source§impl ElfLibrary
impl ElfLibrary
Source§impl ElfLibrary
impl ElfLibrary
Sourcepub fn dlopen(path: impl AsRef<OsStr>, flags: OpenFlags) -> Result<Dylib>
pub fn dlopen(path: impl AsRef<OsStr>, flags: OpenFlags) -> Result<Dylib>
Load a shared library from a specified path. It is the same as dlopen.
§Example
let path = Path::new("/path/to/library.so");
let lib = ElfLibrary::dlopen(path, OpenFlags::RTLD_LOCAL).expect("Failed to load library");
Sourcepub fn dlopen_from_binary(
bytes: &[u8],
path: impl AsRef<str>,
flags: OpenFlags,
) -> Result<Dylib>
pub fn dlopen_from_binary( bytes: &[u8], path: impl AsRef<str>, flags: OpenFlags, ) -> Result<Dylib>
Load a shared library from bytes. It is the same as dlopen. However, it can also be used in the no_std environment, and it will look for dependent libraries in those manually opened dynamic libraries.
Source§impl ElfLibrary
impl ElfLibrary
Sourcepub fn from_file(path: impl AsRef<OsStr>, flags: OpenFlags) -> Result<Self>
pub fn from_file(path: impl AsRef<OsStr>, flags: OpenFlags) -> Result<Self>
Find and load a elf dynamic library from path.
The path
argument may be either:
- The absolute path to the library;
- A relative (to the current working directory) path to the library.
The flags
argument can control how dynamic libraries are loaded.
§Examples
let lib = ElfLibrary::from_file("/path/to/awesome.module", OpenFlags::RTLD_LOCAL)
.unwrap();
Sourcepub fn from_open_file(
file: File,
path: impl AsRef<str>,
flags: OpenFlags,
) -> Result<ElfLibrary>
pub fn from_open_file( file: File, path: impl AsRef<str>, flags: OpenFlags, ) -> Result<ElfLibrary>
Creates a new ElfLibrary
instance from an open file handle.
The flags
argument can control how dynamic libraries are loaded.
§Examples
let file = File::open("/path/to/awesome.module").unwrap();
let lib = ElfLibrary::from_open_file(file, "/path/to/awesome.module", OpenFlags::RTLD_LOCAL).unwrap();
Sourcepub fn from_binary(
bytes: impl AsRef<[u8]>,
path: impl AsRef<str>,
flags: OpenFlags,
) -> Result<Self>
pub fn from_binary( bytes: impl AsRef<[u8]>, path: impl AsRef<str>, flags: OpenFlags, ) -> Result<Self>
Load a elf dynamic library from bytes.
The flags
argument can control how dynamic libraries are loaded.
§Examples
let path = Path::new("/path/to/awesome.module");
let bytes = std::fs::read(path).unwrap();
let lib = ElfLibrary::from_binary(&bytes, "/path/to/awesome.module", OpenFlags::RTLD_LOCAL).unwrap();
Sourcepub fn load_existing(shortname: &str) -> Result<Dylib>
pub fn load_existing(shortname: &str) -> Result<Dylib>
Load an existing dynamic library using the shortname of the library
§Examples
let libc = ElfLibrary::load_existing("libc.so.6").unwrap();
Sourcepub fn needed_libs(&self) -> &[&str]
pub fn needed_libs(&self) -> &[&str]
Gets the name of the dependent libraries
Sourcepub fn relocate(self, libs: impl AsRef<[Dylib]>) -> Result<Dylib>
pub fn relocate(self, libs: impl AsRef<[Dylib]>) -> Result<Dylib>
Use libraries to relocate the current library.
§Examples
let libc = ElfLibrary::load_existing("libc").unwrap();
let libgcc = ElfLibrary::load_existing("libgcc").unwrap();
let lib = ElfLibrary::from_file("/path/to/awesome.module", OpenFlags::RTLD_LOCAL)
.unwrap()
.relocate(&[libgcc, libc]);
Sourcepub fn relocate_with<F>(
self,
libs: impl AsRef<[Dylib]>,
func: F,
) -> Result<Dylib>
pub fn relocate_with<F>( self, libs: impl AsRef<[Dylib]>, func: F, ) -> Result<Dylib>
Use 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 { libc::malloc(size) }
}
let libc = ElfLibrary::load_existing("libc").unwrap();
let libgcc = ElfLibrary::load_existing("libgcc").unwrap();
let lib = ElfLibrary::from_file("/path/to/awesome.module", OpenFlags::RTLD_LOCAL)
.unwrap()
.relocate_with(&[libc, libgcc], |name| {
if name == "malloc" {
return Some(mymalloc as _);
} else {
return None;
}
})
.unwrap();
§Note
It will use function closure to relocate current lib firstly.