-
Notifications
You must be signed in to change notification settings - Fork 158
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
Get proper name for over-8-character sections #100
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
39b1829
Get proper name for over-8-character sections
roblabla 3ece54d
Fix error handling and other code quality issues
roblabla 3478f94
Implement base64 section names
roblabla a15b6f6
avoid using newer rust features
roblabla 9ca571b
Merge branch 'master' into pe-section-names
m4b 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 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
This file contains 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,10 +1,11 @@ | ||
use scroll::{self, Pread}; | ||
use error; | ||
use error::{self, Error}; | ||
|
||
#[repr(C)] | ||
#[derive(Debug, PartialEq, Copy, Clone, Default)] | ||
#[derive(Debug, PartialEq, Clone, Default)] | ||
pub struct SectionTable { | ||
pub name: [u8; 8], | ||
pub real_name: Option<String>, | ||
pub virtual_size: u32, | ||
pub virtual_address: u32, | ||
pub size_of_raw_data: u32, | ||
|
@@ -18,13 +19,40 @@ pub struct SectionTable { | |
|
||
pub const SIZEOF_SECTION_TABLE: usize = 8 * 5; | ||
|
||
// Based on https://github.com/llvm-mirror/llvm/blob/af7b1832a03ab6486c42a40d21695b2c03b2d8a3/lib/Object/COFFObjectFile.cpp#L70 | ||
// Decodes a string table entry in base 64 (//AAAAAA). Expects string without | ||
// prefixed slashes. | ||
fn base64_decode_string_entry(s: &str) -> Result<usize, ()> { | ||
assert!(s.len() <= 6, "String too long, possible overflow."); | ||
|
||
let mut val = 0; | ||
for c in s.bytes() { | ||
let v = if b'A' <= c && c <= b'Z' { | ||
c - b'A' + 00 // 00..=25 | ||
} else if b'a' <= c && c <= b'z' { | ||
c - b'a' + 26 // 26..=51 | ||
} else if b'0' <= c && c <= b'9' { | ||
c - b'0' + 52 // 52..=61 | ||
} else if c == b'+' { | ||
62 // 62 | ||
} else if c == b'/' { | ||
63 // 63 | ||
} else { | ||
return Err(()) | ||
}; | ||
val = val * 64 + v as usize; | ||
} | ||
Ok(val) | ||
} | ||
|
||
impl SectionTable { | ||
pub fn parse(bytes: &[u8], offset: &mut usize) -> error::Result<Self> { | ||
pub fn parse(bytes: &[u8], offset: &mut usize, string_table_offset: usize) -> error::Result<Self> { | ||
let mut table = SectionTable::default(); | ||
let mut name = [0u8; 8]; | ||
for i in 0..8 { | ||
name[i] = bytes.gread_with(offset, scroll::LE)?; | ||
} | ||
|
||
table.name = name; | ||
table.virtual_size = bytes.gread_with(offset, scroll::LE)?; | ||
table.virtual_address = bytes.gread_with(offset, scroll::LE)?; | ||
|
@@ -35,10 +63,28 @@ impl SectionTable { | |
table.number_of_relocations = bytes.gread_with(offset, scroll::LE)?; | ||
table.number_of_linenumbers = bytes.gread_with(offset, scroll::LE)?; | ||
table.characteristics = bytes.gread_with(offset, scroll::LE)?; | ||
|
||
// Based on https://github.com/llvm-mirror/llvm/blob/af7b1832a03ab6486c42a40d21695b2c03b2d8a3/lib/Object/COFFObjectFile.cpp#L1054 | ||
if name[0] == b'/' { | ||
let idx: usize = if name[1] == b'/' { | ||
let b64idx = name.pread::<&str>(2)?; | ||
base64_decode_string_entry(b64idx).map_err(|_| | ||
Error::Malformed(format!("Invalid indirect section name //{}: base64 decoding failed", b64idx)))? | ||
} else { | ||
let name = name.pread::<&str>(1)?; | ||
name.parse().map_err(|err| | ||
Error::Malformed(format!("Invalid indirect section name /{}: {}", name, err)))? | ||
}; | ||
table.real_name = Some(bytes.pread::<&str>(string_table_offset + idx)?.to_string()); | ||
} | ||
Ok(table) | ||
} | ||
|
||
pub fn name(&self) -> error::Result<&str> { | ||
Ok(self.name.pread(0)?) | ||
match self.real_name.as_ref() { | ||
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. Do you need this as_ref() here? 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. Yes, I run into borrow errors otherwise. The second as_ref in the Some case isn't necessary though. |
||
Some(s) => Ok(s), | ||
None => Ok(self.name.pread(0)?) | ||
} | ||
} | ||
} | ||
|
||
|
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 don't think this should assert unless it's a fundamental programmer error somehow. Return an Err is better
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 thought the same, but looking into it I don't think it's possible for this to assert because the section name is at most 8 bytes, and 2 of those are slashes.
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.
This should not return an error. If this assert is triggered, then something went terribly wrong, and we absolutely should abort.