Skip to content

Commit 63aab1b

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 00d2ad5 commit 63aab1b

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/pe/certificate_table.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl<'a> AttributeCertificate<'a> {
134134
bytes: &'a [u8],
135135
current_offset: &mut usize,
136136
) -> Result<AttributeCertificate<'a>, error::Error> {
137+
debug!("reading certificate header at {current_offset}");
137138
// `current_offset` is moved sizeof(AttributeCertificateHeader) = 8 bytes further.
138139
let header: AttributeCertificateHeader = bytes.gread_with(current_offset, scroll::LE)?;
139140
let cert_size = usize::try_from(header.length.saturating_sub(CERTIFICATE_DATA_OFFSET))
@@ -143,6 +144,11 @@ impl<'a> AttributeCertificate<'a> {
143144
)
144145
})?;
145146

147+
debug!(
148+
"parsing certificate header {:#?}, predicted certificate size: {}",
149+
header, cert_size
150+
);
151+
146152
if let Some(bytes) = bytes.get(*current_offset..(*current_offset + cert_size)) {
147153
let attr = Self {
148154
length: header.length,
@@ -178,6 +184,13 @@ impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> {
178184
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
179185
bytes.gwrite(self.certificate, offset)?;
180186
if let Some(cert_padding) = maybe_certificate_padding {
187+
debug!(
188+
"Extending the buffer ({}) at offset {} with {} extra bytes for quadword alignment",
189+
bytes.len(),
190+
*offset,
191+
cert_padding.len()
192+
);
193+
181194
bytes.gwrite(&cert_padding[..], offset)?;
182195
}
183196

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)