#dll #loader #load #pe #shared #memory-buffer #api-bindings

reflective_pe_dll_loader

Reflective PECOFF DLL loader. Loads a DLL from memory for execution.

3 releases

0.1.2 Apr 11, 2024
0.1.1 Apr 6, 2024
0.1.0 Apr 5, 2024

#1977 in Parser implementations

Download history 1/week @ 2024-09-17 11/week @ 2024-09-24 5/week @ 2024-10-01

131 downloads per month

MIT/Apache

150KB
560 lines

Contains (Windows DLL, 120KB) test-dlls/hello_world_lib.dll

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.

Dependencies

~1.2–1.9MB
~35K SLoC