diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:42 +0000 |
commit | 837b550238aa671a591ccf282dddeab29cadb206 (patch) | |
tree | 914b6b8862bace72bd3245ca184d374b08d8a672 /vendor/object/src/read/coff | |
parent | Adding debian version 1.70.0+dfsg2-1. (diff) | |
download | rustc-837b550238aa671a591ccf282dddeab29cadb206.tar.xz rustc-837b550238aa671a591ccf282dddeab29cadb206.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/object/src/read/coff')
-rw-r--r-- | vendor/object/src/read/coff/comdat.rs | 96 | ||||
-rw-r--r-- | vendor/object/src/read/coff/file.rs | 219 | ||||
-rw-r--r-- | vendor/object/src/read/coff/relocation.rs | 25 | ||||
-rw-r--r-- | vendor/object/src/read/coff/section.rs | 98 | ||||
-rw-r--r-- | vendor/object/src/read/coff/symbol.rs | 364 |
5 files changed, 561 insertions, 241 deletions
diff --git a/vendor/object/src/read/coff/comdat.rs b/vendor/object/src/read/coff/comdat.rs index 3be69ecc2..22e061a23 100644 --- a/vendor/object/src/read/coff/comdat.rs +++ b/vendor/object/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<Self::Item> { 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<CoffComdat<'data, 'file, R>> { + ) -> Option<CoffComdat<'data, 'file, R, Coff>> { // 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<Self::Item> { @@ -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/vendor/object/src/read/coff/file.rs b/vendor/object/src/read/coff/file.rs index c6cc9f846..4219f8f02 100644 --- a/vendor/object/src/read/coff/file.rs +++ b/vendor/object/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<Self> { 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<CoffSection<'data, 'file, R>> { + ) -> Option<CoffSection<'data, 'file, R, Coff>> { self.sections() .find(|section| section.name_bytes() == Ok(section_name)) } - fn section_by_index(&'file self, index: SectionIndex) -> Result<CoffSection<'data, 'file, R>> { + fn section_by_index( + &'file self, + index: SectionIndex, + ) -> Result<CoffSection<'data, 'file, R, Coff>> { 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<CoffSymbol<'data, 'file, R>> { + fn symbol_by_index( + &'file self, + index: SymbolIndex, + ) -> Result<CoffSymbol<'data, 'file, R, Coff>> { 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<CoffSymbolTable<'data, 'file, R>> { + fn symbol_table(&'file self) -> Option<CoffSymbolTable<'data, 'file, R, Coff>> { 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<CoffSymbolTable<'data, 'file, R>> { + fn dynamic_symbol_table(&'file self) -> Option<CoffSymbolTable<'data, 'file, R, Coff>> { 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<pe::ClsId> { + let header = data + .read_at::<pe::AnonObjectHeader>(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::<pe::ImageFileHeader>(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<SymbolTable<'data, R>> { + ) -> read::Result<SymbolTable<'data, R, Self>> { 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::<pe::ImageFileHeader>(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::<pe::AnonObjectHeaderBigobj>(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/vendor/object/src/read/coff/relocation.rs b/vendor/object/src/read/coff/relocation.rs index 9a1fcb618..44d2c68d0 100644 --- a/vendor/object/src/read/coff/relocation.rs +++ b/vendor/object/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::Item> { 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/vendor/object/src/read/coff/section.rs b/vendor/object/src/read/coff/section.rs index 731e37ca8..75804034b 100644 --- a/vendor/object/src/read/coff/section.rs +++ b/vendor/object/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<R: ReadRef<'data>>( - header: &pe::ImageFileHeader, + pub fn parse<Coff: CoffHeader, R: ReadRef<'data>>( + header: &Coff, data: R, offset: u64, ) -> Result<Self> { 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::Item> { 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<slice::Iter<'data, pe::ImageSectionHeader>>, } -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::Item> { 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/vendor/object/src/read/coff/symbol.rs b/vendor/object/src/read/coff/symbol.rs index 217e38fca..e95468d7e 100644 --- a/vendor/object/src/read/coff/symbol.rs +++ b/vendor/object/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<Self> { + pub fn parse(header: &Coff, data: R) -> Result<Self> { // 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::<pe::ImageSymbol>(index, 0) + pub fn symbol(&self, index: usize) -> Result<&'data Coff::ImageSymbol> { + self.get::<Coff::ImageSymbol>(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<Entry: SymbolMapEntry, F: Fn(&'data pe::ImageSymbol) -> Option<Entry>>( + pub fn map<Entry: SymbolMapEntry, F: Fn(&'data Coff::ImageSymbol) -> Option<Entry>>( &self, f: F, ) -> SymbolMap<Entry> { @@ -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<Self::Item> { 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<u64> { - 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<Self::Item> { 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, } @@ -506,17 +470,20 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, !self.is_global() } - fn flags(&self) -> SymbolFlags<SectionIndex> { + fn flags(&self) -> SymbolFlags<SectionIndex, SymbolIndex> { 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<u64> { + 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 + } +} |