diff --git a/src/lib.rs b/src/lib.rs index a556ade..dded8fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ mod emulated; +mod pdb; mod sys; use std::{ @@ -276,6 +277,13 @@ const IMAGE_FILE_MACHINE_AMD64: u16 = 0x8664; const IMAGE_FILE_MACHINE_ARM64: u16 = 0xaa64; pub fn execute(pe: &[u8], executable_path: &Path) { + let pdb_path = executable_path.with_extension("pdb"); + let pdb_file = std::fs::File::open(&pdb_path).unwrap(); + let map = unsafe { memmap2::Mmap::map(&pdb_file).unwrap() }; + let pdb = pdb::Pdb::new(&map); + pdb.dump(); + return; + let mut main_tls_slots = [ptr::null_mut(); 64]; let mut main_teb = ThreadEnvironmentBlock { diff --git a/src/pdb.rs b/src/pdb.rs new file mode 100644 index 0000000..b4bdc74 --- /dev/null +++ b/src/pdb.rs @@ -0,0 +1,45 @@ +//! + +pub(crate) struct Pdb<'a> { + data: &'a [u8], +} + +/// +mod msf { + use bytemuck::{AnyBitPattern, Zeroable}; + + const MAGIC: [u8; 32] = *b"Microsoft C/C++ MSF 7.00\r\n\x1A\x44\x53\x00\x00\x00"; + pub(super) const BLOCK_SIZE: usize = 4096; + + #[derive(Debug, Clone, Copy, AnyBitPattern)] + pub(super) struct SuperBlock { + pub(super) magic: [u8; MAGIC.len()], + pub(super) block_size: u32, + pub(super) free_block_map_block: u32, + pub(super) num_blocks: u32, + pub(super) num_directory_bytes: u32, + pub(super) unknown: u32, + pub(super) block_map_addr: u32, + } + + pub(super) struct StreamDirectory { + pub(super) num_streams: u32, + } +} + +impl<'a> Pdb<'a> { + pub(crate) fn new(data: &'a [u8]) -> Self { + Self { data } + } + + pub(crate) fn dump(&self) { + let superblock = bytemuck::cast_slice::<_, msf::SuperBlock>( + &self.data[0..][..size_of::()], + )[0]; + dbg!(superblock); + + assert_eq!(superblock.block_size, 4096); + let free_block_map_1 = &self.data[msf::BLOCK_SIZE..][..msf::BLOCK_SIZE]; + let free_block_map_2 = &self.data[msf::BLOCK_SIZE * 2..][..msf::BLOCK_SIZE]; + } +} diff --git a/test2/Makefile b/test2/Makefile index c89081f..cc33a7e 100644 --- a/test2/Makefile +++ b/test2/Makefile @@ -1,11 +1,8 @@ SHELL = bash -RUSTC = rustc --target x86_64-pc-windows-msvc -Copt-level=3 -Cpanic=abort -Clinker=lld-link -Clink-arg=/NODEFAULTLIB -Clink-arg=/debug:none -Cdebuginfo=0 +RUSTC = rustc --target x86_64-pc-windows-msvc -Copt-level=3 -Cpanic=abort -Clinker=lld-link -Clink-arg=/NODEFAULTLIB build: empty_exe.exe one_dll.exe two_dll.exe tls_exe.exe -tls_exe.exe: tls_exe.rs - $(RUSTC) tls_exe.rs - empty_exe.exe: empty_exe.rs $(RUSTC) empty_exe.rs diff --git a/test2/tls_exe.rs b/test2/tls_exe.rs deleted file mode 100644 index 7f96cb0..0000000 --- a/test2/tls_exe.rs +++ /dev/null @@ -1,38 +0,0 @@ -#![feature(thread_local)] -#![no_std] -#![no_main] -#![windows_subsystem = "console"] - -#[panic_handler] -fn handle_panic(_: &core::panic::PanicInfo<'_>) -> ! { - loop {} -} - -#[thread_local] -static mut A_THREAD_LOCAL: u32 = 50; -#[thread_local] -static mut ANOTHER_THREAD_LOCAL: u32 = 55; - -#[inline(never)] -fn set_tls(value: u32) { - unsafe { A_THREAD_LOCAL = value; } - unsafe { ANOTHER_THREAD_LOCAL = value; } -} - -#[no_mangle] -pub extern "stdcall" fn mainCRTStartup() -> u32 { - // Use some indirection to actually force TLS to happen - set_tls(14); - unsafe { A_THREAD_LOCAL + ANOTHER_THREAD_LOCAL } -} - -/* -!!!!!!!!!!!!!!! -THIS IS WRONG. WE ARE NOT CREATING THE TLS DIRECTORY. THAT WOULD BE OUR JOB. -!!!!!!!!!!!!!! -*/ - - -extern "stdcall" { - static _tls_index: usize; -}