You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.0 KiB
Rust

4 years ago
use std::{
convert::TryFrom,
fmt::Display,
path::{Path, PathBuf},
};
4 years ago
4 years ago
use crate::common::{Relocatable, Section, SectionInfo};
4 years ago
use crate::{common::BSI, error::Error};
use elf_utilities::{file::ELF64, parser::read_elf64};
4 years ago
pub struct ElfObject {
4 years ago
object_index: usize,
4 years ago
origin: PathBuf,
elf: ELF64,
4 years ago
}
impl ElfObject {
4 years ago
pub fn new(origin: PathBuf, object_index: usize) -> 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)),
};
4 years ago
is_relocatable(&elf)?;
4 years ago
let result = ElfObject {
object_index,
origin,
elf,
};
4 years ago
Ok(result)
}
pub fn elf(&self) -> &ELF64 {
&self.elf
}
4 years ago
}
impl Relocatable for ElfObject {
type Index = (usize, usize); // object index, section index
4 years ago
fn new(origin: PathBuf, object_index: usize) -> Result<Self, Error> {
ElfObject::new(origin, object_index)
}
4 years ago
fn origin(&self) -> &Path {
&self.origin
}
4 years ago
fn sections(&self) -> BSI<(usize, usize)> {
use elf_utilities::section::{Contents64, Type};
4 years ago
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((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 {
4 years ago
Some(Err(Error::InvalidSectionData))
}
4 years ago
} else {
None
}
4 years ago
}
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)
4 years ago
}
fn section_data(&self, index: (usize, usize)) -> Result<&[u8], Error> {
use elf_utilities::section::Contents64;
let section = &self.elf.sections[index.1];
match &section.contents {
Contents64::Raw(v) => Ok(&v),
4 years ago
_ => Err(Error::InvalidSectionData),
}
}
4 years ago
}
impl Display for ElfObject {
4 years ago
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(())
}
}
4 years ago
// impl TryFrom<PathBuf> for ElfObject {
// type Error = Error;
4 years ago
4 years ago
// fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
// Self::new(path)
// }
// }
4 years ago
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()));
4 years ago
}
Ok(())
}