This commit is contained in:
nora 2025-01-29 21:50:11 +01:00
parent 00afbbfea3
commit d8c08d9359
3 changed files with 20 additions and 2 deletions

9
README.md Normal file
View file

@ -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

View file

@ -253,6 +253,8 @@ pub fn execute(file: File, pe: &[u8]) {
// just some arbitrary offset that probably won't collide with anything // just some arbitrary offset that probably won't collide with anything
let base = optional_header.image_base as usize + 0xFFFFFF0000; let base = optional_header.image_base as usize + 0xFFFFFF0000;
assert_eq!(base & (4096 - 1), 0);
let map = unsafe { crate::mmap::map(file).unwrap() }; let map = unsafe { crate::mmap::map(file).unwrap() };
// allocate the sections. // allocate the sections.
@ -274,7 +276,14 @@ pub fn execute(file: File, pe: &[u8]) {
} else { } else {
crate::mmap::Mode::Read 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 { unsafe {
map.view( map.view(
mode, mode,

View file

@ -48,7 +48,7 @@ mod imp {
self.0, self.0,
match mode { match mode {
Mode::Read => FILE_MAP_READ, 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, Mode::Execute => FILE_MAP_READ | FILE_MAP_EXECUTE,
}, },
(file_offset << 32) as u32, (file_offset << 32) as u32,