Skip to content

Commit 5a2ce0a

Browse files
RaitoBezariusraito
authored andcommitted
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 9f88ba1 commit 5a2ce0a

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
@@ -145,6 +145,7 @@ impl<'a> AttributeCertificate<'a> {
145145
bytes: &'a [u8],
146146
current_offset: &mut usize,
147147
) -> Result<AttributeCertificate<'a>, error::Error> {
148+
debug!("reading certificate header at {current_offset}");
148149
// `current_offset` is moved sizeof(AttributeCertificateHeader) = 8 bytes further.
149150
let header: AttributeCertificateHeader = bytes.gread_with(current_offset, scroll::LE)?;
150151
let cert_size = usize::try_from(
@@ -156,6 +157,11 @@ impl<'a> AttributeCertificate<'a> {
156157
error::Error::Malformed("Attribute certificate size do not fit in usize".to_string())
157158
})?;
158159

160+
debug!(
161+
"parsing certificate header {:#?}, predicted certificate size: {}",
162+
header, cert_size
163+
);
164+
159165
if let Some(bytes) = bytes.get(*current_offset..(*current_offset + cert_size)) {
160166
let attr = Self {
161167
length: header.length,
@@ -191,6 +197,13 @@ impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> {
191197
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
192198
bytes.gwrite(self.certificate, offset)?;
193199
if let Some(cert_padding) = maybe_certificate_padding {
200+
debug!(
201+
"Extending the buffer ({}) at offset {} with {} extra bytes for quadword alignment",
202+
bytes.len(),
203+
*offset,
204+
cert_padding.len()
205+
);
206+
194207
bytes.gwrite(&cert_padding[..], offset)?;
195208
}
196209

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)