|
|
@ -4,12 +4,12 @@ use std::{
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::common::{Binding, Relocatable, Section, SectionInfo, Symbol, SymbolIterBox};
|
|
|
|
use crate::common::{Relocatable, Section, SectionInfo};
|
|
|
|
use crate::{common::SectionIterBox, error::Error};
|
|
|
|
use crate::{common::SectionIterBox, error::Error};
|
|
|
|
use elf_utilities::{
|
|
|
|
use elf_utilities::{
|
|
|
|
header::Ehdr64,
|
|
|
|
header::Ehdr64,
|
|
|
|
section::{Contents64, Shdr64, Type},
|
|
|
|
section::{Shdr64, Type},
|
|
|
|
symbol::{Bind, Symbol64},
|
|
|
|
symbol::{Bind, Symbol64, Type as SymType},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use memmap::Mmap;
|
|
|
|
use memmap::Mmap;
|
|
|
|
|
|
|
|
|
|
|
@ -19,7 +19,7 @@ pub struct ElfObject {
|
|
|
|
data: Mmap,
|
|
|
|
data: Mmap,
|
|
|
|
ehdr: Ehdr64,
|
|
|
|
ehdr: Ehdr64,
|
|
|
|
shdrs: Vec<Shdr64>,
|
|
|
|
shdrs: Vec<Shdr64>,
|
|
|
|
symbols: Vec<Symbol64>,
|
|
|
|
symbols: Vec<(usize, Symbol64)>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ElfObject {
|
|
|
|
impl ElfObject {
|
|
|
@ -49,6 +49,16 @@ impl ElfObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn resolve_name(&self, name_idx: usize, sh_index: usize) -> Result<&str, Error> {
|
|
|
|
fn resolve_name(&self, name_idx: usize, sh_index: usize) -> Result<&str, Error> {
|
|
|
|
|
|
|
|
if name_idx == 0 {
|
|
|
|
|
|
|
|
let error = if sh_index == self.ehdr.e_shstrndx.into() {
|
|
|
|
|
|
|
|
Error::InvalidSectionName
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Error::InvalidSymbolName
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Err(error);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let strtab = self.parse_strtab(sh_index)?;
|
|
|
|
let strtab = self.parse_strtab(sh_index)?;
|
|
|
|
let strtab_offset: usize = strtab.sh_offset as usize;
|
|
|
|
let strtab_offset: usize = strtab.sh_offset as usize;
|
|
|
|
let start = strtab_offset + name_idx;
|
|
|
|
let start = strtab_offset + name_idx;
|
|
|
@ -91,14 +101,21 @@ impl ElfObject {
|
|
|
|
let offset = shdr.sh_offset as usize;
|
|
|
|
let offset = shdr.sh_offset as usize;
|
|
|
|
let count = shdr.sh_size as usize / ent_size;
|
|
|
|
let count = shdr.sh_size as usize / ent_size;
|
|
|
|
|
|
|
|
|
|
|
|
for s_i in 0..count {
|
|
|
|
for s_i in 1..count {
|
|
|
|
|
|
|
|
// skip null-symbol
|
|
|
|
let start = offset + s_i * ent_size;
|
|
|
|
let start = offset + s_i * ent_size;
|
|
|
|
let s64 = Symbol64::deserialize(&self.data, start)
|
|
|
|
let s64 = Symbol64::deserialize(&self.data, start)
|
|
|
|
.map_err(|err| Error::ParseError(err))?;
|
|
|
|
.map_err(|err| Error::ParseError(err))?;
|
|
|
|
self.symbols.push(s64);
|
|
|
|
|
|
|
|
|
|
|
|
match s64.get_type() {
|
|
|
|
|
|
|
|
SymType::Object | SymType::Func | SymType::NoType => {
|
|
|
|
|
|
|
|
self.symbols.push((shdr.sh_link as usize, s64))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// self.symbols.push();
|
|
|
|
|
|
|
|
self.shdrs.push(shdr);
|
|
|
|
self.shdrs.push(shdr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -132,22 +149,6 @@ impl ElfObject {
|
|
|
|
None
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn make_symbol(&self, s64: &Symbol64) -> 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 {
|
|
|
|
|
|
|
|
object_index: self.object_index,
|
|
|
|
|
|
|
|
binding,
|
|
|
|
|
|
|
|
address: s64.st_value,
|
|
|
|
|
|
|
|
size: s64.st_size,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Relocatable for ElfObject {
|
|
|
|
impl Relocatable for ElfObject {
|
|
|
@ -155,6 +156,10 @@ impl Relocatable for ElfObject {
|
|
|
|
ElfObject::new(origin, object_index)
|
|
|
|
ElfObject::new(origin, object_index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn object_index(&self) -> usize {
|
|
|
|
|
|
|
|
self.object_index
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn origin(&self) -> &Path {
|
|
|
|
fn origin(&self) -> &Path {
|
|
|
|
&self.origin
|
|
|
|
&self.origin
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -177,14 +182,23 @@ impl Relocatable for ElfObject {
|
|
|
|
Ok(&self.data[o..o + s])
|
|
|
|
Ok(&self.data[o..o + s])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn symbols(&self) -> SymbolIterBox {
|
|
|
|
fn symbol_count(&self) -> usize {
|
|
|
|
Box::new(self.symbols.iter().map(move |s| {
|
|
|
|
self.symbols.len()
|
|
|
|
// TODO: change SymbolIterBox to allow errors!
|
|
|
|
}
|
|
|
|
let name = self
|
|
|
|
|
|
|
|
.resolve_name(s.st_name as usize, s.st_other as usize)
|
|
|
|
fn symbol_name(&self, index: usize) -> Result<&str, Error> {
|
|
|
|
.expect("Symbol name resolution");
|
|
|
|
if let Some(symbol) = self.symbols.get(index) {
|
|
|
|
(name, self.make_symbol(s))
|
|
|
|
self.resolve_name(symbol.1.st_name as usize, symbol.0)
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Err(Error::InvalidSymbolIndex)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn symbol_needs_resolving(&self, index: usize) -> bool {
|
|
|
|
|
|
|
|
match self.symbols[index].1.get_bind() {
|
|
|
|
|
|
|
|
Bind::Global | Bind::Weak => true,
|
|
|
|
|
|
|
|
_ => false,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|