plthook

Crates.ioplthook
lib.rsplthook
version0.2.2
sourcesrc
created_at2021-07-29 21:34:02.702229
updated_at2021-08-12 21:29:32.657128
descriptionBindings for the plthook library
homepagehttps://github.com/ayosec/plthook-rust
repositoryhttps://github.com/ayosec/plthook-rust.git
max_upload_size
id428995
size97,880
Ayose C. (ayosec)

documentation

https://docs.rs/plthook

README

Rust bindings for plthook

This crates provides Rust bindings for the plthook library.

Please see the API documentation and the description in the plthook library for more details.

Examples

To print symbols in the current process:

use plthook::ObjectFile;

fn main() {
    let object = ObjectFile::open_main_program().unwrap();

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

To replace a symbol:

use plthook::ObjectFile;
use std::mem::{self, MaybeUninit};
use std::os::raw::{c_char, c_int};

static mut ATOI_FN: MaybeUninit<fn(*const c_char) -> c_int> = MaybeUninit::uninit();

extern "C" fn neg_atoi(nptr: *const c_char) -> c_int {
    let i = unsafe { (ATOI_FN.assume_init())(nptr) };
    -i
}

fn main() {
    let object = ObjectFile::open_main_program().expect("Failed to open main program");

    unsafe {
        let mut atoi_entry = object.replace("atoi", neg_atoi as *const _).unwrap();
        ATOI_FN = MaybeUninit::new(mem::transmute(atoi_entry.original_address()));
        atoi_entry.discard();
    };

    let i = unsafe { libc::atoi(b"100\0".as_ptr().cast()) };
    assert_eq!(i, -100);
}
Commit count: 28

cargo fmt