Skip to content

Commit

Permalink
read: add Object::file_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc committed May 25, 2023
1 parent ffe0cee commit 4cd5215
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 20 deletions.
38 changes: 38 additions & 0 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,38 @@ macro_rules! next_inner {
};
}

/// TODO
#[derive(Debug)]
#[non_exhaustive]
#[allow(missing_docs)]
pub enum FileRef<'data, 'file, Endian: crate::Endian = Endianness, R: ReadRef<'data> = &'data [u8]>
{
#[cfg(feature = "coff")]
Coff(&'file coff::CoffFile<'data, R>),
#[cfg(feature = "coff")]
CoffBig(&'file coff::CoffBigFile<'data, R>),
#[cfg(feature = "elf")]
Elf32(&'file elf::ElfFile32<'data, Endian, R>),
#[cfg(feature = "elf")]
Elf64(&'file elf::ElfFile64<'data, Endian, R>),
#[cfg(feature = "macho")]
MachO32(&'file macho::MachOFile32<'data, Endian, R>),
#[cfg(feature = "macho")]
MachO64(&'file macho::MachOFile64<'data, Endian, R>),
#[cfg(feature = "pe")]
Pe32(&'file pe::PeFile32<'data, R>),
#[cfg(feature = "pe")]
Pe64(&'file pe::PeFile64<'data, R>),
#[cfg(feature = "wasm")]
Wasm(&'file wasm::WasmFile<'data, R>),
#[cfg(feature = "xcoff")]
Xcoff32(&'file xcoff::XcoffFile32<'data, R>),
#[cfg(feature = "xcoff")]
Xcoff64(&'file xcoff::XcoffFile64<'data, R>),
#[doc(hidden)]
None(PhantomData<Endian>),
}

/// An object file.
///
/// Most functionality is provided by the `Object` trait implementation.
Expand Down Expand Up @@ -313,6 +345,8 @@ where
'data: 'file,
R: 'file + ReadRef<'data>,
{
type ReadRef = R;
type Endian = Endianness;
type Segment = Segment<'data, 'file, R>;
type SegmentIterator = SegmentIterator<'data, 'file, R>;
type Section = Section<'data, 'file, R>;
Expand All @@ -324,6 +358,10 @@ where
type SymbolTable = SymbolTable<'data, 'file, R>;
type DynamicRelocationIterator = DynamicRelocationIterator<'data, 'file, R>;

fn file_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
map_inner!(self.inner, FileInternal, FileRef, |x| x)
}

fn architecture(&self) -> Architecture {
with_inner!(self.inner, FileInternal, |x| x.architecture())
}
Expand Down
29 changes: 26 additions & 3 deletions src/read/coff/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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,
self, Architecture, Export, FileFlags, FileRef, Import, NoDynamicRelocationIterator, Object,
ObjectKind, ObjectSection, ReadError, ReadRef, Result, SectionIndex, SymbolIndex,
};
use crate::{pe, LittleEndian as LE, Pod};
use crate::{pe, Endianness, LittleEndian as LE, Pod};

