diff --git a/crates/examples/src/bin/pecopy.rs b/crates/examples/src/bin/pecopy.rs index 9eeaad40..2001ea99 100644 --- a/crates/examples/src/bin/pecopy.rs +++ b/crates/examples/src/bin/pecopy.rs @@ -2,6 +2,7 @@ use std::error::Error; use std::{env, fs, process}; use object::pe; +use object::read::coff::CoffHeader; use object::read::pe::{ImageNtHeaders, ImageOptionalHeader}; use object::LittleEndian as LE; diff --git a/crates/examples/src/readobj/mod.rs b/crates/examples/src/readobj/mod.rs index 92e18de7..39a42518 100644 --- a/crates/examples/src/readobj/mod.rs +++ b/crates/examples/src/readobj/mod.rs @@ -116,6 +116,22 @@ impl<'a> Printer<'a> { self.field_hex(name, value); } + fn field_enum_display( + &mut self, + name: &str, + value: T, + flags: &[Flag], + ) { + for flag in flags { + if value == flag.value { + self.field_name(name); + writeln!(self.w, "{} ({})", flag.name, value).unwrap(); + return; + } + } + self.field(name, value); + } + fn field_enums(&mut self, name: &str, value: T, enums: &[&[Flag]]) { for flags in enums { for flag in *flags { @@ -176,6 +192,7 @@ fn print_object(p: &mut Printer<'_>, data: &[u8]) { match kind { 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::DyldCache => macho::print_dyld_cache(p, data), object::FileKind::Elf32 => elf::print_elf32(p, data), object::FileKind::Elf64 => elf::print_elf64(p, data), diff --git a/crates/examples/src/readobj/pe.rs b/crates/examples/src/readobj/pe.rs index 4b6e543c..0bf67440 100644 --- a/crates/examples/src/readobj/pe.rs +++ b/crates/examples/src/readobj/pe.rs @@ -1,5 +1,7 @@ use super::*; use object::pe::*; +use object::read::coff::ImageSymbol as _; +use object::read::coff::*; use object::read::pe::*; use object::LittleEndian as LE; use object::{Bytes, U32Bytes, U64Bytes}; @@ -20,6 +22,22 @@ pub(super) fn print_coff(p: &mut Printer<'_>, data: &[u8]) { } } +pub(super) fn print_coff_big(p: &mut Printer<'_>, data: &[u8]) { + let mut offset = 0; + if let Some(header) = AnonObjectHeaderBigobj::parse(data, &mut offset).print_err(p) { + writeln!(p.w(), "Format: COFF bigobj").unwrap(); + print_bigobj(p, header); + let sections = header.sections(data, offset).print_err(p); + let symbols = header.symbols(data).print_err(p); + if let Some(ref sections) = sections { + print_sections(p, data, header.machine.get(LE), symbols.as_ref(), sections); + } + if let Some(ref symbols) = symbols { + print_symbols(p, sections.as_ref(), symbols); + } + } +} + pub(super) fn print_pe32(p: &mut Printer<'_>, data: &[u8]) { writeln!(p.w(), "Format: PE 32-bit").unwrap(); print_pe::(p, data); @@ -166,6 +184,43 @@ fn print_optional(p: &mut Printer<'_>, header: &impl ImageOptionalHeader) { }); } +fn print_bigobj(p: &mut Printer<'_>, header: &AnonObjectHeaderBigobj) { + p.group("AnonObjectHeaderBigObj", |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( + "ClassId", + format!( + "{:08X}-{:04X}-{:04X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}", + header.class_id.data1().get(LE), + header.class_id.data2().get(LE), + header.class_id.data3().get(LE), + header.class_id.data4()[0], + header.class_id.data4()[1], + header.class_id.data4()[2], + header.class_id.data4()[3], + header.class_id.data4()[4], + header.class_id.data4()[5], + header.class_id.data4()[6], + header.class_id.data4()[7], + ), + ); + p.field_hex("SizeOfData", header.size_of_data.get(LE)); + p.field_hex("Flags", header.flags.get(LE)); + p.field_hex("MetaDataSize", header.meta_data_size.get(LE)); + p.field_hex("MetaDataOffset", header.meta_data_offset.get(LE)); + p.field("NumberOfSections", header.number_of_sections.get(LE)); + p.field_hex( + "PointerToSymbolTable", + header.pointer_to_symbol_table.get(LE), + ); + p.field("NumberOfSymbols", header.number_of_symbols.get(LE)); + }); +} + fn print_export_dir( p: &mut Printer<'_>, data: &[u8], @@ -443,11 +498,11 @@ fn print_resource_table( }) } -fn print_sections( +fn print_sections<'data, Coff: CoffHeader>( p: &mut Printer<'_>, data: &[u8], machine: u16, - symbols: Option<&SymbolTable>, + symbols: Option<&SymbolTable<'data, &'data [u8], Coff>>, sections: &SectionTable, ) { for (index, section) in sections.iter().enumerate() { @@ -543,36 +598,44 @@ fn print_sections( } } -fn print_symbols(p: &mut Printer<'_>, sections: Option<&SectionTable>, symbols: &SymbolTable) { +fn print_symbols<'data, Coff: CoffHeader>( + p: &mut Printer<'_>, + sections: Option<&SectionTable>, + symbols: &SymbolTable<'data, &'data [u8], Coff>, +) { for (index, symbol) in symbols.iter() { p.group("ImageSymbol", |p| { p.field("Index", index); if let Some(name) = symbol.name(symbols.strings()).print_err(p) { p.field_inline_string("Name", name); } else { - p.field("Name", format!("{:X?}", symbol.name)); + p.field("Name", format!("{:X?}", symbol.raw_name())); } - p.field_hex("Value", symbol.value.get(LE)); - let section = symbol.section_number.get(LE); - if section == 0 || section >= IMAGE_SYM_SECTION_MAX { - p.field_enum("Section", section, FLAGS_IMAGE_SYM); + p.field_hex("Value", symbol.value()); + let section = symbol.section_number(); + if section <= 0 { + p.field_enum_display("Section", section, FLAGS_IMAGE_SYM); } else { let section_name = sections.and_then(|sections| { sections - .section(section.into()) + .section(section as usize) .and_then(|section| section.name(symbols.strings())) .print_err(p) }); p.field_string_option("Section", section, section_name); } - p.field_hex("Type", symbol.typ.get(LE)); + p.field_hex("Type", symbol.typ()); p.field_enum("BaseType", symbol.base_type(), FLAGS_IMAGE_SYM_TYPE); p.field_enum("DerivedType", symbol.derived_type(), FLAGS_IMAGE_SYM_DTYPE); - p.field_enum("StorageClass", symbol.storage_class, FLAGS_IMAGE_SYM_CLASS); - p.field_hex("NumberOfAuxSymbols", symbol.number_of_aux_symbols); + p.field_enum( + "StorageClass", + symbol.storage_class(), + FLAGS_IMAGE_SYM_CLASS, + ); + p.field_hex("NumberOfAuxSymbols", symbol.number_of_aux_symbols()); if symbol.has_aux_file_name() { if let Some(name) = symbols - .aux_file_name(index, symbol.number_of_aux_symbols) + .aux_file_name(index, symbol.number_of_aux_symbols()) .print_err(p) { p.group("ImageAuxSymbolFile", |p| { @@ -1021,7 +1084,7 @@ static FLAGS_IMAGE_REL_EBC: &[Flag] = &flags!( IMAGE_REL_EBC_SECTION, IMAGE_REL_EBC_SECREL, ); -static FLAGS_IMAGE_SYM: &[Flag] = +static FLAGS_IMAGE_SYM: &[Flag] = &flags!(IMAGE_SYM_UNDEFINED, IMAGE_SYM_ABSOLUTE, IMAGE_SYM_DEBUG,); static FLAGS_IMAGE_SYM_TYPE: &[Flag] = &flags!( IMAGE_SYM_TYPE_NULL, diff --git a/crates/examples/testfiles/pe/base-bigobj.o.objdump b/crates/examples/testfiles/pe/base-bigobj.o.objdump new file mode 100644 index 00000000..fe5dc976 --- /dev/null +++ b/crates/examples/testfiles/pe/base-bigobj.o.objdump @@ -0,0 +1,51 @@ +Format: Coff Little-endian 32-bit +Kind: Relocatable +Architecture: X86_64 +Flags: Coff { characteristics: 0 } +Relative Address Base: 0 +Entry Address: 0 +Segment { name: ".text", address: 0, size: 0 } +Segment { name: ".data", address: 0, size: 0 } +Segment { name: ".bss", address: 0, size: 0 } +Segment { name: ".xdata", address: 0, size: 0 } +Segment { name: ".pdata", address: 0, size: 0 } +Segment { name: ".rdata", address: 0, size: 0 } +Segment { name: ".rdata$zzz", address: 0, size: 0 } +1: Section { name: ".text", address: 0, size: 80, align: 10, kind: Text, flags: Coff { characteristics: 60500020 } } +2: Section { name: ".data", address: 0, size: 0, align: 10, kind: Data, flags: Coff { characteristics: c0500040 } } +3: Section { name: ".bss", address: 0, size: 0, align: 10, kind: UninitializedData, flags: Coff { characteristics: c0500080 } } +4: Section { name: ".xdata", address: 0, size: 18, align: 4, kind: ReadOnlyData, flags: Coff { characteristics: 40300040 } } +5: Section { name: ".pdata", address: 0, size: 18, align: 4, kind: ReadOnlyData, flags: Coff { characteristics: 40300040 } } +6: Section { name: ".rdata", address: 0, size: 10, align: 10, kind: ReadOnlyData, flags: Coff { characteristics: 40500040 } } +7: Section { name: ".rdata$zzz", address: 0, size: 30, align: 10, kind: ReadOnlyData, flags: Coff { characteristics: 40500040 } } + +Symbols +0: Symbol { name: "base.c", address: 0, size: 0, kind: File, section: None, scope: Compilation, weak: false, flags: None } +2: Symbol { name: "printf", address: 0, size: 0, kind: Section, section: Section(SectionIndex(1)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +4: Symbol { name: "main", address: 51, size: 0, kind: Text, section: Section(SectionIndex(1)), scope: Linkage, weak: false, flags: None } +5: Symbol { name: ".text", address: 0, size: 75, kind: Section, section: Section(SectionIndex(1)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +7: Symbol { name: ".data", address: 0, size: 0, kind: Section, section: Section(SectionIndex(2)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +9: Symbol { name: ".bss", address: 0, size: 0, kind: Section, section: Section(SectionIndex(3)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +11: Symbol { name: ".xdata", address: 0, size: 18, kind: Section, section: Section(SectionIndex(4)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +13: Symbol { name: ".pdata", address: 0, size: 18, kind: Section, section: Section(SectionIndex(5)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +15: Symbol { name: ".rdata", address: 0, size: d, kind: Section, section: Section(SectionIndex(6)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +17: Symbol { name: ".rdata$zzz", address: 0, size: 2b, kind: Section, section: Section(SectionIndex(7)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +19: Symbol { name: "__imp___acrt_iob_func", address: 0, size: 0, kind: Data, section: Undefined, scope: Linkage, weak: false, flags: None } +20: Symbol { name: "__main", address: 0, size: 0, kind: Text, section: Undefined, scope: Linkage, weak: false, flags: None } +21: Symbol { name: "__mingw_vfprintf", address: 0, size: 0, kind: Text, section: Undefined, scope: Linkage, weak: false, flags: None } + +.text relocations +(2f, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(13)), addend: fffffffffffffffc, implicit_addend: true }) +(40, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(15)), addend: fffffffffffffffc, implicit_addend: true }) +(5a, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(14)), addend: fffffffffffffffc, implicit_addend: true }) +(61, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(f)), addend: fffffffffffffffc, implicit_addend: true }) + +.pdata relocations +(0, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(5)), addend: 0, implicit_addend: true }) +(4, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(5)), addend: 0, implicit_addend: true }) +(8, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(b)), addend: 0, implicit_addend: true }) +(c, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(5)), addend: 0, implicit_addend: true }) +(10, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(5)), addend: 0, implicit_addend: true }) +(14, Relocation { kind: ImageOffset, encoding: Generic, size: 20, target: Symbol(SymbolIndex(b)), addend: 0, implicit_addend: true }) + +Dynamic symbols diff --git a/crates/examples/testfiles/pe/base-bigobj.o.readobj b/crates/examples/testfiles/pe/base-bigobj.o.readobj new file mode 100644 index 00000000..60c6c0eb --- /dev/null +++ b/crates/examples/testfiles/pe/base-bigobj.o.readobj @@ -0,0 +1,404 @@ +Format: COFF bigobj +AnonObjectHeaderBigObj { + Signature1: 0x0 + Signature2: 0xFFFF + Version: 2 + Machine: IMAGE_FILE_MACHINE_AMD64 (0x8664) + TimeDateStamp: 0 + ClassId: D1BAA1C7-BAEE-4BA9-AF20FAF66AA4DCB8 + SizeOfData: 0x0 + Flags: 0x0 + MetaDataSize: 0x0 + MetaDataOffset: 0x0 + NumberOfSections: 7 + PointerToSymbolTable: 0x2A4 + NumberOfSymbols: 22 +} +ImageSectionHeader { + Index: 1 + Name: ".text" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x80 + PointerToRawData: 0x150 + PointerToRelocations: 0x240 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 4 + NumberOfLinenumbers: 0 + Characteristics: 0x60500020 + IMAGE_SCN_CNT_CODE (0x20) + IMAGE_SCN_MEM_EXECUTE (0x20000000) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) + ImageRelocation { + VirtualAddress: 0x2F + Symbol: "__imp___acrt_iob_func" (0x13) + Type: IMAGE_REL_AMD64_REL32 (0x4) + } + ImageRelocation { + VirtualAddress: 0x40 + Symbol: "__mingw_vfprintf" (0x15) + Type: IMAGE_REL_AMD64_REL32 (0x4) + } + ImageRelocation { + VirtualAddress: 0x5A + Symbol: "__main" (0x14) + Type: IMAGE_REL_AMD64_REL32 (0x4) + } + ImageRelocation { + VirtualAddress: 0x61 + Symbol: ".rdata" (0xF) + Type: IMAGE_REL_AMD64_REL32 (0x4) + } +} +ImageSectionHeader { + Index: 2 + Name: ".data" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x0 + PointerToRawData: 0x0 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0xC0500040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_MEM_WRITE (0x80000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) +} +ImageSectionHeader { + Index: 3 + Name: ".bss" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x0 + PointerToRawData: 0x0 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0xC0500080 + IMAGE_SCN_CNT_UNINITIALIZED_DATA (0x80) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_MEM_WRITE (0x80000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) +} +ImageSectionHeader { + Index: 4 + Name: ".xdata" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x18 + PointerToRawData: 0x1D0 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0x40300040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_4BYTES (0x300000) +} +ImageSectionHeader { + Index: 5 + Name: ".pdata" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x18 + PointerToRawData: 0x1E8 + PointerToRelocations: 0x268 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 6 + NumberOfLinenumbers: 0 + Characteristics: 0x40300040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_4BYTES (0x300000) + ImageRelocation { + VirtualAddress: 0x0 + Symbol: ".text" (0x5) + Type: IMAGE_REL_AMD64_ADDR32NB (0x3) + } + ImageRelocation { + VirtualAddress: 0x4 + Symbol: ".text" (0x5) + Type: IMAGE_REL_AMD64_ADDR32NB (0x3) + } + ImageRelocation { + VirtualAddress: 0x8 + Symbol: ".xdata" (0xB) + Type: IMAGE_REL_AMD64_ADDR32NB (0x3) + } + ImageRelocation { + VirtualAddress: 0xC + Symbol: ".text" (0x5) + Type: IMAGE_REL_AMD64_ADDR32NB (0x3) + } + ImageRelocation { + VirtualAddress: 0x10 + Symbol: ".text" (0x5) + Type: IMAGE_REL_AMD64_ADDR32NB (0x3) + } + ImageRelocation { + VirtualAddress: 0x14 + Symbol: ".xdata" (0xB) + Type: IMAGE_REL_AMD64_ADDR32NB (0x3) + } +} +ImageSectionHeader { + Index: 6 + Name: ".rdata" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x10 + PointerToRawData: 0x200 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0x40500040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) +} +ImageSectionHeader { + Index: 7 + Name: ".rdata$zzz" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x30 + PointerToRawData: 0x210 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0x40500040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) +} +ImageSymbol { + Index: 0 + Name: ".file" + Value: 0x0 + Section: IMAGE_SYM_DEBUG (-2) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_FILE (0x67) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolFile { + Name: "base.c" + } +} +ImageSymbol { + Index: 2 + Name: "printf" + Value: 0x0 + Section: ".text" (0x1) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolFunction { + TagIndex: 0 + TotalSize: 1 + PointerToLinenumber: 0x0 + PointerToNextFunction: 0 + Unused: [0, 0] + } +} +ImageSymbol { + Index: 4 + Name: "main" + Value: 0x51 + Section: ".text" (0x1) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 5 + Name: ".text" + Value: 0x0 + Section: ".text" (0x1) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x75 + NumberOfRelocations: 4 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 7 + Name: ".data" + Value: 0x0 + Section: ".data" (0x2) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 9 + Name: ".bss" + Value: 0x0 + Section: ".bss" (0x3) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 11 + Name: ".xdata" + Value: 0x0 + Section: ".xdata" (0x4) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x18 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 13 + Name: ".pdata" + Value: 0x0 + Section: ".pdata" (0x5) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x18 + NumberOfRelocations: 6 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 15 + Name: ".rdata" + Value: 0x0 + Section: ".rdata" (0x6) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0xD + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 17 + Name: ".rdata$zzz" + Value: 0x0 + Section: ".rdata$zzz" (0x7) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x2B + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 19 + Name: "__imp___acrt_iob_func" + Value: 0x0 + Section: IMAGE_SYM_UNDEFINED (0) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 20 + Name: "__main" + Value: 0x0 + Section: IMAGE_SYM_UNDEFINED (0) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 21 + Name: "__mingw_vfprintf" + Value: 0x0 + Section: IMAGE_SYM_UNDEFINED (0) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} diff --git a/crates/examples/testfiles/pe/base-bigobj.obj.objdump b/crates/examples/testfiles/pe/base-bigobj.obj.objdump new file mode 100644 index 00000000..373144dc --- /dev/null +++ b/crates/examples/testfiles/pe/base-bigobj.obj.objdump @@ -0,0 +1,62 @@ +Format: Coff Little-endian 32-bit +Kind: Relocatable +Architecture: I386 +Flags: Coff { characteristics: 0 } +Relative Address Base: 0 +Entry Address: 0 +Segment { name: ".drectve", address: 0, size: 0 } +Segment { name: ".debug$S", address: 0, size: 0 } +Segment { name: ".text$mn", address: 0, size: 0 } +Segment { name: ".text$mn", address: 0, size: 0 } +Segment { name: ".text$mn", address: 0, size: 0 } +Segment { name: ".text$mn", address: 0, size: 0 } +Segment { name: ".data", address: 0, size: 0 } +Segment { name: ".chks64", address: 0, size: 0 } +1: Section { name: ".drectve", address: 0, size: 2f, align: 1, kind: Linker, flags: Coff { characteristics: 100a00 } } +2: Section { name: ".debug$S", address: 0, size: 7c, align: 1, kind: Other, flags: Coff { characteristics: 42100040 } } +3: Section { name: ".text$mn", address: 0, size: 14, align: 10, kind: Text, flags: Coff { characteristics: 60500020 } } +4: Section { name: ".text$mn", address: 0, size: a, align: 10, kind: Text, flags: Coff { characteristics: 60501020 } } +5: Section { name: ".text$mn", address: 0, size: 29, align: 10, kind: Text, flags: Coff { characteristics: 60501020 } } +6: Section { name: ".text$mn", address: 0, size: 3a, align: 10, kind: Text, flags: Coff { characteristics: 60501020 } } +7: Section { name: ".data", address: 0, size: d, align: 4, kind: Data, flags: Coff { characteristics: c0300040 } } +8: Section { name: ".chks64", address: 0, size: 40, align: 10, kind: Linker, flags: Coff { characteristics: a00 } } +Comdat { symbol: SymbolIndex(14), name: "___local_stdio_printf_options", kind: Any } Sections: 4 +Comdat { symbol: SymbolIndex(17), name: "__vfprintf_l", kind: Any } Sections: 5 +Comdat { symbol: SymbolIndex(18), name: "_printf", kind: Any } Sections: 6 + +Symbols +0: Symbol { name: "@comp.id", address: 0, size: 0, kind: Data, section: Absolute, scope: Compilation, weak: false, flags: None } +1: Symbol { name: "@feat.00", address: 0, size: 0, kind: Data, section: Absolute, scope: Compilation, weak: false, flags: None } +2: Symbol { name: ".drectve", address: 0, size: 2f, kind: Section, section: Section(SectionIndex(1)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +4: Symbol { name: ".debug$S", address: 0, size: 7c, kind: Section, section: Section(SectionIndex(2)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +6: Symbol { name: ".text$mn", address: 0, size: 14, kind: Section, section: Section(SectionIndex(3)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +8: Symbol { name: ".text$mn", address: 0, size: a, kind: Section, section: Section(SectionIndex(4)), scope: Compilation, weak: false, flags: CoffSection { selection: 2, associative_section: None } } +10: Symbol { name: ".text$mn", address: 0, size: 29, kind: Section, section: Section(SectionIndex(5)), scope: Compilation, weak: false, flags: CoffSection { selection: 2, associative_section: None } } +12: Symbol { name: ".text$mn", address: 0, size: 3a, kind: Section, section: Section(SectionIndex(6)), scope: Compilation, weak: false, flags: CoffSection { selection: 2, associative_section: None } } +14: Symbol { name: "___local_stdio_printf_options", address: 0, size: 0, kind: Text, section: Section(SectionIndex(4)), scope: Linkage, weak: false, flags: None } +15: Symbol { name: "___acrt_iob_func", address: 0, size: 0, kind: Text, section: Undefined, scope: Linkage, weak: false, flags: None } +16: Symbol { name: "___stdio_common_vfprintf", address: 0, size: 0, kind: Text, section: Undefined, scope: Linkage, weak: false, flags: None } +17: Symbol { name: "__vfprintf_l", address: 0, size: 0, kind: Text, section: Section(SectionIndex(5)), scope: Linkage, weak: false, flags: None } +18: Symbol { name: "_printf", address: 0, size: 0, kind: Text, section: Section(SectionIndex(6)), scope: Linkage, weak: false, flags: None } +19: Symbol { name: "_main", address: 0, size: 0, kind: Text, section: Section(SectionIndex(3)), scope: Linkage, weak: false, flags: None } +20: Symbol { name: "?_OptionsStorage@?1??__local_stdio_printf_options@@9@9", address: 0, size: 8, kind: Data, section: Common, scope: Linkage, weak: false, flags: None } +21: Symbol { name: ".data", address: 0, size: d, kind: Section, section: Section(SectionIndex(7)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } +23: Symbol { name: "$SG9248", address: 0, size: 0, kind: Data, section: Section(SectionIndex(7)), scope: Compilation, weak: false, flags: None } +24: Symbol { name: ".chks64", address: 0, size: 40, kind: Section, section: Section(SectionIndex(8)), scope: Compilation, weak: false, flags: CoffSection { selection: 0, associative_section: None } } + +.text$mn relocations +(4, Relocation { kind: Absolute, encoding: Generic, size: 20, target: Symbol(SymbolIndex(17)), addend: 0, implicit_addend: true }) +(9, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(12)), addend: fffffffffffffffc, implicit_addend: true }) + +.text$mn relocations +(4, Relocation { kind: Absolute, encoding: Generic, size: 20, target: Symbol(SymbolIndex(14)), addend: 0, implicit_addend: true }) + +.text$mn relocations +(14, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(e)), addend: fffffffffffffffc, implicit_addend: true }) +(20, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(10)), addend: fffffffffffffffc, implicit_addend: true }) + +.text$mn relocations +(19, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(f)), addend: fffffffffffffffc, implicit_addend: true }) +(22, Relocation { kind: Relative, encoding: Generic, size: 20, target: Symbol(SymbolIndex(11)), addend: fffffffffffffffc, implicit_addend: true }) + +Dynamic symbols diff --git a/crates/examples/testfiles/pe/base-bigobj.obj.readobj b/crates/examples/testfiles/pe/base-bigobj.obj.readobj new file mode 100644 index 00000000..57b9fa13 --- /dev/null +++ b/crates/examples/testfiles/pe/base-bigobj.obj.readobj @@ -0,0 +1,465 @@ +Format: COFF bigobj +AnonObjectHeaderBigObj { + Signature1: 0x0 + Signature2: 0xFFFF + Version: 2 + Machine: IMAGE_FILE_MACHINE_I386 (0x14C) + TimeDateStamp: 1669951910 + ClassId: D1BAA1C7-BAEE-4BA9-AF20FAF66AA4DCB8 + SizeOfData: 0x0 + Flags: 0x0 + MetaDataSize: 0x0 + MetaDataOffset: 0x0 + NumberOfSections: 8 + PointerToSymbolTable: 0x337 + NumberOfSymbols: 26 +} +ImageSectionHeader { + Index: 1 + Name: ".drectve" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x2F + PointerToRawData: 0x178 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0x100A00 + IMAGE_SCN_LNK_INFO (0x200) + IMAGE_SCN_LNK_REMOVE (0x800) + IMAGE_SCN_ALIGN_1BYTES (0x100000) +} +ImageSectionHeader { + Index: 2 + Name: ".debug$S" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x7C + PointerToRawData: 0x1A7 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0x42100040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_DISCARDABLE (0x2000000) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_1BYTES (0x100000) +} +ImageSectionHeader { + Index: 3 + Name: ".text$mn" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x14 + PointerToRawData: 0x223 + PointerToRelocations: 0x237 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 2 + NumberOfLinenumbers: 0 + Characteristics: 0x60500020 + IMAGE_SCN_CNT_CODE (0x20) + IMAGE_SCN_MEM_EXECUTE (0x20000000) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) + ImageRelocation { + VirtualAddress: 0x4 + Symbol: "$SG9248" (0x17) + Type: IMAGE_REL_I386_DIR32 (0x6) + } + ImageRelocation { + VirtualAddress: 0x9 + Symbol: "_printf" (0x12) + Type: IMAGE_REL_I386_REL32 (0x14) + } +} +ImageSectionHeader { + Index: 4 + Name: ".text$mn" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0xA + PointerToRawData: 0x24B + PointerToRelocations: 0x255 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 1 + NumberOfLinenumbers: 0 + Characteristics: 0x60501020 + IMAGE_SCN_CNT_CODE (0x20) + IMAGE_SCN_LNK_COMDAT (0x1000) + IMAGE_SCN_MEM_EXECUTE (0x20000000) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) + ImageRelocation { + VirtualAddress: 0x4 + Symbol: "?_OptionsStorage@?1??__local_stdio_printf_options@@9@9" (0x14) + Type: IMAGE_REL_I386_DIR32 (0x6) + } +} +ImageSectionHeader { + Index: 5 + Name: ".text$mn" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x29 + PointerToRawData: 0x25F + PointerToRelocations: 0x288 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 2 + NumberOfLinenumbers: 0 + Characteristics: 0x60501020 + IMAGE_SCN_CNT_CODE (0x20) + IMAGE_SCN_LNK_COMDAT (0x1000) + IMAGE_SCN_MEM_EXECUTE (0x20000000) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) + ImageRelocation { + VirtualAddress: 0x14 + Symbol: "___local_stdio_printf_options" (0xE) + Type: IMAGE_REL_I386_REL32 (0x14) + } + ImageRelocation { + VirtualAddress: 0x20 + Symbol: "___stdio_common_vfprintf" (0x10) + Type: IMAGE_REL_I386_REL32 (0x14) + } +} +ImageSectionHeader { + Index: 6 + Name: ".text$mn" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x3A + PointerToRawData: 0x29C + PointerToRelocations: 0x2D6 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 2 + NumberOfLinenumbers: 0 + Characteristics: 0x60501020 + IMAGE_SCN_CNT_CODE (0x20) + IMAGE_SCN_LNK_COMDAT (0x1000) + IMAGE_SCN_MEM_EXECUTE (0x20000000) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_ALIGN_16BYTES (0x500000) + ImageRelocation { + VirtualAddress: 0x19 + Symbol: "___acrt_iob_func" (0xF) + Type: IMAGE_REL_I386_REL32 (0x14) + } + ImageRelocation { + VirtualAddress: 0x22 + Symbol: "__vfprintf_l" (0x11) + Type: IMAGE_REL_I386_REL32 (0x14) + } +} +ImageSectionHeader { + Index: 7 + Name: ".data" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0xD + PointerToRawData: 0x2EA + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0xC0300040 + IMAGE_SCN_CNT_INITIALIZED_DATA (0x40) + IMAGE_SCN_MEM_READ (0x40000000) + IMAGE_SCN_MEM_WRITE (0x80000000) + IMAGE_SCN_ALIGN_4BYTES (0x300000) +} +ImageSectionHeader { + Index: 8 + Name: ".chks64" + VirtualSize: 0x0 + VirtualAddress: 0x0 + SizeOfRawData: 0x40 + PointerToRawData: 0x2F7 + PointerToRelocations: 0x0 + PointerToLinenumbers: 0x0 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + Characteristics: 0xA00 + IMAGE_SCN_LNK_INFO (0x200) + IMAGE_SCN_LNK_REMOVE (0x800) +} +ImageSymbol { + Index: 0 + Name: "@comp.id" + Value: 0x1047556 + Section: IMAGE_SYM_ABSOLUTE (-1) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 1 + Name: "@feat.00" + Value: 0x80010191 + Section: IMAGE_SYM_ABSOLUTE (-1) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 2 + Name: ".drectve" + Value: 0x0 + Section: ".drectve" (0x1) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x2F + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 4 + Name: ".debug$S" + Value: 0x0 + Section: ".debug$S" (0x2) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x7C + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 6 + Name: ".text$mn" + Value: 0x0 + Section: ".text$mn" (0x3) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x14 + NumberOfRelocations: 2 + NumberOfLinenumbers: 0 + CheckSum: 0xF829D91C + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 8 + Name: ".text$mn" + Value: 0x0 + Section: ".text$mn" (0x4) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0xA + NumberOfRelocations: 1 + NumberOfLinenumbers: 0 + CheckSum: 0x71A05264 + Number: 0 + Selection: IMAGE_COMDAT_SELECT_ANY (0x2) + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 10 + Name: ".text$mn" + Value: 0x0 + Section: ".text$mn" (0x5) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x29 + NumberOfRelocations: 2 + NumberOfLinenumbers: 0 + CheckSum: 0x2B25B17F + Number: 0 + Selection: IMAGE_COMDAT_SELECT_ANY (0x2) + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 12 + Name: ".text$mn" + Value: 0x0 + Section: ".text$mn" (0x6) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x3A + NumberOfRelocations: 2 + NumberOfLinenumbers: 0 + CheckSum: 0xCAE6D625 + Number: 0 + Selection: IMAGE_COMDAT_SELECT_ANY (0x2) + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 14 + Name: "___local_stdio_printf_options" + Value: 0x0 + Section: ".text$mn" (0x4) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 15 + Name: "___acrt_iob_func" + Value: 0x0 + Section: IMAGE_SYM_UNDEFINED (0) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 16 + Name: "___stdio_common_vfprintf" + Value: 0x0 + Section: IMAGE_SYM_UNDEFINED (0) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 17 + Name: "__vfprintf_l" + Value: 0x0 + Section: ".text$mn" (0x5) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 18 + Name: "_printf" + Value: 0x0 + Section: ".text$mn" (0x6) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 19 + Name: "_main" + Value: 0x0 + Section: ".text$mn" (0x3) + Type: 0x20 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 20 + Name: "?_OptionsStorage@?1??__local_stdio_printf_options@@9@9" + Value: 0x8 + Section: IMAGE_SYM_UNDEFINED (0) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_EXTERNAL (0x2) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 21 + Name: ".data" + Value: 0x0 + Section: ".data" (0x7) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0xD + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0xA8FB903A + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} +ImageSymbol { + Index: 23 + Name: "$SG9248" + Value: 0x0 + Section: ".data" (0x7) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x0 +} +ImageSymbol { + Index: 24 + Name: ".chks64" + Value: 0x0 + Section: ".chks64" (0x8) + Type: 0x0 + BaseType: IMAGE_SYM_TYPE_NULL (0x0) + DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) + StorageClass: IMAGE_SYM_CLASS_STATIC (0x3) + NumberOfAuxSymbols: 0x1 + ImageAuxSymbolSection { + Length: 0x40 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0x0 + Number: 0 + Selection: 0x0 + Reserved: 0x0 + HighNumber: 0 + } +} diff --git a/crates/examples/testfiles/pe/base-gnu.exe.readobj b/crates/examples/testfiles/pe/base-gnu.exe.readobj index 816a2da0..6979a89c 100644 --- a/crates/examples/testfiles/pe/base-gnu.exe.readobj +++ b/crates/examples/testfiles/pe/base-gnu.exe.readobj @@ -472,7 +472,7 @@ ImageSymbol { Index: 0 Name: ".file" Value: 0x5F - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -1228,7 +1228,7 @@ ImageSymbol { Index: 95 Name: ".file" Value: 0x70 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -1337,7 +1337,7 @@ ImageSymbol { Index: 112 Name: ".file" Value: 0x83 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -1467,7 +1467,7 @@ ImageSymbol { Index: 131 Name: ".file" Value: 0xA5 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -1686,7 +1686,7 @@ ImageSymbol { Index: 165 Name: ".file" Value: 0xB9 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -1799,7 +1799,7 @@ ImageSymbol { Index: 185 Name: ".file" Value: 0xCB - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -1901,7 +1901,7 @@ ImageSymbol { Index: 203 Name: ".file" Value: 0xE5 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -2054,7 +2054,7 @@ ImageSymbol { Index: 229 Name: ".file" Value: 0xF7 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -2156,7 +2156,7 @@ ImageSymbol { Index: 247 Name: ".file" Value: 0x12D - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -2495,7 +2495,7 @@ ImageSymbol { Index: 301 Name: ".file" Value: 0x13F - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -2597,7 +2597,7 @@ ImageSymbol { Index: 319 Name: ".file" Value: 0x159 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -2753,7 +2753,7 @@ ImageSymbol { Index: 345 Name: ".file" Value: 0x179 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -2939,7 +2939,7 @@ ImageSymbol { Index: 377 Name: ".file" Value: 0x194 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -3103,7 +3103,7 @@ ImageSymbol { Index: 404 Name: ".file" Value: 0x1A6 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -3205,7 +3205,7 @@ ImageSymbol { Index: 422 Name: ".file" Value: 0x1D1 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -3479,7 +3479,7 @@ ImageSymbol { Index: 465 Name: ".file" Value: 0x1F1 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -3676,7 +3676,7 @@ ImageSymbol { Index: 497 Name: ".file" Value: 0x203 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -3778,7 +3778,7 @@ ImageSymbol { Index: 515 Name: ".file" Value: 0x223 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -3964,7 +3964,7 @@ ImageSymbol { Index: 547 Name: ".file" Value: 0x249 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -4216,7 +4216,7 @@ ImageSymbol { Index: 585 Name: ".file" Value: 0x25B - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -4318,7 +4318,7 @@ ImageSymbol { Index: 603 Name: ".file" Value: 0x26D - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -4420,7 +4420,7 @@ ImageSymbol { Index: 621 Name: ".file" Value: 0x295 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -4694,7 +4694,7 @@ ImageSymbol { Index: 661 Name: ".file" Value: 0x2A7 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -4796,7 +4796,7 @@ ImageSymbol { Index: 679 Name: ".file" Value: 0x2B9 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -4898,7 +4898,7 @@ ImageSymbol { Index: 697 Name: ".file" Value: 0x2CB - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -5000,7 +5000,7 @@ ImageSymbol { Index: 715 Name: ".file" Value: 0x2E9 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -5175,7 +5175,7 @@ ImageSymbol { Index: 745 Name: ".file" Value: 0x31B - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -5548,7 +5548,7 @@ ImageSymbol { Index: 795 Name: ".file" Value: 0x33E - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -5767,7 +5767,7 @@ ImageSymbol { Index: 830 Name: ".file" Value: 0x362 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -5975,7 +5975,7 @@ ImageSymbol { Index: 866 Name: ".file" Value: 0x381 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -6161,7 +6161,7 @@ ImageSymbol { Index: 897 Name: ".file" Value: 0x3B7 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -6578,7 +6578,7 @@ ImageSymbol { Index: 951 Name: ".file" Value: 0x3D3 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -6742,7 +6742,7 @@ ImageSymbol { Index: 979 Name: ".file" Value: 0x4AC - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -8985,7 +8985,7 @@ ImageSymbol { Index: 1196 Name: ".file" Value: 0x4D2 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -9237,7 +9237,7 @@ ImageSymbol { Index: 1234 Name: ".file" Value: 0x4F4 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -9445,7 +9445,7 @@ ImageSymbol { Index: 1268 Name: ".file" Value: 0x512 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -9620,7 +9620,7 @@ ImageSymbol { Index: 1298 Name: ".file" Value: 0x532 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -9828,7 +9828,7 @@ ImageSymbol { Index: 1330 Name: ".file" Value: 0x553 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -10025,7 +10025,7 @@ ImageSymbol { Index: 1363 Name: ".file" Value: 0x56F - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -10189,7 +10189,7 @@ ImageSymbol { Index: 1391 Name: ".file" Value: 0x58B - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -10353,7 +10353,7 @@ ImageSymbol { Index: 1419 Name: ".file" Value: 0x5A7 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -10517,7 +10517,7 @@ ImageSymbol { Index: 1447 Name: ".file" Value: 0x5DF - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -11081,7 +11081,7 @@ ImageSymbol { Index: 1503 Name: ".file" Value: 0x64F - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -12239,7 +12239,7 @@ ImageSymbol { Index: 1615 Name: ".file" Value: 0x65D - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -12351,7 +12351,7 @@ ImageSymbol { Index: 1629 Name: ".file" Value: 0x679 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -12585,7 +12585,7 @@ ImageSymbol { Index: 1657 Name: ".file" Value: 0x68E - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -12925,7 +12925,7 @@ ImageSymbol { Index: 1697 Name: "__ImageBase" Value: 0x40000000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -12991,7 +12991,7 @@ ImageSymbol { Index: 1703 Name: "__dll_characteristics__" Value: 0x160 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13002,7 +13002,7 @@ ImageSymbol { Index: 1704 Name: "__size_of_stack_commit__" Value: 0x1000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13046,7 +13046,7 @@ ImageSymbol { Index: 1708 Name: "__size_of_stack_reserve__" Value: 0x200000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13057,7 +13057,7 @@ ImageSymbol { Index: 1709 Name: "__major_subsystem_version__" Value: 0x5 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13376,7 +13376,7 @@ ImageSymbol { Index: 1738 Name: "__size_of_heap_commit__" Value: 0x1000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13563,7 +13563,7 @@ ImageSymbol { Index: 1755 Name: "__dll__" Value: 0x0 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13574,7 +13574,7 @@ ImageSymbol { Index: 1756 Name: "__minor_os_version__" Value: 0x0 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13618,7 +13618,7 @@ ImageSymbol { Index: 1760 Name: "__image_base__" Value: 0x40000000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -13640,7 +13640,7 @@ ImageSymbol { Index: 1762 Name: "__section_alignment__" Value: 0x1000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14047,7 +14047,7 @@ ImageSymbol { Index: 1799 Name: "__rt_psrelocs_size" Value: 0x0 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14124,7 +14124,7 @@ ImageSymbol { Index: 1806 Name: "__file_alignment__" Value: 0x200 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14212,7 +14212,7 @@ ImageSymbol { Index: 1814 Name: "__major_os_version__" Value: 0x4 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14289,7 +14289,7 @@ ImageSymbol { Index: 1821 Name: "__end__" Value: 0x0 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14432,7 +14432,7 @@ ImageSymbol { Index: 1834 Name: "__size_of_heap_reserve__" Value: 0x100000 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14454,7 +14454,7 @@ ImageSymbol { Index: 1836 Name: "__subsystem__" Value: 0x3 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14784,7 +14784,7 @@ ImageSymbol { Index: 1866 Name: "__major_image_version__" Value: 0x0 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14795,7 +14795,7 @@ ImageSymbol { Index: 1867 Name: "__loader_flags__" Value: 0x0 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14938,7 +14938,7 @@ ImageSymbol { Index: 1880 Name: "__minor_subsystem_version__" Value: 0x2 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -14949,7 +14949,7 @@ ImageSymbol { Index: 1881 Name: "__minor_image_version__" Value: 0x0 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) diff --git a/crates/examples/testfiles/pe/base.o.readobj b/crates/examples/testfiles/pe/base.o.readobj index 9b950a9f..c8f94870 100644 --- a/crates/examples/testfiles/pe/base.o.readobj +++ b/crates/examples/testfiles/pe/base.o.readobj @@ -178,7 +178,7 @@ ImageSymbol { Index: 0 Name: ".file" Value: 0x0 - Section: IMAGE_SYM_DEBUG (0xFFFE) + Section: IMAGE_SYM_DEBUG (-2) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -368,7 +368,7 @@ ImageSymbol { Index: 19 Name: "__imp___acrt_iob_func" Value: 0x0 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -379,7 +379,7 @@ ImageSymbol { Index: 20 Name: "__main" Value: 0x0 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x20 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) @@ -390,7 +390,7 @@ ImageSymbol { Index: 21 Name: "__mingw_vfprintf" Value: 0x0 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x20 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) diff --git a/crates/examples/testfiles/pe/base.obj.readobj b/crates/examples/testfiles/pe/base.obj.readobj index cb90af78..f316babb 100644 --- a/crates/examples/testfiles/pe/base.obj.readobj +++ b/crates/examples/testfiles/pe/base.obj.readobj @@ -183,7 +183,7 @@ ImageSymbol { Index: 0 Name: "@comp.id" Value: 0x1047556 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -194,7 +194,7 @@ ImageSymbol { Index: 1 Name: "@feat.00" Value: 0x80010191 - Section: IMAGE_SYM_ABSOLUTE (0xFFFF) + Section: IMAGE_SYM_ABSOLUTE (-1) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) @@ -342,7 +342,7 @@ ImageSymbol { Index: 15 Name: "___acrt_iob_func" Value: 0x0 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x20 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) @@ -353,7 +353,7 @@ ImageSymbol { Index: 16 Name: "___stdio_common_vfprintf" Value: 0x0 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x20 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_FUNCTION (0x2) @@ -397,7 +397,7 @@ ImageSymbol { Index: 20 Name: "?_OptionsStorage@?1??__local_stdio_printf_options@@9@9" Value: 0x8 - Section: IMAGE_SYM_UNDEFINED (0x0) + Section: IMAGE_SYM_UNDEFINED (0) Type: 0x0 BaseType: IMAGE_SYM_TYPE_NULL (0x0) DerivedType: IMAGE_SYM_DTYPE_NULL (0x0) diff --git a/src/endian.rs b/src/endian.rs index 1287aa65..e4a36ba1 100644 --- a/src/endian.rs +++ b/src/endian.rs @@ -402,6 +402,11 @@ mod aligned { pub struct U16(u16, PhantomData); impl U16 { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 2]) -> Self { + Self(u16::from_ne_bytes(n), PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: u16) -> Self { Self(e.write_u16(n), PhantomData) @@ -424,6 +429,11 @@ mod aligned { pub struct U32(u32, PhantomData); impl U32 { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 4]) -> Self { + Self(u32::from_ne_bytes(n), PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: u32) -> Self { Self(e.write_u32(n), PhantomData) @@ -444,6 +454,11 @@ mod aligned { pub struct U64(u64, PhantomData); impl U64 { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 8]) -> Self { + Self(u64::from_ne_bytes(n), PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: u64) -> Self { Self(e.write_u64(n), PhantomData) @@ -464,6 +479,11 @@ mod aligned { pub struct I16(i16, PhantomData); impl I16 { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 2]) -> Self { + Self(i16::from_ne_bytes(n), PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: i16) -> Self { Self(e.write_i16(n), PhantomData) @@ -484,6 +504,11 @@ mod aligned { pub struct I32(i32, PhantomData); impl I32 { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 4]) -> Self { + Self(i32::from_ne_bytes(n), PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: i32) -> Self { Self(e.write_i32(n), PhantomData) @@ -504,6 +529,11 @@ mod aligned { pub struct I64(i64, PhantomData); impl I64 { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 8]) -> Self { + Self(i64::from_ne_bytes(n), PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: i64) -> Self { Self(e.write_i64(n), PhantomData) @@ -590,6 +620,11 @@ pub type I64 = I64Bytes; pub struct U16Bytes([u8; 2], PhantomData); impl U16Bytes { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 2]) -> Self { + Self(n, PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: u16) -> Self { Self(e.write_u16_bytes(n), PhantomData) @@ -612,6 +647,11 @@ impl U16Bytes { pub struct U32Bytes([u8; 4], PhantomData); impl U32Bytes { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 4]) -> Self { + Self(n, PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: u32) -> Self { Self(e.write_u32_bytes(n), PhantomData) @@ -634,6 +674,11 @@ impl U32Bytes { pub struct U64Bytes([u8; 8], PhantomData); impl U64Bytes { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 8]) -> Self { + Self(n, PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: u64) -> Self { Self(e.write_u64_bytes(n), PhantomData) @@ -656,6 +701,11 @@ impl U64Bytes { pub struct I16Bytes([u8; 2], PhantomData); impl I16Bytes { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 2]) -> Self { + Self(n, PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: i16) -> Self { Self(e.write_i16_bytes(n), PhantomData) @@ -678,6 +728,11 @@ impl I16Bytes { pub struct I32Bytes([u8; 4], PhantomData); impl I32Bytes { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 4]) -> Self { + Self(n, PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: i32) -> Self { Self(e.write_i32_bytes(n), PhantomData) @@ -700,6 +755,11 @@ impl I32Bytes { pub struct I64Bytes([u8; 8], PhantomData); impl I64Bytes { + /// Construct a new value given bytes that already have the required endianness. + pub fn from_bytes(n: [u8; 8]) -> Self { + Self(n, PhantomData) + } + /// Construct a new value given a native endian value. pub fn new(e: E, n: i64) -> Self { Self(e.write_i64_bytes(n), PhantomData) diff --git a/src/pe.rs b/src/pe.rs index 45cc7011..bac101ea 100644 --- a/src/pe.rs +++ b/src/pe.rs @@ -7,7 +7,9 @@ #![allow(missing_docs)] -use crate::endian::{LittleEndian as LE, U16Bytes, U32Bytes, I32, U16, U32, U64}; +use core::convert::TryInto; + +use crate::endian::{I32Bytes, LittleEndian as LE, U16Bytes, U32Bytes, I32, U16, U32, U64}; use crate::pod::Pod; /// MZ @@ -589,16 +591,33 @@ pub const IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT: usize = 13; /// COM Runtime descriptor pub const IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR: usize = 14; -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[repr(C)] -pub struct Guid { - pub data1: U32, - pub data2: U16, - pub data3: U16, - pub data4: [u8; 8], +pub struct Guid(pub [u8; 16]); + +impl Guid { + #[inline] + pub fn data1(self) -> U32 { + U32::from_bytes(self.0[0..4].try_into().unwrap()) + } + + #[inline] + pub fn data2(self) -> U16 { + U16::from_bytes(self.0[4..6].try_into().unwrap()) + } + + #[inline] + pub fn data3(self) -> U16 { + U16::from_bytes(self.0[6..8].try_into().unwrap()) + } + + #[inline] + pub fn data4(self) -> [u8; 8] { + self.0[8..16].try_into().unwrap() + } } -pub type ClsId = Guid; +pub use Guid as ClsId; /// Non-COFF Object file header #[derive(Debug, Clone, Copy)] @@ -641,6 +660,11 @@ pub struct AnonObjectHeaderV2 { pub meta_data_offset: U32, } +/// The required value of `AnonObjectHeaderBigobj::class_id`. +pub const ANON_OBJECT_HEADER_BIGOBJ_CLASS_ID: ClsId = ClsId([ + 0xC7, 0xA1, 0xBA, 0xD1, 0xEE, 0xBA, 0xA9, 0x4B, 0xAF, 0x20, 0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8, +]); + #[derive(Debug, Clone, Copy)] #[repr(C)] pub struct AnonObjectHeaderBigobj { @@ -654,7 +678,7 @@ pub struct AnonObjectHeaderBigobj { /// Actual machine - IMAGE_FILE_MACHINE_xxx pub machine: U16, pub time_date_stamp: U32, - /// {D1BAA1C7-BAEE-4ba9-AF20-FAF66AA4DCB8} + /// Must be `ANON_OBJECT_HEADER_BIGOBJ_CLASS_ID`. pub class_id: ClsId, /// Size of data that follows the header pub size_of_data: U32, @@ -805,7 +829,7 @@ pub struct ImageSymbolEx { /// If first 4 bytes are 0, then second 4 bytes are offset into string table. pub name: [u8; 8], pub value: U32Bytes, - pub section_number: U32Bytes, + pub section_number: I32Bytes, pub typ: U16Bytes, pub storage_class: u8, pub number_of_aux_symbols: u8, @@ -823,11 +847,11 @@ pub struct ImageSymbolExBytes(pub [u8; IMAGE_SIZEOF_SYMBOL_EX]); // defined. Otherwise, section numbers have the following meanings: /// Symbol is undefined or is common. -pub const IMAGE_SYM_UNDEFINED: u16 = 0; +pub const IMAGE_SYM_UNDEFINED: i32 = 0; /// Symbol is an absolute value. -pub const IMAGE_SYM_ABSOLUTE: u16 = 0xffff; +pub const IMAGE_SYM_ABSOLUTE: i32 = -1; /// Symbol is a special debug item. -pub const IMAGE_SYM_DEBUG: u16 = 0xfffe; +pub const IMAGE_SYM_DEBUG: i32 = -2; /// Values 0xFF00-0xFFFF are special pub const IMAGE_SYM_SECTION_MAX: u16 = 0xFEFF; pub const IMAGE_SYM_SECTION_MAX_EX: u32 = 0x7fff_ffff; @@ -913,30 +937,6 @@ pub const N_TSHIFT: usize = 2; pub const IMAGE_SYM_DTYPE_SHIFT: usize = N_BTSHFT; -impl ImageSymbol { - #[inline] - pub fn base_type(&self) -> u16 { - self.typ.get(LE) & N_BTMASK - } - - #[inline] - pub fn derived_type(&self) -> u16 { - (self.typ.get(LE) & N_TMASK) >> N_BTSHFT - } -} - -impl ImageSymbolEx { - #[inline] - pub fn base_type(&self) -> u16 { - self.typ.get(LE) & N_BTMASK - } - - #[inline] - pub fn derived_type(&self) -> u16 { - (self.typ.get(LE) & N_TMASK) >> N_BTSHFT - } -} - // // Auxiliary entry format. // diff --git a/src/read/any.rs b/src/read/any.rs index 111affc4..06af4abb 100644 --- a/src/read/any.rs +++ b/src/read/any.rs @@ -32,6 +32,8 @@ macro_rules! with_inner { match $inner { #[cfg(feature = "coff")] $enum::Coff(ref $var) => $body, + #[cfg(feature = "coff")] + $enum::CoffBig(ref $var) => $body, #[cfg(feature = "elf")] $enum::Elf32(ref $var) => $body, #[cfg(feature = "elf")] @@ -59,6 +61,8 @@ macro_rules! with_inner_mut { match $inner { #[cfg(feature = "coff")] $enum::Coff(ref mut $var) => $body, + #[cfg(feature = "coff")] + $enum::CoffBig(ref mut $var) => $body, #[cfg(feature = "elf")] $enum::Elf32(ref mut $var) => $body, #[cfg(feature = "elf")] @@ -87,6 +91,8 @@ macro_rules! map_inner { match $inner { #[cfg(feature = "coff")] $from::Coff(ref $var) => $to::Coff($body), + #[cfg(feature = "coff")] + $from::CoffBig(ref $var) => $to::CoffBig($body), #[cfg(feature = "elf")] $from::Elf32(ref $var) => $to::Elf32($body), #[cfg(feature = "elf")] @@ -115,6 +121,8 @@ macro_rules! map_inner_option { match $inner { #[cfg(feature = "coff")] $from::Coff(ref $var) => $body.map($to::Coff), + #[cfg(feature = "coff")] + $from::CoffBig(ref $var) => $body.map($to::CoffBig), #[cfg(feature = "elf")] $from::Elf32(ref $var) => $body.map($to::Elf32), #[cfg(feature = "elf")] @@ -142,6 +150,8 @@ macro_rules! map_inner_option_mut { match $inner { #[cfg(feature = "coff")] $from::Coff(ref mut $var) => $body.map($to::Coff), + #[cfg(feature = "coff")] + $from::CoffBig(ref mut $var) => $body.map($to::CoffBig), #[cfg(feature = "elf")] $from::Elf32(ref mut $var) => $body.map($to::Elf32), #[cfg(feature = "elf")] @@ -170,6 +180,8 @@ macro_rules! next_inner { match $inner { #[cfg(feature = "coff")] $from::Coff(ref mut iter) => iter.next().map($to::Coff), + #[cfg(feature = "coff")] + $from::CoffBig(ref mut iter) => iter.next().map($to::CoffBig), #[cfg(feature = "elf")] $from::Elf32(ref mut iter) => iter.next().map($to::Elf32), #[cfg(feature = "elf")] @@ -204,6 +216,8 @@ pub struct File<'data, R: ReadRef<'data> = &'data [u8]> { enum FileInternal<'data, R: ReadRef<'data>> { #[cfg(feature = "coff")] Coff(coff::CoffFile<'data, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigFile<'data, R>), #[cfg(feature = "elf")] Elf32(elf::ElfFile32<'data, Endianness, R>), #[cfg(feature = "elf")] @@ -244,6 +258,8 @@ impl<'data, R: ReadRef<'data>> File<'data, R> { FileKind::Pe64 => FileInternal::Pe64(pe::PeFile64::parse(data)?), #[cfg(feature = "coff")] FileKind::Coff => FileInternal::Coff(coff::CoffFile::parse(data)?), + #[cfg(feature = "coff")] + FileKind::CoffBig => FileInternal::CoffBig(coff::CoffBigFile::parse(data)?), #[cfg(feature = "xcoff")] FileKind::Xcoff32 => FileInternal::Xcoff32(xcoff::XcoffFile32::parse(data)?), #[cfg(feature = "xcoff")] @@ -275,7 +291,7 @@ impl<'data, R: ReadRef<'data>> File<'data, R> { pub fn format(&self) -> BinaryFormat { match self.inner { #[cfg(feature = "coff")] - FileInternal::Coff(_) => BinaryFormat::Coff, + FileInternal::Coff(_) | FileInternal::CoffBig(_) => BinaryFormat::Coff, #[cfg(feature = "elf")] FileInternal::Elf32(_) | FileInternal::Elf64(_) => BinaryFormat::Elf, #[cfg(feature = "macho")] @@ -490,6 +506,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffSegmentIterator<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigSegmentIterator<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfSegmentIterator32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -534,6 +552,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffSegment<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigSegment<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfSegment32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -630,6 +650,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffSectionIterator<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigSectionIterator<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfSectionIterator32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -673,6 +695,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffSection<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigSection<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfSection32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -809,6 +833,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffComdatIterator<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigComdatIterator<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfComdatIterator32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -852,6 +878,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffComdat<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigComdat<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfComdat32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -931,6 +959,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffComdatSectionIterator<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigComdatSectionIterator<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfComdatSectionIterator32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] @@ -977,6 +1007,8 @@ where { #[cfg(feature = "coff")] Coff((coff::CoffSymbolTable<'data, 'file, R>, PhantomData)), + #[cfg(feature = "coff")] + CoffBig((coff::CoffBigSymbolTable<'data, 'file, R>, PhantomData)), #[cfg(feature = "elf")] Elf32( ( @@ -1061,6 +1093,8 @@ where { #[cfg(feature = "coff")] Coff((coff::CoffSymbolIterator<'data, 'file, R>, PhantomData)), + #[cfg(feature = "coff")] + CoffBig((coff::CoffBigSymbolIterator<'data, 'file, R>, PhantomData)), #[cfg(feature = "elf")] Elf32( ( @@ -1138,6 +1172,8 @@ where { #[cfg(feature = "coff")] Coff((coff::CoffSymbol<'data, 'file, R>, PhantomData)), + #[cfg(feature = "coff")] + CoffBig((coff::CoffBigSymbol<'data, 'file, R>, PhantomData)), #[cfg(feature = "elf")] Elf32( ( @@ -1312,6 +1348,8 @@ where { #[cfg(feature = "coff")] Coff(coff::CoffRelocationIterator<'data, 'file, R>), + #[cfg(feature = "coff")] + CoffBig(coff::CoffBigRelocationIterator<'data, 'file, R>), #[cfg(feature = "elf")] Elf32(elf::ElfSectionRelocationIterator32<'data, 'file, Endianness, R>), #[cfg(feature = "elf")] diff --git a/src/read/coff/comdat.rs b/src/read/coff/comdat.rs index 3be69ecc..22e061a2 100644 --- a/src/read/coff/comdat.rs +++ b/src/read/coff/comdat.rs @@ -6,23 +6,34 @@ use crate::read::{ self, ComdatKind, ObjectComdat, ReadError, ReadRef, Result, SectionIndex, SymbolIndex, }; -use super::CoffFile; +use super::{CoffFile, CoffHeader, ImageSymbol}; + +/// An iterator over the COMDAT section groups of a `CoffBigFile`. +pub type CoffBigComdatIterator<'data, 'file, R = &'data [u8]> = + CoffComdatIterator<'data, 'file, R, pe::AnonObjectHeaderBigobj>; /// An iterator over the COMDAT section groups of a `CoffFile`. #[derive(Debug)] -pub struct CoffComdatIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - pub(super) file: &'file CoffFile<'data, R>, +pub struct CoffComdatIterator< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + pub(super) file: &'file CoffFile<'data, R, Coff>, pub(super) index: usize, } -impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffComdatIterator<'data, 'file, R> { - type Item = CoffComdat<'data, 'file, R>; +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for CoffComdatIterator<'data, 'file, R, Coff> +{ + type Item = CoffComdat<'data, 'file, R, Coff>; fn next(&mut self) -> Option { loop { let index = self.index; let symbol = self.file.common.symbols.symbol(index).ok()?; - self.index += 1 + symbol.number_of_aux_symbols as usize; + self.index += 1 + symbol.number_of_aux_symbols() as usize; if let Some(comdat) = CoffComdat::parse(self.file, symbol, index) { return Some(comdat); } @@ -30,21 +41,30 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffComdatIterator<'data, 'fi } } +/// A COMDAT section group of a `CoffBigFile`. +pub type CoffBigComdat<'data, 'file, R = &'data [u8]> = + CoffComdat<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// A COMDAT section group of a `CoffFile`. #[derive(Debug)] -pub struct CoffComdat<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - file: &'file CoffFile<'data, R>, +pub struct CoffComdat< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + file: &'file CoffFile<'data, R, Coff>, symbol_index: SymbolIndex, - symbol: &'data pe::ImageSymbol, + symbol: &'data Coff::ImageSymbol, selection: u8, } -impl<'data, 'file, R: ReadRef<'data>> CoffComdat<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffComdat<'data, 'file, R, Coff> { fn parse( - file: &'file CoffFile<'data, R>, - section_symbol: &'data pe::ImageSymbol, + file: &'file CoffFile<'data, R, Coff>, + section_symbol: &'data Coff::ImageSymbol, index: usize, - ) -> Option> { + ) -> Option> { // Must be a section symbol. if !section_symbol.has_aux_section() { return None; @@ -60,11 +80,11 @@ impl<'data, 'file, R: ReadRef<'data>> CoffComdat<'data, 'file, R> { // Find the COMDAT symbol. let mut symbol_index = index; let mut symbol = section_symbol; - let section_number = section_symbol.section_number.get(LE); + let section_number = section_symbol.section_number(); loop { - symbol_index += 1 + symbol.number_of_aux_symbols as usize; + symbol_index += 1 + symbol.number_of_aux_symbols() as usize; symbol = file.common.symbols.symbol(symbol_index).ok()?; - if section_number == symbol.section_number.get(LE) { + if section_number == symbol.section_number() { break; } } @@ -78,10 +98,15 @@ impl<'data, 'file, R: ReadRef<'data>> CoffComdat<'data, 'file, R> { } } -impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffComdat<'data, 'file, R> {} +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed + for CoffComdat<'data, 'file, R, Coff> +{ +} -impl<'data, 'file, R: ReadRef<'data>> ObjectComdat<'data> for CoffComdat<'data, 'file, R> { - type SectionIterator = CoffComdatSectionIterator<'data, 'file, R>; +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectComdat<'data> + for CoffComdat<'data, 'file, R, Coff> +{ + type SectionIterator = CoffComdatSectionIterator<'data, 'file, R, Coff>; #[inline] fn kind(&self) -> ComdatKind { @@ -119,21 +144,32 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectComdat<'data> for CoffComdat<'data, fn sections(&self) -> Self::SectionIterator { CoffComdatSectionIterator { file: self.file, - section_number: self.symbol.section_number.get(LE), + section_number: self.symbol.section_number(), index: 0, } } } +/// An iterator over the sections in a COMDAT section group of a `CoffBigFile`. +pub type CoffBigComdatSectionIterator<'data, 'file, R = &'data [u8]> = + CoffComdatSectionIterator<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// An iterator over the sections in a COMDAT section group of a `CoffFile`. #[derive(Debug)] -pub struct CoffComdatSectionIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - file: &'file CoffFile<'data, R>, - section_number: u16, +pub struct CoffComdatSectionIterator< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + file: &'file CoffFile<'data, R, Coff>, + section_number: i32, index: usize, } -impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffComdatSectionIterator<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for CoffComdatSectionIterator<'data, 'file, R, Coff> +{ type Item = SectionIndex; fn next(&mut self) -> Option { @@ -142,19 +178,23 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffComdatSectionIterator<'da loop { let index = self.index; let symbol = self.file.common.symbols.symbol(index).ok()?; - self.index += 1 + symbol.number_of_aux_symbols as usize; + self.index += 1 + symbol.number_of_aux_symbols() as usize; // Must be a section symbol. if !symbol.has_aux_section() { continue; } - let section_number = symbol.section_number.get(LE); + let section_number = symbol.section_number(); let aux = self.file.common.symbols.aux_section(index).ok()?; if aux.selection == pe::IMAGE_COMDAT_SELECT_ASSOCIATIVE { - // TODO: use high_number for bigobj - if aux.number.get(LE) == self.section_number { + let number = if Coff::is_type_bigobj() { + u32::from(aux.number.get(LE)) | (u32::from(aux.high_number.get(LE)) << 16) + } else { + u32::from(aux.number.get(LE)) + }; + if number as i32 == self.section_number { return Some(SectionIndex(section_number as usize)); } } else if aux.selection != 0 { diff --git a/src/read/coff/file.rs b/src/read/coff/file.rs index c6cc9f84..4219f8f0 100644 --- a/src/read/coff/file.rs +++ b/src/read/coff/file.rs @@ -1,39 +1,43 @@ use alloc::vec::Vec; +use core::fmt::Debug; use crate::read::{ self, Architecture, Export, FileFlags, Import, NoDynamicRelocationIterator, Object, ObjectKind, ObjectSection, ReadError, ReadRef, Result, SectionIndex, SymbolIndex, }; -use crate::{pe, LittleEndian as LE}; +use crate::{pe, LittleEndian as LE, Pod}; use super::{ CoffComdat, CoffComdatIterator, CoffSection, CoffSectionIterator, CoffSegment, - CoffSegmentIterator, CoffSymbol, CoffSymbolIterator, CoffSymbolTable, SectionTable, - SymbolTable, + CoffSegmentIterator, CoffSymbol, CoffSymbolIterator, CoffSymbolTable, ImageSymbol, + SectionTable, SymbolTable, }; /// The common parts of `PeFile` and `CoffFile`. #[derive(Debug)] -pub(crate) struct CoffCommon<'data, R: ReadRef<'data>> { +pub(crate) struct CoffCommon<'data, R: ReadRef<'data>, Coff: CoffHeader = pe::ImageFileHeader> { pub(crate) sections: SectionTable<'data>, - // TODO: ImageSymbolExBytes - pub(crate) symbols: SymbolTable<'data, R>, + pub(crate) symbols: SymbolTable<'data, R, Coff>, pub(crate) image_base: u64, } +/// A COFF bigobj object file with 32-bit section numbers. +pub type CoffBigFile<'data, R = &'data [u8]> = CoffFile<'data, R, pe::AnonObjectHeaderBigobj>; + /// A COFF object file. #[derive(Debug)] -pub struct CoffFile<'data, R: ReadRef<'data> = &'data [u8]> { - pub(super) header: &'data pe::ImageFileHeader, - pub(super) common: CoffCommon<'data, R>, +pub struct CoffFile<'data, R: ReadRef<'data> = &'data [u8], Coff: CoffHeader = pe::ImageFileHeader> +{ + pub(super) header: &'data Coff, + pub(super) common: CoffCommon<'data, R, Coff>, pub(super) data: R, } -impl<'data, R: ReadRef<'data>> CoffFile<'data, R> { +impl<'data, R: ReadRef<'data>, Coff: CoffHeader> CoffFile<'data, R, Coff> { /// Parse the raw COFF file data. pub fn parse(data: R) -> Result { let mut offset = 0; - let header = pe::ImageFileHeader::parse(data, &mut offset)?; + let header = Coff::parse(data, &mut offset)?; let sections = header.sections(data, offset)?; let symbols = header.symbols(data)?; @@ -49,26 +53,30 @@ impl<'data, R: ReadRef<'data>> CoffFile<'data, R> { } } -impl<'data, R: ReadRef<'data>> read::private::Sealed for CoffFile<'data, R> {} +impl<'data, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed + for CoffFile<'data, R, Coff> +{ +} -impl<'data, 'file, R> Object<'data, 'file> for CoffFile<'data, R> +impl<'data, 'file, R, Coff> Object<'data, 'file> for CoffFile<'data, R, Coff> where 'data: 'file, R: 'file + ReadRef<'data>, + Coff: CoffHeader, { - type Segment = CoffSegment<'data, 'file, R>; - type SegmentIterator = CoffSegmentIterator<'data, 'file, R>; - type Section = CoffSection<'data, 'file, R>; - type SectionIterator = CoffSectionIterator<'data, 'file, R>; - type Comdat = CoffComdat<'data, 'file, R>; - type ComdatIterator = CoffComdatIterator<'data, 'file, R>; - type Symbol = CoffSymbol<'data, 'file, R>; - type SymbolIterator = CoffSymbolIterator<'data, 'file, R>; - type SymbolTable = CoffSymbolTable<'data, 'file, R>; + type Segment = CoffSegment<'data, 'file, R, Coff>; + type SegmentIterator = CoffSegmentIterator<'data, 'file, R, Coff>; + type Section = CoffSection<'data, 'file, R, Coff>; + type SectionIterator = CoffSectionIterator<'data, 'file, R, Coff>; + type Comdat = CoffComdat<'data, 'file, R, Coff>; + type ComdatIterator = CoffComdatIterator<'data, 'file, R, Coff>; + type Symbol = CoffSymbol<'data, 'file, R, Coff>; + type SymbolIterator = CoffSymbolIterator<'data, 'file, R, Coff>; + type SymbolTable = CoffSymbolTable<'data, 'file, R, Coff>; type DynamicRelocationIterator = NoDynamicRelocationIterator; fn architecture(&self) -> Architecture { - match self.header.machine.get(LE) { + match self.header.machine() { pe::IMAGE_FILE_MACHINE_ARMNT => Architecture::Arm, pe::IMAGE_FILE_MACHINE_ARM64 => Architecture::Aarch64, pe::IMAGE_FILE_MACHINE_I386 => Architecture::I386, @@ -92,7 +100,7 @@ where ObjectKind::Relocatable } - fn segments(&'file self) -> CoffSegmentIterator<'data, 'file, R> { + fn segments(&'file self) -> CoffSegmentIterator<'data, 'file, R, Coff> { CoffSegmentIterator { file: self, iter: self.common.sections.iter(), @@ -102,12 +110,15 @@ where fn section_by_name_bytes( &'file self, section_name: &[u8], - ) -> Option> { + ) -> Option> { self.sections() .find(|section| section.name_bytes() == Ok(section_name)) } - fn section_by_index(&'file self, index: SectionIndex) -> Result> { + fn section_by_index( + &'file self, + index: SectionIndex, + ) -> Result> { let section = self.common.sections.section(index.0)?; Ok(CoffSection { file: self, @@ -116,21 +127,24 @@ where }) } - fn sections(&'file self) -> CoffSectionIterator<'data, 'file, R> { + fn sections(&'file self) -> CoffSectionIterator<'data, 'file, R, Coff> { CoffSectionIterator { file: self, iter: self.common.sections.iter().enumerate(), } } - fn comdats(&'file self) -> CoffComdatIterator<'data, 'file, R> { + fn comdats(&'file self) -> CoffComdatIterator<'data, 'file, R, Coff> { CoffComdatIterator { file: self, index: 0, } } - fn symbol_by_index(&'file self, index: SymbolIndex) -> Result> { + fn symbol_by_index( + &'file self, + index: SymbolIndex, + ) -> Result> { let symbol = self.common.symbols.symbol(index.0)?; Ok(CoffSymbol { file: &self.common, @@ -139,7 +153,7 @@ where }) } - fn symbols(&'file self) -> CoffSymbolIterator<'data, 'file, R> { + fn symbols(&'file self) -> CoffSymbolIterator<'data, 'file, R, Coff> { CoffSymbolIterator { file: &self.common, index: 0, @@ -147,11 +161,11 @@ where } #[inline] - fn symbol_table(&'file self) -> Option> { + fn symbol_table(&'file self) -> Option> { Some(CoffSymbolTable { file: &self.common }) } - fn dynamic_symbols(&'file self) -> CoffSymbolIterator<'data, 'file, R> { + fn dynamic_symbols(&'file self) -> CoffSymbolIterator<'data, 'file, R, Coff> { CoffSymbolIterator { file: &self.common, // Hack: don't return any. @@ -160,7 +174,7 @@ where } #[inline] - fn dynamic_symbol_table(&'file self) -> Option> { + fn dynamic_symbol_table(&'file self) -> Option> { None } @@ -196,37 +210,51 @@ where fn flags(&self) -> FileFlags { FileFlags::Coff { - characteristics: self.header.characteristics.get(LE), + characteristics: self.header.characteristics(), } } } -impl pe::ImageFileHeader { +/// Read the `class_id` field from an anon object header. +/// +/// This can be used to determine the format of the header. +pub fn anon_object_class_id<'data, R: ReadRef<'data>>(data: R) -> Result { + let header = data + .read_at::(0) + .read_error("Invalid anon object header size or alignment")?; + Ok(header.class_id) +} + +/// A trait for generic access to `ImageFileHeader` and `AnonObjectHeaderBigobj`. +#[allow(missing_docs)] +pub trait CoffHeader: Debug + Pod { + type ImageSymbol: ImageSymbol; + type ImageSymbolBytes: Debug + Pod; + + /// Return true if this type is `AnonObjectHeaderBigobj`. + /// + /// This is a property of the type, not a value in the header data. + fn is_type_bigobj() -> bool; + + fn machine(&self) -> u16; + fn number_of_sections(&self) -> u32; + fn pointer_to_symbol_table(&self) -> u32; + fn number_of_symbols(&self) -> u32; + fn characteristics(&self) -> u16; + /// Read the file header. /// /// `data` must be the entire file data. /// `offset` must be the file header offset. It is updated to point after the optional header, /// which is where the section headers are located. - pub fn parse<'data, R: ReadRef<'data>>(data: R, offset: &mut u64) -> read::Result<&'data Self> { - let header = data - .read::(offset) - .read_error("Invalid COFF file header size or alignment")?; - - // Skip over the optional header. - *offset = offset - .checked_add(header.size_of_optional_header.get(LE).into()) - .read_error("Invalid COFF optional header size")?; - - // TODO: maybe validate that the machine is known? - Ok(header) - } + fn parse<'data, R: ReadRef<'data>>(data: R, offset: &mut u64) -> read::Result<&'data Self>; /// Read the section table. /// /// `data` must be the entire file data. /// `offset` must be after the optional file header. #[inline] - pub fn sections<'data, R: ReadRef<'data>>( + fn sections<'data, R: ReadRef<'data>>( &self, data: R, offset: u64, @@ -238,10 +266,99 @@ impl pe::ImageFileHeader { /// /// `data` must be the entire file data. #[inline] - pub fn symbols<'data, R: ReadRef<'data>>( + fn symbols<'data, R: ReadRef<'data>>( &self, data: R, - ) -> read::Result> { + ) -> read::Result> { SymbolTable::parse(self, data) } } + +impl CoffHeader for pe::ImageFileHeader { + type ImageSymbol = pe::ImageSymbol; + type ImageSymbolBytes = pe::ImageSymbolBytes; + + fn is_type_bigobj() -> bool { + false + } + + fn machine(&self) -> u16 { + self.machine.get(LE) + } + + fn number_of_sections(&self) -> u32 { + self.number_of_sections.get(LE).into() + } + + fn pointer_to_symbol_table(&self) -> u32 { + self.pointer_to_symbol_table.get(LE) + } + + fn number_of_symbols(&self) -> u32 { + self.number_of_symbols.get(LE) + } + + fn characteristics(&self) -> u16 { + self.characteristics.get(LE) + } + + fn parse<'data, R: ReadRef<'data>>(data: R, offset: &mut u64) -> read::Result<&'data Self> { + let header = data + .read::(offset) + .read_error("Invalid COFF file header size or alignment")?; + + // Skip over the optional header. + *offset = offset + .checked_add(header.size_of_optional_header.get(LE).into()) + .read_error("Invalid COFF optional header size")?; + + // TODO: maybe validate that the machine is known? + Ok(header) + } +} + +impl CoffHeader for pe::AnonObjectHeaderBigobj { + type ImageSymbol = pe::ImageSymbolEx; + type ImageSymbolBytes = pe::ImageSymbolExBytes; + + fn is_type_bigobj() -> bool { + true + } + + fn machine(&self) -> u16 { + self.machine.get(LE) + } + + fn number_of_sections(&self) -> u32 { + self.number_of_sections.get(LE) + } + + fn pointer_to_symbol_table(&self) -> u32 { + self.pointer_to_symbol_table.get(LE) + } + + fn number_of_symbols(&self) -> u32 { + self.number_of_symbols.get(LE) + } + + fn characteristics(&self) -> u16 { + 0 + } + + fn parse<'data, R: ReadRef<'data>>(data: R, offset: &mut u64) -> read::Result<&'data Self> { + let header = data + .read::(offset) + .read_error("Invalid COFF bigobj file header size or alignment")?; + + if header.sig1.get(LE) != pe::IMAGE_FILE_MACHINE_UNKNOWN + || header.sig2.get(LE) != 0xffff + || header.version.get(LE) < 2 + || header.class_id != pe::ANON_OBJECT_HEADER_BIGOBJ_CLASS_ID + { + return Err(read::Error("Invalid COFF bigobj header values")); + } + + // TODO: maybe validate that the machine is known? + Ok(header) + } +} diff --git a/src/read/coff/relocation.rs b/src/read/coff/relocation.rs index 9a1fcb61..44d2c68d 100644 --- a/src/read/coff/relocation.rs +++ b/src/read/coff/relocation.rs @@ -7,20 +7,31 @@ use crate::read::{ ReadRef, Relocation, RelocationEncoding, RelocationKind, RelocationTarget, SymbolIndex, }; -use super::CoffFile; +use super::{CoffFile, CoffHeader}; + +/// An iterator over the relocations in a `CoffBigSection`. +pub type CoffBigRelocationIterator<'data, 'file, R = &'data [u8]> = + CoffRelocationIterator<'data, 'file, R, pe::AnonObjectHeaderBigobj>; /// An iterator over the relocations in a `CoffSection`. -pub struct CoffRelocationIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - pub(super) file: &'file CoffFile<'data, R>, +pub struct CoffRelocationIterator< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + pub(super) file: &'file CoffFile<'data, R, Coff>, pub(super) iter: slice::Iter<'data, pe::ImageRelocation>, } -impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffRelocationIterator<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for CoffRelocationIterator<'data, 'file, R, Coff> +{ type Item = (u64, Relocation); fn next(&mut self) -> Option { self.iter.next().map(|relocation| { - let (kind, size, addend) = match self.file.header.machine.get(LE) { + let (kind, size, addend) = match self.file.header.machine() { pe::IMAGE_FILE_MACHINE_ARMNT => match relocation.typ.get(LE) { pe::IMAGE_REL_ARM_ADDR32 => (RelocationKind::Absolute, 32, 0), pe::IMAGE_REL_ARM_ADDR32NB => (RelocationKind::ImageOffset, 32, 0), @@ -84,7 +95,9 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffRelocationIterator<'data, } } -impl<'data, 'file, R: ReadRef<'data>> fmt::Debug for CoffRelocationIterator<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> fmt::Debug + for CoffRelocationIterator<'data, 'file, R, Coff> +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("CoffRelocationIterator").finish() } diff --git a/src/read/coff/section.rs b/src/read/coff/section.rs index 731e37ca..75804034 100644 --- a/src/read/coff/section.rs +++ b/src/read/coff/section.rs @@ -9,7 +9,7 @@ use crate::read::{ ReadRef, Result, SectionFlags, SectionIndex, SectionKind, SegmentFlags, }; -use super::{CoffFile, CoffRelocationIterator}; +use super::{CoffFile, CoffHeader, CoffRelocationIterator}; /// The table of section headers in a COFF or PE file. #[derive(Debug, Default, Clone, Copy)] @@ -22,13 +22,13 @@ impl<'data> SectionTable<'data> { /// /// `data` must be the entire file data. /// `offset` must be after the optional file header. - pub fn parse>( - header: &pe::ImageFileHeader, + pub fn parse>( + header: &Coff, data: R, offset: u64, ) -> Result { let sections = data - .read_slice_at(offset, header.number_of_sections.get(LE).into()) + .read_slice_at(offset, header.number_of_sections() as usize) .read_error("Invalid COFF/PE section headers")?; Ok(SectionTable { sections }) } @@ -104,15 +104,26 @@ impl<'data> SectionTable<'data> { } } +/// An iterator over the loadable sections of a `CoffBigFile`. +pub type CoffBigSegmentIterator<'data, 'file, R = &'data [u8]> = + CoffSegmentIterator<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// An iterator over the loadable sections of a `CoffFile`. #[derive(Debug)] -pub struct CoffSegmentIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - pub(super) file: &'file CoffFile<'data, R>, +pub struct CoffSegmentIterator< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + pub(super) file: &'file CoffFile<'data, R, Coff>, pub(super) iter: slice::Iter<'data, pe::ImageSectionHeader>, } -impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffSegmentIterator<'data, 'file, R> { - type Item = CoffSegment<'data, 'file, R>; +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for CoffSegmentIterator<'data, 'file, R, Coff> +{ + type Item = CoffSegment<'data, 'file, R, Coff>; fn next(&mut self) -> Option { self.iter.next().map(|section| CoffSegment { @@ -122,14 +133,23 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffSegmentIterator<'data, 'f } } +/// A loadable section of a `CoffBigFile`. +pub type CoffBigSegment<'data, 'file, R = &'data [u8]> = + CoffSegment<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// A loadable section of a `CoffFile`. #[derive(Debug)] -pub struct CoffSegment<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - pub(super) file: &'file CoffFile<'data, R>, +pub struct CoffSegment< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + pub(super) file: &'file CoffFile<'data, R, Coff>, pub(super) section: &'data pe::ImageSectionHeader, } -impl<'data, 'file, R: ReadRef<'data>> CoffSegment<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffSegment<'data, 'file, R, Coff> { fn bytes(&self) -> Result<&'data [u8]> { self.section .coff_data(self.file.data) @@ -137,9 +157,14 @@ impl<'data, 'file, R: ReadRef<'data>> CoffSegment<'data, 'file, R> { } } -impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSegment<'data, 'file, R> {} +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed + for CoffSegment<'data, 'file, R, Coff> +{ +} -impl<'data, 'file, R: ReadRef<'data>> ObjectSegment<'data> for CoffSegment<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSegment<'data> + for CoffSegment<'data, 'file, R, Coff> +{ #[inline] fn address(&self) -> u64 { u64::from(self.section.virtual_address.get(LE)) @@ -197,15 +222,26 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSegment<'data> for CoffSegment<'data } } +/// An iterator over the sections of a `CoffBigFile`. +pub type CoffBigSectionIterator<'data, 'file, R = &'data [u8]> = + CoffSectionIterator<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// An iterator over the sections of a `CoffFile`. #[derive(Debug)] -pub struct CoffSectionIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - pub(super) file: &'file CoffFile<'data, R>, +pub struct CoffSectionIterator< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + pub(super) file: &'file CoffFile<'data, R, Coff>, pub(super) iter: iter::Enumerate>, } -impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffSectionIterator<'data, 'file, R> { - type Item = CoffSection<'data, 'file, R>; +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for CoffSectionIterator<'data, 'file, R, Coff> +{ + type Item = CoffSection<'data, 'file, R, Coff>; fn next(&mut self) -> Option { self.iter.next().map(|(index, section)| CoffSection { @@ -216,15 +252,24 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffSectionIterator<'data, 'f } } +/// A section of a `CoffBigFile`. +pub type CoffBigSection<'data, 'file, R = &'data [u8]> = + CoffSection<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// A section of a `CoffFile`. #[derive(Debug)] -pub struct CoffSection<'data, 'file, R: ReadRef<'data> = &'data [u8]> { - pub(super) file: &'file CoffFile<'data, R>, +pub struct CoffSection< + 'data, + 'file, + R: ReadRef<'data> = &'data [u8], + Coff: CoffHeader = pe::ImageFileHeader, +> { + pub(super) file: &'file CoffFile<'data, R, Coff>, pub(super) index: SectionIndex, pub(super) section: &'data pe::ImageSectionHeader, } -impl<'data, 'file, R: ReadRef<'data>> CoffSection<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffSection<'data, 'file, R, Coff> { fn bytes(&self) -> Result<&'data [u8]> { self.section .coff_data(self.file.data) @@ -232,10 +277,15 @@ impl<'data, 'file, R: ReadRef<'data>> CoffSection<'data, 'file, R> { } } -impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSection<'data, 'file, R> {} +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed + for CoffSection<'data, 'file, R, Coff> +{ +} -impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for CoffSection<'data, 'file, R> { - type RelocationIterator = CoffRelocationIterator<'data, 'file, R>; +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSection<'data> + for CoffSection<'data, 'file, R, Coff> +{ + type RelocationIterator = CoffRelocationIterator<'data, 'file, R, Coff>; #[inline] fn index(&self) -> SectionIndex { @@ -315,7 +365,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for CoffSection<'data self.section.kind() } - fn relocations(&self) -> CoffRelocationIterator<'data, 'file, R> { + fn relocations(&self) -> CoffRelocationIterator<'data, 'file, R, Coff> { let relocations = self.section.coff_relocations(self.file.data).unwrap_or(&[]); CoffRelocationIterator { file: self.file, diff --git a/src/read/coff/symbol.rs b/src/read/coff/symbol.rs index e9f65829..1c26d0a9 100644 --- a/src/read/coff/symbol.rs +++ b/src/read/coff/symbol.rs @@ -1,12 +1,13 @@ use alloc::fmt; use alloc::vec::Vec; use core::convert::TryInto; +use core::fmt::Debug; use core::str; -use super::{CoffCommon, SectionTable}; +use super::{CoffCommon, CoffHeader, SectionTable}; use crate::endian::{LittleEndian as LE, U32Bytes}; use crate::pe; -use crate::pod::{bytes_of_slice, Pod}; +use crate::pod::{bytes_of, bytes_of_slice, Pod}; use crate::read::util::StringTable; use crate::read::{ self, Bytes, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, Result, SectionIndex, @@ -17,15 +18,16 @@ use crate::read::{ /// /// Also includes the string table used for the symbol names. #[derive(Debug)] -pub struct SymbolTable<'data, R = &'data [u8]> +pub struct SymbolTable<'data, R = &'data [u8], Coff = pe::ImageFileHeader> where R: ReadRef<'data>, + Coff: CoffHeader, { - symbols: &'data [pe::ImageSymbolBytes], + symbols: &'data [Coff::ImageSymbolBytes], strings: StringTable<'data, R>, } -impl<'data, R: ReadRef<'data>> Default for SymbolTable<'data, R> { +impl<'data, R: ReadRef<'data>, Coff: CoffHeader> Default for SymbolTable<'data, R, Coff> { fn default() -> Self { Self { symbols: &[], @@ -34,14 +36,14 @@ impl<'data, R: ReadRef<'data>> Default for SymbolTable<'data, R> { } } -impl<'data, R: ReadRef<'data>> SymbolTable<'data, R> { +impl<'data, R: ReadRef<'data>, Coff: CoffHeader> SymbolTable<'data, R, Coff> { /// Read the symbol table. - pub fn parse(header: &pe::ImageFileHeader, data: R) -> Result { + pub fn parse(header: &Coff, data: R) -> Result { // The symbol table may not be present. - let mut offset = header.pointer_to_symbol_table.get(LE).into(); + let mut offset = header.pointer_to_symbol_table().into(); let (symbols, strings) = if offset != 0 { let symbols = data - .read_slice(&mut offset, header.number_of_symbols.get(LE) as usize) + .read_slice(&mut offset, header.number_of_symbols() as usize) .read_error("Invalid COFF symbol table offset or size")?; // Note: don't update data when reading length; the length includes itself. @@ -84,7 +86,7 @@ impl<'data, R: ReadRef<'data>> SymbolTable<'data, R> { /// Iterate over the symbols. #[inline] - pub fn iter<'table>(&'table self) -> SymbolIterator<'data, 'table, R> { + pub fn iter<'table>(&'table self) -> SymbolIterator<'data, 'table, R, Coff> { SymbolIterator { symbols: self, index: 0, @@ -93,8 +95,8 @@ impl<'data, R: ReadRef<'data>> SymbolTable<'data, R> { /// Return the symbol table entry at the given index. #[inline] - pub fn symbol(&self, index: usize) -> Result<&'data pe::ImageSymbol> { - self.get::(index, 0) + pub fn symbol(&self, index: usize) -> Result<&'data Coff::ImageSymbol> { + self.get::(index, 0) } /// Return the auxiliary function symbol for the symbol table entry at the given index. @@ -136,13 +138,13 @@ impl<'data, R: ReadRef<'data>> SymbolTable<'data, R> { .checked_add(offset) .and_then(|x| self.symbols.get(x)) .read_error("Invalid COFF symbol index")?; - Bytes(&bytes.0[..]) + Bytes(bytes_of(bytes)) .read() .read_error("Invalid COFF symbol data") } /// Construct a map from addresses to a user-defined map entry. - pub fn map Option>( + pub fn map Option>( &self, f: F, ) -> SymbolMap { @@ -163,109 +165,52 @@ impl<'data, R: ReadRef<'data>> SymbolTable<'data, R> { /// /// Yields the index and symbol structure for each symbol. #[derive(Debug)] -pub struct SymbolIterator<'data, 'table, R = &'data [u8]> +pub struct SymbolIterator<'data, 'table, R = &'data [u8], Coff = pe::ImageFileHeader> where R: ReadRef<'data>, + Coff: CoffHeader, { - symbols: &'table SymbolTable<'data, R>, + symbols: &'table SymbolTable<'data, R, Coff>, index: usize, } -impl<'data, 'table, R: ReadRef<'data>> Iterator for SymbolIterator<'data, 'table, R> { - type Item = (usize, &'data pe::ImageSymbol); +impl<'data, 'table, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for SymbolIterator<'data, 'table, R, Coff> +{ + type Item = (usize, &'data Coff::ImageSymbol); fn next(&mut self) -> Option { let index = self.index; let symbol = self.symbols.symbol(index).ok()?; - self.index += 1 + symbol.number_of_aux_symbols as usize; + self.index += 1 + symbol.number_of_aux_symbols() as usize; Some((index, symbol)) } } -impl pe::ImageSymbol { - /// Parse a COFF symbol name. - /// - /// `strings` must be the string table used for symbol names. - pub fn name<'data, R: ReadRef<'data>>( - &'data self, - strings: StringTable<'data, R>, - ) -> Result<&'data [u8]> { - if self.name[0] == 0 { - // If the name starts with 0 then the last 4 bytes are a string table offset. - let offset = u32::from_le_bytes(self.name[4..8].try_into().unwrap()); - strings - .get(offset) - .read_error("Invalid COFF symbol name offset") - } else { - // The name is inline and padded with nulls. - Ok(match memchr::memchr(b'\0', &self.name) { - Some(end) => &self.name[..end], - None => &self.name[..], - }) - } - } - - /// Return the symbol address. - /// - /// This takes into account the image base and the section address. - pub fn address(&self, image_base: u64, sections: &SectionTable) -> Result { - let section_number = self.section_number.get(LE) as usize; - let section = sections.section(section_number)?; - let virtual_address = u64::from(section.virtual_address.get(LE)); - let value = u64::from(self.value.get(LE)); - Ok(image_base + virtual_address + value) - } - - /// Return true if the symbol is a definition of a function or data object. - pub fn is_definition(&self) -> bool { - let section_number = self.section_number.get(LE); - if section_number == pe::IMAGE_SYM_UNDEFINED { - return false; - } - match self.storage_class { - pe::IMAGE_SYM_CLASS_STATIC => { - // Exclude section symbols. - !(self.value.get(LE) == 0 && self.number_of_aux_symbols > 0) - } - pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => true, - _ => false, - } - } - - /// Return true if the symbol has an auxiliary file name. - pub fn has_aux_file_name(&self) -> bool { - self.number_of_aux_symbols > 0 && self.storage_class == pe::IMAGE_SYM_CLASS_FILE - } - - /// Return true if the symbol has an auxiliary function symbol. - pub fn has_aux_function(&self) -> bool { - self.number_of_aux_symbols > 0 && self.derived_type() == pe::IMAGE_SYM_DTYPE_FUNCTION - } - - /// Return true if the symbol has an auxiliary section symbol. - pub fn has_aux_section(&self) -> bool { - self.number_of_aux_symbols > 0 - && self.storage_class == pe::IMAGE_SYM_CLASS_STATIC - && self.value.get(LE) == 0 - } -} +/// A symbol table of a `CoffBigFile`. +pub type CoffBigSymbolTable<'data, 'file, R = &'data [u8]> = + CoffSymbolTable<'data, 'file, R, pe::AnonObjectHeaderBigobj>; /// A symbol table of a `CoffFile`. #[derive(Debug, Clone, Copy)] -pub struct CoffSymbolTable<'data, 'file, R = &'data [u8]> +pub struct CoffSymbolTable<'data, 'file, R = &'data [u8], Coff = pe::ImageFileHeader> where R: ReadRef<'data>, + Coff: CoffHeader, { - pub(crate) file: &'file CoffCommon<'data, R>, + pub(crate) file: &'file CoffCommon<'data, R, Coff>, } -impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSymbolTable<'data, 'file, R> {} +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed + for CoffSymbolTable<'data, 'file, R, Coff> +{ +} -impl<'data, 'file, R: ReadRef<'data>> ObjectSymbolTable<'data> - for CoffSymbolTable<'data, 'file, R> +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSymbolTable<'data> + for CoffSymbolTable<'data, 'file, R, Coff> { - type Symbol = CoffSymbol<'data, 'file, R>; - type SymbolIterator = CoffSymbolIterator<'data, 'file, R>; + type Symbol = CoffSymbol<'data, 'file, R, Coff>; + type SymbolIterator = CoffSymbolIterator<'data, 'file, R, Coff>; fn symbols(&self) -> Self::SymbolIterator { CoffSymbolIterator { @@ -284,28 +229,37 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbolTable<'data> } } +/// An iterator over the symbols of a `CoffBigFile`. +pub type CoffBigSymbolIterator<'data, 'file, R = &'data [u8]> = + CoffSymbolIterator<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// An iterator over the symbols of a `CoffFile`. -pub struct CoffSymbolIterator<'data, 'file, R = &'data [u8]> +pub struct CoffSymbolIterator<'data, 'file, R = &'data [u8], Coff = pe::ImageFileHeader> where R: ReadRef<'data>, + Coff: CoffHeader, { - pub(crate) file: &'file CoffCommon<'data, R>, + pub(crate) file: &'file CoffCommon<'data, R, Coff>, pub(crate) index: usize, } -impl<'data, 'file, R: ReadRef<'data>> fmt::Debug for CoffSymbolIterator<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> fmt::Debug + for CoffSymbolIterator<'data, 'file, R, Coff> +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("CoffSymbolIterator").finish() } } -impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffSymbolIterator<'data, 'file, R> { - type Item = CoffSymbol<'data, 'file, R>; +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> Iterator + for CoffSymbolIterator<'data, 'file, R, Coff> +{ + type Item = CoffSymbol<'data, 'file, R, Coff>; fn next(&mut self) -> Option { let index = self.index; let symbol = self.file.symbols.symbol(index).ok()?; - self.index += 1 + symbol.number_of_aux_symbols as usize; + self.index += 1 + symbol.number_of_aux_symbols() as usize; Some(CoffSymbol { file: self.file, index: SymbolIndex(index), @@ -314,28 +268,38 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for CoffSymbolIterator<'data, 'fi } } +/// A symbol of a `CoffBigFile`. +pub type CoffBigSymbol<'data, 'file, R = &'data [u8]> = + CoffSymbol<'data, 'file, R, pe::AnonObjectHeaderBigobj>; + /// A symbol of a `CoffFile`. #[derive(Debug, Clone, Copy)] -pub struct CoffSymbol<'data, 'file, R = &'data [u8]> +pub struct CoffSymbol<'data, 'file, R = &'data [u8], Coff = pe::ImageFileHeader> where R: ReadRef<'data>, + Coff: CoffHeader, { - pub(crate) file: &'file CoffCommon<'data, R>, + pub(crate) file: &'file CoffCommon<'data, R, Coff>, pub(crate) index: SymbolIndex, - pub(crate) symbol: &'data pe::ImageSymbol, + pub(crate) symbol: &'data Coff::ImageSymbol, } -impl<'data, 'file, R: ReadRef<'data>> CoffSymbol<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffSymbol<'data, 'file, R, Coff> { #[inline] /// Get the raw `ImageSymbol` struct. - pub fn raw_symbol(&self) -> &'data pe::ImageSymbol { + pub fn raw_symbol(&self) -> &'data Coff::ImageSymbol { self.symbol } } -impl<'data, 'file, R: ReadRef<'data>> read::private::Sealed for CoffSymbol<'data, 'file, R> {} +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed + for CoffSymbol<'data, 'file, R, Coff> +{ +} -impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSymbol<'data> + for CoffSymbol<'data, 'file, R, Coff> +{ #[inline] fn index(&self) -> SymbolIndex { self.index @@ -345,7 +309,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, if self.symbol.has_aux_file_name() { self.file .symbols - .aux_file_name(self.index.0, self.symbol.number_of_aux_symbols) + .aux_file_name(self.index.0, self.symbol.number_of_aux_symbols()) } else { self.symbol.name(self.file.symbols.strings()) } @@ -360,12 +324,12 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, fn address(&self) -> u64 { // Only return an address for storage classes that we know use an address. - match self.symbol.storage_class { + match self.symbol.storage_class() { pe::IMAGE_SYM_CLASS_STATIC | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL | pe::IMAGE_SYM_CLASS_LABEL => {} pe::IMAGE_SYM_CLASS_EXTERNAL => { - if self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED { + if self.symbol.section_number() == pe::IMAGE_SYM_UNDEFINED { // Undefined or common data, neither of which have an address. return 0; } @@ -378,7 +342,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, } fn size(&self) -> u64 { - match self.symbol.storage_class { + match self.symbol.storage_class() { pe::IMAGE_SYM_CLASS_STATIC => { // Section symbols may duplicate the size from the section table. if self.symbol.has_aux_section() { @@ -392,10 +356,10 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, } } pe::IMAGE_SYM_CLASS_EXTERNAL => { - if self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED { + if self.symbol.section_number() == pe::IMAGE_SYM_UNDEFINED { // For undefined symbols, symbol.value is 0 and the size is 0. // For common data, symbol.value is the size. - u64::from(self.symbol.value.get(LE)) + u64::from(self.symbol.value()) } else if self.symbol.has_aux_function() { // Function symbols may have a size. if let Ok(aux) = self.file.symbols.aux_function(self.index.0) { @@ -418,9 +382,9 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, } else { SymbolKind::Data }; - match self.symbol.storage_class { + match self.symbol.storage_class() { pe::IMAGE_SYM_CLASS_STATIC => { - if self.symbol.value.get(LE) == 0 && self.symbol.number_of_aux_symbols > 0 { + if self.symbol.value() == 0 && self.symbol.number_of_aux_symbols() > 0 { SymbolKind::Section } else { derived_kind @@ -435,10 +399,10 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, } fn section(&self) -> SymbolSection { - match self.symbol.section_number.get(LE) { + match self.symbol.section_number() { pe::IMAGE_SYM_UNDEFINED => { - if self.symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL - && self.symbol.value.get(LE) == 0 + if self.symbol.storage_class() == pe::IMAGE_SYM_CLASS_EXTERNAL + && self.symbol.value() == 0 { SymbolSection::Undefined } else { @@ -447,22 +411,22 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, } pe::IMAGE_SYM_ABSOLUTE => SymbolSection::Absolute, pe::IMAGE_SYM_DEBUG => { - if self.symbol.storage_class == pe::IMAGE_SYM_CLASS_FILE { + if self.symbol.storage_class() == pe::IMAGE_SYM_CLASS_FILE { SymbolSection::None } else { SymbolSection::Unknown } } - index if index > 0 => SymbolSection::Section(SectionIndex(index.into())), + index if index > 0 => SymbolSection::Section(SectionIndex(index as usize)), _ => SymbolSection::Unknown, } } #[inline] fn is_undefined(&self) -> bool { - self.symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL - && self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED - && self.symbol.value.get(LE) == 0 + self.symbol.storage_class() == pe::IMAGE_SYM_CLASS_EXTERNAL + && self.symbol.section_number() == pe::IMAGE_SYM_UNDEFINED + && self.symbol.value() == 0 } #[inline] @@ -472,19 +436,19 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, #[inline] fn is_common(&self) -> bool { - self.symbol.storage_class == pe::IMAGE_SYM_CLASS_EXTERNAL - && self.symbol.section_number.get(LE) == pe::IMAGE_SYM_UNDEFINED - && self.symbol.value.get(LE) != 0 + self.symbol.storage_class() == pe::IMAGE_SYM_CLASS_EXTERNAL + && self.symbol.section_number() == pe::IMAGE_SYM_UNDEFINED + && self.symbol.value() != 0 } #[inline] fn is_weak(&self) -> bool { - self.symbol.storage_class == pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL + self.symbol.storage_class() == pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL } #[inline] fn scope(&self) -> SymbolScope { - match self.symbol.storage_class { + match self.symbol.storage_class() { pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => { // TODO: determine if symbol is exported SymbolScope::Linkage @@ -495,7 +459,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, #[inline] fn is_global(&self) -> bool { - match self.symbol.storage_class { + match self.symbol.storage_class() { pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => true, _ => false, } @@ -509,14 +473,17 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, fn flags(&self) -> SymbolFlags { if self.symbol.has_aux_section() { if let Ok(aux) = self.file.symbols.aux_section(self.index.0) { - // TODO: use high_number for bigobj - let number = aux.number.get(LE) as usize; + let number = if Coff::is_type_bigobj() { + u32::from(aux.number.get(LE)) | (u32::from(aux.high_number.get(LE)) << 16) + } else { + u32::from(aux.number.get(LE)) + }; return SymbolFlags::CoffSection { selection: aux.selection, associative_section: if number == 0 { None } else { - Some(SectionIndex(number)) + Some(SectionIndex(number as usize)) }, }; } @@ -524,3 +491,136 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, SymbolFlags::None } } + +/// A trait for generic access to `ImageSymbol` and `ImageSymbolEx`. +#[allow(missing_docs)] +pub trait ImageSymbol: Debug + Pod { + fn raw_name(&self) -> &[u8; 8]; + fn value(&self) -> u32; + fn section_number(&self) -> i32; + fn typ(&self) -> u16; + fn storage_class(&self) -> u8; + fn number_of_aux_symbols(&self) -> u8; + + /// Parse a COFF symbol name. + /// + /// `strings` must be the string table used for symbol names. + fn name<'data, R: ReadRef<'data>>( + &'data self, + strings: StringTable<'data, R>, + ) -> Result<&'data [u8]> { + let name = self.raw_name(); + if name[0] == 0 { + // If the name starts with 0 then the last 4 bytes are a string table offset. + let offset = u32::from_le_bytes(name[4..8].try_into().unwrap()); + strings + .get(offset) + .read_error("Invalid COFF symbol name offset") + } else { + // The name is inline and padded with nulls. + Ok(match memchr::memchr(b'\0', name) { + Some(end) => &name[..end], + None => &name[..], + }) + } + } + + /// Return the symbol address. + /// + /// This takes into account the image base and the section address. + fn address(&self, image_base: u64, sections: &SectionTable) -> Result { + let section_number = self.section_number() as usize; + let section = sections.section(section_number)?; + let virtual_address = u64::from(section.virtual_address.get(LE)); + let value = u64::from(self.value()); + Ok(image_base + virtual_address + value) + } + + /// Return true if the symbol is a definition of a function or data object. + fn is_definition(&self) -> bool { + let section_number = self.section_number(); + if section_number == pe::IMAGE_SYM_UNDEFINED { + return false; + } + match self.storage_class() { + pe::IMAGE_SYM_CLASS_STATIC => { + // Exclude section symbols. + !(self.value() == 0 && self.number_of_aux_symbols() > 0) + } + pe::IMAGE_SYM_CLASS_EXTERNAL | pe::IMAGE_SYM_CLASS_WEAK_EXTERNAL => true, + _ => false, + } + } + + /// Return true if the symbol has an auxiliary file name. + fn has_aux_file_name(&self) -> bool { + self.number_of_aux_symbols() > 0 && self.storage_class() == pe::IMAGE_SYM_CLASS_FILE + } + + /// Return true if the symbol has an auxiliary function symbol. + fn has_aux_function(&self) -> bool { + self.number_of_aux_symbols() > 0 && self.derived_type() == pe::IMAGE_SYM_DTYPE_FUNCTION + } + + /// Return true if the symbol has an auxiliary section symbol. + fn has_aux_section(&self) -> bool { + self.number_of_aux_symbols() > 0 + && self.storage_class() == pe::IMAGE_SYM_CLASS_STATIC + && self.value() == 0 + } + + fn base_type(&self) -> u16 { + self.typ() & pe::N_BTMASK + } + + fn derived_type(&self) -> u16 { + (self.typ() & pe::N_TMASK) >> pe::N_BTSHFT + } +} + +impl ImageSymbol for pe::ImageSymbol { + fn raw_name(&self) -> &[u8; 8] { + &self.name + } + fn value(&self) -> u32 { + self.value.get(LE) + } + fn section_number(&self) -> i32 { + let section_number = self.section_number.get(LE); + if section_number >= pe::IMAGE_SYM_SECTION_MAX { + (section_number as i16) as i32 + } else { + section_number as i32 + } + } + fn typ(&self) -> u16 { + self.typ.get(LE) + } + fn storage_class(&self) -> u8 { + self.storage_class + } + fn number_of_aux_symbols(&self) -> u8 { + self.number_of_aux_symbols + } +} + +impl ImageSymbol for pe::ImageSymbolEx { + fn raw_name(&self) -> &[u8; 8] { + &self.name + } + fn value(&self) -> u32 { + self.value.get(LE) + } + fn section_number(&self) -> i32 { + self.section_number.get(LE) + } + fn typ(&self) -> u16 { + self.typ.get(LE) + } + fn storage_class(&self) -> u8 { + self.storage_class + } + fn number_of_aux_symbols(&self) -> u8 { + self.number_of_aux_symbols + } +} diff --git a/src/read/mod.rs b/src/read/mod.rs index 91a5c05a..d91ba7fa 100644 --- a/src/read/mod.rs +++ b/src/read/mod.rs @@ -151,6 +151,11 @@ pub enum FileKind { /// A COFF object file. #[cfg(feature = "coff")] Coff, + /// A COFF bigobj object file. + /// + /// This supports a larger number of sections. + #[cfg(feature = "coff")] + CoffBig, /// A dyld cache file containing Mach-O images. #[cfg(feature = "macho")] DyldCache, @@ -226,7 +231,7 @@ impl FileKind { #[cfg(feature = "wasm")] [0x00, b'a', b's', b'm', ..] => FileKind::Wasm, #[cfg(feature = "pe")] - [b'M', b'Z', ..] => { + [b'M', b'Z', ..] if offset == 0 => { match pe::optional_header_magic(data) { Ok(crate::pe::IMAGE_NT_OPTIONAL_HDR32_MAGIC) => { FileKind::Pe32 @@ -247,6 +252,13 @@ impl FileKind { | [0x4c, 0x01, ..] // COFF x86-64 | [0x64, 0x86, ..] => FileKind::Coff, + #[cfg(feature = "coff")] + [0x00, 0x00, 0xff, 0xff, 0x02, 0x00, ..] if offset == 0 => { + match coff::anon_object_class_id(data) { + Ok(crate::pe::ANON_OBJECT_HEADER_BIGOBJ_CLASS_ID) => FileKind::CoffBig, + _ => return Err(Error("Unknown anon object file")), + } + } #[cfg(feature = "xcoff")] [0x01, 0xDF, ..] => FileKind::Xcoff32, #[cfg(feature = "xcoff")] diff --git a/src/write/coff.rs b/src/write/coff.rs index f623a116..e9181658 100644 --- a/src/write/coff.rs +++ b/src/write/coff.rs @@ -575,11 +575,11 @@ impl<'a> Object<'a> { let section_number = match symbol.section { SymbolSection::None => { debug_assert_eq!(symbol.kind, SymbolKind::File); - coff::IMAGE_SYM_DEBUG + coff::IMAGE_SYM_DEBUG as u16 } - SymbolSection::Undefined => coff::IMAGE_SYM_UNDEFINED, - SymbolSection::Absolute => coff::IMAGE_SYM_ABSOLUTE, - SymbolSection::Common => coff::IMAGE_SYM_UNDEFINED, + SymbolSection::Undefined => coff::IMAGE_SYM_UNDEFINED as u16, + SymbolSection::Absolute => coff::IMAGE_SYM_ABSOLUTE as u16, + SymbolSection::Common => coff::IMAGE_SYM_UNDEFINED as u16, SymbolSection::Section(id) => id.0 as u16 + 1, }; let typ = if symbol.kind == SymbolKind::Text { diff --git a/testfiles b/testfiles index 1f989e6b..46e8c9af 160000 --- a/testfiles +++ b/testfiles @@ -1 +1 @@ -Subproject commit 1f989e6bb927b8a8dfd00f9e03ead88258cbd820 +Subproject commit 46e8c9afc1ef757de0248e1acdd6699cd12433b1 diff --git a/tests/read/coff.rs b/tests/read/coff.rs index 3e61ec28..dcf3b3c6 100644 --- a/tests/read/coff.rs +++ b/tests/read/coff.rs @@ -8,7 +8,7 @@ fn coff_extended_relocations() { let path_to_obj: PathBuf = ["testfiles", "coff", "relocs_overflow.o"].iter().collect(); let contents = fs::read(&path_to_obj).expect("Could not read relocs_overflow.o"); let file = - read::coff::CoffFile::parse(&contents[..]).expect("Could not parse relocs_overflow.o"); + read::coff::CoffFile::<_>::parse(&contents[..]).expect("Could not parse relocs_overflow.o"); let code_section = file .section_by_name(".text") .expect("Could not find .text section in relocs_overflow.o");