Skip to content
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

pe.import: Add get_imports_rva to SynthethicImportDirectoryEntry #278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/pe/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ pub enum SyntheticImportLookupTableEntry<'a> {
HintNameTableRVA((u32, HintNameTableEntry<'a>)), // [u8; 31] bitfield :/
}

pub struct SyntheticImportAddressTableEntry<'a> {
pub rva: u64,
pub name: &'a str,
}

pub type ImportLookupTable<'a> = Vec<SyntheticImportLookupTableEntry<'a>>;

impl<'a> SyntheticImportLookupTableEntry<'a> {
Expand Down Expand Up @@ -293,6 +298,32 @@ impl<'a> SyntheticImportDirectoryEntry<'a> {
import_address_table,
})
}

pub fn get_imports_rva<T: Bitfield<'a>>(
&self,
) -> error::Result<Vec<SyntheticImportAddressTableEntry<'a>>> {
let mut out = Vec::<SyntheticImportAddressTableEntry>::new();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t see any fallibility here, so I’d drop the error return. Also I would attempt to get rid of allocation via returning a vec and try impl Iterator<Item = SyntheticImportAddressTableEntry<‘a>> + ‘a

(you may have to play with the lifetimes). Since it’s optional too you may have to do self.import_table.unwrap_or(&[]).iter().enumerate().flatmap

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it will implement it as an iterator

if let Some(import_lookup_table) = &(self.import_lookup_table) {
for (i, lookup_entry) in import_lookup_table.iter().enumerate() {
match lookup_entry {
SyntheticImportLookupTableEntry::OrdinalNumber(_) => {
continue;
}
SyntheticImportLookupTableEntry::HintNameTableRVA((_, v)) => {
let import_rva = (self.import_directory_entry.import_address_table_rva
as u64)
+ ((i * std::mem::size_of::<T>()) as u64);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned needing to know the size of the PE; this information is usually in the parsing context but I’m not sure if you have access to it here, haven’t looked at this code in a while. Or is the mem::size_of sufficient for this ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mem::size_of is sufficient for this, but honestly passing a boolean called is_64bit would be a cleaner option IMO.

out.push(SyntheticImportAddressTableEntry {
rva: import_rva,
name: v.name,
});
}
};
}
}

Ok(out)
}
}

#[derive(Debug)]
Expand Down