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

read/coff: lower level parsing support for ImportObjectHeader #556

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crates/examples/src/objdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn print<W: Write, E: Write>(
writeln!(w)?;
writeln!(w, "{}:", String::from_utf8_lossy(member.name()))?;
if let Ok(data) = member.data(file) {
if FileKind::parse(data) == Ok(FileKind::CoffImportFile) {
if FileKind::parse(data) == Ok(FileKind::CoffImport) {
dump_import(w, e, data)?;
} else {
dump_object(w, e, data)?;
Expand Down Expand Up @@ -252,7 +252,7 @@ fn dump_parsed_object<W: Write, E: Write>(w: &mut W, e: &mut E, file: &object::F
}

fn dump_import<W: Write, E: Write>(w: &mut W, e: &mut E, data: &[u8]) -> Result<()> {
let file = match coff::CoffImportFile::parse(data) {
let file = match coff::ImportFile::parse(data) {
Ok(import) => import,
Err(err) => {
writeln!(e, "Failed to parse short import: {}", err)?;
Expand Down
1 change: 1 addition & 0 deletions crates/examples/src/readobj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ fn print_object(p: &mut Printer<'_>, data: &[u8]) {
object::FileKind::Archive => print_archive(p, data),
object::FileKind::Coff => pe::print_coff(p, data),
object::FileKind::CoffBig => pe::print_coff_big(p, data),
object::FileKind::CoffImport => pe::print_coff_import(p, data),
object::FileKind::DyldCache => macho::print_dyld_cache(p, data),
object::FileKind::Elf32 => elf::print_elf32(p, data),
object::FileKind::Elf64 => elf::print_elf64(p, data),
Expand Down
34 changes: 34 additions & 0 deletions crates/examples/src/readobj/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ pub(super) fn print_coff_big(p: &mut Printer<'_>, data: &[u8]) {
}
}

pub(super) fn print_coff_import(p: &mut Printer<'_>, data: &[u8]) {
let mut offset = 0;
if let Some(header) = ImportObjectHeader::parse(data, &mut offset).print_err(p) {
writeln!(p.w(), "Format: COFF import").unwrap();
p.group("ImportObjectHeader", |p| {
p.field_hex("Signature1", header.sig1.get(LE));
p.field_hex("Signature2", header.sig2.get(LE));
p.field("Version", header.version.get(LE));
p.field_enum("Machine", header.machine.get(LE), FLAGS_IMAGE_FILE_MACHINE);
p.field("TimeDateStamp", header.time_date_stamp.get(LE));
p.field_hex("SizeOfData", header.size_of_data.get(LE));
p.field("OrdinalOrHint", header.ordinal_or_hint.get(LE));
p.field_enum("ImportType", header.import_type(), FLAGS_IMAGE_OBJECT_TYPE);
p.field_enum("NameType", header.name_type(), FLAGS_IMAGE_OBJECT_NAME);
if let Some(data) = header.parse_data(data, &mut offset).print_err(p) {
p.field_inline_string("Symbol", data.symbol());
p.field_inline_string("Dll", data.dll());
if let Some(export) = data.export() {
p.field_inline_string("Export", export);
}
}
});
}
}

pub(super) fn print_pe32(p: &mut Printer<'_>, data: &[u8]) {
writeln!(p.w(), "Format: PE 32-bit").unwrap();
print_pe::<ImageNtHeaders32>(p, data);
Expand Down Expand Up @@ -1243,3 +1268,12 @@ const FLAGS_RT: &[Flag<u16>] = &flags!(
RT_HTML,
RT_MANIFEST,
);
const FLAGS_IMAGE_OBJECT_TYPE: &[Flag<u16>] =
&flags!(IMPORT_OBJECT_CODE, IMPORT_OBJECT_DATA, IMPORT_OBJECT_CONST);
const FLAGS_IMAGE_OBJECT_NAME: &[Flag<u16>] = &flags!(
IMPORT_OBJECT_ORDINAL,
IMPORT_OBJECT_NAME,
IMPORT_OBJECT_NAME_NO_PREFIX,
IMPORT_OBJECT_NAME_UNDECORATE,
IMPORT_OBJECT_NAME_EXPORTAS,
);
Loading