Struct ElfLibrary

Source
pub struct ElfLibrary { /* private fields */ }
Expand description

An unrelocated dynamic library

Implementations§

Source§

impl ElfLibrary

Source

pub fn dl_iterate_phdr<F>(callback: F) -> Result<()>
where F: FnMut(&DlPhdrInfo<'_>) -> Result<()>,

Iterate over the program headers of all dynamic libraries.

Source§

impl ElfLibrary

Source

pub fn dladdr(addr: usize) -> Option<DlInfo>

determines whether the address specified in addr is located in one of the shared objects loaded by the calling application. If it is, then dladdr returns information about the shared object and symbol that overlaps addr.

Source§

impl ElfLibrary

Source

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");
Source

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

Source

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();
Source

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();
Source

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();
Source

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();
Source

pub fn needed_libs(&self) -> &[&str]

Gets the name of the dependent libraries

Source

pub fn name(&self) -> &str

Gets the name of the dynamic library.

Source

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]);
Source

pub fn relocate_with<F>( self, libs: impl AsRef<[Dylib]>, func: F, ) -> Result<Dylib>
where F: for<'b> Fn(&'b str) -> Option<*const ()> + 'static,

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.

Trait Implementations§

Source§

impl Debug for ElfLibrary

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.