From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/object/src/read/any.rs | 132 ++++---- vendor/object/src/read/coff/comdat.rs | 96 ++++-- vendor/object/src/read/coff/file.rs | 219 ++++++++++--- vendor/object/src/read/coff/relocation.rs | 25 +- vendor/object/src/read/coff/section.rs | 98 ++++-- vendor/object/src/read/coff/symbol.rs | 364 ++++++++++++++-------- vendor/object/src/read/elf/attributes.rs | 303 ++++++++++++++++++ vendor/object/src/read/elf/comdat.rs | 2 - vendor/object/src/read/elf/file.rs | 25 ++ vendor/object/src/read/elf/hash.rs | 4 +- vendor/object/src/read/elf/mod.rs | 3 + vendor/object/src/read/elf/note.rs | 90 +++++- vendor/object/src/read/elf/section.rs | 112 +++++-- vendor/object/src/read/elf/segment.rs | 1 - vendor/object/src/read/elf/symbol.rs | 5 +- vendor/object/src/read/elf/version.rs | 4 +- vendor/object/src/read/macho/dyld_cache.rs | 2 +- vendor/object/src/read/macho/file.rs | 2 +- vendor/object/src/read/macho/load_command.rs | 22 +- vendor/object/src/read/macho/relocation.rs | 17 +- vendor/object/src/read/macho/section.rs | 9 +- vendor/object/src/read/macho/segment.rs | 8 +- vendor/object/src/read/macho/symbol.rs | 2 +- vendor/object/src/read/mod.rs | 39 ++- vendor/object/src/read/pe/data_directory.rs | 2 +- vendor/object/src/read/pe/file.rs | 4 +- vendor/object/src/read/pe/resource.rs | 2 +- vendor/object/src/read/pe/rich.rs | 2 +- vendor/object/src/read/pe/section.rs | 2 - vendor/object/src/read/read_cache.rs | 5 +- vendor/object/src/read/read_ref.rs | 2 +- vendor/object/src/read/traits.rs | 4 +- vendor/object/src/read/util.rs | 42 +++ vendor/object/src/read/wasm.rs | 447 +++++++++++++++------------ vendor/object/src/read/xcoff/comdat.rs | 1 - vendor/object/src/read/xcoff/relocation.rs | 1 - vendor/object/src/read/xcoff/section.rs | 7 +- vendor/object/src/read/xcoff/segment.rs | 2 - vendor/object/src/read/xcoff/symbol.rs | 121 ++++++-- 39 files changed, 1592 insertions(+), 636 deletions(-) create mode 100644 vendor/object/src/read/elf/attributes.rs (limited to 'vendor/object/src/read') diff --git a/vendor/object/src/read/any.rs b/vendor/object/src/read/any.rs index c390b21b6..342ad75fd 100644 --- a/vendor/object/src/read/any.rs +++ b/vendor/object/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")] @@ -457,7 +473,7 @@ where } #[inline] - fn pdb_info(&self) -> Result> { + fn pdb_info(&self) -> Result>> { with_inner!(self.inner, FileInternal, |x| x.pdb_info()) } @@ -476,20 +492,16 @@ where /// An iterator over the segments of a `File`. #[derive(Debug)] -pub struct SegmentIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct SegmentIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: SegmentIteratorInternal<'data, 'file, R>, } #[derive(Debug)] -enum SegmentIteratorInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum SegmentIteratorInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -520,20 +532,16 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for SegmentIterator<'data, 'file, } /// A segment of a `File`. -pub struct Segment<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct Segment<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: SegmentInternal<'data, 'file, R>, } #[derive(Debug)] -enum SegmentInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum SegmentInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -615,21 +623,17 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSegment<'data> for Segment<'data, 'f /// An iterator of the sections of a `File`. #[derive(Debug)] -pub struct SectionIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct SectionIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: SectionIteratorInternal<'data, 'file, R>, } // we wrap our enums in a struct so that they are kept private. #[derive(Debug)] -enum SectionIteratorInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum SectionIteratorInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -660,19 +664,15 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for SectionIterator<'data, 'file, } /// A Section of a File -pub struct Section<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct Section<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: SectionInternal<'data, 'file, R>, } -enum SectionInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum SectionInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -795,20 +795,16 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for Section<'data, 'f /// An iterator of the COMDAT section groups of a `File`. #[derive(Debug)] -pub struct ComdatIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct ComdatIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: ComdatIteratorInternal<'data, 'file, R>, } #[derive(Debug)] -enum ComdatIteratorInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum ComdatIteratorInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -839,19 +835,15 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for ComdatIterator<'data, 'file, } /// A COMDAT section group of a `File`. -pub struct Comdat<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct Comdat<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: ComdatInternal<'data, 'file, R>, } -enum ComdatInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum ComdatInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -917,20 +909,16 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectComdat<'data> for Comdat<'data, 'fil /// An iterator over COMDAT section entries. #[derive(Debug)] -pub struct ComdatSectionIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct ComdatSectionIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: ComdatSectionIteratorInternal<'data, 'file, R>, } #[derive(Debug)] -enum ComdatSectionIteratorInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum ComdatSectionIteratorInternal<'data, 'file, R: ReadRef<'data>> { #[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")] @@ -963,7 +951,6 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for ComdatSectionIterator<'data, #[derive(Debug)] pub struct SymbolTable<'data, 'file, R = &'data [u8]> where - 'data: 'file, R: ReadRef<'data>, { inner: SymbolTableInternal<'data, 'file, R>, @@ -972,11 +959,12 @@ where #[derive(Debug)] enum SymbolTableInternal<'data, 'file, R> where - 'data: 'file, R: ReadRef<'data>, { #[cfg(feature = "coff")] Coff((coff::CoffSymbolTable<'data, 'file, R>, PhantomData)), + #[cfg(feature = "coff")] + CoffBig((coff::CoffBigSymbolTable<'data, 'file, R>, PhantomData)), #[cfg(feature = "elf")] Elf32( ( @@ -1047,7 +1035,6 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbolTable<'data> for SymbolTable<' #[derive(Debug)] pub struct SymbolIterator<'data, 'file, R = &'data [u8]> where - 'data: 'file, R: ReadRef<'data>, { inner: SymbolIteratorInternal<'data, 'file, R>, @@ -1056,11 +1043,12 @@ where #[derive(Debug)] enum SymbolIteratorInternal<'data, 'file, R> where - 'data: 'file, R: ReadRef<'data>, { #[cfg(feature = "coff")] Coff((coff::CoffSymbolIterator<'data, 'file, R>, PhantomData)), + #[cfg(feature = "coff")] + CoffBig((coff::CoffBigSymbolIterator<'data, 'file, R>, PhantomData)), #[cfg(feature = "elf")] Elf32( ( @@ -1125,7 +1113,6 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for SymbolIterator<'data, 'file, /// A symbol table entry. pub struct Symbol<'data, 'file, R = &'data [u8]> where - 'data: 'file, R: ReadRef<'data>, { inner: SymbolInternal<'data, 'file, R>, @@ -1133,11 +1120,12 @@ where enum SymbolInternal<'data, 'file, R> where - 'data: 'file, R: ReadRef<'data>, { #[cfg(feature = "coff")] Coff((coff::CoffSymbol<'data, 'file, R>, PhantomData)), + #[cfg(feature = "coff")] + CoffBig((coff::CoffBigSymbol<'data, 'file, R>, PhantomData)), #[cfg(feature = "elf")] Elf32( ( @@ -1252,7 +1240,7 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for Symbol<'data, 'fil with_inner!(self.inner, SymbolInternal, |x| x.0.is_local()) } - fn flags(&self) -> SymbolFlags { + fn flags(&self) -> SymbolFlags { with_inner!(self.inner, SymbolInternal, |x| x.0.flags()) } } @@ -1261,7 +1249,6 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for Symbol<'data, 'fil #[derive(Debug)] pub struct DynamicRelocationIterator<'data, 'file, R = &'data [u8]> where - 'data: 'file, R: ReadRef<'data>, { inner: DynamicRelocationIteratorInternal<'data, 'file, R>, @@ -1270,7 +1257,6 @@ where #[derive(Debug)] enum DynamicRelocationIteratorInternal<'data, 'file, R> where - 'data: 'file, R: ReadRef<'data>, { #[cfg(feature = "elf")] @@ -1298,20 +1284,16 @@ impl<'data, 'file, R: ReadRef<'data>> Iterator for DynamicRelocationIterator<'da /// An iterator over section relocation entries. #[derive(Debug)] -pub struct SectionRelocationIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> -where - 'data: 'file, -{ +pub struct SectionRelocationIterator<'data, 'file, R: ReadRef<'data> = &'data [u8]> { inner: SectionRelocationIteratorInternal<'data, 'file, R>, } #[derive(Debug)] -enum SectionRelocationIteratorInternal<'data, 'file, R: ReadRef<'data>> -where - 'data: 'file, -{ +enum SectionRelocationIteratorInternal<'data, 'file, R: ReadRef<'data>> { #[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/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 { 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/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 { 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/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.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>( - 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/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 { + 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, } @@ -506,17 +470,20 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSymbol<'data> for CoffSymbol<'data, !self.is_global() } - fn flags(&self) -> SymbolFlags { + 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/vendor/object/src/read/elf/attributes.rs b/vendor/object/src/read/elf/attributes.rs new file mode 100644 index 000000000..6ec535d72 --- /dev/null +++ b/vendor/object/src/read/elf/attributes.rs @@ -0,0 +1,303 @@ +use core::convert::TryInto; + +use crate::elf; +use crate::endian; +use crate::read::{Bytes, Error, ReadError, Result}; + +use super::FileHeader; + +/// An ELF attributes section. +/// +/// This may be a GNU attributes section, or an architecture specific attributes section. +/// +/// An attributes section contains a series of subsections. +#[derive(Debug, Clone)] +pub struct AttributesSection<'data, Elf: FileHeader> { + endian: Elf::Endian, + version: u8, + data: Bytes<'data>, +} + +impl<'data, Elf: FileHeader> AttributesSection<'data, Elf> { + /// Parse an ELF attributes section given the section data. + pub fn new(endian: Elf::Endian, data: &'data [u8]) -> Result { + let mut data = Bytes(data); + + // Skip the version field that is one byte long. + let version = *data + .read::() + .read_error("Invalid ELF attributes section offset or size")?; + + Ok(AttributesSection { + endian, + version, + data, + }) + } + + /// Return the version of the attributes section. + pub fn version(&self) -> u8 { + self.version + } + + /// Return an iterator over the subsections. + pub fn subsections(&self) -> Result> { + // There is currently only one format version. + if self.version != b'A' { + return Err(Error("Unsupported ELF attributes section version")); + } + + Ok(AttributesSubsectionIterator { + endian: self.endian, + data: self.data, + }) + } +} + +/// An iterator over the subsections in an ELF attributes section. +#[derive(Debug, Clone)] +pub struct AttributesSubsectionIterator<'data, Elf: FileHeader> { + endian: Elf::Endian, + data: Bytes<'data>, +} + +impl<'data, Elf: FileHeader> AttributesSubsectionIterator<'data, Elf> { + /// Return the next subsection. + pub fn next(&mut self) -> Result>> { + if self.data.is_empty() { + return Ok(None); + } + + let result = self.parse(); + if result.is_err() { + self.data = Bytes(&[]); + } + result + } + + fn parse(&mut self) -> Result>> { + // First read the subsection length. + let mut data = self.data; + let length = data + .read::>() + .read_error("ELF attributes section is too short")? + .get(self.endian); + + // Now read the entire subsection, updating self.data. + let mut data = self + .data + .read_bytes(length as usize) + .read_error("Invalid ELF attributes subsection length")?; + // Skip the subsection length field. + data.skip(4) + .read_error("Invalid ELF attributes subsection length")?; + + let vendor = data + .read_string() + .read_error("Invalid ELF attributes vendor")?; + + Ok(Some(AttributesSubsection { + endian: self.endian, + length, + vendor, + data, + })) + } +} + +/// A subsection in an ELF attributes section. +/// +/// A subsection is identified by a vendor name. It contains a series of sub-subsections. +#[derive(Debug, Clone)] +pub struct AttributesSubsection<'data, Elf: FileHeader> { + endian: Elf::Endian, + length: u32, + vendor: &'data [u8], + data: Bytes<'data>, +} + +impl<'data, Elf: FileHeader> AttributesSubsection<'data, Elf> { + /// Return the length of the attributes subsection. + pub fn length(&self) -> u32 { + self.length + } + + /// Return the vendor name of the attributes subsection. + pub fn vendor(&self) -> &'data [u8] { + self.vendor + } + + /// Return an iterator over the sub-subsections. + pub fn subsubsections(&self) -> AttributesSubsubsectionIterator<'data, Elf> { + AttributesSubsubsectionIterator { + endian: self.endian, + data: self.data, + } + } +} + +/// An iterator over the sub-subsections in an ELF attributes section. +#[derive(Debug, Clone)] +pub struct AttributesSubsubsectionIterator<'data, Elf: FileHeader> { + endian: Elf::Endian, + data: Bytes<'data>, +} + +impl<'data, Elf: FileHeader> AttributesSubsubsectionIterator<'data, Elf> { + /// Return the next sub-subsection. + pub fn next(&mut self) -> Result>> { + if self.data.is_empty() { + return Ok(None); + } + + let result = self.parse(); + if result.is_err() { + self.data = Bytes(&[]); + } + result + } + + fn parse(&mut self) -> Result>> { + // The format of a sub-section looks like this: + // + // * + // | * 0 * + // | * 0 * + let mut data = self.data; + let tag = *data + .read::() + .read_error("ELF attributes subsection is too short")?; + let length = data + .read::>() + .read_error("ELF attributes subsection is too short")? + .get(self.endian); + + // Now read the entire sub-subsection, updating self.data. + let mut data = self + .data + .read_bytes(length as usize) + .read_error("Invalid ELF attributes sub-subsection length")?; + // Skip the tag and sub-subsection size field. + data.skip(1 + 4) + .read_error("Invalid ELF attributes sub-subsection length")?; + + let indices = if tag == elf::Tag_Section || tag == elf::Tag_Symbol { + data.read_string() + .map(Bytes) + .read_error("Missing ELF attributes sub-subsection indices")? + } else if tag == elf::Tag_File { + Bytes(&[]) + } else { + return Err(Error("Unimplemented ELF attributes sub-subsection tag")); + }; + + Ok(Some(AttributesSubsubsection { + tag, + length, + indices, + data, + })) + } +} + +/// A sub-subsection in an ELF attributes section. +/// +/// A sub-subsection is identified by a tag. It contains an optional series of indices, +/// followed by a series of attributes. +#[derive(Debug, Clone)] +pub struct AttributesSubsubsection<'data> { + tag: u8, + length: u32, + indices: Bytes<'data>, + data: Bytes<'data>, +} + +impl<'data> AttributesSubsubsection<'data> { + /// Return the tag of the attributes sub-subsection. + pub fn tag(&self) -> u8 { + self.tag + } + + /// Return the length of the attributes sub-subsection. + pub fn length(&self) -> u32 { + self.length + } + + /// Return the data containing the indices. + pub fn indices_data(&self) -> &'data [u8] { + self.indices.0 + } + + /// Return the indices. + /// + /// This will be section indices if the tag is `Tag_Section`, + /// or symbol indices if the tag is `Tag_Symbol`, + /// and otherwise it will be empty. + pub fn indices(&self) -> AttributeIndexIterator<'data> { + AttributeIndexIterator { data: self.indices } + } + + /// Return the data containing the attributes. + pub fn attributes_data(&self) -> &'data [u8] { + self.data.0 + } + + /// Return a parser for the data containing the attributes. + pub fn attributes(&self) -> AttributeReader<'data> { + AttributeReader { data: self.data } + } +} + +/// An iterator over the indices in a sub-subsection in an ELF attributes section. +#[derive(Debug, Clone)] +pub struct AttributeIndexIterator<'data> { + data: Bytes<'data>, +} + +impl<'data> AttributeIndexIterator<'data> { + /// Parse the next index. + pub fn next(&mut self) -> Result> { + if self.data.is_empty() { + return Ok(None); + } + let err = "Invalid ELF attribute index"; + self.data + .read_uleb128() + .read_error(err)? + .try_into() + .map_err(|_| ()) + .read_error(err) + .map(Some) + } +} + +/// A parser for the attributes in a sub-subsection in an ELF attributes section. +/// +/// The parser relies on the caller to know the format of the data for each attribute tag. +#[derive(Debug, Clone)] +pub struct AttributeReader<'data> { + data: Bytes<'data>, +} + +impl<'data> AttributeReader<'data> { + /// Parse a tag. + pub fn read_tag(&mut self) -> Result> { + if self.data.is_empty() { + return Ok(None); + } + let err = "Invalid ELF attribute tag"; + self.data.read_uleb128().read_error(err).map(Some) + } + + /// Parse an integer value. + pub fn read_integer(&mut self) -> Result { + let err = "Invalid ELF attribute integer value"; + self.data.read_uleb128().read_error(err) + } + + /// Parse a string value. + pub fn read_string(&mut self) -> Result<&'data [u8]> { + let err = "Invalid ELF attribute string value"; + self.data.read_string().read_error(err) + } +} diff --git a/vendor/object/src/read/elf/comdat.rs b/vendor/object/src/read/elf/comdat.rs index 7cee85bb4..1a2f2f44a 100644 --- a/vendor/object/src/read/elf/comdat.rs +++ b/vendor/object/src/read/elf/comdat.rs @@ -18,7 +18,6 @@ pub type ElfComdatIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> #[derive(Debug)] pub struct ElfComdatIterator<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { @@ -140,7 +139,6 @@ pub type ElfComdatSectionIterator64<'data, 'file, Endian = Endianness, R = &'dat #[derive(Debug)] pub struct ElfComdatSectionIterator<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/elf/file.rs b/vendor/object/src/read/elf/file.rs index 259da7906..aac66e7cc 100644 --- a/vendor/object/src/read/elf/file.rs +++ b/vendor/object/src/read/elf/file.rs @@ -456,6 +456,15 @@ pub trait FileHeader: Debug + Pod { /// This is a property of the type, not a value in the header data. fn is_type_64(&self) -> bool; + /// Return true if this type is a 64-bit header. + /// + /// This is a property of the type, not a value in the header data. + /// + /// This is the same as `is_type_64`, but is non-dispatchable. + fn is_type_64_sized() -> bool + where + Self: Sized; + fn e_ident(&self) -> &elf::Ident; fn e_type(&self, endian: Self::Endian) -> u16; fn e_machine(&self, endian: Self::Endian) -> u16; @@ -724,6 +733,14 @@ impl FileHeader for elf::FileHeader32 { false } + #[inline] + fn is_type_64_sized() -> bool + where + Self: Sized, + { + false + } + #[inline] fn e_ident(&self) -> &elf::Ident { &self.e_ident @@ -813,6 +830,14 @@ impl FileHeader for elf::FileHeader64 { true } + #[inline] + fn is_type_64_sized() -> bool + where + Self: Sized, + { + true + } + #[inline] fn e_ident(&self) -> &elf::Ident { &self.e_ident diff --git a/vendor/object/src/read/elf/hash.rs b/vendor/object/src/read/elf/hash.rs index aa1039ac1..aadbb9208 100644 --- a/vendor/object/src/read/elf/hash.rs +++ b/vendor/object/src/read/elf/hash.rs @@ -45,7 +45,7 @@ impl<'data, Elf: FileHeader> HashTable<'data, Elf> { endian: Elf::Endian, name: &[u8], hash: u32, - version: Option<&Version>, + version: Option<&Version<'_>>, symbols: &SymbolTable<'data, Elf, R>, versions: &VersionTable<'data, Elf>, ) -> Option<(usize, &'data Elf::Sym)> { @@ -160,7 +160,7 @@ impl<'data, Elf: FileHeader> GnuHashTable<'data, Elf> { endian: Elf::Endian, name: &[u8], hash: u32, - version: Option<&Version>, + version: Option<&Version<'_>>, symbols: &SymbolTable<'data, Elf, R>, versions: &VersionTable<'data, Elf>, ) -> Option<(usize, &'data Elf::Sym)> { diff --git a/vendor/object/src/read/elf/mod.rs b/vendor/object/src/read/elf/mod.rs index 5b7d7f9f7..07db6cd66 100644 --- a/vendor/object/src/read/elf/mod.rs +++ b/vendor/object/src/read/elf/mod.rs @@ -37,3 +37,6 @@ pub use hash::*; mod version; pub use version::*; + +mod attributes; +pub use attributes::*; diff --git a/vendor/object/src/read/elf/note.rs b/vendor/object/src/read/elf/note.rs index 34024dbb8..fc5aa7753 100644 --- a/vendor/object/src/read/elf/note.rs +++ b/vendor/object/src/read/elf/note.rs @@ -2,7 +2,7 @@ use core::fmt::Debug; use core::mem; use crate::elf; -use crate::endian; +use crate::endian::{self, U32}; use crate::pod::Pod; use crate::read::util; use crate::read::{self, Bytes, Error, ReadError}; @@ -24,12 +24,15 @@ impl<'data, Elf> NoteIterator<'data, Elf> where Elf: FileHeader, { + /// An iterator over the notes in an ELF section or segment. + /// + /// `align` should be from the `p_align` field of the segment, + /// or the `sh_addralign` field of the section. Supported values are + /// either 4 or 8, but values less than 4 are treated as 4. + /// This matches the behaviour of binutils. + /// /// Returns `Err` if `align` is invalid. - pub(super) fn new( - endian: Elf::Endian, - align: Elf::Word, - data: &'data [u8], - ) -> read::Result { + pub fn new(endian: Elf::Endian, align: Elf::Word, data: &'data [u8]) -> read::Result { let align = match align.into() { 0u64..=4 => 4, 8 => 8, @@ -134,6 +137,24 @@ impl<'data, Elf: FileHeader> Note<'data, Elf> { pub fn desc(&self) -> &'data [u8] { self.desc } + + /// Return an iterator for properties if this note's type is `NT_GNU_PROPERTY_TYPE_0`. + pub fn gnu_properties( + &self, + endian: Elf::Endian, + ) -> Option> { + if self.name() != elf::ELF_NOTE_GNU || self.n_type(endian) != elf::NT_GNU_PROPERTY_TYPE_0 { + return None; + } + // Use the ELF class instead of the section alignment. + // This matches what other parsers do. + let align = if Elf::is_type_64_sized() { 8 } else { 4 }; + Some(GnuPropertyIterator { + endian, + align, + data: Bytes(self.desc), + }) + } } /// A trait for generic access to `NoteHeader32` and `NoteHeader64`. @@ -183,3 +204,60 @@ impl NoteHeader for elf::NoteHeader64 { self.n_type.get(endian) } } + +/// An iterator over the properties in a `NT_GNU_PROPERTY_TYPE_0` note. +#[derive(Debug)] +pub struct GnuPropertyIterator<'data, Endian: endian::Endian> { + endian: Endian, + align: usize, + data: Bytes<'data>, +} + +impl<'data, Endian: endian::Endian> GnuPropertyIterator<'data, Endian> { + /// Returns the next property. + pub fn next(&mut self) -> read::Result>> { + let mut data = self.data; + if data.is_empty() { + return Ok(None); + } + + (|| -> Result<_, ()> { + let pr_type = data.read_at::>(0)?.get(self.endian); + let pr_datasz = data.read_at::>(4)?.get(self.endian) as usize; + let pr_data = data.read_bytes_at(8, pr_datasz)?.0; + data.skip(util::align(8 + pr_datasz, self.align))?; + self.data = data; + Ok(Some(GnuProperty { pr_type, pr_data })) + })() + .read_error("Invalid ELF GNU property") + } +} + +/// A property in a `NT_GNU_PROPERTY_TYPE_0` note. +#[derive(Debug)] +pub struct GnuProperty<'data> { + pr_type: u32, + pr_data: &'data [u8], +} + +impl<'data> GnuProperty<'data> { + /// Return the property type. + /// + /// This is one of the `GNU_PROPERTY_*` constants. + pub fn pr_type(&self) -> u32 { + self.pr_type + } + + /// Return the property data. + pub fn pr_data(&self) -> &'data [u8] { + self.pr_data + } + + /// Parse the property data as an unsigned 32-bit integer. + pub fn data_u32(&self, endian: E) -> read::Result { + Bytes(self.pr_data) + .read_at::>(0) + .read_error("Invalid ELF GNU property data") + .map(|val| val.get(endian)) + } +} diff --git a/vendor/object/src/read/elf/section.rs b/vendor/object/src/read/elf/section.rs index 3f8a08216..df08f9e3e 100644 --- a/vendor/object/src/read/elf/section.rs +++ b/vendor/object/src/read/elf/section.rs @@ -10,8 +10,9 @@ use crate::read::{ }; use super::{ - CompressionHeader, ElfFile, ElfSectionRelocationIterator, FileHeader, GnuHashTable, HashTable, - NoteIterator, RelocationSections, SymbolTable, VerdefIterator, VerneedIterator, VersionTable, + AttributesSection, CompressionHeader, ElfFile, ElfSectionRelocationIterator, FileHeader, + GnuHashTable, HashTable, NoteIterator, RelocationSections, SymbolTable, VerdefIterator, + VerneedIterator, VersionTable, }; /// The table of section headers in an ELF file. @@ -362,7 +363,6 @@ pub type ElfSection64<'data, 'file, Endian = Endianness, R = &'data [u8]> = #[derive(Debug)] pub struct ElfSection<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { @@ -380,32 +380,24 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ElfSection<'data, 'file, fn maybe_compressed(&self) -> read::Result> { let endian = self.file.endian; - if (self.section.sh_flags(endian).into() & u64::from(elf::SHF_COMPRESSED)) == 0 { - return Ok(None); - } - let (section_offset, section_size) = self - .section - .file_range(endian) - .read_error("Invalid ELF compressed section type")?; - let mut offset = section_offset; - let header = self - .file - .data - .read::(&mut offset) - .read_error("Invalid ELF compressed section offset")?; - if header.ch_type(endian) != elf::ELFCOMPRESS_ZLIB { - return Err(Error("Unsupported ELF compression type")); + if let Some((header, offset, compressed_size)) = + self.section.compression(endian, self.file.data)? + { + let format = match header.ch_type(endian) { + elf::ELFCOMPRESS_ZLIB => CompressionFormat::Zlib, + elf::ELFCOMPRESS_ZSTD => CompressionFormat::Zstandard, + _ => return Err(Error("Unsupported ELF compression type")), + }; + let uncompressed_size = header.ch_size(endian).into(); + Ok(Some(CompressedFileRange { + format, + offset, + compressed_size, + uncompressed_size, + })) + } else { + Ok(None) } - let uncompressed_size = header.ch_size(endian).into(); - let compressed_size = section_size - .checked_sub(offset - section_offset) - .read_error("Invalid ELF compressed section size")?; - Ok(Some(CompressedFileRange { - format: CompressionFormat::Zlib, - offset, - compressed_size, - uncompressed_size, - })) } /// Try GNU-style "ZLIB" header decompression. @@ -975,6 +967,70 @@ pub trait SectionHeader: Debug + Pod { let link = SectionIndex(self.sh_link(endian) as usize); Ok(Some((VerneedIterator::new(endian, verneed), link))) } + + /// Return the contents of a `SHT_GNU_ATTRIBUTES` section. + /// + /// Returns `Ok(None)` if the section type is not `SHT_GNU_ATTRIBUTES`. + /// Returns `Err` for invalid values. + fn gnu_attributes<'data, R: ReadRef<'data>>( + &self, + endian: Self::Endian, + data: R, + ) -> read::Result>> { + if self.sh_type(endian) != elf::SHT_GNU_ATTRIBUTES { + return Ok(None); + } + self.attributes(endian, data).map(Some) + } + + /// Parse the contents of the section as attributes. + /// + /// This function does not check whether section type corresponds + /// to a section that contains attributes. + /// + /// Returns `Err` for invalid values. + fn attributes<'data, R: ReadRef<'data>>( + &self, + endian: Self::Endian, + data: R, + ) -> read::Result> { + let data = self.data(endian, data)?; + AttributesSection::new(endian, data) + } + + /// Parse the compression header if present. + /// + /// Returns the header, and the offset and size of the compressed section data + /// in the file. + /// + /// Returns `Ok(None)` if the section flags do not have `SHF_COMPRESSED`. + /// Returns `Err` for invalid values. + fn compression<'data, R: ReadRef<'data>>( + &self, + endian: Self::Endian, + data: R, + ) -> read::Result< + Option<( + &'data ::CompressionHeader, + u64, + u64, + )>, + > { + if (self.sh_flags(endian).into() & u64::from(elf::SHF_COMPRESSED)) == 0 { + return Ok(None); + } + let (section_offset, section_size) = self + .file_range(endian) + .read_error("Invalid ELF compressed section type")?; + let mut offset = section_offset; + let header = data + .read::<::CompressionHeader>(&mut offset) + .read_error("Invalid ELF compressed section offset")?; + let compressed_size = section_size + .checked_sub(offset - section_offset) + .read_error("Invalid ELF compressed section size")?; + Ok(Some((header, offset, compressed_size))) + } } impl SectionHeader for elf::SectionHeader32 { diff --git a/vendor/object/src/read/elf/segment.rs b/vendor/object/src/read/elf/segment.rs index 445893c8d..3972731ec 100644 --- a/vendor/object/src/read/elf/segment.rs +++ b/vendor/object/src/read/elf/segment.rs @@ -57,7 +57,6 @@ pub type ElfSegment64<'data, 'file, Endian = Endianness, R = &'data [u8]> = #[derive(Debug)] pub struct ElfSegment<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/elf/symbol.rs b/vendor/object/src/read/elf/symbol.rs index 5d8d29f27..ac1095705 100644 --- a/vendor/object/src/read/elf/symbol.rs +++ b/vendor/object/src/read/elf/symbol.rs @@ -208,7 +208,6 @@ pub type ElfSymbolTable64<'data, 'file, Endian = Endianness, R = &'data [u8]> = #[derive(Debug, Clone, Copy)] pub struct ElfSymbolTable<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { @@ -256,7 +255,6 @@ pub type ElfSymbolIterator64<'data, 'file, Endian = Endianness, R = &'data [u8]> /// An iterator over the symbols of an `ElfFile`. pub struct ElfSymbolIterator<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { @@ -302,7 +300,6 @@ pub type ElfSymbol64<'data, 'file, Endian = Endianness, R = &'data [u8]> = #[derive(Debug, Clone, Copy)] pub struct ElfSymbol<'data, 'file, Elf, R = &'data [u8]> where - 'data: 'file, Elf: FileHeader, R: ReadRef<'data>, { @@ -430,7 +427,7 @@ impl<'data, 'file, Elf: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> } #[inline] - fn flags(&self) -> SymbolFlags { + fn flags(&self) -> SymbolFlags { SymbolFlags::Elf { st_info: self.symbol.st_info(), st_other: self.symbol.st_other(), diff --git a/vendor/object/src/read/elf/version.rs b/vendor/object/src/read/elf/version.rs index 6d80ba1e3..cc87bbef1 100644 --- a/vendor/object/src/read/elf/version.rs +++ b/vendor/object/src/read/elf/version.rs @@ -183,12 +183,12 @@ impl<'data, Elf: FileHeader> VersionTable<'data, Elf> { .map(Some) } - /// Return true if the given symbol index satisifies the requirements of `need`. + /// Return true if the given symbol index satisfies the requirements of `need`. /// /// Returns false for any error. /// /// Note: this function hasn't been fully tested and is likely to be incomplete. - pub fn matches(&self, endian: Elf::Endian, index: usize, need: Option<&Version>) -> bool { + pub fn matches(&self, endian: Elf::Endian, index: usize, need: Option<&Version<'_>>) -> bool { let version_index = self.version_index(endian, index); let def = match self.version(version_index) { Ok(def) => def, diff --git a/vendor/object/src/read/macho/dyld_cache.rs b/vendor/object/src/read/macho/dyld_cache.rs index 0839ded7d..68f27f549 100644 --- a/vendor/object/src/read/macho/dyld_cache.rs +++ b/vendor/object/src/read/macho/dyld_cache.rs @@ -191,7 +191,7 @@ where /// The file system path of this image. pub fn path(&self) -> Result<&'data str> { let path = self.image_info.path(self.cache.endian, self.cache.data)?; - // The path should always be ascii, so from_utf8 should alway succeed. + // The path should always be ascii, so from_utf8 should always succeed. let path = core::str::from_utf8(path).map_err(|_| Error("Path string not valid utf-8"))?; Ok(path) } diff --git a/vendor/object/src/read/macho/file.rs b/vendor/object/src/read/macho/file.rs index ab8c05757..368c28bbd 100644 --- a/vendor/object/src/read/macho/file.rs +++ b/vendor/object/src/read/macho/file.rs @@ -192,6 +192,7 @@ where match self.header.cputype(self.endian) { macho::CPU_TYPE_ARM => Architecture::Arm, macho::CPU_TYPE_ARM64 => Architecture::Aarch64, + macho::CPU_TYPE_ARM64_32 => Architecture::Aarch64_Ilp32, macho::CPU_TYPE_X86 => Architecture::I386, macho::CPU_TYPE_X86_64 => Architecture::X86_64, macho::CPU_TYPE_MIPS => Architecture::Mips, @@ -531,7 +532,6 @@ pub type MachOComdatSectionIterator64<'data, 'file, Endian = Endianness, R = &'d #[derive(Debug)] pub struct MachOComdatSectionIterator<'data, 'file, Mach, R = &'data [u8]> where - 'data: 'file, Mach: MachHeader, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/macho/load_command.rs b/vendor/object/src/read/macho/load_command.rs index 10daf4ed1..e9af89d8b 100644 --- a/vendor/object/src/read/macho/load_command.rs +++ b/vendor/object/src/read/macho/load_command.rs @@ -1,10 +1,11 @@ use core::marker::PhantomData; +use core::mem; use crate::endian::Endian; use crate::macho; use crate::pod::Pod; use crate::read::macho::{MachHeader, SymbolTable}; -use crate::read::{Bytes, ReadError, ReadRef, Result, StringTable}; +use crate::read::{Bytes, Error, ReadError, ReadRef, Result, StringTable}; /// An iterator over the load commands of a `MachHeader`. #[derive(Debug, Default, Clone, Copy)] @@ -34,6 +35,9 @@ impl<'data, E: Endian> LoadCommandIterator<'data, E> { .read_error("Invalid Mach-O load command header")?; let cmd = header.cmd.get(self.endian); let cmdsize = header.cmdsize.get(self.endian) as usize; + if cmdsize < mem::size_of::>() { + return Err(Error("Invalid Mach-O load command size")); + } let data = self .data .read_bytes(cmdsize) @@ -351,3 +355,19 @@ impl macho::SymtabCommand { Ok(SymbolTable::new(symbols, strings)) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::LittleEndian; + + #[test] + fn cmd_size_invalid() { + let mut commands = LoadCommandIterator::new(LittleEndian, &[0; 8], 10); + assert!(commands.next().is_err()); + let mut commands = LoadCommandIterator::new(LittleEndian, &[0, 0, 0, 0, 7, 0, 0, 0, 0], 10); + assert!(commands.next().is_err()); + let mut commands = LoadCommandIterator::new(LittleEndian, &[0, 0, 0, 0, 8, 0, 0, 0, 0], 10); + assert!(commands.next().is_ok()); + } +} diff --git a/vendor/object/src/read/macho/relocation.rs b/vendor/object/src/read/macho/relocation.rs index 5dd7df896..18e22ef70 100644 --- a/vendor/object/src/read/macho/relocation.rs +++ b/vendor/object/src/read/macho/relocation.rs @@ -19,7 +19,6 @@ pub type MachORelocationIterator64<'data, 'file, Endian = Endianness, R = &'data /// An iterator over the relocations in a `MachOSection`. pub struct MachORelocationIterator<'data, 'file, Mach, R = &'data [u8]> where - 'data: 'file, Mach: MachHeader, R: ReadRef<'data>, { @@ -54,13 +53,15 @@ where relative: reloc.r_pcrel, }, }, - macho::CPU_TYPE_ARM64 => match (reloc.r_type, reloc.r_pcrel) { - (macho::ARM64_RELOC_UNSIGNED, false) => RelocationKind::Absolute, - _ => RelocationKind::MachO { - value: reloc.r_type, - relative: reloc.r_pcrel, - }, - }, + macho::CPU_TYPE_ARM64 | macho::CPU_TYPE_ARM64_32 => { + match (reloc.r_type, reloc.r_pcrel) { + (macho::ARM64_RELOC_UNSIGNED, false) => RelocationKind::Absolute, + _ => RelocationKind::MachO { + value: reloc.r_type, + relative: reloc.r_pcrel, + }, + } + } macho::CPU_TYPE_X86 => match (reloc.r_type, reloc.r_pcrel) { (macho::GENERIC_RELOC_VANILLA, false) => RelocationKind::Absolute, _ => RelocationKind::MachO { diff --git a/vendor/object/src/read/macho/section.rs b/vendor/object/src/read/macho/section.rs index 9e71aa8fd..f43a5b83d 100644 --- a/vendor/object/src/read/macho/section.rs +++ b/vendor/object/src/read/macho/section.rs @@ -21,7 +21,6 @@ pub type MachOSectionIterator64<'data, 'file, Endian = Endianness, R = &'data [u /// An iterator over the sections of a `MachOFile`. pub struct MachOSectionIterator<'data, 'file, Mach, R = &'data [u8]> where - 'data: 'file, Mach: MachHeader, R: ReadRef<'data>, { @@ -66,7 +65,6 @@ pub type MachOSection64<'data, 'file, Endian = Endianness, R = &'data [u8]> = #[derive(Debug)] pub struct MachOSection<'data, 'file, Mach, R = &'data [u8]> where - 'data: 'file, Mach: MachHeader, R: ReadRef<'data>, { @@ -120,7 +118,12 @@ where #[inline] fn align(&self) -> u64 { - 1 << self.internal.section.align(self.file.endian) + let align = self.internal.section.align(self.file.endian); + if align < 64 { + 1 << align + } else { + 0 + } } #[inline] diff --git a/vendor/object/src/read/macho/segment.rs b/vendor/object/src/read/macho/segment.rs index c7eaa6fff..01037e1dd 100644 --- a/vendor/object/src/read/macho/segment.rs +++ b/vendor/object/src/read/macho/segment.rs @@ -19,7 +19,6 @@ pub type MachOSegmentIterator64<'data, 'file, Endian = Endianness, R = &'data [u #[derive(Debug)] pub struct MachOSegmentIterator<'data, 'file, Mach, R = &'data [u8]> where - 'data: 'file, Mach: MachHeader, R: ReadRef<'data>, { @@ -53,7 +52,6 @@ pub type MachOSegment64<'data, 'file, Endian = Endianness, R = &'data [u8]> = #[derive(Debug)] pub struct MachOSegment<'data, 'file, Mach, R = &'data [u8]> where - 'data: 'file, Mach: MachHeader, R: ReadRef<'data>, { @@ -160,7 +158,7 @@ pub trait Segment: Debug + Pod { type Endian: endian::Endian; type Section: Section; - fn from_command(command: LoadCommandData) -> Result>; + fn from_command(command: LoadCommandData<'_, Self::Endian>) -> Result>; fn cmd(&self, endian: Self::Endian) -> u32; fn cmdsize(&self, endian: Self::Endian) -> u32; @@ -219,7 +217,7 @@ impl Segment for macho::SegmentCommand32 { type Endian = Endian; type Section = macho::Section32; - fn from_command(command: LoadCommandData) -> Result> { + fn from_command(command: LoadCommandData<'_, Self::Endian>) -> Result> { command.segment_32() } @@ -263,7 +261,7 @@ impl Segment for macho::SegmentCommand64 { type Endian = Endian; type Section = macho::Section64; - fn from_command(command: LoadCommandData) -> Result> { + fn from_command(command: LoadCommandData<'_, Self::Endian>) -> Result> { command.segment_64() } diff --git a/vendor/object/src/read/macho/symbol.rs b/vendor/object/src/read/macho/symbol.rs index e102c5d0b..ef8852145 100644 --- a/vendor/object/src/read/macho/symbol.rs +++ b/vendor/object/src/read/macho/symbol.rs @@ -388,7 +388,7 @@ where } #[inline] - fn flags(&self) -> SymbolFlags { + fn flags(&self) -> SymbolFlags { let n_desc = self.nlist.n_desc(self.file.endian); SymbolFlags::MachO { n_desc } } diff --git a/vendor/object/src/read/mod.rs b/vendor/object/src/read/mod.rs index 91a5c05a5..0a450359f 100644 --- a/vendor/object/src/read/mod.rs +++ b/vendor/object/src/read/mod.rs @@ -70,7 +70,7 @@ pub struct Error(&'static str); impl fmt::Display for Error { #[inline] - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(self.0) } } @@ -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")] @@ -620,6 +632,10 @@ pub enum CompressionFormat { /// /// Used for ELF compression and GNU compressed debug information. Zlib, + /// Zstandard. + /// + /// Used for ELF compression. + Zstandard, } /// A range in a file that may be compressed. @@ -719,6 +735,25 @@ impl<'data> CompressedData<'data> { .read_error("Invalid zlib compressed data")?; Ok(Cow::Owned(decompressed)) } + #[cfg(feature = "compression")] + CompressionFormat::Zstandard => { + use core::convert::TryInto; + use std::io::Read; + let size = self + .uncompressed_size + .try_into() + .ok() + .read_error("Uncompressed data size is too large.")?; + let mut decompressed = Vec::with_capacity(size); + let mut decoder = ruzstd::StreamingDecoder::new(self.data) + .ok() + .read_error("Invalid zstd compressed data")?; + decoder + .read_to_end(&mut decompressed) + .ok() + .read_error("Invalid zstd compressed data")?; + Ok(Cow::Owned(decompressed)) + } _ => Err(Error("Unsupported compressed data.")), } } diff --git a/vendor/object/src/read/pe/data_directory.rs b/vendor/object/src/read/pe/data_directory.rs index f5d98774e..0e10244bf 100644 --- a/vendor/object/src/read/pe/data_directory.rs +++ b/vendor/object/src/read/pe/data_directory.rs @@ -178,7 +178,7 @@ impl pe::ImageDataDirectory { /// not desirable for all data directories. /// - It uses the `virtual_address` of the directory entry as an address, /// which is not valid for `IMAGE_DIRECTORY_ENTRY_SECURITY`. - pub fn file_range<'data>(&self, sections: &SectionTable<'data>) -> Result<(u32, u32)> { + pub fn file_range(&self, sections: &SectionTable<'_>) -> Result<(u32, u32)> { let (offset, section_size) = sections .pe_file_range_at(self.virtual_address.get(LE)) .read_error("Invalid data dir virtual address")?; diff --git a/vendor/object/src/read/pe/file.rs b/vendor/object/src/read/pe/file.rs index 8dd85131a..0f8ce9f25 100644 --- a/vendor/object/src/read/pe/file.rs +++ b/vendor/object/src/read/pe/file.rs @@ -80,7 +80,7 @@ where } /// Returns information about the rich header of this file (if any). - pub fn rich_header_info(&self) -> Option { + pub fn rich_header_info(&self) -> Option> { RichHeaderInfo::parse(self.data, self.dos_header.nt_headers_offset().into()) } @@ -298,7 +298,7 @@ where Ok(exports) } - fn pdb_info(&self) -> Result> { + fn pdb_info(&self) -> Result>> { let data_dir = match self.data_directory(pe::IMAGE_DIRECTORY_ENTRY_DEBUG) { Some(data_dir) => data_dir, None => return Ok(None), diff --git a/vendor/object/src/read/pe/resource.rs b/vendor/object/src/read/pe/resource.rs index e667f0d98..646eaefaa 100644 --- a/vendor/object/src/read/pe/resource.rs +++ b/vendor/object/src/read/pe/resource.rs @@ -143,7 +143,7 @@ pub struct ResourceName { impl ResourceName { /// Converts to a `String`. - pub fn to_string_lossy(&self, directory: ResourceDirectory) -> Result { + pub fn to_string_lossy(&self, directory: ResourceDirectory<'_>) -> Result { let d = self.data(directory)?.iter().map(|c| c.get(LE)); Ok(char::decode_utf16(d) diff --git a/vendor/object/src/read/pe/rich.rs b/vendor/object/src/read/pe/rich.rs index 687dfc995..33dd039c9 100644 --- a/vendor/object/src/read/pe/rich.rs +++ b/vendor/object/src/read/pe/rich.rs @@ -77,7 +77,7 @@ impl<'data> RichHeaderInfo<'data> { } } -/// Find the offset of the first occurence of needle in the data. +/// Find the offset of the first occurrence of needle in the data. /// /// The offset must have the given alignment. fn memmem(data: &[u8], needle: &[u8], align: usize) -> Option { diff --git a/vendor/object/src/read/pe/section.rs b/vendor/object/src/read/pe/section.rs index 439d42dac..2880e401f 100644 --- a/vendor/object/src/read/pe/section.rs +++ b/vendor/object/src/read/pe/section.rs @@ -143,7 +143,6 @@ pub type PeSectionIterator64<'data, 'file, R = &'data [u8]> = #[derive(Debug)] pub struct PeSectionIterator<'data, 'file, Pe, R = &'data [u8]> where - 'data: 'file, Pe: ImageNtHeaders, R: ReadRef<'data>, { @@ -178,7 +177,6 @@ pub type PeSection64<'data, 'file, R = &'data [u8]> = #[derive(Debug)] pub struct PeSection<'data, 'file, Pe, R = &'data [u8]> where - 'data: 'file, Pe: ImageNtHeaders, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/read_cache.rs b/vendor/object/src/read/read_cache.rs index 19a98a44d..dfce1e1b1 100644 --- a/vendor/object/src/read/read_cache.rs +++ b/vendor/object/src/read/read_cache.rs @@ -77,10 +77,7 @@ impl<'a, R: Read + Seek> ReadRef<'a> for &'a ReadCache { Entry::Occupied(entry) => entry.into_mut(), Entry::Vacant(entry) => { let size = size.try_into().map_err(|_| ())?; - cache - .read - .seek(SeekFrom::Start(offset as u64)) - .map_err(|_| ())?; + cache.read.seek(SeekFrom::Start(offset)).map_err(|_| ())?; let mut bytes = vec![0; size].into_boxed_slice(); cache.read.read_exact(&mut bytes).map_err(|_| ())?; entry.insert(bytes) diff --git a/vendor/object/src/read/read_ref.rs b/vendor/object/src/read/read_ref.rs index 2f547a4e2..a9b425221 100644 --- a/vendor/object/src/read/read_ref.rs +++ b/vendor/object/src/read/read_ref.rs @@ -67,7 +67,7 @@ pub trait ReadRef<'a>: Clone + Copy { /// The default implementation uses `read_bytes`, and returns an error if /// `read_bytes` does not return bytes with the correct alignment for `T`. /// Implementors may want to provide their own implementation that ensures - /// the alignment can be satisified. Alternatively, only use this method with + /// the alignment can be satisfied. Alternatively, only use this method with /// types that do not need alignment (see the `unaligned` feature of this crate). fn read(self, offset: &mut u64) -> Result<&'a T> { let size = mem::size_of::().try_into().map_err(|_| ())?; diff --git a/vendor/object/src/read/traits.rs b/vendor/object/src/read/traits.rs index f1a473e0a..d35b0b0ca 100644 --- a/vendor/object/src/read/traits.rs +++ b/vendor/object/src/read/traits.rs @@ -210,7 +210,7 @@ pub trait Object<'data: 'file, 'file>: read::private::Sealed { /// The filename and GUID from the PE CodeView section #[inline] - fn pdb_info(&self) -> Result> { + fn pdb_info(&self) -> Result>> { Ok(None) } @@ -452,7 +452,7 @@ pub trait ObjectSymbol<'data>: read::private::Sealed { fn is_local(&self) -> bool; /// Symbol flags that are specific to each file format. - fn flags(&self) -> SymbolFlags; + fn flags(&self) -> SymbolFlags; } /// An iterator for files that don't have dynamic relocations. diff --git a/vendor/object/src/read/util.rs b/vendor/object/src/read/util.rs index 842bd6ca1..7c3c65ec9 100644 --- a/vendor/object/src/read/util.rs +++ b/vendor/object/src/read/util.rs @@ -165,6 +165,48 @@ impl<'data> Bytes<'data> { self.skip(offset)?; self.read_string() } + + /// Read an unsigned LEB128 number. + pub fn read_uleb128(&mut self) -> Result { + let mut result = 0; + let mut shift = 0; + + loop { + let byte = *self.read::()?; + if shift == 63 && byte != 0x00 && byte != 0x01 { + return Err(()); + } + result |= u64::from(byte & 0x7f) << shift; + shift += 7; + + if byte & 0x80 == 0 { + return Ok(result); + } + } + } + + /// Read a signed LEB128 number. + pub fn read_sleb128(&mut self) -> Result { + let mut result = 0; + let mut shift = 0; + + loop { + let byte = *self.read::()?; + if shift == 63 && byte != 0x00 && byte != 0x7f { + return Err(()); + } + result |= i64::from(byte & 0x7f) << shift; + shift += 7; + + if byte & 0x80 == 0 { + if shift < 64 && (byte & 0x40) != 0 { + // Sign extend the result. + result |= !0 << shift; + } + return Ok(result); + } + } + } } // Only for Debug impl of `Bytes`. diff --git a/vendor/object/src/read/wasm.rs b/vendor/object/src/read/wasm.rs index 0113f5971..b950ef2b2 100644 --- a/vendor/object/src/read/wasm.rs +++ b/vendor/object/src/read/wasm.rs @@ -6,6 +6,7 @@ use alloc::boxed::Box; use alloc::vec::Vec; use core::marker::PhantomData; +use core::ops::Range; use core::{slice, str}; use wasmparser as wp; @@ -17,27 +18,33 @@ use crate::read::{ SymbolScope, SymbolSection, }; -const SECTION_CUSTOM: usize = 0; -const SECTION_TYPE: usize = 1; -const SECTION_IMPORT: usize = 2; -const SECTION_FUNCTION: usize = 3; -const SECTION_TABLE: usize = 4; -const SECTION_MEMORY: usize = 5; -const SECTION_GLOBAL: usize = 6; -const SECTION_EXPORT: usize = 7; -const SECTION_START: usize = 8; -const SECTION_ELEMENT: usize = 9; -const SECTION_CODE: usize = 10; -const SECTION_DATA: usize = 11; -const SECTION_DATA_COUNT: usize = 12; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(usize)] +enum SectionId { + Custom = 0, + Type = 1, + Import = 2, + Function = 3, + Table = 4, + Memory = 5, + Global = 6, + Export = 7, + Start = 8, + Element = 9, + Code = 10, + Data = 11, + DataCount = 12, +} // Update this constant when adding new section id: -const MAX_SECTION_ID: usize = SECTION_DATA_COUNT; +const MAX_SECTION_ID: usize = SectionId::DataCount as usize; /// A WebAssembly object file. #[derive(Debug)] pub struct WasmFile<'data, R = &'data [u8]> { + data: &'data [u8], + has_memory64: bool, // All sections, including custom sections. - sections: Vec>, + sections: Vec>, // Indices into `sections` of sections with a non-zero id. id_sections: Box<[Option; MAX_SECTION_ID + 1]>, // Whether the file has DWARF information. @@ -49,6 +56,13 @@ pub struct WasmFile<'data, R = &'data [u8]> { marker: PhantomData, } +#[derive(Debug)] +struct SectionHeader<'data> { + id: SectionId, + range: Range, + name: &'data str, +} + #[derive(Clone)] enum LocalFunctionKind { Unknown, @@ -67,9 +81,11 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> { pub fn parse(data: R) -> Result { let len = data.len().read_error("Unknown Wasm file size")?; let data = data.read_bytes_at(0, len).read_error("Wasm read failed")?; - let module = wp::ModuleReader::new(data).read_error("Invalid Wasm header")?; + let parser = wp::Parser::new(0).parse_all(data); let mut file = WasmFile { + data, + has_memory64: false, sections: Vec::new(), id_sections: Default::default(), has_debug_symbols: false, @@ -90,18 +106,23 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> { let mut imported_funcs_count = 0; let mut local_func_kinds = Vec::new(); let mut entry_func_id = None; + let mut code_range_start = 0; + let mut code_func_index = 0; + // One-to-one mapping of globals to their value (if the global is a constant integer). + let mut global_values = Vec::new(); - for section in module { - let section = section.read_error("Invalid Wasm section header")?; + for payload in parser { + let payload = payload.read_error("Invalid Wasm section header")?; - match section.code { - wp::SectionCode::Import => { + match payload { + wp::Payload::TypeSection(section) => { + file.add_section(SectionId::Type, section.range(), ""); + } + wp::Payload::ImportSection(section) => { + file.add_section(SectionId::Import, section.range(), ""); let mut last_module_name = None; - for import in section - .get_import_section_reader() - .read_error("Couldn't read header of the import section")? - { + for import in section { let import = import.read_error("Couldn't read an import item")?; let module_name = import.module; @@ -118,17 +139,20 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> { } let kind = match import.ty { - wp::ImportSectionEntryType::Function(_) => { + wp::TypeRef::Func(_) => { imported_funcs_count += 1; SymbolKind::Text } - wp::ImportSectionEntryType::Table(_) - | wp::ImportSectionEntryType::Memory(_) - | wp::ImportSectionEntryType::Global(_) => SymbolKind::Data, + wp::TypeRef::Memory(memory) => { + file.has_memory64 |= memory.memory64; + SymbolKind::Data + } + wp::TypeRef::Table(_) | wp::TypeRef::Global(_) => SymbolKind::Data, + wp::TypeRef::Tag(_) => SymbolKind::Unknown, }; file.symbols.push(WasmSymbolInternal { - name: import.field, + name: import.name, address: 0, size: 0, kind, @@ -137,28 +161,49 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> { }); } } - wp::SectionCode::Function => { - local_func_kinds = vec![ - LocalFunctionKind::Unknown; - section - .get_function_section_reader() - .read_error("Couldn't read header of the function section")? - .get_count() as usize - ]; + wp::Payload::FunctionSection(section) => { + file.add_section(SectionId::Function, section.range(), ""); + local_func_kinds = + vec![LocalFunctionKind::Unknown; section.into_iter().count()]; + } + wp::Payload::TableSection(section) => { + file.add_section(SectionId::Table, section.range(), ""); + } + wp::Payload::MemorySection(section) => { + file.add_section(SectionId::Memory, section.range(), ""); + for memory in section { + let memory = memory.read_error("Couldn't read a memory item")?; + file.has_memory64 |= memory.memory64; + } + } + wp::Payload::GlobalSection(section) => { + file.add_section(SectionId::Global, section.range(), ""); + for global in section { + let global = global.read_error("Couldn't read a global item")?; + let mut address = None; + if !global.ty.mutable { + // There should be exactly one instruction. + let init = global.init_expr.get_operators_reader().read(); + address = match init.read_error("Couldn't read a global init expr")? { + wp::Operator::I32Const { value } => Some(value as u64), + wp::Operator::I64Const { value } => Some(value as u64), + _ => None, + }; + } + global_values.push(address); + } } - wp::SectionCode::Export => { + wp::Payload::ExportSection(section) => { + file.add_section(SectionId::Export, section.range(), ""); if let Some(main_file_symbol) = main_file_symbol.take() { file.symbols.push(main_file_symbol); } - for export in section - .get_export_section_reader() - .read_error("Couldn't read header of the export section")? - { + for export in section { let export = export.read_error("Couldn't read an export item")?; let (kind, section_idx) = match export.kind { - wp::ExternalKind::Function => { + wp::ExternalKind::Func => { if let Some(local_func_id) = export.index.checked_sub(imported_funcs_count) { @@ -175,133 +220,149 @@ impl<'data, R: ReadRef<'data>> WasmFile<'data, R> { }; symbol_ids.push(file.symbols.len() as u32); } - (SymbolKind::Text, SECTION_CODE) + (SymbolKind::Text, SectionId::Code) } wp::ExternalKind::Table | wp::ExternalKind::Memory - | wp::ExternalKind::Global => (SymbolKind::Data, SECTION_DATA), + | wp::ExternalKind::Global => (SymbolKind::Data, SectionId::Data), + // TODO + wp::ExternalKind::Tag => continue, }; + // Try to guess the symbol address. Rust and C export a global containing + // the address in linear memory of the symbol. + let mut address = 0; + if export.kind == wp::ExternalKind::Global { + if let Some(&Some(x)) = global_values.get(export.index as usize) { + address = x; + } + } + file.symbols.push(WasmSymbolInternal { - name: export.field, - address: 0, + name: export.name, + address, size: 0, kind, - section: SymbolSection::Section(SectionIndex(section_idx)), + section: SymbolSection::Section(SectionIndex(section_idx as usize)), scope: SymbolScope::Dynamic, }); } } - wp::SectionCode::Start => { - entry_func_id = Some( - section - .get_start_section_content() - .read_error("Couldn't read contents of the start section")?, - ); + wp::Payload::StartSection { func, range, .. } => { + file.add_section(SectionId::Start, range, ""); + entry_func_id = Some(func); } - wp::SectionCode::Code => { + wp::Payload::ElementSection(section) => { + file.add_section(SectionId::Element, section.range(), ""); + } + wp::Payload::CodeSectionStart { range, .. } => { + code_range_start = range.start; + file.add_section(SectionId::Code, range, ""); if let Some(main_file_symbol) = main_file_symbol.take() { file.symbols.push(main_file_symbol); } + } + wp::Payload::CodeSectionEntry(body) => { + let i = code_func_index; + code_func_index += 1; - for (i, (body, local_func_kind)) in section - .get_code_section_reader() - .read_error("Couldn't read header of the code section")? - .into_iter() - .zip(&mut local_func_kinds) - .enumerate() - { - let body = body.read_error("Couldn't read a function body")?; - let range = body.range(); - - let address = range.start as u64 - section.range().start as u64; - let size = (range.end - range.start) as u64; - - if entry_func_id == Some(i as u32) { - file.entry = address; - } + let range = body.range(); - match local_func_kind { - LocalFunctionKind::Unknown => { - *local_func_kind = LocalFunctionKind::Local { - symbol_id: file.symbols.len() as u32, - }; - file.symbols.push(WasmSymbolInternal { - name: "", - address, - size, - kind: SymbolKind::Text, - section: SymbolSection::Section(SectionIndex(SECTION_CODE)), - scope: SymbolScope::Compilation, - }); - } - LocalFunctionKind::Exported { symbol_ids } => { - for symbol_id in core::mem::take(symbol_ids) { - let export_symbol = &mut file.symbols[symbol_id as usize]; - export_symbol.address = address; - export_symbol.size = size; - } + let address = range.start as u64 - code_range_start as u64; + let size = (range.end - range.start) as u64; + + if entry_func_id == Some(i as u32) { + file.entry = address; + } + + let local_func_kind = &mut local_func_kinds[i]; + match local_func_kind { + LocalFunctionKind::Unknown => { + *local_func_kind = LocalFunctionKind::Local { + symbol_id: file.symbols.len() as u32, + }; + file.symbols.push(WasmSymbolInternal { + name: "", + address, + size, + kind: SymbolKind::Text, + section: SymbolSection::Section(SectionIndex( + SectionId::Code as usize, + )), + scope: SymbolScope::Compilation, + }); + } + LocalFunctionKind::Exported { symbol_ids } => { + for symbol_id in core::mem::take(symbol_ids) { + let export_symbol = &mut file.symbols[symbol_id as usize]; + export_symbol.address = address; + export_symbol.size = size; } - _ => unreachable!(), } + _ => unreachable!(), } } - wp::SectionCode::Custom { - kind: wp::CustomSectionKind::Name, - .. - } => { - for name in section - .get_name_section_reader() - .read_error("Couldn't read header of the name section")? - { - // TODO: Right now, ill-formed name subsections - // are silently ignored in order to maintain - // compatibility with extended name sections, which - // are not yet supported by the version of - // `wasmparser` currently used. - // A better fix would be to update `wasmparser` to - // the newest version, but this requires - // a major rewrite of this file. - if let Ok(wp::Name::Function(name)) = name { - let mut name_map = name.get_map().read_error( - "Couldn't read header of the function name subsection", - )?; - for _ in 0..name_map.get_count() { - let naming = name_map - .read() - .read_error("Couldn't read a function name")?; - if let Some(local_index) = - naming.index.checked_sub(imported_funcs_count) - { - if let LocalFunctionKind::Local { symbol_id } = - local_func_kinds[local_index as usize] + wp::Payload::DataSection(section) => { + file.add_section(SectionId::Data, section.range(), ""); + } + wp::Payload::DataCountSection { range, .. } => { + file.add_section(SectionId::DataCount, range, ""); + } + wp::Payload::CustomSection(section) => { + let name = section.name(); + let size = section.data().len(); + let mut range = section.range(); + range.start = range.end - size; + file.add_section(SectionId::Custom, range, name); + if name == "name" { + for name in + wp::NameSectionReader::new(section.data(), section.data_offset()) + { + // TODO: Right now, ill-formed name subsections + // are silently ignored in order to maintain + // compatibility with extended name sections, which + // are not yet supported by the version of + // `wasmparser` currently used. + // A better fix would be to update `wasmparser` to + // the newest version, but this requires + // a major rewrite of this file. + if let Ok(wp::Name::Function(name_map)) = name { + for naming in name_map { + let naming = + naming.read_error("Couldn't read a function name")?; + if let Some(local_index) = + naming.index.checked_sub(imported_funcs_count) { - file.symbols[symbol_id as usize].name = naming.name; + if let LocalFunctionKind::Local { symbol_id } = + local_func_kinds[local_index as usize] + { + file.symbols[symbol_id as usize].name = naming.name; + } } } } } + } else if name.starts_with(".debug_") { + file.has_debug_symbols = true; } } - wp::SectionCode::Custom { name, .. } if name.starts_with(".debug_") => { - file.has_debug_symbols = true; - } _ => {} } - - let id = section_code_to_id(section.code); - file.id_sections[id] = Some(file.sections.len()); - - file.sections.push(section); } Ok(file) } + + fn add_section(&mut self, id: SectionId, range: Range, name: &'data str) { + let section = SectionHeader { id, range, name }; + self.id_sections[id as usize] = Some(self.sections.len()); + self.sections.push(section); + } } impl<'data, R> read::private::Sealed for WasmFile<'data, R> {} -impl<'data, 'file, R> Object<'data, 'file> for WasmFile<'data, R> +impl<'data, 'file, R: ReadRef<'data>> Object<'data, 'file> for WasmFile<'data, R> where 'data: 'file, R: 'file, @@ -319,7 +380,11 @@ where #[inline] fn architecture(&self) -> Architecture { - Architecture::Wasm32 + if self.has_memory64 { + Architecture::Wasm64 + } else { + Architecture::Wasm32 + } } #[inline] @@ -329,7 +394,7 @@ where #[inline] fn is_64(&self) -> bool { - false + self.has_memory64 } fn kind(&self) -> ObjectKind { @@ -358,15 +423,15 @@ where .read_error("Invalid Wasm section index")?; let section = self.sections.get(id_section).unwrap(); Ok(WasmSection { + file: self, section, - marker: PhantomData, }) } fn sections(&'file self) -> Self::SectionIterator { WasmSectionIterator { + file: self, sections: self.sections.iter(), - marker: PhantomData, } } @@ -513,8 +578,8 @@ impl<'data, 'file, R> ObjectSegment<'data> for WasmSegment<'data, 'file, R> { /// An iterator over the sections of a `WasmFile`. #[derive(Debug)] pub struct WasmSectionIterator<'data, 'file, R = &'data [u8]> { - sections: slice::Iter<'file, wp::Section<'data>>, - marker: PhantomData, + file: &'file WasmFile<'data, R>, + sections: slice::Iter<'file, SectionHeader<'data>>, } impl<'data, 'file, R> Iterator for WasmSectionIterator<'data, 'file, R> { @@ -523,8 +588,8 @@ impl<'data, 'file, R> Iterator for WasmSectionIterator<'data, 'file, R> { fn next(&mut self) -> Option { let section = self.sections.next()?; Some(WasmSection { + file: self.file, section, - marker: PhantomData, }) } } @@ -532,20 +597,20 @@ impl<'data, 'file, R> Iterator for WasmSectionIterator<'data, 'file, R> { /// A section of a `WasmFile`. #[derive(Debug)] pub struct WasmSection<'data, 'file, R = &'data [u8]> { - section: &'file wp::Section<'data>, - marker: PhantomData, + file: &'file WasmFile<'data, R>, + section: &'file SectionHeader<'data>, } impl<'data, 'file, R> read::private::Sealed for WasmSection<'data, 'file, R> {} -impl<'data, 'file, R> ObjectSection<'data> for WasmSection<'data, 'file, R> { +impl<'data, 'file, R: ReadRef<'data>> ObjectSection<'data> for WasmSection<'data, 'file, R> { type RelocationIterator = WasmRelocationIterator<'data, 'file, R>; #[inline] fn index(&self) -> SectionIndex { // Note that we treat all custom sections as index 0. // This is ok because they are never looked up by index. - SectionIndex(section_code_to_id(self.section.code)) + SectionIndex(self.section.id as usize) } #[inline] @@ -555,7 +620,7 @@ impl<'data, 'file, R> ObjectSection<'data> for WasmSection<'data, 'file, R> { #[inline] fn size(&self) -> u64 { - let range = self.section.range(); + let range = &self.section.range; (range.end - range.start) as u64 } @@ -566,16 +631,17 @@ impl<'data, 'file, R> ObjectSection<'data> for WasmSection<'data, 'file, R> { #[inline] fn file_range(&self) -> Option<(u64, u64)> { - let range = self.section.range(); + let range = &self.section.range; Some((range.start as _, range.end as _)) } #[inline] fn data(&self) -> Result<&'data [u8]> { - let mut reader = self.section.get_binary_reader(); - // TODO: raise a feature request upstream to be able - // to get remaining slice from a BinaryReader directly. - Ok(reader.read_bytes(reader.bytes_remaining()).unwrap()) + let range = &self.section.range; + self.file + .data + .read_bytes_at(range.start as u64, range.end as u64 - range.start as u64) + .read_error("Invalid Wasm section size or offset") } fn data_range(&self, _address: u64, _size: u64) -> Result> { @@ -599,20 +665,20 @@ impl<'data, 'file, R> ObjectSection<'data> for WasmSection<'data, 'file, R> { #[inline] fn name(&self) -> Result<&str> { - Ok(match self.section.code { - wp::SectionCode::Custom { name, .. } => name, - wp::SectionCode::Type => "", - wp::SectionCode::Import => "", - wp::SectionCode::Function => "", - wp::SectionCode::Table => "", - wp::SectionCode::Memory => "", - wp::SectionCode::Global => "", - wp::SectionCode::Export => "", - wp::SectionCode::Start => "", - wp::SectionCode::Element => "", - wp::SectionCode::Code => "", - wp::SectionCode::Data => "", - wp::SectionCode::DataCount => "", + Ok(match self.section.id { + SectionId::Custom => self.section.name, + SectionId::Type => "", + SectionId::Import => "", + SectionId::Function => "", + SectionId::Table => "
", + SectionId::Memory => "", + SectionId::Global => "", + SectionId::Export => "", + SectionId::Start => "", + SectionId::Element => "", + SectionId::Code => "", + SectionId::Data => "", + SectionId::DataCount => "", }) } @@ -628,25 +694,23 @@ impl<'data, 'file, R> ObjectSection<'data> for WasmSection<'data, 'file, R> { #[inline] fn kind(&self) -> SectionKind { - match self.section.code { - wp::SectionCode::Custom { kind, .. } => match kind { - wp::CustomSectionKind::Reloc | wp::CustomSectionKind::Linking => { - SectionKind::Linker - } + match self.section.id { + SectionId::Custom => match self.section.name { + "reloc." | "linking" => SectionKind::Linker, _ => SectionKind::Other, }, - wp::SectionCode::Type => SectionKind::Metadata, - wp::SectionCode::Import => SectionKind::Linker, - wp::SectionCode::Function => SectionKind::Metadata, - wp::SectionCode::Table => SectionKind::UninitializedData, - wp::SectionCode::Memory => SectionKind::UninitializedData, - wp::SectionCode::Global => SectionKind::Data, - wp::SectionCode::Export => SectionKind::Linker, - wp::SectionCode::Start => SectionKind::Linker, - wp::SectionCode::Element => SectionKind::Data, - wp::SectionCode::Code => SectionKind::Text, - wp::SectionCode::Data => SectionKind::Data, - wp::SectionCode::DataCount => SectionKind::UninitializedData, + SectionId::Type => SectionKind::Metadata, + SectionId::Import => SectionKind::Linker, + SectionId::Function => SectionKind::Metadata, + SectionId::Table => SectionKind::UninitializedData, + SectionId::Memory => SectionKind::UninitializedData, + SectionId::Global => SectionKind::Data, + SectionId::Export => SectionKind::Linker, + SectionId::Start => SectionKind::Linker, + SectionId::Element => SectionKind::Data, + SectionId::Code => SectionKind::Text, + SectionId::Data => SectionKind::Data, + SectionId::DataCount => SectionKind::UninitializedData, } } @@ -717,10 +781,7 @@ impl<'data, 'file, R> ObjectComdat<'data> for WasmComdat<'data, 'file, R> { /// An iterator over the sections in a COMDAT section group of a `WasmFile`. #[derive(Debug)] -pub struct WasmComdatSectionIterator<'data, 'file, R = &'data [u8]> -where - 'data: 'file, -{ +pub struct WasmComdatSectionIterator<'data, 'file, R = &'data [u8]> { #[allow(unused)] file: &'file WasmFile<'data, R>, } @@ -869,7 +930,7 @@ impl<'data, 'file> ObjectSymbol<'data> for WasmSymbol<'data, 'file> { } #[inline] - fn flags(&self) -> SymbolFlags { + fn flags(&self) -> SymbolFlags { SymbolFlags::None } } @@ -888,21 +949,3 @@ impl<'data, 'file, R> Iterator for WasmRelocationIterator<'data, 'file, R> { None } } - -fn section_code_to_id(code: wp::SectionCode) -> usize { - match code { - wp::SectionCode::Custom { .. } => SECTION_CUSTOM, - wp::SectionCode::Type => SECTION_TYPE, - wp::SectionCode::Import => SECTION_IMPORT, - wp::SectionCode::Function => SECTION_FUNCTION, - wp::SectionCode::Table => SECTION_TABLE, - wp::SectionCode::Memory => SECTION_MEMORY, - wp::SectionCode::Global => SECTION_GLOBAL, - wp::SectionCode::Export => SECTION_EXPORT, - wp::SectionCode::Start => SECTION_START, - wp::SectionCode::Element => SECTION_ELEMENT, - wp::SectionCode::Code => SECTION_CODE, - wp::SectionCode::Data => SECTION_DATA, - wp::SectionCode::DataCount => SECTION_DATA_COUNT, - } -} diff --git a/vendor/object/src/read/xcoff/comdat.rs b/vendor/object/src/read/xcoff/comdat.rs index eeed2f54d..2b23d1dba 100644 --- a/vendor/object/src/read/xcoff/comdat.rs +++ b/vendor/object/src/read/xcoff/comdat.rs @@ -109,7 +109,6 @@ pub type XcoffComdatSectionIterator64<'data, 'file, R = &'data [u8]> = #[derive(Debug)] pub struct XcoffComdatSectionIterator<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/xcoff/relocation.rs b/vendor/object/src/read/xcoff/relocation.rs index 8107a2e82..78c6acfc7 100644 --- a/vendor/object/src/read/xcoff/relocation.rs +++ b/vendor/object/src/read/xcoff/relocation.rs @@ -19,7 +19,6 @@ pub type XcoffRelocationIterator64<'data, 'file, R = &'data [u8]> = /// An iterator over the relocations in a `XcoffSection`. pub struct XcoffRelocationIterator<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/xcoff/section.rs b/vendor/object/src/read/xcoff/section.rs index 0944e10c8..77453fcd2 100644 --- a/vendor/object/src/read/xcoff/section.rs +++ b/vendor/object/src/read/xcoff/section.rs @@ -36,7 +36,7 @@ where fn next(&mut self) -> Option { self.iter.next().map(|(index, section)| XcoffSection { - index: SectionIndex(index), + index: SectionIndex(index + 1), file: self.file, section, }) @@ -54,7 +54,6 @@ pub type XcoffSection64<'data, 'file, R = &'data [u8]> = #[derive(Debug)] pub struct XcoffSection<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { @@ -252,9 +251,11 @@ where } /// Return the section header at the given index. + /// + /// The index is 1-based. pub fn section(&self, index: SectionIndex) -> read::Result<&'data Xcoff::SectionHeader> { self.sections - .get(index.0) + .get(index.0.wrapping_sub(1)) .read_error("Invalid XCOFF section index") } } diff --git a/vendor/object/src/read/xcoff/segment.rs b/vendor/object/src/read/xcoff/segment.rs index 49969438d..7eca72367 100644 --- a/vendor/object/src/read/xcoff/segment.rs +++ b/vendor/object/src/read/xcoff/segment.rs @@ -19,7 +19,6 @@ pub type XcoffSegmentIterator64<'data, 'file, R = &'data [u8]> = #[derive(Debug)] pub struct XcoffSegmentIterator<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { @@ -50,7 +49,6 @@ pub type XcoffSegment64<'data, 'file, R = &'data [u8]> = #[derive(Debug)] pub struct XcoffSegment<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { diff --git a/vendor/object/src/read/xcoff/symbol.rs b/vendor/object/src/read/xcoff/symbol.rs index 6738ad171..7ce215fac 100644 --- a/vendor/object/src/read/xcoff/symbol.rs +++ b/vendor/object/src/read/xcoff/symbol.rs @@ -5,9 +5,9 @@ use core::marker::PhantomData; use core::str; use crate::endian::{BigEndian as BE, U32Bytes}; -use crate::pod::Pod; +use crate::pod::{bytes_of, Pod}; use crate::read::util::StringTable; -use crate::{bytes_of, xcoff, Object, ObjectSection, SectionKind}; +use crate::xcoff; use crate::read::{ self, Bytes, Error, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, Result, SectionIndex, @@ -95,10 +95,10 @@ where self.get::(index, 0) } - /// Return the file auxiliary symbol. - pub fn aux_file(&self, index: usize) -> Result<&'data Xcoff::FileAux> { + /// Return a file auxiliary symbol. + pub fn aux_file(&self, index: usize, offset: usize) -> Result<&'data Xcoff::FileAux> { debug_assert!(self.symbol(index)?.has_aux_file()); - let aux_file = self.get::(index, 1)?; + let aux_file = self.get::(index, offset)?; if let Some(aux_type) = aux_file.x_auxtype() { if aux_type != xcoff::AUX_FILE { return Err(Error("Invalid index for file auxiliary symbol.")); @@ -145,7 +145,6 @@ pub type XcoffSymbolTable64<'data, 'file, R = &'data [u8]> = #[derive(Debug, Clone, Copy)] pub struct XcoffSymbolTable<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { @@ -193,7 +192,6 @@ pub type XcoffSymbolIterator64<'data, 'file, R = &'data [u8]> = /// An iterator over the symbols of an `XcoffFile`. pub struct XcoffSymbolIterator<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { @@ -240,7 +238,6 @@ pub type XcoffSymbol64<'data, 'file, R = &'data [u8]> = #[derive(Debug, Clone, Copy)] pub struct XcoffSymbol<'data, 'file, Xcoff, R = &'data [u8]> where - 'data: 'file, Xcoff: FileHeader, R: ReadRef<'data>, { @@ -264,7 +261,14 @@ impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> } fn name_bytes(&self) -> Result<&'data [u8]> { - self.symbol.name(self.symbols.strings) + if self.symbol.has_aux_file() { + // By convention the file name is in the first auxiliary entry. + self.symbols + .aux_file(self.index.0, 1)? + .fname(self.symbols.strings) + } else { + self.symbol.name(self.symbols.strings) + } } fn name(&self) -> Result<&'data str> { @@ -283,7 +287,8 @@ impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> | xcoff::C_HIDEXT | xcoff::C_FCN | xcoff::C_BLOCK - | xcoff::C_STAT => self.symbol.n_value().into(), + | xcoff::C_STAT + | xcoff::C_INFO => self.symbol.n_value().into(), _ => 0, } } @@ -300,32 +305,46 @@ impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> { let sym_type = aux_csect.sym_type() & 0x07; if sym_type == xcoff::XTY_SD || sym_type == xcoff::XTY_CM { - aux_csect.x_scnlen() - } else { - 0 + return aux_csect.x_scnlen(); } - } else { - 0 } - } else { - 0 } + 0 } fn kind(&self) -> SymbolKind { + if self.symbol.has_aux_csect() { + if let Ok(aux_csect) = self + .file + .symbols + .aux_csect(self.index.0, self.symbol.n_numaux() as usize) + { + let sym_type = aux_csect.sym_type() & 0x07; + if sym_type == xcoff::XTY_SD || sym_type == xcoff::XTY_CM { + return match aux_csect.x_smclas() { + xcoff::XMC_PR | xcoff::XMC_GL => SymbolKind::Text, + xcoff::XMC_RO | xcoff::XMC_RW | xcoff::XMC_TD | xcoff::XMC_BS => { + SymbolKind::Data + } + xcoff::XMC_TL | xcoff::XMC_UL => SymbolKind::Tls, + xcoff::XMC_DS | xcoff::XMC_TC0 | xcoff::XMC_TC => { + // `Metadata` might be a better kind for these if we had it. + SymbolKind::Data + } + _ => SymbolKind::Unknown, + }; + } else if sym_type == xcoff::XTY_LD { + // A function entry point. Neither `Text` nor `Label` are a good fit for this. + return SymbolKind::Text; + } else if sym_type == xcoff::XTY_ER { + return SymbolKind::Unknown; + } + } + } match self.symbol.n_sclass() { - xcoff::C_FILE => SymbolKind::File, xcoff::C_NULL => SymbolKind::Null, - _ => self - .file - .section_by_index(SectionIndex((self.symbol.n_scnum() - 1) as usize)) - .map(|section| match section.kind() { - SectionKind::Data | SectionKind::UninitializedData => SymbolKind::Data, - SectionKind::UninitializedTls | SectionKind::Tls => SymbolKind::Tls, - SectionKind::Text => SymbolKind::Text, - _ => SymbolKind::Unknown, - }) - .unwrap_or(SymbolKind::Unknown), + xcoff::C_FILE => SymbolKind::File, + _ => SymbolKind::Unknown, } } @@ -407,8 +426,29 @@ impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> ObjectSymbol<'data> } #[inline] - fn flags(&self) -> SymbolFlags { - SymbolFlags::None + fn flags(&self) -> SymbolFlags { + let mut x_smtyp = 0; + let mut x_smclas = 0; + let mut containing_csect = None; + if self.symbol.has_aux_csect() { + if let Ok(aux_csect) = self + .file + .symbols + .aux_csect(self.index.0, self.symbol.n_numaux() as usize) + { + x_smtyp = aux_csect.x_smtyp(); + x_smclas = aux_csect.x_smclas(); + if x_smtyp == xcoff::XTY_LD { + containing_csect = Some(SymbolIndex(aux_csect.x_scnlen() as usize)) + } + } + } + SymbolFlags::Xcoff { + n_sclass: self.symbol.n_sclass(), + x_smtyp, + x_smclas, + containing_csect, + } } } @@ -536,6 +576,27 @@ pub trait FileAux: Debug + Pod { fn x_fname(&self) -> &[u8; 8]; fn x_ftype(&self) -> u8; fn x_auxtype(&self) -> Option; + + /// Parse the x_fname field, which may be an inline string or a string table offset. + fn fname<'data, R: ReadRef<'data>>( + &'data self, + strings: StringTable<'data, R>, + ) -> Result<&'data [u8]> { + let x_fname = self.x_fname(); + if x_fname[0] == 0 { + // If the name starts with 0 then the last 4 bytes are a string table offset. + let offset = u32::from_be_bytes(x_fname[4..8].try_into().unwrap()); + strings + .get(offset) + .read_error("Invalid XCOFF symbol name offset") + } else { + // The name is inline and padded with nulls. + Ok(match memchr::memchr(b'\0', x_fname) { + Some(end) => &x_fname[..end], + None => x_fname, + }) + } + } } impl FileAux for xcoff::FileAux64 { -- cgit v1.2.3