use core::convert::TryInto;
use core::fmt::Debug;
use core::slice;
use crate::elf;
use crate::endian;
use crate::pod::Pod;
use crate::read::{ReadError, ReadRef, Result, SectionIndex, StringTable};
use super::{FileHeader, SectionHeader, SectionTable};
#[derive(Debug, Clone, Copy)]
pub struct DynamicTable<'data, Elf: FileHeader, R = &'data [u8]>
where
R: ReadRef<'data>,
{
endian: Elf::Endian,
dynamics: &'data [Elf::Dyn],
strings: StringTable<'data, R>,
}
impl<'data, Elf: FileHeader, R: ReadRef<'data>> Default for DynamicTable<'data, Elf, R> {
fn default() -> Self {
DynamicTable {
endian: Default::default(),
dynamics: &[],
strings: Default::default(),
}
}
}
impl<'data, Elf: FileHeader, R: ReadRef<'data>> DynamicTable<'data, Elf, R> {
pub(crate) fn parse(
endian: Elf::Endian,
data: R,
sections: &SectionTable<'data, Elf, R>,
section: &Elf::SectionHeader,
) -> Result<DynamicTable<'data, Elf, R>> {
debug_assert!(section.sh_type(endian) == elf::SHT_DYNAMIC);
let dynamics = section
.data_as_array(endian, data)
.read_error("Invalid ELF dynamic table data")?;
let link = SectionIndex(section.sh_link(endian) as usize);
let strings = sections.strings(endian, data, link)?;
Ok(DynamicTable {
endian,
dynamics,
strings,
})
}
#[inline]
pub fn strings(&self) -> &StringTable<'data, R> {
&self.strings
}
#[inline]
pub fn dynamics(&self) -> &'data [Elf::Dyn] {
self.dynamics
}
#[inline]
pub fn iter(&self) -> DynamicIterator<'data, Elf> {
DynamicIterator::new(self.endian, self.dynamics)
}
#[inline]
pub fn is_empty(&self) -> bool {
self.dynamics.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.dynamics.len()
}
pub fn string(&self, d: Dynamic) -> Result<&'data [u8]> {
d.string(&self.strings)
}
}
impl<'a, 'data, Elf: FileHeader, R: ReadRef<'data>> IntoIterator
for &'a DynamicTable<'data, Elf, R>
{
type Item = Dynamic;
type IntoIter = DynamicIterator<'data, Elf>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(Debug)]
pub struct DynamicIterator<'data, Elf: FileHeader> {
endian: Elf::Endian,
dynamics: slice::Iter<'data, Elf::Dyn>,
}
impl<'data, Elf> DynamicIterator<'data, Elf>
where
Elf: FileHeader,
{
fn new(endian: Elf::Endian, dynamics: &'data [Elf::Dyn]) -> Self {
DynamicIterator {
endian,
dynamics: dynamics.iter(),
}
}
}
impl<'data, Elf: FileHeader> Iterator for DynamicIterator<'data, Elf> {
type Item = Dynamic;
fn next(&mut self) -> Option<Self::Item> {
let d = self.dynamics.next()?;
let tag = d.d_tag(self.endian).into();
if tag == elf::DT_NULL {
self.dynamics = [].iter();
return None;
}
let val = d.d_val(self.endian).into();
Some(Dynamic { tag, val })
}
}
#[derive(Debug, Clone, Copy)]
pub struct Dynamic {
pub tag: i64,
pub val: u64,
}
impl Dynamic {
pub fn is_address(&self) -> bool {
tag_is_address(self.tag)
}
pub fn is_string(&self) -> bool {
tag_is_string(self.tag)
}
pub fn string<'data, R: ReadRef<'data>>(
&self,
strings: &StringTable<'data, R>,
) -> Result<&'data [u8]> {
self.val
.try_into()
.ok()
.and_then(|val| strings.get(val).ok())
.read_error("Invalid ELF dyn string")
}
}
#[allow(missing_docs)]
pub trait Dyn: Debug + Pod {
type Word: Into<u64>;
type Sword: Into<i64>;
type Endian: endian::Endian;
fn d_tag(&self, endian: Self::Endian) -> Self::Sword;
fn d_val(&self, endian: Self::Endian) -> Self::Word;
fn tag(&self, endian: Self::Endian) -> i64 {
self.d_tag(endian).into()
}
fn val(&self, endian: Self::Endian) -> u64 {
self.d_val(endian).into()
}
fn tag32(&self, endian: Self::Endian) -> Option<i32> {
self.d_tag(endian).into().try_into().ok()
}
fn val32(&self, endian: Self::Endian) -> Option<u32> {
self.d_val(endian).into().try_into().ok()
}
fn is_string(&self, endian: Self::Endian) -> bool {
tag_is_string(self.tag(endian))
}
fn string<'data, R: ReadRef<'data>>(
&self,
endian: Self::Endian,
strings: StringTable<'data, R>,
) -> Result<&'data [u8]> {
self.val32(endian)
.and_then(|val| strings.get(val).ok())
.read_error("Invalid ELF dyn string")
}
fn is_address(&self, endian: Self::Endian) -> bool {
tag_is_address(self.tag(endian))
}
}
impl<Endian: endian::Endian> Dyn for elf::Dyn32<Endian> {
type Word = u32;
type Sword = i32;
type Endian = Endian;
#[inline]
fn d_tag(&self, endian: Self::Endian) -> Self::Sword {
self.d_tag.get(endian)
}
#[inline]
fn d_val(&self, endian: Self::Endian) -> Self::Word {
self.d_val.get(endian)
}
}
impl<Endian: endian::Endian> Dyn for elf::Dyn64<Endian> {
type Word = u64;
type Sword = i64;
type Endian = Endian;
#[inline]
fn d_tag(&self, endian: Self::Endian) -> Self::Sword {
self.d_tag.get(endian)
}
#[inline]
fn d_val(&self, endian: Self::Endian) -> Self::Word {
self.d_val.get(endian)
}
}
fn tag_is_string(tag: i64) -> bool {
match tag {
elf::DT_NEEDED
| elf::DT_SONAME
| elf::DT_RPATH
| elf::DT_RUNPATH
| elf::DT_AUXILIARY
| elf::DT_FILTER => true,
_ => false,
}
}
fn tag_is_address(tag: i64) -> bool {
match tag {
elf::DT_PLTGOT
| elf::DT_HASH
| elf::DT_STRTAB
| elf::DT_SYMTAB
| elf::DT_RELA
| elf::DT_INIT
| elf::DT_FINI
| elf::DT_REL
| elf::DT_DEBUG
| elf::DT_JMPREL
| elf::DT_FINI_ARRAY
| elf::DT_INIT_ARRAY
| elf::DT_PREINIT_ARRAY
| elf::DT_SYMTAB_SHNDX
| elf::DT_VERDEF
| elf::DT_VERNEED
| elf::DT_VERSYM
| elf::DT_ADDRRNGLO..=elf::DT_ADDRRNGHI => true,
_ => false,
}
}