|
|
|
@ -4,9 +4,14 @@ use std::{
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::common::{DataIndex, Relocatable, Section, SectionInfo};
|
|
|
|
|
use crate::{common::BSI, error::Error};
|
|
|
|
|
use elf_utilities::{file::ELF64, parser::read_elf64};
|
|
|
|
|
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,
|
|
|
|
@ -45,9 +50,7 @@ impl Relocatable for ElfObject {
|
|
|
|
|
&self.origin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sections(&self) -> BSI {
|
|
|
|
|
use elf_utilities::section::{Contents64, Type};
|
|
|
|
|
|
|
|
|
|
fn sections(&self) -> SectionIterBox {
|
|
|
|
|
let iter = self
|
|
|
|
|
.elf
|
|
|
|
|
.sections
|
|
|
|
@ -95,7 +98,6 @@ impl Relocatable for ElfObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn section_data(&self, index: DataIndex) -> Result<&[u8], Error> {
|
|
|
|
|
use elf_utilities::section::Contents64;
|
|
|
|
|
let section = &self.elf.sections[index.section_index];
|
|
|
|
|
|
|
|
|
|
match §ion.contents {
|
|
|
|
@ -103,6 +105,39 @@ impl Relocatable for ElfObject {
|
|
|
|
|
_ => 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 {
|
|
|
|
@ -123,13 +158,34 @@ impl Display for ElfObject {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// impl TryFrom<PathBuf> for ElfObject {
|
|
|
|
|
// type Error = Error;
|
|
|
|
|
fn make_symbol(s64: &Symbol64, strtab: &Vec<u8>) -> 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!
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
|
|
|
|
|
// Self::new(path)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
Symbol {
|
|
|
|
|
name: parse_strtab_name(&strtab, s64.st_name),
|
|
|
|
|
binding,
|
|
|
|
|
address: s64.st_value,
|
|
|
|
|
size: s64.st_size,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_strtab_name(strtab: &Vec<u8>, idx: u32) -> String {
|
|
|
|
|
let bytes: Vec<u8> = 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;
|
|
|
|
|