-
Notifications
You must be signed in to change notification settings - Fork 163
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
mach.parse: Handle DyldExportsTrie #303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -265,22 +265,11 @@ impl<'a> ExportTrie<'a> { | |
} | ||
} | ||
|
||
/// Walk the export trie for symbols exported by this binary, using the provided `libs` to resolve re-exports | ||
pub fn exports(&self, libs: &[&'a str]) -> error::Result<Vec<Export<'a>>> { | ||
let offset = self.location.start; | ||
let current_symbol = String::new(); | ||
let mut exports = Vec::new(); | ||
self.walk_trie(libs, current_symbol, offset, &mut exports)?; | ||
Ok(exports) | ||
} | ||
|
||
/// Create a new, lazy, zero-copy export trie from the `DyldInfo` `command` | ||
pub fn new(bytes: &'a [u8], command: &load_command::DyldInfoCommand) -> Self { | ||
let start = command.export_off as usize; | ||
fn new_impl(bytes: &'a [u8], start: usize, size: usize) -> Self { | ||
// FIXME: Ideally, this should validate `command`, but the best we can | ||
// do for now is return an empty `Range`. | ||
let location = match start | ||
.checked_add(command.export_size as usize) | ||
.checked_add(size) | ||
.and_then(|end| bytes.get(start..end).map(|_| start..end)) | ||
{ | ||
Some(location) => location, | ||
|
@@ -294,6 +283,32 @@ impl<'a> ExportTrie<'a> { | |
location, | ||
} | ||
} | ||
|
||
/// Walk the export trie for symbols exported by this binary, using the provided `libs` to resolve re-exports | ||
pub fn exports(&self, libs: &[&'a str]) -> error::Result<Vec<Export<'a>>> { | ||
let offset = self.location.start; | ||
let current_symbol = String::new(); | ||
let mut exports = Vec::new(); | ||
self.walk_trie(libs, current_symbol, offset, &mut exports)?; | ||
Ok(exports) | ||
} | ||
|
||
/// Create a new, lazy, zero-copy export trie from the `DyldInfo` `command` | ||
pub fn new(bytes: &'a [u8], command: &load_command::DyldInfoCommand) -> Self { | ||
Self::new_impl( | ||
bytes, | ||
command.export_off as usize, | ||
command.export_size as usize, | ||
) | ||
} | ||
|
||
/// Create a new, lazy, zero-copy export trie from the `LinkeditDataCommand` `command` | ||
pub fn new_from_linkedit_data_command( | ||
bytes: &'a [u8], | ||
command: &load_command::LinkeditDataCommand, | ||
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. Unrelated to your PR< but i just noticed the capitalization for this is strange, surprised it's not LinkEditDataCommand; is this how it occurs in mach sources? 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. Given it's |
||
) -> Self { | ||
Self::new_impl(bytes, command.dataoff as usize, command.datasize as usize) | ||
} | ||
} | ||
|
||
impl<'a> Debug for ExportTrie<'a> { | ||
|
@@ -331,6 +346,28 @@ mod tests { | |
assert_eq!(exports.len() as usize, 3usize) | ||
} | ||
|
||
#[test] | ||
fn export_trie_linkedit_data() { | ||
const EXPORTS: [u8; 64] = [ | ||
0x00, 0x01, 0x5f, 0x00, 0x05, 0x00, 0x02, 0x5f, 0x6d, 0x68, 0x5f, 0x65, 0x78, 0x65, | ||
0x63, 0x75, 0x74, 0x65, 0x5f, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x00, 0x1f, 0x6d, | ||
0x61, 0x00, 0x23, 0x02, 0x00, 0x00, 0x00, 0x00, 0x02, 0x78, 0x69, 0x6d, 0x75, 0x6d, | ||
0x00, 0x30, 0x69, 0x6e, 0x00, 0x35, 0x03, 0x00, 0xc0, 0x1e, 0x00, 0x03, 0x00, 0xd0, | ||
0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
]; | ||
let exports = &EXPORTS[..]; | ||
let libs = vec!["/usr/lib/libderp.so", "/usr/lib/libthuglife.so"]; | ||
let command = load_command::LinkeditDataCommand { | ||
datasize: exports.len() as u32, | ||
..Default::default() | ||
}; | ||
let trie = ExportTrie::new_from_linkedit_data_command(exports, &command); | ||
println!("trie: {:#?}", &trie); | ||
let exports = trie.exports(&libs).unwrap(); | ||
println!("len: {} exports: {:#?}", exports.len(), &exports); | ||
assert_eq!(exports.len() as usize, 3usize); | ||
} | ||
|
||
#[test] | ||
fn invalid_range() { | ||
let mut command = load_command::DyldInfoCommand::default(); | ||
|
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.
Presumably, we don't want a variation of the
new
function, but was simple to implement. What do you think?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.
The name is kind of long, but fine; however, can you dispatch both of these implemenations to a new private constructor, say
new_impl
which takes the bytes + the offset + size, since that is how they only differ, e.g.: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.
@m4b Updated.