use std::{ convert::TryFrom, fmt::Display, path::{Path, PathBuf}, }; use crate::common::{Binding, DataIndex, Relocatable, Section, SectionInfo, Symbol, SymbolIterBox}; use crate::{common::SectionIterBox, error::Error}; use elf_utilities::{ file::ELF64, parser::read_elf64, section::{Contents64, Type}, symbol::{Bind, Symbol64}, }; pub struct ElfObject { object_index: usize, origin: PathBuf, elf: ELF64, } impl ElfObject { pub fn new(origin: PathBuf, object_index: usize) -> Result { let str_origin = origin .as_path() .to_str() .ok_or(Error::InvalidObjectType(404))?; let elf = match read_elf64(str_origin) { Ok(val) => val, Err(err) => return Err(Error::ParseError(err)), }; is_relocatable(&elf)?; let result = ElfObject { object_index, origin, elf, }; Ok(result) } } impl Relocatable for ElfObject { fn new(origin: PathBuf, di: DataIndex) -> Result { ElfObject::new(origin, di.object_index) } fn origin(&self) -> &Path { &self.origin } fn sections(&self) -> SectionIterBox { let iter = self .elf .sections .iter() .enumerate() .filter_map(move |(i, s)| match s.header.get_type() { Type::ProgBits => { if s.header.sh_size > 0 { if match &s.contents { Contents64::Raw(v) => Some(v), _ => None, } .is_some() { let si = SectionInfo { file_size: s.header.sh_size, data_size: s.header.sh_size, data_index: Some(DataIndex::new(self.object_index, i)), offset: s.header.sh_offset, }; let s_name: &str = &s.name; match Section::try_from((s_name, si)) { Ok(s) => Some(Ok(s)), Err(Error::InvalidSectionName) => None, // skip Err(err) => Some(Err(err)), } } else { Some(Err(Error::InvalidSectionData)) } } else { None } } Type::NoBits => Some(Ok(Section::Bss(SectionInfo { file_size: 0, data_size: s.header.sh_size, data_index: None, offset: s.header.sh_offset, }))), _ => None, }); Box::new(iter) } fn section_data(&self, index: DataIndex) -> Result<&[u8], Error> { let section = &self.elf.sections[index.section_index]; match §ion.contents { Contents64::Raw(v) => Ok(&v), _ => Err(Error::InvalidSectionData), } } fn symbols(&self) -> SymbolIterBox { if let Some(strtab_section) = self .elf .first_section_by(|s64| s64.header.get_type() == Type::StrTab && s64.name == ".strtab") { let strtab = match &strtab_section.contents { Contents64::Raw(bytes) => bytes, _ => panic!("Unexpected strtab content type"), }; let iter = self .elf .sections .iter() .filter_map(move |s| match &s.contents { Contents64::Symbols(symbols) => { Some(symbols.iter().filter_map(move |sym| match sym.get_bind() { Bind::Global | Bind::Local | Bind::Weak => { Some(make_symbol(sym, strtab)) } _ => None, })) } _ => None, }) .flatten(); Box::new(iter) } else { Box::new(std::iter::empty()) } } } impl Display for ElfObject { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "==={:?}===\n>Symbols:\n{}\n>Sections:\n", self.origin().file_name().unwrap(), "TODO" )?; for section in self.sections() { let u = section.unwrap(); write!(f, "{}", u)?; } Ok(()) } } fn make_symbol(s64: &Symbol64, strtab: &Vec) -> Symbol { let binding = match s64.get_bind() { Bind::Global => Binding::Global, Bind::Local => Binding::Local, Bind::Weak => Binding::Weak, _ => panic!("Unexpected binding type encountered on symbol"), // this is screened! }; Symbol { name: parse_strtab_name(&strtab, s64.st_name), binding, address: s64.st_value, size: s64.st_size, } } fn parse_strtab_name(strtab: &Vec, idx: u32) -> String { let bytes: Vec = strtab .iter() .skip(idx as usize) .take_while(|byte| **byte != 0x00) .copied() .collect(); std::str::from_utf8(&bytes) .expect("Symbol name parse") .to_string() } fn is_relocatable(elf: &ELF64) -> Result<(), Error> { use elf_utilities::header::Type; if elf.ehdr.get_type() != Type::Rel { return Err(Error::InvalidObjectType(elf.ehdr.e_type.into())); } Ok(()) }