use super::{
CoffComdat, CoffComdatIterator, CoffSection, CoffSectionIterator, CoffSegment,
Expand Down Expand Up @@ -64,6 +64,8 @@ where
R: 'file + ReadRef<'data>,
Coff: CoffHeader,
{
type ReadRef = R;
type Endian = Endianness;
type Segment = CoffSegment<'data, 'file, R, Coff>;
type SegmentIterator = CoffSegmentIterator<'data, 'file, R, Coff>;
type Section = CoffSection<'data, 'file, R, Coff>;
Expand All @@ -75,6 +77,10 @@ where
type SymbolTable = CoffSymbolTable<'data, 'file, R, Coff>;
type DynamicRelocationIterator = NoDynamicRelocationIterator;

fn file_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Coff::file_ref(self)
}

fn architecture(&self) -> Architecture {
match self.header.machine() {
pe::IMAGE_FILE_MACHINE_ARMNT => Architecture::Arm,
Expand Down Expand Up @@ -231,6 +237,11 @@ pub trait CoffHeader: Debug + Pod {
type ImageSymbol: ImageSymbol;
type ImageSymbolBytes: Debug + Pod;

/// TODO
fn file_ref<'data, 'file, Endian: crate::Endian, R: ReadRef<'data>>(
file: &'file CoffFile<'data, R, Self>,
) -> FileRef<'data, 'file, Endian, R>;

/// Return true if this type is `AnonObjectHeaderBigobj`.
///
/// This is a property of the type, not a value in the header data.
Expand Down Expand Up @@ -278,6 +289,12 @@ impl CoffHeader for pe::ImageFileHeader {
type ImageSymbol = pe::ImageSymbol;
type ImageSymbolBytes = pe::ImageSymbolBytes;

fn file_ref<'data, 'file, Endian: crate::Endian, R: ReadRef<'data>>(
file: &'file CoffFile<'data, R, Self>,
) -> FileRef<'data, 'file, Endian, R> {
FileRef::Coff(file)
}

fn is_type_bigobj() -> bool {
false
}
Expand Down Expand Up @@ -321,6 +338,12 @@ impl CoffHeader for pe::AnonObjectHeaderBigobj {
type ImageSymbol = pe::ImageSymbolEx;
type ImageSymbolBytes = pe::ImageSymbolExBytes;

fn file_ref<'data, 'file, Endian: crate::Endian, R: ReadRef<'data>>(
file: &'file CoffFile<'data, R, Self>,
) -> FileRef<'data, 'file, Endian, R> {
FileRef::CoffBig(file)
}

fn is_type_bigobj() -> bool {
true
}
Expand Down
27 changes: 26 additions & 1 deletion src/read/elf/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::fmt::Debug;
use core::mem;

use crate::read::{
self, util, Architecture, ByteString, Bytes, Error, Export, FileFlags, Import, Object,
self, util, Architecture, ByteString, Bytes, Error, Export, FileFlags, FileRef, Import, Object,
ObjectKind, ReadError, ReadRef, SectionIndex, StringTable, SymbolIndex,
};
use crate::{elf, endian, Endian, Endianness, Pod, U32};
Expand Down Expand Up @@ -140,6 +140,8 @@ where
Elf: FileHeader,
R: 'file + ReadRef<'data>,
{
type ReadRef = R;
type Endian = Elf::Endian;
type Segment = ElfSegment<'data, 'file, Elf, R>;
type SegmentIterator = ElfSegmentIterator<'data, 'file, Elf, R>;
type Section = ElfSection<'data, 'file, Elf, R>;
Expand All @@ -151,6 +153,10 @@ where
type SymbolTable = ElfSymbolTable<'data, 'file, Elf, R>;
type DynamicRelocationIterator = ElfDynamicRelocationIterator<'data, 'file, Elf, R>;

fn file_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Elf::file_ref(self)
}

fn architecture(&self) -> Architecture {
match (
self.header.e_machine(self.endian),
Expand Down Expand Up @@ -451,6 +457,11 @@ pub trait FileHeader: Debug + Pod {
type Rel: Rel<Endian = Self::Endian, Word = Self::Word>;
type Rela: Rela<Endian = Self::Endian, Word = Self::Word> + From<Self::Rel>;

/// TODO
fn file_ref<'data, 'file, R: ReadRef<'data>>(
file: &'file ElfFile<'data, Self, R>,
) -> FileRef<'data, 'file, Self::Endian, R>;

/// Return true if this type is a 64-bit header.
///
/// This is a property of the type, not a value in the header data.
Expand Down Expand Up @@ -728,6 +739,13 @@ impl<Endian: endian::Endian> FileHeader for elf::FileHeader32<Endian> {
type Rel = elf::Rel32<Endian>;
type Rela = elf::Rela32<Endian>;

/// TODO
fn file_ref<'data, 'file, R: ReadRef<'data>>(
file: &'file ElfFile<'data, Self, R>,
) -> FileRef<'data, 'file, Self::Endian, R> {
FileRef::Elf32(file)
}

#[inline]
fn is_type_64(&self) -> bool {
false
Expand Down Expand Up @@ -825,6 +843,13 @@ impl<Endian: endian::Endian> FileHeader for elf::FileHeader64<Endian> {
type Rel = elf::Rel64<Endian>;
type Rela = elf::Rela64<Endian>;

/// TODO
fn file_ref<'data, 'file, R: ReadRef<'data>>(
file: &'file ElfFile<'data, Self, R>,
) -> FileRef<'data, 'file, Self::Endian, R> {
FileRef::Elf64(file)
}

#[inline]
fn is_type_64(&self) -> bool {
true
Expand Down
29 changes: 26 additions & 3 deletions src/read/macho/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use core::fmt::Debug;
use core::{mem, str};

use crate::read::{
self, Architecture, ComdatKind, Error, Export, FileFlags, Import, NoDynamicRelocationIterator,
Object, ObjectComdat, ObjectKind, ObjectMap, ObjectSection, ReadError, ReadRef, Result,
SectionIndex, SymbolIndex,
self, Architecture, ComdatKind, Error, Export, FileFlags, FileRef, Import,
NoDynamicRelocationIterator, Object, ObjectComdat, ObjectKind, ObjectMap, ObjectSection,
ReadError, ReadRef, Result, SectionIndex, SymbolIndex,
};
use crate::{endian, macho, BigEndian, ByteString, Endian, Endianness, Pod};

Expand Down Expand Up @@ -177,6 +177,8 @@ where
Mach: MachHeader,
R: 'file + ReadRef<'data>,
{
type ReadRef = R;
type Endian = Mach::Endian;
type Segment = MachOSegment<'data, 'file, Mach, R>;
type SegmentIterator = MachOSegmentIterator<'data, 'file, Mach, R>;
type Section = MachOSection<'data, 'file, Mach, R>;
Expand All @@ -188,6 +190,10 @@ where
type SymbolTable = MachOSymbolTable<'data, 'file, Mach, R>;
type DynamicRelocationIterator = NoDynamicRelocationIterator;

fn file_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Mach::file_ref(self)
}

fn architecture(&self) -> Architecture {
match self.header.cputype(self.endian) {
macho::CPU_TYPE_ARM => Architecture::Arm,
Expand Down Expand Up @@ -560,6 +566,11 @@ pub trait MachHeader: Debug + Pod {
type Section: Section<Endian = Self::Endian>;
type Nlist: Nlist<Endian = Self::Endian>;

/// TODO
fn file_ref<'data, 'file, R: ReadRef<'data>>(
file: &'file MachOFile<'data, Self, R>,
) -> FileRef<'data, 'file, Self::Endian, R>;

/// Return true if this type is a 64-bit header.
///
/// This is a property of the type, not a value in the header data.
Expand Down Expand Up @@ -641,6 +652,12 @@ impl<Endian: endian::Endian> MachHeader for macho::MachHeader32<Endian> {
type Section = macho::Section32<Endian>;
type Nlist = macho::Nlist32<Endian>;

fn file_ref<'data, 'file, R: ReadRef<'data>>(
file: &'file MachOFile<'data, Self, R>,
) -> FileRef<'data, 'file, Self::Endian, R> {
FileRef::MachO32(file)
}

fn is_type_64(&self) -> bool {
false
}
Expand Down Expand Up @@ -689,6 +706,12 @@ impl<Endian: endian::Endian> MachHeader for macho::MachHeader64<Endian> {
type Section = macho::Section64<Endian>;
type Nlist = macho::Nlist64<Endian>;

fn file_ref<'data, 'file, R: ReadRef<'data>>(
file: &'file MachOFile<'data, Self, R>,
) -> FileRef<'data, 'file, Self::Endian, R> {
FileRef::MachO64(file)
}

fn is_type_64(&self) -> bool {
true
}
Expand Down
30 changes: 27 additions & 3 deletions src/read/pe/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use core::convert::TryInto;

use crate::read::coff::{CoffCommon, CoffSymbol, CoffSymbolIterator, CoffSymbolTable, SymbolTable};
use crate::read::{
self, Architecture, ComdatKind, Error, Export, FileFlags, Import, NoDynamicRelocationIterator,
Object, ObjectComdat, ObjectKind, ReadError, ReadRef, Result, SectionIndex, SymbolIndex,
self, Architecture, ComdatKind, Error, Export, FileFlags, FileRef, Import,
NoDynamicRelocationIterator, Object, ObjectComdat, ObjectKind, ReadError, ReadRef, Result,
SectionIndex, SymbolIndex,
};
use crate::{pe, ByteString, Bytes, CodeView, LittleEndian as LE, Pod, U32};
use crate::{pe, ByteString, Bytes, CodeView, Endianness, LittleEndian as LE, Pod, U32};

use super::{
DataDirectories, ExportTable, ImageThunkData, ImportTable, PeSection, PeSectionIterator,
Expand Down Expand Up @@ -133,6 +134,8 @@ where
Pe: ImageNtHeaders,
R: 'file + ReadRef<'data>,
{
type ReadRef = R;
type Endian = Endianness;
type Segment = PeSegment<'data, 'file, Pe, R>;
type SegmentIterator = PeSegmentIterator<'data, 'file, Pe, R>;
type Section = PeSection<'data, 'file, Pe, R>;
Expand All @@ -144,6 +147,10 @@ where
type SymbolTable = CoffSymbolTable<'data, 'file, R>;
type DynamicRelocationIterator = NoDynamicRelocationIterator;

fn file_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Pe::file_ref(self)
}

fn architecture(&self) -> Architecture {
match self.nt_headers.file_header().machine.get(LE) {
pe::IMAGE_FILE_MACHINE_ARMNT => Architecture::Arm,
Expand Down Expand Up @@ -546,6 +553,11 @@ pub trait ImageNtHeaders: Debug + Pod {
type ImageOptionalHeader: ImageOptionalHeader;
type ImageThunkData: ImageThunkData;

/// TODO
fn file_ref<'data, 'file, Endian: crate::Endian, R: ReadRef<'data>>(
file: &'file PeFile<'data, Self, R>,
) -> FileRef<'data, 'file, Endian, R>;

/// Return true if this type is a 64-bit header.
///
/// This is a property of the type, not a value in the header data.
Expand Down Expand Up @@ -668,6 +680,12 @@ impl ImageNtHeaders for pe::ImageNtHeaders32 {
type ImageOptionalHeader = pe::ImageOptionalHeader32;
type ImageThunkData = pe::ImageThunkData32;

fn file_ref<'data, 'file, Endian: crate::Endian, R: ReadRef<'data>>(
file: &'file PeFile<'data, Self, R>,
) -> FileRef<'data, 'file, Endian, R> {
FileRef::Pe32(file)
}

#[inline]
fn is_type_64(&self) -> bool {
false
Expand Down Expand Up @@ -850,6 +868,12 @@ impl ImageNtHeaders for pe::ImageNtHeaders64 {
type ImageOptionalHeader = pe::ImageOptionalHeader64;
type ImageThunkData = pe::ImageThunkData64;

fn file_ref<'data, 'file, Endian: crate::Endian, R: ReadRef<'data>>(
file: &'file PeFile<'data, Self, R>,
) -> FileRef<'data, 'file, Endian, R> {
FileRef::Pe64(file)
}

#[inline]
fn is_type_64(&self) -> bool {
true
Expand Down
14 changes: 11 additions & 3 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ use alloc::vec::Vec;

use crate::read::{
self, Architecture, CodeView, ComdatKind, CompressedData, CompressedFileRange, Export,
FileFlags, Import, ObjectKind, ObjectMap, Relocation, Result, SectionFlags, SectionIndex,
SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind, SymbolMap, SymbolMapName,
SymbolScope, SymbolSection,
FileFlags, FileRef, Import, ObjectKind, ObjectMap, Relocation, Result, SectionFlags,
SectionIndex, SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind, SymbolMap,
SymbolMapName, SymbolScope, SymbolSection,
};
use crate::Endianness;

/// An object file.
pub trait Object<'data: 'file, 'file>: read::private::Sealed {
/// TODO
type ReadRef: crate::ReadRef<'data>;
/// TODO
type Endian: crate::Endian;

/// A segment in the object file.
type Segment: ObjectSegment<'data>;

Expand Down Expand Up @@ -48,6 +53,9 @@ pub trait Object<'data: 'file, 'file>: read::private::Sealed {
/// that the relocation applies to.
type DynamicRelocationIterator: Iterator<Item = (u64, Relocation)>;

/// Get details specific to each file format.
fn file_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef>;

/// Get the architecture type of the file.
fn architecture(&self) -> Architecture;

Expand Down
Loading

0 comments on commit 4cd5215

Please sign in to comment.