Skip to content

Commit

Permalink
read/coff: add helpers to unified API for accessing lower level API
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc committed May 4, 2024
1 parent 9a7202f commit e6a6044
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/read/coff/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ impl<'data, R: ReadRef<'data>, Coff: CoffHeader> CoffFile<'data, R, Coff> {
data,
})
}

/// Get the raw COFF file header.
pub fn coff_header(&self) -> &'data Coff {
self.header
}

/// Get the COFF section table.
pub fn coff_section_table(&self) -> SectionTable<'data> {
self.common.sections
}

/// Get the COFF symbol table.
pub fn coff_symbol_table(&self) -> &SymbolTable<'data, R, Coff> {
&self.common.symbols
}
}

impl<'data, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed
Expand Down
27 changes: 26 additions & 1 deletion src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ pub struct CoffSegment<
}

impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffSegment<'data, 'file, R, Coff> {
/// Get the COFF file containing this segment.
pub fn coff_file(&self) -> &'file CoffFile<'data, R, Coff> {
self.file
}

/// Get the raw COFF section header.
pub fn coff_section(&self) -> &'data pe::ImageSectionHeader {
self.section
}

fn bytes(&self) -> Result<&'data [u8]> {
self.section
.coff_data(self.file.data)
Expand Down Expand Up @@ -281,6 +291,21 @@ pub struct CoffSection<
}

impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffSection<'data, 'file, R, Coff> {
/// Get the COFF file containing this section.
pub fn coff_file(&self) -> &'file CoffFile<'data, R, Coff> {
self.file
}

/// Get the raw COFF section header.
pub fn coff_section(&self) -> &'data pe::ImageSectionHeader {
self.section
}

/// Get the raw COFF relocations for this section.
pub fn coff_relocations(&self) -> Result<&'data [pe::ImageRelocation]> {
self.section.coff_relocations(self.file.data)
}

fn bytes(&self) -> Result<&'data [u8]> {
self.section
.coff_data(self.file.data)
Expand Down Expand Up @@ -377,7 +402,7 @@ impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> ObjectSection<'data>
}

fn relocations(&self) -> CoffRelocationIterator<'data, 'file, R, Coff> {
let relocations = self.section.coff_relocations(self.file.data).unwrap_or(&[]);
let relocations = self.coff_relocations().unwrap_or(&[]);
CoffRelocationIterator {
file: self.file,
iter: relocations.iter(),
Expand Down
6 changes: 6 additions & 0 deletions src/read/coff/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,15 @@ where
impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> CoffSymbol<'data, 'file, R, Coff> {
#[inline]
/// Get the raw `ImageSymbol` struct.
#[deprecated(note = "Use `coff_symbol` instead")]
pub fn raw_symbol(&self) -> &'data Coff::ImageSymbol {
self.symbol
}

/// Get the raw `ImageSymbol` struct.
pub fn coff_symbol(&self) -> &'data Coff::ImageSymbol {
self.symbol
}
}

impl<'data, 'file, R: ReadRef<'data>, Coff: CoffHeader> read::private::Sealed
Expand Down
32 changes: 32 additions & 0 deletions src/read/pe/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ where
section: &'data pe::ImageSectionHeader,
}

impl<'data, 'file, Pe, R> PeSegment<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
/// Get the PE file containing this segment.
pub fn pe_file(&self) -> &'file PeFile<'data, Pe, R> {
self.file
}

/// Get the raw PE section header.
pub fn pe_section(&self) -> &'data pe::ImageSectionHeader {
self.section
}
}

impl<'data, 'file, Pe, R> read::private::Sealed for PeSegment<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
Expand Down Expand Up @@ -189,6 +205,22 @@ where
pub(super) section: &'data pe::ImageSectionHeader,
}

impl<'data, 'file, Pe, R> PeSection<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
/// Get the PE file containing this segment.
pub fn pe_file(&self) -> &'file PeFile<'data, Pe, R> {
self.file
}

/// Get the raw PE section header.
pub fn pe_section(&self) -> &'data pe::ImageSectionHeader {
self.section
}
}

impl<'data, 'file, Pe, R> read::private::Sealed for PeSection<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
Expand Down

0 comments on commit e6a6044

Please sign in to comment.