-
Notifications
You must be signed in to change notification settings - Fork 168
fix: Iterate all ImageDebugDirectory entries #319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Swatinem
wants to merge
1
commit into
m4b:master
Choose a base branch
from
Swatinem:fix/debug-directory
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use crate::error; | ||
use scroll::ctx::SizeWith; | ||
use scroll::{Pread, Pwrite, SizeWith}; | ||
|
||
use crate::pe::data_directories; | ||
|
@@ -8,6 +9,9 @@ use crate::pe::utils; | |
|
||
#[derive(Debug, PartialEq, Copy, Clone, Default)] | ||
pub struct DebugData<'a> { | ||
// TODO: There can be more than one ImageDebugDirectory. | ||
// To avoid breaking the public API here, we just return the ImageDebugDirectory that has the | ||
// `IMAGE_DEBUG_TYPE_CODEVIEW` type, or the last one if none has a codeview record. | ||
pub image_debug_directory: ImageDebugDirectory, | ||
pub codeview_pdb70_debug_info: Option<CodeviewPDB70DebugInfo<'a>>, | ||
} | ||
|
@@ -35,14 +39,36 @@ impl<'a> DebugData<'a> { | |
file_alignment: u32, | ||
opts: &options::ParseOptions, | ||
) -> error::Result<Self> { | ||
let image_debug_directory = | ||
ImageDebugDirectory::parse_with_opts(bytes, dd, sections, file_alignment, opts)?; | ||
let codeview_pdb70_debug_info = | ||
CodeviewPDB70DebugInfo::parse_with_opts(bytes, &image_debug_directory, opts)?; | ||
|
||
let rva = dd.virtual_address as usize; | ||
let mut offset = | ||
utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| { | ||
error::Error::Malformed(format!( | ||
"Cannot map ImageDebugDirectory rva {:#x} into offset", | ||
rva | ||
)) | ||
})?; | ||
|
||
let len = dd.size as usize; | ||
let sizeof_directory = ImageDebugDirectory::size_with(&scroll::LE); | ||
let num_entries = len / sizeof_directory; | ||
|
||
let mut entries = (0..num_entries) | ||
.map(|_| bytes.gread_with(&mut offset, scroll::LE)) | ||
.collect::<Result<Vec<ImageDebugDirectory>, _>>()?; | ||
|
||
// find the debug directory that references the codeview record | ||
for (idx, idd) in entries.iter().enumerate() { | ||
if let Some(cv_record) = CodeviewPDB70DebugInfo::parse_with_opts(bytes, idd, opts)? { | ||
return Ok(DebugData { | ||
image_debug_directory: entries[idx], | ||
codeview_pdb70_debug_info: Some(cv_record), | ||
}); | ||
} | ||
} | ||
// if we don't have a codeview record, just return the last debug directory | ||
Ok(DebugData { | ||
image_debug_directory, | ||
codeview_pdb70_debug_info, | ||
image_debug_directory: entries.pop().unwrap(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this unwrap ever fail? |
||
codeview_pdb70_debug_info: None, | ||
}) | ||
} | ||
|
||
|
@@ -75,42 +101,6 @@ pub const IMAGE_DEBUG_TYPE_EXCEPTION: u32 = 5; | |
pub const IMAGE_DEBUG_TYPE_FIXUP: u32 = 6; | ||
pub const IMAGE_DEBUG_TYPE_BORLAND: u32 = 9; | ||
|
||
impl ImageDebugDirectory { | ||
#[allow(unused)] | ||
fn parse( | ||
bytes: &[u8], | ||
dd: data_directories::DataDirectory, | ||
sections: &[section_table::SectionTable], | ||
file_alignment: u32, | ||
) -> error::Result<Self> { | ||
Self::parse_with_opts( | ||
bytes, | ||
dd, | ||
sections, | ||
file_alignment, | ||
&options::ParseOptions::default(), | ||
) | ||
} | ||
|
||
fn parse_with_opts( | ||
bytes: &[u8], | ||
dd: data_directories::DataDirectory, | ||
sections: &[section_table::SectionTable], | ||
file_alignment: u32, | ||
opts: &options::ParseOptions, | ||
) -> error::Result<Self> { | ||
let rva = dd.virtual_address as usize; | ||
let offset = utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| { | ||
error::Error::Malformed(format!( | ||
"Cannot map ImageDebugDirectory rva {:#x} into offset", | ||
rva | ||
)) | ||
})?; | ||
let idd: Self = bytes.pread_with(offset, scroll::LE)?; | ||
Ok(idd) | ||
} | ||
} | ||
|
||
pub const CODEVIEW_PDB70_MAGIC: u32 = 0x5344_5352; | ||
pub const CODEVIEW_PDB20_MAGIC: u32 = 0x3031_424e; | ||
pub const CODEVIEW_CV50_MAGIC: u32 = 0x3131_424e; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know the original code just tries parsing
CodeviewPDB70DebugInfo
directly, but you should additionally be able to look at thedata_type
field to see if it isIMAGE_DEBUG_TYPE_CODEVIEW
per the MSDN docs.