pub struct ElfStream<E: EndianParse, S: Read + Seek> {
pub ehdr: FileHeader<E>,
/* private fields */
}
Expand description
This type encapsulates the stream-oriented interface for parsing ELF objects from
a Read + Seek
.
Fields§
§ehdr: FileHeader<E>
Implementations§
Source§impl<E: EndianParse, S: Read + Seek> ElfStream<E, S>
impl<E: EndianParse, S: Read + Seek> ElfStream<E, S>
Sourcepub fn open_stream(reader: S) -> Result<ElfStream<E, S>, ParseError>
pub fn open_stream(reader: S) -> Result<ElfStream<E, S>, ParseError>
Do a minimal amount of parsing work to open an ElfStream handle from a Read+Seek containing an ELF object.
This parses the ELF FileHeader, SectionHeader table, and ProgramHeader (segments) table. All other file data (section data, segment data) is left unread and unparsed.
Sourcepub fn segments(&self) -> &Vec<ProgramHeader>
pub fn segments(&self) -> &Vec<ProgramHeader>
Get the parsed section headers table
Sourcepub fn section_headers(&self) -> &Vec<SectionHeader>
pub fn section_headers(&self) -> &Vec<SectionHeader>
Get the parsed section headers table
Sourcepub fn section_headers_with_strtab(
&mut self,
) -> Result<(&Vec<SectionHeader>, Option<StringTable<'_>>), ParseError>
pub fn section_headers_with_strtab( &mut self, ) -> Result<(&Vec<SectionHeader>, Option<StringTable<'_>>), ParseError>
Get an lazy-parsing table for the Section Headers in the file and its associated StringTable.
The underlying ELF bytes backing the section headers table and string table are read all at once when the table is requested, but parsing is deferred to be lazily parsed on demand on each table.get(), strtab.get(), or table.iter().next() call.
Returns a ParseError if the data bytes for these tables cannot be read i.e. if the ELF FileHeader’s e_shnum, e_shoff, e_shentsize, e_shstrndx are invalid and point to a ranges in the file data that does not actually exist.
Sourcepub fn section_header_by_name(
&mut self,
name: &str,
) -> Result<Option<&SectionHeader>, ParseError>
pub fn section_header_by_name( &mut self, name: &str, ) -> Result<Option<&SectionHeader>, ParseError>
Find the parsed section header with the given name (if any).
Returns a ParseError if the section headers string table can’t be read
Example to get the ELF file’s ABI-tag note
use elf::ElfStream;
use elf::endian::AnyEndian;
use elf::section::SectionHeader;
use elf::note::Note;
use elf::note::NoteGnuAbiTag;
let path = std::path::PathBuf::from("sample-objects/basic.x86_64");
let io = std::fs::File::open(path).expect("Could not open file.");
let mut file = ElfStream::<AnyEndian, _>::open_stream(io).expect("Open test1");
let shdr: SectionHeader = *file
.section_header_by_name(".note.ABI-tag")
.expect("section table should be parseable")
.expect("file should have a .note.ABI-tag section");
let notes: Vec<_> = file
.section_data_as_notes(&shdr)
.expect("Should be able to get note section data")
.collect();
assert_eq!(
notes[0],
Note::GnuAbiTag(NoteGnuAbiTag {
os: 0,
major: 2,
minor: 6,
subminor: 32
}));
Sourcepub fn section_data(
&mut self,
shdr: &SectionHeader,
) -> Result<(&[u8], Option<CompressionHeader>), ParseError>
pub fn section_data( &mut self, shdr: &SectionHeader, ) -> Result<(&[u8], Option<CompressionHeader>), ParseError>
Read the section data for the given SectionHeader. Returns both the secion data and an optional CompressionHeader.
No compression header signals that the section contents are uncompressed and can be used as-is.
Some(chdr) signals that the section contents are compressed and need to be uncompressed via the compression algorithm described in ch_type. The returned buffer represents the compressed section bytes as found in the file, without the CompressionHeader.
It is up to the user to perform the decompression themselves with the compression library of their choosing.
SHT_NOBITS sections yield an empty slice.
Sourcepub fn section_data_as_strtab(
&mut self,
shdr: &SectionHeader,
) -> Result<StringTable<'_>, ParseError>
pub fn section_data_as_strtab( &mut self, shdr: &SectionHeader, ) -> Result<StringTable<'_>, ParseError>
Read the section data for the given SectionHeader and interpret it in-place as a StringTable.
Returns a ParseError if the sh_type is not SHT_STRTAB.
Sourcepub fn symbol_table(
&mut self,
) -> Result<Option<(SymbolTable<'_, E>, StringTable<'_>)>, ParseError>
pub fn symbol_table( &mut self, ) -> Result<Option<(SymbolTable<'_, E>, StringTable<'_>)>, ParseError>
Get the symbol table (section of type SHT_SYMTAB) and its associated string table.
The gABI specifies that ELF object files may have zero or one sections of type SHT_SYMTAB.
Sourcepub fn dynamic_symbol_table(
&mut self,
) -> Result<Option<(SymbolTable<'_, E>, StringTable<'_>)>, ParseError>
pub fn dynamic_symbol_table( &mut self, ) -> Result<Option<(SymbolTable<'_, E>, StringTable<'_>)>, ParseError>
Get the dynamic symbol table (section of type SHT_DYNSYM) and its associated string table.
The gABI specifies that ELF object files may have zero or one sections of type SHT_DYNSYM.
Sourcepub fn dynamic(&mut self) -> Result<Option<DynamicTable<'_, E>>, ParseError>
pub fn dynamic(&mut self) -> Result<Option<DynamicTable<'_, E>>, ParseError>
Get the .dynamic section/segment contents.
Sourcepub fn symbol_version_table(
&mut self,
) -> Result<Option<SymbolVersionTable<'_, E>>, ParseError>
pub fn symbol_version_table( &mut self, ) -> Result<Option<SymbolVersionTable<'_, E>>, ParseError>
Read the section data for the various GNU Symbol Versioning sections (if any) and return them in a SymbolVersionTable that which can interpret them in-place to yield SymbolRequirements and SymbolDefinitions
This is a GNU extension and not all objects use symbol versioning. Returns an empty Option if the object does not use symbol versioning.
Sourcepub fn section_data_as_rels(
&mut self,
shdr: &SectionHeader,
) -> Result<RelIterator<'_, E>, ParseError>
pub fn section_data_as_rels( &mut self, shdr: &SectionHeader, ) -> Result<RelIterator<'_, E>, ParseError>
Read the section data for the given SectionHeader and interpret it in-place as a RelIterator.
Returns a ParseError if the sh_type is not SHT_REL.
Sourcepub fn section_data_as_relas(
&mut self,
shdr: &SectionHeader,
) -> Result<RelaIterator<'_, E>, ParseError>
pub fn section_data_as_relas( &mut self, shdr: &SectionHeader, ) -> Result<RelaIterator<'_, E>, ParseError>
Read the section data for the given SectionHeader and interpret it in-place as a RelaIterator.
Returns a ParseError if the sh_type is not SHT_RELA.
Sourcepub fn section_data_as_notes(
&mut self,
shdr: &SectionHeader,
) -> Result<NoteIterator<'_, E>, ParseError>
pub fn section_data_as_notes( &mut self, shdr: &SectionHeader, ) -> Result<NoteIterator<'_, E>, ParseError>
Read the section data for the given SectionHeader and interpret it in-place as a NoteIterator.
Returns a ParseError if the sh_type is not SHT_RELA.
Sourcepub fn segment_data_as_notes(
&mut self,
phdr: &ProgramHeader,
) -> Result<NoteIterator<'_, E>, ParseError>
pub fn segment_data_as_notes( &mut self, phdr: &ProgramHeader, ) -> Result<NoteIterator<'_, E>, ParseError>
Read the segment data for the given Segment and interpret it in-place as a NoteIterator.
Returns a ParseError if the p_type is not PT_RELA.