Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read: add Object::file_ref #550

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use crate::read::wasm;
use crate::read::xcoff;
use crate::read::{
self, Architecture, BinaryFormat, CodeView, ComdatKind, CompressedData, CompressedFileRange,
Error, Export, FileFlags, FileKind, Import, Object, ObjectComdat, ObjectKind, ObjectMap,
ObjectSection, ObjectSegment, ObjectSymbol, ObjectSymbolTable, ReadRef, Relocation, Result,
SectionFlags, SectionIndex, SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind,
SymbolMap, SymbolMapName, SymbolScope, SymbolSection,
Error, Export, FileFlags, FileKind, FileRef, Import, Object, ObjectComdat, ObjectKind,
ObjectMap, ObjectSection, ObjectSegment, ObjectSymbol, ObjectSymbolTable, ReadRef, Relocation,
Result, SectionFlags, SectionIndex, SectionKind, SegmentFlags, SymbolFlags, SymbolIndex,
SymbolKind, SymbolMap, SymbolMapName, SymbolScope, SymbolSection,
};
#[allow(unused_imports)]
use crate::{AddressSize, Endian, Endianness};
Expand Down Expand Up @@ -313,6 +313,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 +326,10 @@ where
type SymbolTable = SymbolTable<'data, 'file, R>;
type DynamicRelocationIterator = DynamicRelocationIterator<'data, 'file, R>;

fn as_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 as_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Coff::as_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 as_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 as_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 as_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 as_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Elf::as_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 as_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 as_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 as_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 as_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Mach::as_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 as_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 as_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 as_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 as_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef> {
Pe::as_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 as_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 as_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 as_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
47 changes: 44 additions & 3 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::marker::PhantomData;

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, Import, ObjectKind, ObjectMap, ReadRef, 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 +54,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 as_ref(&'file self) -> FileRef<'data, 'file, Self::Endian, Self::ReadRef>;

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

Expand Down Expand Up @@ -467,3 +476,35 @@ impl Iterator for NoDynamicRelocationIterator {
None
}
}

/// 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 crate::read::coff::CoffFile<'data, R>),
#[cfg(feature = "coff")]
CoffBig(&'file crate::read::coff::CoffBigFile<'data, R>),
#[cfg(feature = "elf")]
Elf32(&'file crate::read::elf::ElfFile32<'data, Endian, R>),
#[cfg(feature = "elf")]
Elf64(&'file crate::read::elf::ElfFile64<'data, Endian, R>),
#[cfg(feature = "macho")]
MachO32(&'file crate::read::macho::MachOFile32<'data, Endian, R>),
#[cfg(feature = "macho")]
MachO64(&'file crate::read::macho::MachOFile64<'data, Endian, R>),
#[cfg(feature = "pe")]
Pe32(&'file crate::read::pe::PeFile32<'data, R>),
#[cfg(feature = "pe")]
Pe64(&'file crate::read::pe::PeFile64<'data, R>),
#[cfg(feature = "wasm")]
Wasm(&'file crate::read::wasm::WasmFile<'data, R>),
#[cfg(feature = "xcoff")]
Xcoff32(&'file crate::read::xcoff::XcoffFile32<'data, R>),
#[cfg(feature = "xcoff")]
Xcoff64(&'file crate::read::xcoff::XcoffFile64<'data, R>),
#[doc(hidden)]
None(PhantomData<Endian>),
}
Loading