|
|
|
@ -4,7 +4,7 @@ use std::{
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use crate::common::{Relocatable, Section, SectionInfo};
|
|
|
|
|
use crate::common::{Relocatable, Section, SectionInfo, SymbolOffsets, SymbolValue};
|
|
|
|
|
use crate::{common::SectionIterBox, error::Error};
|
|
|
|
|
use elf_utilities::{
|
|
|
|
|
header::Ehdr64,
|
|
|
|
@ -13,13 +13,19 @@ use elf_utilities::{
|
|
|
|
|
};
|
|
|
|
|
use memmap::Mmap;
|
|
|
|
|
|
|
|
|
|
struct ElfSymbol {
|
|
|
|
|
s64: Symbol64,
|
|
|
|
|
sh_index: usize,
|
|
|
|
|
rel_offset: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ElfObject {
|
|
|
|
|
object_index: usize,
|
|
|
|
|
origin: PathBuf,
|
|
|
|
|
data: Mmap,
|
|
|
|
|
ehdr: Ehdr64,
|
|
|
|
|
shdrs: Vec<Shdr64>,
|
|
|
|
|
symbols: Vec<(usize, Symbol64)>,
|
|
|
|
|
symbols: Vec<ElfSymbol>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ElfObject {
|
|
|
|
@ -48,15 +54,22 @@ impl ElfObject {
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn resolve_symbol_value(&self, symbol_index: usize) -> Result<&[u8], Error> {
|
|
|
|
|
if let Some((_, s64)) = self.symbols.get(symbol_index) {
|
|
|
|
|
if s64.st_size == 0 {
|
|
|
|
|
Ok(&[])
|
|
|
|
|
} else if let Some(shdr) = self.shdrs.get(s64.st_shndx as usize) {
|
|
|
|
|
let start: usize = (shdr.sh_offset + s64.st_value) as usize;
|
|
|
|
|
let end: usize = start + s64.st_size as usize;
|
|
|
|
|
|
|
|
|
|
Ok(&self.data[start..end])
|
|
|
|
|
fn resolve_symbol_value(&self, symbol_index: usize) -> Result<SymbolValue, Error> {
|
|
|
|
|
if let Some(symbol) = self.symbols.get(symbol_index) {
|
|
|
|
|
let idx = symbol.s64.st_shndx as usize;
|
|
|
|
|
if idx == 0 {
|
|
|
|
|
Ok(SymbolValue::Undefined)
|
|
|
|
|
} else if let Some(shdr) = self.shdrs.get(idx) {
|
|
|
|
|
let sym_size = symbol.s64.st_size as usize;
|
|
|
|
|
let start: usize = (shdr.sh_offset + symbol.s64.st_value) as usize;
|
|
|
|
|
|
|
|
|
|
if sym_size > 0 {
|
|
|
|
|
let end: usize = start + sym_size;
|
|
|
|
|
|
|
|
|
|
Ok(SymbolValue::Value(&self.data[start..end]))
|
|
|
|
|
} else {
|
|
|
|
|
Ok(SymbolValue::Address(start))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::InvalidSectionIndex)
|
|
|
|
|
}
|
|
|
|
@ -104,7 +117,8 @@ impl ElfObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn populate(&mut self) -> Result<(), Error> {
|
|
|
|
|
for i in 0..self.ehdr.e_shnum {
|
|
|
|
|
for i in 1..self.ehdr.e_shnum {
|
|
|
|
|
// skip null section
|
|
|
|
|
let i_usize: usize = i.into();
|
|
|
|
|
let sh_start = self.ehdr.e_shoff as usize;
|
|
|
|
|
let sh_size = usize::from(self.ehdr.e_shentsize);
|
|
|
|
@ -113,24 +127,10 @@ impl ElfObject {
|
|
|
|
|
let shdr: Shdr64 = bincode::deserialize(&self.data[offset..offset + sh_size])
|
|
|
|
|
.map_err(|err| Error::ParseError(err))?;
|
|
|
|
|
|
|
|
|
|
if shdr.get_type() == Type::SymTab {
|
|
|
|
|
let ent_size = shdr.sh_entsize as usize;
|
|
|
|
|
let offset = shdr.sh_offset as usize;
|
|
|
|
|
let count = shdr.sh_size as usize / ent_size;
|
|
|
|
|
|
|
|
|
|
for s_i in 1..count {
|
|
|
|
|
// skip null-symbol
|
|
|
|
|
let start = offset + s_i * ent_size;
|
|
|
|
|
let s64 = Symbol64::deserialize(&self.data, start)
|
|
|
|
|
.map_err(|err| Error::ParseError(err))?;
|
|
|
|
|
|
|
|
|
|
match s64.get_type() {
|
|
|
|
|
SymType::Object | SymType::Func | SymType::NoType => {
|
|
|
|
|
self.symbols.push((shdr.sh_link as usize, s64))
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
match shdr.get_type() {
|
|
|
|
|
Type::SymTab => self.populate_symtab(&shdr, i as usize)?,
|
|
|
|
|
Type::Rel | Type::Rela => self.populate_rela(&shdr)?,
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.shdrs.push(shdr);
|
|
|
|
@ -139,7 +139,36 @@ impl ElfObject {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_section(&self, shdr: &Shdr64) -> Option<Result<Section, Error>> {
|
|
|
|
|
fn populate_symtab(&mut self, shdr: &Shdr64, sh_index: usize) -> Result<(), Error> {
|
|
|
|
|
let ent_size = shdr.sh_entsize as usize;
|
|
|
|
|
let offset = shdr.sh_offset as usize;
|
|
|
|
|
let count = shdr.sh_size as usize / ent_size;
|
|
|
|
|
|
|
|
|
|
for s_i in 1..count {
|
|
|
|
|
// skip null-symbol
|
|
|
|
|
let start = offset + s_i * ent_size;
|
|
|
|
|
let s64 =
|
|
|
|
|
Symbol64::deserialize(&self.data, start).map_err(|err| Error::ParseError(err))?;
|
|
|
|
|
|
|
|
|
|
match s64.get_type() {
|
|
|
|
|
SymType::Object | SymType::Func | SymType::NoType => self.symbols.push(ElfSymbol {
|
|
|
|
|
sh_index,
|
|
|
|
|
s64,
|
|
|
|
|
rel_offset: 0,
|
|
|
|
|
}),
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn populate_rela(&mut self, shdr: &Shdr64) -> Result<(), Error> {
|
|
|
|
|
// TODO
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_section(&self, shdr: &Shdr64, section_index: usize) -> Option<Result<Section, Error>> {
|
|
|
|
|
let sh_index: usize = self.ehdr.e_shstrndx.into();
|
|
|
|
|
let name = match self.resolve_name(shdr.sh_name as usize, sh_index) {
|
|
|
|
|
Ok(n) => n,
|
|
|
|
@ -148,6 +177,7 @@ impl ElfObject {
|
|
|
|
|
|
|
|
|
|
let mut si = SectionInfo {
|
|
|
|
|
object_index: self.object_index,
|
|
|
|
|
section_index,
|
|
|
|
|
file_size: shdr.sh_size,
|
|
|
|
|
data_size: shdr.sh_size,
|
|
|
|
|
offset: shdr.sh_offset,
|
|
|
|
@ -182,13 +212,14 @@ impl Relocatable for ElfObject {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sections(&self) -> SectionIterBox {
|
|
|
|
|
let iter = self
|
|
|
|
|
.shdrs
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(move |shdr| match shdr.get_type() {
|
|
|
|
|
Type::ProgBits | Type::NoBits => self.make_section(shdr),
|
|
|
|
|
_ => None,
|
|
|
|
|
});
|
|
|
|
|
let iter =
|
|
|
|
|
self.shdrs
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.filter_map(move |(i, shdr)| match shdr.get_type() {
|
|
|
|
|
Type::ProgBits | Type::NoBits => self.make_section(shdr, i),
|
|
|
|
|
_ => None,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Box::new(iter)
|
|
|
|
|
}
|
|
|
|
@ -205,30 +236,39 @@ impl Relocatable for ElfObject {
|
|
|
|
|
|
|
|
|
|
fn symbol_name(&self, index: usize) -> Result<&str, Error> {
|
|
|
|
|
if let Some(symbol) = self.symbols.get(index) {
|
|
|
|
|
self.resolve_name(symbol.1.st_name as usize, symbol.0)
|
|
|
|
|
self.resolve_name(symbol.s64.st_name as usize, symbol.sh_index)
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::InvalidSymbolIndex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn symbol_value(&self, index: usize) -> Result<&[u8], Error> {
|
|
|
|
|
fn symbol_value(&self, index: usize) -> Result<SymbolValue, Error> {
|
|
|
|
|
self.resolve_symbol_value(index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn symbol_file_offset(&self, index: usize) -> Result<usize, Error> {
|
|
|
|
|
let (sh_index, s64) = self.symbols.get(index).ok_or(Error::InvalidSymbolIndex)?;
|
|
|
|
|
fn symbol_section_index(&self, index: usize) -> Result<usize, Error> {
|
|
|
|
|
let symbol = self.symbols.get(index).ok_or(Error::InvalidSymbolIndex)?;
|
|
|
|
|
|
|
|
|
|
Ok(symbol.sh_index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn symbol_offsets(&self, index: usize) -> Result<SymbolOffsets, Error> {
|
|
|
|
|
let symbol = self.symbols.get(index).ok_or(Error::InvalidSymbolIndex)?;
|
|
|
|
|
|
|
|
|
|
let shdr = self
|
|
|
|
|
.shdrs
|
|
|
|
|
.get(*sh_index)
|
|
|
|
|
.get(symbol.sh_index)
|
|
|
|
|
.ok_or(Error::InvalidSectionIndex)?;
|
|
|
|
|
let sh_offset: usize = shdr.sh_offset as usize;
|
|
|
|
|
|
|
|
|
|
Ok(sh_offset + s64.st_value as usize)
|
|
|
|
|
Ok(SymbolOffsets {
|
|
|
|
|
section_offset: sh_offset,
|
|
|
|
|
symbol_offset: symbol.rel_offset,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn symbol_needs_resolving(&self, index: usize) -> bool {
|
|
|
|
|
match self.symbols[index].1.get_bind() {
|
|
|
|
|
match self.symbols[index].s64.get_bind() {
|
|
|
|
|
Bind::Global | Bind::Weak => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|