|
|
|
use std::{convert::TryFrom, fmt::Display, path::{Path, PathBuf}};
|
|
|
|
|
|
|
|
use crate::common::{Relocatable, Section, SectionInfo};
|
|
|
|
use crate::{common::BSI, error::Error};
|
|
|
|
use elf_utilities::{file::ELF64, parser::read_elf64};
|
|
|
|
|
|
|
|
pub struct ElfObject {
|
|
|
|
origin: PathBuf,
|
|
|
|
elf: ELF64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ElfObject {
|
|
|
|
pub fn new(origin: PathBuf) -> Result<Self, Error> {
|
|
|
|
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 { origin, elf };
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn elf(&self) -> &ELF64 {
|
|
|
|
&self.elf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Relocatable for ElfObject {
|
|
|
|
fn origin(&self) -> &Path {
|
|
|
|
&self.origin
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sections<'sections>(self: &'sections Self) -> BSI<'sections> {
|
|
|
|
use elf_utilities::section::{Contents64, Type};
|
|
|
|
|
|
|
|
let iter = self
|
|
|
|
.elf
|
|
|
|
.sections
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(i, s)| match s.header.get_type() {
|
|
|
|
Type::ProgBits => {
|
|
|
|
if s.header.sh_size > 0 {
|
|
|
|
if let Some(di) = match &s.contents {
|
|
|
|
Contents64::Raw(v) => Some(v),
|
|
|
|
_ => None,
|
|
|
|
} {
|
|
|
|
let si = SectionInfo {
|
|
|
|
file_size: s.header.sh_size,
|
|
|
|
data_size: s.header.sh_size,
|
|
|
|
data_index: Some((0, i)), // TODO
|
|
|
|
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, section_index: usize) -> Result<&[u8], Error> {
|
|
|
|
use elf_utilities::section::Contents64;
|
|
|
|
let section = &self.elf.sections[section_index];
|
|
|
|
|
|
|
|
match §ion.contents {
|
|
|
|
Contents64::Raw(v) => Ok(&v),
|
|
|
|
_ => Err(Error::InvalidSectionData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<PathBuf> for ElfObject {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
|
|
|
|
Self::new(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|