|
|
@ -21,6 +21,7 @@ struct ElfSymbol {
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ElfObject {
|
|
|
|
pub struct ElfObject {
|
|
|
|
object_index: usize,
|
|
|
|
object_index: usize,
|
|
|
|
|
|
|
|
strtab_idx: usize, // .strtab internal index
|
|
|
|
origin: PathBuf,
|
|
|
|
origin: PathBuf,
|
|
|
|
data: Mmap,
|
|
|
|
data: Mmap,
|
|
|
|
ehdr: Ehdr64,
|
|
|
|
ehdr: Ehdr64,
|
|
|
@ -38,9 +39,11 @@ 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 strtab_idx = 0;
|
|
|
|
|
|
|
|
|
|
|
|
let mut result = ElfObject {
|
|
|
|
let mut result = ElfObject {
|
|
|
|
object_index,
|
|
|
|
object_index,
|
|
|
|
|
|
|
|
strtab_idx,
|
|
|
|
origin,
|
|
|
|
origin,
|
|
|
|
data,
|
|
|
|
data,
|
|
|
|
ehdr,
|
|
|
|
ehdr,
|
|
|
@ -78,9 +81,9 @@ impl ElfObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn resolve_name(&self, name_idx: usize, sh_index: usize) -> Result<&str, Error> {
|
|
|
|
fn resolve_name(&self, name_idx: usize, strtab_idx: usize) -> Result<&str, Error> {
|
|
|
|
if name_idx == 0 {
|
|
|
|
if name_idx == 0 {
|
|
|
|
let error = if sh_index == self.ehdr.e_shstrndx.into() {
|
|
|
|
let error = if strtab_idx == self.ehdr.e_shstrndx.into() {
|
|
|
|
Error::InvalidSectionName
|
|
|
|
Error::InvalidSectionName
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Error::InvalidSymbolName
|
|
|
|
Error::InvalidSymbolName
|
|
|
@ -89,7 +92,7 @@ impl ElfObject {
|
|
|
|
return Err(error);
|
|
|
|
return Err(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let strtab = self.parse_strtab(sh_index)?;
|
|
|
|
let strtab = self.parse_strtab(strtab_idx)?;
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
@ -130,6 +133,7 @@ impl ElfObject {
|
|
|
|
match shdr.get_type() {
|
|
|
|
match shdr.get_type() {
|
|
|
|
Type::SymTab => self.populate_symtab(&shdr, i as usize)?,
|
|
|
|
Type::SymTab => self.populate_symtab(&shdr, i as usize)?,
|
|
|
|
Type::Rel | Type::Rela => self.populate_rela(&shdr)?,
|
|
|
|
Type::Rel | Type::Rela => self.populate_rela(&shdr)?,
|
|
|
|
|
|
|
|
Type::StrTab => self.strtab_idx = self.shdrs.len(),
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -236,7 +240,8 @@ 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.s64.st_name as usize, symbol.sh_index)
|
|
|
|
let name_idx = symbol.s64.st_name as usize;
|
|
|
|
|
|
|
|
self.resolve_name(name_idx, self.strtab_idx)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidSymbolIndex)
|
|
|
|
Err(Error::InvalidSymbolIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|