mod file;
mod header;
mod program_header;
mod section;
mod error;
pub use error::*;
use crate::header::{ELF_IDENT_DATA_2LSB, ELF_IDENT_DATA_2MSB};
use crate::prelude::*;
#[derive(Default)]
pub struct ElfParser {
pub(crate) parse_config: ElfParserConfig,
}
impl ElfParser {
pub(crate) fn parse_elf64_half<'a>(
&'a self,
encoding: u8,
b: &'a [u8],
) -> nom::IResult<&'a [u8], Elf64Half> {
self.parse_elf64_u16(encoding, b)
}
pub(crate) fn parse_elf64_section_index<'a>(
&'a self,
encoding: u8,
b: &'a [u8],
) -> nom::IResult<&'a [u8], Elf64SectionIndex> {
self.parse_elf64_u16(encoding, b)
}
pub(crate) fn parse_elf64_word<'a>(
&'a self,
encoding: u8,
b: &'a [u8],
) -> nom::IResult<&'a [u8], Elf64Word> {
self.parse_elf64_u32(encoding, b)
}
pub(crate) fn parse_elf64_offset<'a>(
&'a self,
encoding: u8,
b: &'a [u8],
) -> nom::IResult<&'a [u8], Elf64Off> {
self.parse_elf64_u64(encoding, b)
}
pub(crate) fn parse_elf64_address<'a>(
&'a self,
encoding: u8,
b: &'a [u8],
) -> nom::IResult<&'a [u8], Elf64Off> {
self.parse_elf64_u64(encoding, b)
}
pub(crate) fn parse_elf64_xword<'a>(
&'a self,
encoding: u8,
b: &'a [u8],
) -> nom::IResult<&'a [u8], Elf64Xword> {
self.parse_elf64_u64(encoding, b)
}
fn parse_elf64_u16<'a>(&'a self, encoding: u8, b: &'a [u8]) -> nom::IResult<&'a [u8], u16> {
match encoding {
ELF_IDENT_DATA_2LSB => nom::number::complete::le_u16(b),
ELF_IDENT_DATA_2MSB => nom::number::complete::be_u16(b),
_ => unreachable!(),
}
}
fn parse_elf64_u32<'a>(&'a self, encoding: u8, b: &'a [u8]) -> nom::IResult<&'a [u8], u32> {
match encoding {
ELF_IDENT_DATA_2LSB => nom::number::complete::le_u32(b),
ELF_IDENT_DATA_2MSB => nom::number::complete::be_u32(b),
_ => unreachable!(),
}
}
fn parse_elf64_u64<'a>(&'a self, encoding: u8, b: &'a [u8]) -> nom::IResult<&'a [u8], u64> {
match encoding {
ELF_IDENT_DATA_2LSB => nom::number::complete::le_u64(b),
ELF_IDENT_DATA_2MSB => nom::number::complete::be_u64(b),
_ => unreachable!(),
}
}
}
pub struct ElfParserConfig {
pub(crate) _parse_header: bool,
pub(crate) _parse_pht: bool,
pub(crate) _parse_sections: bool,
}
impl ElfParserConfig {
pub fn new() -> Self {
Self {
_parse_header: true,
_parse_pht: true,
_parse_sections: true,
}
}
pub fn build(self) -> ElfParser {
ElfParser { parse_config: self }
}
pub fn parse_header(mut self, parse_header: bool) -> Self {
self._parse_header = parse_header;
self
}
pub fn parse_program_header_table(mut self, parse_pht: bool) -> Self {
self._parse_pht = parse_pht;
self
}
pub fn parse_sections(mut self, parse_sections: bool) -> Self {
self._parse_sections = parse_sections;
self
}
}
impl Default for ElfParserConfig {
fn default() -> Self {
Self {
_parse_header: true,
_parse_pht: true,
_parse_sections: true,
}
}
}