dlopen_rs

Struct ElfLibrary

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

Implementations§

Source§

impl ElfLibrary

Source

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

Source

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

pub fn sys_load(name: &str) -> Result<RelocatedDylib>

Use the system dynamic linker (ld.so) to load the dynamic library, allowing it to be used in the same way as dlopen.

§Examples
let libc = ELFLibrary::sys_load("libc.so.6").unwrap();
Source§

impl ElfLibrary

Source

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

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

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

pub fn needed_libs(&self) -> &Vec<&str>

get the name of the dependent libraries

Source

pub fn is_finished(&self) -> bool

Source

pub fn name(&self) -> &str

Source

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

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

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

Source

pub fn finish(self) -> Result<RelocatedDylib>

Trait Implementations§

Source§

impl Debug for ElfLibrary

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Register for ElfLibrary

Source§

fn register(self) -> Self

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.