Skip to content

Commit

Permalink
read/coff: lower level parsing support for ImportObjectHeader (gimli-…
Browse files Browse the repository at this point in the history
…rs#556)

Add lower level parsing support for ImportObjectHeader and
use it in the readobj example.

Also some style changes for the import file support.
  • Loading branch information
philipc authored Jul 4, 2023
1 parent c56b740 commit 31af34d
Show file tree
Hide file tree
Showing 7 changed files with 1,212 additions and 49 deletions.
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

0 comments on commit 31af34d

Please sign in to comment.