Skip to main content

Loader

Struct Loader 

Source
pub struct Loader<M = DefaultMmap, H = (), D = (), Tls = DefaultTlsResolver>
where M: Mmap, H: LoadHook, Tls: TlsResolver,
{ /* private fields */ }
Expand description

The ELF object loader.

Loader is responsible for orchestrating the loading of ELF objects into memory. It supports customization through various with_* methods for memory mapping, hooks, user data, and TLS resolution.

§Examples

use elf_loader::{Loader, input::ElfBinary};

let mut loader = Loader::new();
let bytes = std::fs::read("liba.so").unwrap();
let lib = loader.load_dylib(ElfBinary::new("liba.so", &bytes)).unwrap();

Implementations§

Source§

impl<M, H, D, Tls> Loader<M, H, D, Tls>
where M: Mmap, H: LoadHook, D: Default, Tls: TlsResolver,

Source

pub fn load_dylib<'a, I>(&mut self, input: I) -> Result<RawDylib<D>>
where I: IntoElfReader<'a>,

Loads a dynamic library into memory and prepares it for relocation.

§Examples
use elf_loader::{Loader, input::ElfBinary};

let mut loader = Loader::new();
let bytes = &[]; // ELF file bytes
let lib = loader.load_dylib(ElfBinary::new("liba.so", bytes)).unwrap();
Source§

impl<M, H, D, Tls> Loader<M, H, D, Tls>
where M: Mmap, H: LoadHook, D: Default, Tls: TlsResolver,

Source

pub fn load_exec<'a, I>(&mut self, input: I) -> Result<RawExec<D>>
where I: IntoElfReader<'a>,

Loads an executable file into memory and prepares it for relocation.

§Examples
use elf_loader::{Loader, input::ElfBinary};

let mut loader = Loader::new();
let bytes = &[]; // ELF executable bytes
let exec = loader.load_exec(ElfBinary::new("my_exec", bytes)).unwrap();
Source§

impl<M, H, D, Tls> Loader<M, H, D, Tls>
where M: Mmap, H: LoadHook, D: Default + 'static, Tls: TlsResolver,

Source

pub fn load_object<'a, I>(&mut self, input: I) -> Result<RawObject<D>>
where I: IntoElfReader<'a>,

Loads a relocatable object file into memory and prepares it for relocation.

§Examples
use elf_loader::{Loader, input::ElfBinary};

let mut loader = Loader::new();
let bytes = &[]; // Relocatable ELF bytes
let rel = loader.load_object(ElfBinary::new("liba.o", bytes)).unwrap();
Source§

impl<M: Mmap, H: LoadHook, D: Default + 'static> Loader<M, H, D>

Source

pub fn load<'a, I>(&mut self, input: I) -> Result<RawElf<D>>
where I: IntoElfReader<'a>,

Load an ELF file into memory

§Arguments
  • object - The ELF object to load
§Returns
  • Ok(Elf) - The loaded ELF file
  • Err(Error) - If loading fails
Source§

impl Loader<DefaultMmap, (), (), ()>

Source

pub fn new() -> Self

Creates a new Loader with default settings.

Source§

impl<M, H, D, Tls> Loader<M, H, D, Tls>
where H: LoadHook, M: Mmap, D: 'static, Tls: TlsResolver,

Source

pub fn with_init<F>(self, init_fn: F) -> Self
where F: LifecycleHandler + 'static,

Sets the initialization function handler.

This handler is responsible for calling the initialization functions (e.g., .init and .init_array) of the loaded ELF object.

Note: glibc passes argc, argv, and envp to functions in .init_array as a non-standard extension.

Source

pub fn with_fini<F>(self, fini_fn: F) -> Self
where F: LifecycleHandler + 'static,

Sets the finalization function handler.

This handler is responsible for calling the finalization functions (e.g., .fini and .fini_array) of the loaded ELF object.

Source

pub fn with_context<NewD>(self) -> Loader<M, H, NewD, Tls>
where NewD: Default + 'static,

Consumes the current loader and returns a new one with the specified context data type.

Source

pub fn with_context_loader<NewD>( self, loader: impl Fn(&UserDataLoaderContext<'_>) -> NewD + 'static, ) -> Loader<M, H, NewD, Tls>
where NewD: 'static,

Consumes the current loader and returns a new one with the specified user data generator.

Source

pub fn with_hook<NewHook>(self, hook: NewHook) -> Loader<M, NewHook, D, Tls>
where NewHook: LoadHook,

Consumes the current loader and returns a new one with the specified hook.

Source

pub fn with_mmap<NewMmap: Mmap>(self) -> Loader<NewMmap, H, D, Tls>

Returns a new loader with a custom Mmap implementation.

Source

pub fn with_tls_resolver<NewTls>(self) -> Loader<M, H, D, NewTls>
where NewTls: TlsResolver,

Consumes the current loader and returns a new one with the specified TLS resolver.

Source

pub fn with_default_tls_resolver(self) -> Loader<M, H, D, DefaultTlsResolver>

Consumes the current loader and returns a new one with the default TLS resolver.

Source

pub fn with_static_tls(self, enabled: bool) -> Self

Sets whether to force static TLS for all loaded modules.

Source

pub fn read_ehdr(&mut self, object: &mut impl ElfReader) -> Result<ElfHeader>

Reads the ELF header.

Source

pub fn read_phdr( &mut self, object: &mut impl ElfReader, ehdr: &ElfHeader, ) -> Result<Option<&[ElfPhdr]>>

Reads the program header table.

Auto Trait Implementations§

§

impl<M, H, D, Tls> Freeze for Loader<M, H, D, Tls>
where H: Freeze,

§

impl<M = DefaultMmap, H = (), D = (), Tls = DefaultTlsResolver> !RefUnwindSafe for Loader<M, H, D, Tls>

§

impl<M = DefaultMmap, H = (), D = (), Tls = DefaultTlsResolver> !Send for Loader<M, H, D, Tls>

§

impl<M = DefaultMmap, H = (), D = (), Tls = DefaultTlsResolver> !Sync for Loader<M, H, D, Tls>

§

impl<M, H, D, Tls> Unpin for Loader<M, H, D, Tls>
where H: Unpin, M: Unpin, Tls: Unpin,

§

impl<M = DefaultMmap, H = (), D = (), Tls = DefaultTlsResolver> !UnwindSafe for Loader<M, H, D, Tls>

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.