Skip to main content

PeLoader

Struct PeLoader 

pub struct PeLoader { /* private fields */ }
Expand description

PE image loader for mapping Windows executables into the emulation address space.

PeLoader parses Portable Executable (PE) files and maps them into an AddressSpace with proper section alignment, memory protection, and optional relocation support.

§Features

  • Parses both PE32 (32-bit) and PE32+ (64-bit) formats
  • Maps sections at their correct virtual addresses
  • Applies section-specific memory protection flags
  • Handles BSS (uninitialized data) sections
  • Detects .NET assemblies via CLR header

§Configuration

Loading behavior can be customized via PeLoaderConfig:

  • Override the base address
  • Disable memory protection enforcement
  • Enable/disable import resolution
  • Control relocation application

§Thread Safety

PeLoader instances are not Sync due to internal state, but multiple loaders can operate on the same address space concurrently.

§Example

use dotscope::emulation::loader::PeLoader;
use std::path::Path;

let loader = PeLoader::new();
let image = loader.load_file(
    Path::new("program.exe"),
    &address_space,
)?;

println!("Loaded {} ({} sections)", image.name, image.sections.len());

Implementations§

§

impl PeLoader

pub fn new() -> Self

Creates a new PE loader with default configuration.

The default configuration uses the PE’s preferred base address, applies section permissions, and handles relocations automatically.

§Returns

A new PeLoader instance with default settings.

§Example
let loader = PeLoader::new();

pub fn with_config(config: PeLoaderConfig) -> Self

Creates a new PE loader with the specified configuration.

Use this constructor when you need to customize loading behavior, such as specifying a base address or disabling permissions.

§Arguments
  • config - The configuration to use for loading
§Returns

A new PeLoader instance with the specified configuration.

§Example
let config = PeLoaderConfig::new()
    .with_base_address(0x10000000)
    .without_permissions();
let loader = PeLoader::with_config(config);

pub fn load( &self, pe_bytes: &[u8], address_space: &AddressSpace, name: impl Into<String>, ) -> Result<LoadedImage>

Loads a PE image from a byte slice into the address space.

Parses the PE headers from the provided bytes, allocates space in the address space, and maps each section to its virtual address. The image is loaded according to the loader’s configuration.

§Arguments
  • pe_bytes - The raw bytes of the PE file
  • address_space - The address space to map the image into
  • name - A name/label for the loaded image (used for debugging)
§Returns

Returns Ok(LoadedImage) containing metadata about the loaded image, or an error if parsing or mapping fails.

§Errors

Returns an error if:

  • The bytes are not a valid PE file
  • The PE headers are malformed or unsupported
  • The target address range is already mapped
  • Memory allocation fails
§Example
let pe_data = std::fs::read("program.exe")?;
let image = loader.load(&pe_data, &address_space, "program.exe")?;

pub fn load_file( &self, path: &Path, address_space: &AddressSpace, ) -> Result<LoadedImage>

Loads a PE image from a file path into the address space.

Convenience method that reads the file from disk and then calls load. The filename (without directory path) is used as the image name.

§Arguments
  • path - Path to the PE file to load
  • address_space - The address space to map the image into
§Returns

Returns Ok(LoadedImage) containing metadata about the loaded image, or an error if reading or loading fails.

§Errors

Returns an error if:

  • The file cannot be read (permission denied, not found, I/O error)
  • The file is not a valid PE (see load for parsing errors)
  • The address space cannot accommodate the image
§Example
use std::path::Path;

let loader = PeLoader::new();
let image = loader.load_file(
    Path::new("C:\\Windows\\System32\\kernel32.dll"),
    &address_space,
)?;

println!("Loaded: {}", image.name);
println!("Base: 0x{:X}", image.base_address);
println!("Size: 0x{:X} bytes", image.size_of_image);

Trait Implementations§

§

impl Default for PeLoader

§

fn default() -> Self

Returns the “default value” for a type. Read more

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, A> IntoAst<A> for T
where T: Into<A>, A: Ast,

Source§

fn into_ast(self, _a: &A) -> A

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.