|
|
@ -8,7 +8,7 @@ use crate::common::{Binding, Relocatable, Section, SectionInfo, Symbol, SymbolIt
|
|
|
|
use crate::{common::SectionIterBox, error::Error};
|
|
|
|
use crate::{common::SectionIterBox, error::Error};
|
|
|
|
use elf_utilities::{
|
|
|
|
use elf_utilities::{
|
|
|
|
header::Ehdr64,
|
|
|
|
header::Ehdr64,
|
|
|
|
section::Shdr64,
|
|
|
|
section::{Contents64, Shdr64, Type},
|
|
|
|
symbol::{Bind, Symbol64},
|
|
|
|
symbol::{Bind, Symbol64},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use memmap::Mmap;
|
|
|
|
use memmap::Mmap;
|
|
|
@ -18,7 +18,8 @@ pub struct ElfObject {
|
|
|
|
origin: PathBuf,
|
|
|
|
origin: PathBuf,
|
|
|
|
data: Mmap,
|
|
|
|
data: Mmap,
|
|
|
|
ehdr: Ehdr64,
|
|
|
|
ehdr: Ehdr64,
|
|
|
|
sh_name_offset: usize,
|
|
|
|
shdrs: Vec<Shdr64>,
|
|
|
|
|
|
|
|
symbols: Vec<Symbol64>,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ElfObject {
|
|
|
|
impl ElfObject {
|
|
|
@ -31,22 +32,26 @@ impl ElfObject {
|
|
|
|
let file = File::open(str_origin)?;
|
|
|
|
let file = File::open(str_origin)?;
|
|
|
|
let data = unsafe { Mmap::map(&file)? };
|
|
|
|
let data = unsafe { Mmap::map(&file)? };
|
|
|
|
let ehdr = parse_elf_header(&data)?;
|
|
|
|
let ehdr = parse_elf_header(&data)?;
|
|
|
|
let shstrtab = parse_shstrtab(&data, &ehdr)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let result = ElfObject {
|
|
|
|
let mut result = ElfObject {
|
|
|
|
object_index,
|
|
|
|
object_index,
|
|
|
|
origin,
|
|
|
|
origin,
|
|
|
|
data,
|
|
|
|
data,
|
|
|
|
ehdr,
|
|
|
|
ehdr,
|
|
|
|
sh_name_offset: shstrtab.sh_offset as usize,
|
|
|
|
shdrs: Vec::new(),
|
|
|
|
|
|
|
|
symbols: Vec::new(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: make paralellization from outside possible?
|
|
|
|
|
|
|
|
result.populate()?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn section_name(&self, shdr: &Shdr64) -> Result<&str, Error> {
|
|
|
|
fn resolve_name(&self, name_idx: usize, sh_index: usize) -> Result<&str, Error> {
|
|
|
|
let idx: usize = shdr.sh_name as usize;
|
|
|
|
let strtab = self.parse_strtab(sh_index)?;
|
|
|
|
let start = self.sh_name_offset + idx;
|
|
|
|
let strtab_offset: usize = strtab.sh_offset as usize;
|
|
|
|
|
|
|
|
let start = strtab_offset + name_idx;
|
|
|
|
|
|
|
|
|
|
|
|
let mut i = start;
|
|
|
|
let mut i = start;
|
|
|
|
while self.data[i] != 0 {
|
|
|
|
while self.data[i] != 0 {
|
|
|
@ -61,17 +66,57 @@ impl ElfObject {
|
|
|
|
std::str::from_utf8(&self.data[start..i]).map_err(|err| Error::ParseError(err.into()))
|
|
|
|
std::str::from_utf8(&self.data[start..i]).map_err(|err| Error::ParseError(err.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn make_section(&self, offset: usize, sh: &Shdr64) -> Option<Result<Section, Error>> {
|
|
|
|
// TODO: consider caching these
|
|
|
|
let name = match self.section_name(sh) {
|
|
|
|
fn parse_strtab(&self, sh_index: usize) -> Result<Shdr64, Error> {
|
|
|
|
|
|
|
|
let sh_start: usize = self.ehdr.e_shoff as usize;
|
|
|
|
|
|
|
|
let sh_size: usize = self.ehdr.e_shentsize.into();
|
|
|
|
|
|
|
|
let offset: usize = sh_start + sh_size * sh_index;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bincode::deserialize(&self.data[offset..offset + sh_size])
|
|
|
|
|
|
|
|
.map_err(|err| Error::ParseError(err))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn populate(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
for i in 0..self.ehdr.e_shnum {
|
|
|
|
|
|
|
|
let i_usize: usize = i.into();
|
|
|
|
|
|
|
|
let sh_start = self.ehdr.e_shoff as usize;
|
|
|
|
|
|
|
|
let sh_size = usize::from(self.ehdr.e_shentsize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let offset: usize = sh_start + i_usize * sh_size;
|
|
|
|
|
|
|
|
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 0..count {
|
|
|
|
|
|
|
|
let start = offset + s_i * ent_size;
|
|
|
|
|
|
|
|
let s64 = Symbol64::deserialize(&self.data, start)
|
|
|
|
|
|
|
|
.map_err(|err| Error::ParseError(err))?;
|
|
|
|
|
|
|
|
self.symbols.push(s64);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// self.symbols.push();
|
|
|
|
|
|
|
|
self.shdrs.push(shdr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn make_section(&self, shdr: &Shdr64) -> 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,
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(err) => return Some(Err(err)),
|
|
|
|
Err(err) => return Some(Err(err)),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut si = SectionInfo {
|
|
|
|
let mut si = SectionInfo {
|
|
|
|
object_index: self.object_index,
|
|
|
|
object_index: self.object_index,
|
|
|
|
file_size: sh.sh_size,
|
|
|
|
file_size: shdr.sh_size,
|
|
|
|
data_size: sh.sh_size,
|
|
|
|
data_size: shdr.sh_size,
|
|
|
|
offset: offset as u64,
|
|
|
|
offset: shdr.sh_offset,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if name.starts_with(".bss") {
|
|
|
|
if name.starts_with(".bss") {
|
|
|
@ -88,7 +133,7 @@ impl ElfObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn make_symbol(&self, s64: &Symbol64, strtab: &[u8]) -> Symbol {
|
|
|
|
fn make_symbol(&self, s64: &Symbol64) -> Symbol {
|
|
|
|
let binding = match s64.get_bind() {
|
|
|
|
let binding = match s64.get_bind() {
|
|
|
|
Bind::Global => Binding::Global,
|
|
|
|
Bind::Global => Binding::Global,
|
|
|
|
Bind::Local => Binding::Local,
|
|
|
|
Bind::Local => Binding::Local,
|
|
|
@ -98,7 +143,6 @@ impl ElfObject {
|
|
|
|
|
|
|
|
|
|
|
|
Symbol {
|
|
|
|
Symbol {
|
|
|
|
object_index: self.object_index,
|
|
|
|
object_index: self.object_index,
|
|
|
|
name: parse_strtab_name(&strtab, s64.st_name),
|
|
|
|
|
|
|
|
binding,
|
|
|
|
binding,
|
|
|
|
address: s64.st_value,
|
|
|
|
address: s64.st_value,
|
|
|
|
size: s64.st_size,
|
|
|
|
size: s64.st_size,
|
|
|
@ -116,20 +160,12 @@ impl Relocatable for ElfObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn sections(&self) -> SectionIterBox {
|
|
|
|
fn sections(&self) -> SectionIterBox {
|
|
|
|
let iter = (0..self.ehdr.e_shnum).into_iter().filter_map(move |i| {
|
|
|
|
let iter = self
|
|
|
|
let i_usize = usize::from(i);
|
|
|
|
.shdrs
|
|
|
|
let sh_start = self.ehdr.e_shoff as usize;
|
|
|
|
.iter()
|
|
|
|
let sh_size = usize::from(self.ehdr.e_shentsize);
|
|
|
|
.filter_map(move |shdr| match shdr.get_type() {
|
|
|
|
|
|
|
|
Type::ProgBits | Type::NoBits => self.make_section(shdr),
|
|
|
|
let offset: usize = sh_start + i_usize * sh_size;
|
|
|
|
_ => None,
|
|
|
|
let shr: Result<Shdr64, Error> =
|
|
|
|
|
|
|
|
bincode::deserialize(&self.data[offset..offset + sh_size])
|
|
|
|
|
|
|
|
.map_err(|err| Error::ParseError(err));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match shr {
|
|
|
|
|
|
|
|
Err(err) => Some(Err(err)),
|
|
|
|
|
|
|
|
Ok(sh) => self.make_section(offset, &sh),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Box::new(iter)
|
|
|
|
Box::new(iter)
|
|
|
@ -142,36 +178,13 @@ impl Relocatable for ElfObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn symbols(&self) -> SymbolIterBox {
|
|
|
|
fn symbols(&self) -> SymbolIterBox {
|
|
|
|
// if let Some(strtab_section) = self
|
|
|
|
Box::new(self.symbols.iter().map(move |s| {
|
|
|
|
// .elf
|
|
|
|
// TODO: change SymbolIterBox to allow errors!
|
|
|
|
// .first_section_by(|s64| s64.header.get_type() == Type::StrTab && s64.name == ".strtab")
|
|
|
|
let name = self
|
|
|
|
// {
|
|
|
|
.resolve_name(s.st_name as usize, s.st_other as usize)
|
|
|
|
// let strtab = match &strtab_section.contents {
|
|
|
|
.expect("Symbol name resolution");
|
|
|
|
// Contents64::Raw(bytes) => bytes,
|
|
|
|
(name, self.make_symbol(s))
|
|
|
|
// _ => 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(self.make_symbol(sym, strtab))
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// _ => None,
|
|
|
|
|
|
|
|
// }))
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// _ => None,
|
|
|
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
// .flatten();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Box::new(iter)
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
Box::new(std::iter::empty())
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -221,12 +234,3 @@ fn parse_elf_header(data: &Mmap) -> Result<Ehdr64, Error> {
|
|
|
|
|
|
|
|
|
|
|
|
Ok(ehdr)
|
|
|
|
Ok(ehdr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_shstrtab(data: &Mmap, ehdr: &Ehdr64) -> Result<Shdr64, Error> {
|
|
|
|
|
|
|
|
let idx: usize = ehdr.e_shstrndx.into();
|
|
|
|
|
|
|
|
let sh_start: usize = ehdr.e_shoff as usize;
|
|
|
|
|
|
|
|
let sh_size: usize = ehdr.e_shentsize.into();
|
|
|
|
|
|
|
|
let offset: usize = sh_start + sh_size * idx;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bincode::deserialize(&data[offset..offset + sh_size]).map_err(|err| Error::ParseError(err))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|