From d8c08d9359f58bcc1513e1abc92a5ce48f0c2b4f Mon Sep 17 00:00:00 2001 From: Noratrieb <48135649+Noratrieb@users.noreply.github.com> Date: Wed, 29 Jan 2025 21:50:11 +0100 Subject: [PATCH] stuff --- README.md | 9 +++++++++ src/lib.rs | 11 ++++++++++- src/mmap.rs | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..18fd8a7 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# portability + +a PE loader for educational purposes. + +## references + +- https://learn.microsoft.com/en-us/windows/win32/debug/pe-format +- https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail +- https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/march/inside-windows-an-in-depth-look-into-the-win32-portable-executable-file-format-part-2 \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 34b6ac8..dd17707 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -253,6 +253,8 @@ pub fn execute(file: File, pe: &[u8]) { // just some arbitrary offset that probably won't collide with anything let base = optional_header.image_base as usize + 0xFFFFFF0000; + assert_eq!(base & (4096 - 1), 0); + let map = unsafe { crate::mmap::map(file).unwrap() }; // allocate the sections. @@ -274,7 +276,14 @@ pub fn execute(file: File, pe: &[u8]) { } else { crate::mmap::Mode::Read }; - let address = std::ptr::with_exposed_provenance(base + section.virtual_address as usize); + let address = + std::ptr::with_exposed_provenance::<()>(base + section.virtual_address as usize); + dbg!(section); + + // assert stuff is aligned (yes 4096 as a hardcoded page is bad) + //assert_eq!(section.pointer_to_raw_data & (4096 - 1), 0); + assert_eq!(address.addr() & (4096 - 1), 0); + unsafe { map.view( mode, diff --git a/src/mmap.rs b/src/mmap.rs index 268f106..c6e8987 100644 --- a/src/mmap.rs +++ b/src/mmap.rs @@ -48,7 +48,7 @@ mod imp { self.0, match mode { Mode::Read => FILE_MAP_READ, - Mode::Write => FILE_MAP_READ | FILE_MAP_COPY, + Mode::Write => FILE_MAP_COPY, Mode::Execute => FILE_MAP_READ | FILE_MAP_EXECUTE, }, (file_offset << 32) as u32,