Skip to content

Commit e744736

Browse files
committed
pe(write): some debug! traces
It can be hard to debug why your writer is not working as intended, here are some `debug!` traces to help in that endeavor.
1 parent fa01bf3 commit e744736

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/pe/certificate_table.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only
44
/// https://learn.microsoft.com/en-us/windows/win32/api/wintrust/ns-wintrust-win_certificate
55
use crate::error;
6+
use crate::pe::debug;
67
use scroll::{ctx, Pread, Pwrite};
78

89
use alloc::string::ToString;
@@ -112,6 +113,7 @@ impl<'a> AttributeCertificate<'a> {
112113
bytes: &'a [u8],
113114
current_offset: &mut usize,
114115
) -> Result<AttributeCertificate<'a>, error::Error> {
116+
debug!("reading certificate header at {current_offset}");
115117
// `current_offset` is moved sizeof(AttributeCertificateHeader) = 8 bytes further.
116118
let header: AttributeCertificateHeader = bytes.gread_with(current_offset, scroll::LE)?;
117119
let cert_size = usize::try_from(header.length.saturating_sub(CERTIFICATE_DATA_OFFSET))
@@ -121,6 +123,11 @@ impl<'a> AttributeCertificate<'a> {
121123
)
122124
})?;
123125

126+
debug!(
127+
"parsing certificate header {:#?}, predicted certificate size: {}",
128+
header, cert_size
129+
);
130+
124131
if let Some(bytes) = bytes.get(*current_offset..(*current_offset + cert_size)) {
125132
let attr = Self {
126133
length: header.length,

src/pe/header.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ impl CoffHeader {
253253
let string_table_offset = self.pointer_to_symbol_table as usize
254254
+ symbol::SymbolTable::size(self.number_of_symbol_table as usize);
255255
for i in 0..nsections {
256+
debug!("parsing section at offset {offset}");
256257
let section =
257258
section_table::SectionTable::parse(bytes, offset, string_table_offset as usize)?;
258259
debug!("({}) {:#?}", i, section);
@@ -342,6 +343,7 @@ impl ctx::TryIntoCtx<scroll::Endian> for Header {
342343
bytes.gwrite_with(self.dos_stub, offset, ctx)?;
343344
bytes.gwrite_with(self.signature, offset, scroll::LE)?;
344345
bytes.gwrite_with(self.coff_header, offset, ctx)?;
346+
debug!("Non-optional header written, current offset: {}", offset);
345347
if let Some(opt_header) = self.optional_header {
346348
bytes.gwrite_with(opt_header, offset, ctx)?;
347349
}

src/pe/optional_header.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::container;
22
use crate::error;
33

44
use crate::pe::data_directories;
5+
use crate::pe::debug;
56

67
use scroll::{ctx, Endian, LE};
78
use scroll::{Pread, Pwrite, SizeWith};
@@ -358,12 +359,16 @@ impl ctx::TryIntoCtx<scroll::Endian> for OptionalHeader {
358359
match self.standard_fields.magic {
359360
MAGIC_32 => {
360361
bytes.gwrite_with::<StandardFields32>(self.standard_fields.into(), offset, ctx)?;
362+
debug!("Wrote standard fields 32 bits (offset: {})", offset);
361363
bytes.gwrite_with(WindowsFields32::try_from(self.windows_fields)?, offset, ctx)?;
364+
debug!("Wrote windows fields 32 bits (offset: {})", offset);
362365
bytes.gwrite_with(self.data_directories, offset, ctx)?;
363366
}
364367
MAGIC_64 => {
365368
bytes.gwrite_with::<StandardFields64>(self.standard_fields.into(), offset, ctx)?;
369+
debug!("Wrote standard fields 64 bits (offset: {})", offset);
366370
bytes.gwrite_with(self.windows_fields, offset, ctx)?;
371+
debug!("Wrote windows fields 64 bits (offset: {})", offset);
367372
bytes.gwrite_with(self.data_directories, offset, ctx)?;
368373
}
369374
_ => panic!(),

0 commit comments

Comments
 (0)