Skip to content

Commit

Permalink
pe(write): some debug! traces
Browse files Browse the repository at this point in the history
It can be hard to debug why your writer is not working as intended, here
are some `debug!` traces to help in that endeavor.
  • Loading branch information
RaitoBezarius committed Jan 25, 2024
1 parent 00d2ad5 commit 63aab1b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/pe/certificate_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl<'a> AttributeCertificate<'a> {
bytes: &'a [u8],
current_offset: &mut usize,
) -> Result<AttributeCertificate<'a>, error::Error> {
debug!("reading certificate header at {current_offset}");
// `current_offset` is moved sizeof(AttributeCertificateHeader) = 8 bytes further.
let header: AttributeCertificateHeader = bytes.gread_with(current_offset, scroll::LE)?;
let cert_size = usize::try_from(header.length.saturating_sub(CERTIFICATE_DATA_OFFSET))
Expand All @@ -143,6 +144,11 @@ impl<'a> AttributeCertificate<'a> {
)
})?;

debug!(
"parsing certificate header {:#?}, predicted certificate size: {}",
header, cert_size
);

if let Some(bytes) = bytes.get(*current_offset..(*current_offset + cert_size)) {
let attr = Self {
length: header.length,
Expand Down Expand Up @@ -178,6 +184,13 @@ impl<'a> ctx::TryIntoCtx<scroll::Endian> for &AttributeCertificate<'a> {
let maybe_certificate_padding = pad(self.certificate.len(), Some(16usize));
bytes.gwrite(self.certificate, offset)?;
if let Some(cert_padding) = maybe_certificate_padding {
debug!(
"Extending the buffer ({}) at offset {} with {} extra bytes for quadword alignment",
bytes.len(),
*offset,
cert_padding.len()
);

bytes.gwrite(&cert_padding[..], offset)?;
}

Expand Down
2 changes: 2 additions & 0 deletions src/pe/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ impl CoffHeader {
let string_table_offset = self.pointer_to_symbol_table as usize
+ symbol::SymbolTable::size(self.number_of_symbol_table as usize);
for i in 0..nsections {
debug!("parsing section at offset {offset}");
let section =
section_table::SectionTable::parse(bytes, offset, string_table_offset as usize)?;
debug!("({}) {:#?}", i, section);
Expand Down Expand Up @@ -342,6 +343,7 @@ impl ctx::TryIntoCtx<scroll::Endian> for Header {
bytes.gwrite_with(self.dos_stub, offset, ctx)?;
bytes.gwrite_with(self.signature, offset, scroll::LE)?;
bytes.gwrite_with(self.coff_header, offset, ctx)?;
debug!("Non-optional header written, current offset: {}", offset);
if let Some(opt_header) = self.optional_header {
bytes.gwrite_with(opt_header, offset, ctx)?;
}
Expand Down
5 changes: 5 additions & 0 deletions src/pe/optional_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::container;
use crate::error;

use crate::pe::data_directories;
use crate::pe::debug;

use scroll::{ctx, Endian, LE};
use scroll::{Pread, Pwrite, SizeWith};
Expand Down Expand Up @@ -358,12 +359,16 @@ impl ctx::TryIntoCtx<scroll::Endian> for OptionalHeader {
match self.standard_fields.magic {
MAGIC_32 => {
bytes.gwrite_with::<StandardFields32>(self.standard_fields.into(), offset, ctx)?;
debug!("Wrote standard fields 32 bits (offset: {})", offset);
bytes.gwrite_with(WindowsFields32::try_from(self.windows_fields)?, offset, ctx)?;
debug!("Wrote windows fields 32 bits (offset: {})", offset);
bytes.gwrite_with(self.data_directories, offset, ctx)?;
}
MAGIC_64 => {
bytes.gwrite_with::<StandardFields64>(self.standard_fields.into(), offset, ctx)?;
debug!("Wrote standard fields 64 bits (offset: {})", offset);
bytes.gwrite_with(self.windows_fields, offset, ctx)?;
debug!("Wrote windows fields 64 bits (offset: {})", offset);
bytes.gwrite_with(self.data_directories, offset, ctx)?;
}
_ => panic!(),
Expand Down

0 comments on commit 63aab1b

Please sign in to comment.