Skip to content

Commit

Permalink
read/xcoff: 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 e6a6044 commit 8f4251d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/read/xcoff/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::read::{
use crate::xcoff;

use super::{
CsectAux, FileAux, SectionHeader, SectionTable, Symbol, SymbolTable, XcoffComdat,
CsectAux, FileAux, Rel, SectionHeader, SectionTable, Symbol, SymbolTable, XcoffComdat,
XcoffComdatIterator, XcoffSection, XcoffSectionIterator, XcoffSegment, XcoffSegmentIterator,
XcoffSymbol, XcoffSymbolIterator, XcoffSymbolTable,
};
Expand Down Expand Up @@ -72,9 +72,30 @@ where
}

/// Returns the raw XCOFF file header.
#[deprecated(note = "Use `xcoff_header` instead")]
pub fn raw_header(&self) -> &'data Xcoff {
self.header
}

/// Get the raw XCOFF file header.
pub fn xcoff_header(&self) -> &'data Xcoff {
self.header
}

/// Get the raw XCOFF auxiliary header.
pub fn xcoff_aux_header(&self) -> Option<&'data Xcoff::AuxHeader> {
self.aux_header
}

/// Get the XCOFF section table.
pub fn xcoff_section_table(&self) -> &SectionTable<'data, Xcoff> {
&self.sections
}

/// Get the XCOFF symbol table.
pub fn xcoff_symbol_table(&self) -> &SymbolTable<'data, Xcoff, R> {
&self.symbols
}
}

impl<'data, Xcoff, R> read::private::Sealed for XcoffFile<'data, Xcoff, R>
Expand Down Expand Up @@ -245,10 +266,11 @@ where
pub trait FileHeader: Debug + Pod {
type Word: Into<u64>;
type AuxHeader: AuxHeader<Word = Self::Word>;
type SectionHeader: SectionHeader<Word = Self::Word>;
type SectionHeader: SectionHeader<Word = Self::Word, Rel = Self::Rel>;
type Symbol: Symbol<Word = Self::Word>;
type FileAux: FileAux;
type CsectAux: CsectAux;
type Rel: Rel<Word = Self::Word>;

/// Return true if this type is a 64-bit header.
fn is_type_64(&self) -> bool;
Expand Down Expand Up @@ -331,6 +353,7 @@ impl FileHeader for xcoff::FileHeader32 {
type Symbol = xcoff::Symbol32;
type FileAux = xcoff::FileAux32;
type CsectAux = xcoff::CsectAux32;
type Rel = xcoff::Rel32;

fn is_type_64(&self) -> bool {
false
Expand Down Expand Up @@ -372,6 +395,7 @@ impl FileHeader for xcoff::FileHeader64 {
type Symbol = xcoff::Symbol64;
type FileAux = xcoff::FileAux64;
type CsectAux = xcoff::CsectAux64;
type Rel = xcoff::Rel64;

fn is_type_64(&self) -> bool {
true
Expand Down
17 changes: 16 additions & 1 deletion src/read/xcoff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ where
}

impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> XcoffSection<'data, 'file, Xcoff, R> {
/// Get the XCOFF file containing this section.
pub fn xcoff_file(&self) -> &'file XcoffFile<'data, Xcoff, R> {
self.file
}

/// Get the raw XCOFF section header.
pub fn xcoff_section(&self) -> &'data Xcoff::SectionHeader {
self.section
}

/// Get the raw XCOFF relocation entries for this section.
pub fn xcoff_relocations(&self) -> Result<&'data [Xcoff::Rel]> {
self.section.relocations(self.file.data)
}

fn bytes(&self) -> Result<&'data [u8]> {
self.section
.data(self.file.data)
Expand Down Expand Up @@ -184,7 +199,7 @@ where
}

fn relocations(&self) -> Self::RelocationIterator {
let rel = self.section.relocations(self.file.data).unwrap_or(&[]);
let rel = self.xcoff_relocations().unwrap_or(&[]);
XcoffRelocationIterator {
file: self.file,
relocations: rel.iter(),
Expand Down
16 changes: 16 additions & 0 deletions src/read/xcoff/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ where
pub(super) symbol: &'data Xcoff::Symbol,
}

impl<'data, 'file, Xcoff, R> XcoffSymbol<'data, 'file, Xcoff, R>
where
Xcoff: FileHeader,
R: ReadRef<'data>,
{
/// Get the XCOFF file containing this symbol.
pub fn xcoff_file(&self) -> &'file XcoffFile<'data, Xcoff, R> {
self.file
}

/// Get the raw XCOFF symbol structure.
pub fn xcoff_symbol(&self) -> &'data Xcoff::Symbol {
self.symbol
}
}

impl<'data, 'file, Xcoff: FileHeader, R: ReadRef<'data>> read::private::Sealed
for XcoffSymbol<'data, 'file, Xcoff, R>
{
Expand Down

0 comments on commit 8f4251d

Please sign in to comment.