ObjectFile

Struct ObjectFile 

Source
pub struct ObjectFile(/* private fields */);
Expand description

An object file loaded in memory.

Please see the top-level documentation for more details.

Implementations§

Source§

impl ObjectFile

Source

pub fn open_main_program() -> Result<Self>

Load the object for the main program.

Source

pub fn open_file<P: AsRef<Path>>(filename: P) -> Result<Self>

Load an object from a file.

Source

pub unsafe fn open_by_handle(handle: *const c_void) -> Result<Self>

Load a dynamic loaded shared object.

handle is the address of the shared object. This value can be obtained by a function like dlopen.

§Safety

This constructor is unsafe because we don’t check that the handle is a valid address.

Source

pub unsafe fn replace( &self, symbol_name: &str, func_address: *const c_void, ) -> Result<Replacement>

Replace the address of a symbol in the PLT section, and returns a reference to the previous entry. When this reference is dropped, the entry is restored to the previous value.

The reference to the previous entry can be used to invoke the original function.

§Safety

The caller has to verify that the new address for the symbol is valid.

The function is not thread-safe.

§Example
use plthook::ObjectFile;
use std::process;

let pid = process::id();

extern "C" fn broken_getpid() -> libc::pid_t {
    -1
}

let replacement = unsafe {
    ObjectFile::open_main_program()
        .unwrap()
        .replace("getpid", broken_getpid as *const _)
        .unwrap()
};

assert_eq!(process::id(), u32::MAX);

drop(replacement);
assert_eq!(process::id(), pid);
Source

pub fn symbols(&self) -> impl Iterator<Item = Symbol> + '_

Returns an iterator to get all symbols in the PLT section.

§Example
use plthook::ObjectFile;

let object = ObjectFile::open_main_program()?;
for symbol in object.symbols() {
    println!("{:?} {:?}", symbol.func_address, symbol.name);
}

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.