reflective_pe_dll_loader

Crates.ioreflective_pe_dll_loader
lib.rsreflective_pe_dll_loader
version0.1.2
sourcesrc
created_at2024-04-05 10:07:08.592116
updated_at2024-04-11 17:29:50.434034
descriptionReflective PECOFF DLL loader. Loads a DLL from memory for execution.
homepage
repositoryhttps://github.com/JohnScience/reflective_pe_dll_loader
max_upload_size
id1197192
size148,203
Dmitrii - Demenev (JohnScience)

documentation

https://docs.rs/reflective_pe_dll_loader

README

Reflective PE COFF DLL loader

Crates.io Downloads Documentation License Dependency Status

A loader is a program that loads some executable code (e.g. in ELF, PE COFF, or Mach-O formats) into memory so that it can be executed.

A reflective loader is such a loader that loads the executable code from a memory buffer, rather than from a file on disk.

use reflective_pe_dll_loader::{PeDll, Symbol};

let bytes: &[u8] = include_bytes!("../test-dlls/hello_world_lib.dll");
let pe_dll = PeDll::new(&bytes).unwrap();

let add: Symbol<extern "C" fn(i32, i32) -> i32> = {
    let symbol = pe_dll.get_symbol_by_name("add").unwrap();
    unsafe { symbol.assume_type() }
};

assert_eq!(add(1, 2), 3);

Recommendations

This crate has limited use cases. If you can avoid building a dynamic library that you'd embed in your executable and instead create an object file that you'd statically link with your executable, you should do that.

Credits

It is largely based on the code from https://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory/.

Note: the tutorial is incomplete and, for example, does not cover TLS callbacks. This may be a problem for some DLLs but may be fixed in the future.

Commit count: 10

cargo fmt