pub struct ElfLibrary { /* private fields */ }Expand description
An unrelocated dynamic library
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
LD_LIBRARY_PATHenvironment variable before calling this function. dlopen-rs will look for dependent dynamic libraries in the paths saved inLD_LIBRARY_PATH. In addition, dlopen-rs also looks for dependent dynamic libraries in the registered dynamic library - Lazy binding is forcibly turned on when
LD_BIND_NOW = 0, forcibly turned off whenLD_BIND_NOW = 1, and determined by the compilation parameter of the dynamic library itself whenLD_BIND_NOWenvironment variable is not set.
§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");Sourcepub fn dlopen_from_binary(
bytes: &[u8],
name: impl AsRef<str>,
) -> Result<RelocatedDylib>
pub fn dlopen_from_binary( bytes: &[u8], name: impl AsRef<str>, ) -> Result<RelocatedDylib>
Load a shared library from bytes
§Note
Please set the LD_LIBRARY_PATH environment variable before calling this function.
dlopen-rs will look for dependent dynamic libraries in the paths saved in LD_LIBRARY_PATH.
In addition, dlopen-rs also looks for dependent dynamic libraries in the registered dynamic 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>,
lazy_bind: Option<bool>,
) -> Result<Self>
pub fn from_file( path: impl AsRef<OsStr>, lazy_bind: Option<bool>, ) -> 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 lazy_bind argument can be used to force whether lazy binding is enabled or not
§Examples
let lib = ElfLibrary::from_file("/path/to/awesome.module",Some(true))
.unwrap();Sourcepub fn from_open_file(
file: File,
name: impl AsRef<str>,
lazy_bind: Option<bool>,
) -> Result<ElfLibrary>
pub fn from_open_file( file: File, name: impl AsRef<str>, lazy_bind: Option<bool>, ) -> Result<ElfLibrary>
Creates a new ElfLibrary instance from an open file handle.
The lazy_bind argument can be used to force whether lazy binding is enabled or not
§Examples
let file = File::open("path_to_elf").unwrap();
let lib = ElfLibrary::from_open_file(file, "my_elf_library", None).unwrap();Sourcepub fn from_binary(
bytes: impl AsRef<[u8]>,
name: impl AsRef<str>,
lazy_bind: Option<bool>,
) -> Result<Self>
pub fn from_binary( bytes: impl AsRef<[u8]>, name: impl AsRef<str>, lazy_bind: Option<bool>, ) -> Result<Self>
load a elf dynamic library from bytes.
The lazy_bind argument can be used to force whether delayed binding is enabled or not
§Examples
let path = Path::new("/path/to/awesome.module");
let bytes = std::fs::read(path).unwrap();
let lib = ElfLibrary::from_binary(&bytes, false).unwarp();Sourcepub fn needed_libs(&self) -> &Vec<&str>
pub fn needed_libs(&self) -> &Vec<&str>
get the name of the dependent libraries
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Whether there are any items that have not been relocated
Sourcepub fn relocate(self, libs: impl AsRef<[RelocatedDylib]>) -> Self
pub fn relocate(self, libs: impl AsRef<[RelocatedDylib]>) -> Self
use libraries to relocate the current library
§Examples
let libc = ElfLibrary::sys_load("libc").unwrap();
let libgcc = ElfLibrary::sys_load("libgcc").unwrap();
let lib = ElfLibrary::from_file("/path/to/awesome.module", None)
.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 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::sys_load("libc").unwrap();
let libgcc = ElfLibrary::sys_load("libgcc").unwrap();
let lib = ElfLibrary::from_file("/path/to/awesome.module", None)
.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
Sourcepub fn finish(self) -> Result<RelocatedDylib>
pub fn finish(self) -> Result<RelocatedDylib>
finish the relocation and return a relocated dylib