dlopen_rs

Struct ElfLibrary

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

An unrelocated dynamic library

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 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
  • Lazy binding is forcibly turned on when LD_BIND_NOW = 0, forcibly turned off when LD_BIND_NOW = 1, and determined by the compilation parameter of the dynamic library itself when LD_BIND_NOW environment 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");
Source

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

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

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

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

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

get the name of the dependent libraries

Source

pub fn is_finished(&self) -> bool

Whether there are any items that have not been relocated

Source

pub fn name(&self) -> &str

Get the name of the dynamic library.

Source

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

pub fn relocate_with<F>( self, libs: impl AsRef<[RelocatedDylib]>, func: F, ) -> Self
where F: Fn(&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 { 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

Source

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

finish the relocation and return a relocated dylib

Source§

impl ElfLibrary

Source

pub fn register(self) -> Self

Register dynamic libraries as globally visible. When loading a dynamic library with dlopen, it looks up the globally visible dynamic library to see if the dynamic library being loaded already exists

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.