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