Crate reflective_pe_dll_loader

Source
Expand description

§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.

Structs§

MemoryAllocationError
An error occured while allocating the memory for loading a DLL.
MemoryCommitError
An error occured while committing memory for a section.
PeDll
A Windows PECOFF DLL loaded into memory.
Symbol
A symbol exported by a DLL.
VirtualProtectError
An error occured while protecting memory.

Enums§

PeDllLoadError
Error type for loading a PE DLL.