fix symbol name resolving

master
Ales Katona 4 years ago
parent 1644e67242
commit 87bceb3cb5
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

@ -21,6 +21,7 @@ struct ElfSymbol {
pub struct ElfObject {
object_index: usize,
strtab_idx: usize, // .strtab internal index
origin: PathBuf,
data: Mmap,
ehdr: Ehdr64,
@ -38,9 +39,11 @@ impl ElfObject {
let file = File::open(str_origin)?;
let data = unsafe { Mmap::map(&file)? };
let ehdr = parse_elf_header(&data)?;
let strtab_idx = 0;
let mut result = ElfObject {
object_index,
strtab_idx,
origin,
data,
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 {
let error = if sh_index == self.ehdr.e_shstrndx.into() {
let error = if strtab_idx == self.ehdr.e_shstrndx.into() {
Error::InvalidSectionName
} else {
Error::InvalidSymbolName
@ -89,7 +92,7 @@ impl ElfObject {
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 start = strtab_offset + name_idx;
@ -130,6 +133,7 @@ impl ElfObject {
match shdr.get_type() {
Type::SymTab => self.populate_symtab(&shdr, i as usize)?,
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> {
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 {
Err(Error::InvalidSymbolIndex)
}

@ -42,14 +42,14 @@ where
}
pub fn link(mut self) -> Result<PathBuf, Error> {
// self.process_symbols()?;
self.process_symbols()?;
self.loadable.set_start_offset(4096); // TODO
self.output
.relocate_sections(&self.objects, &mut self.loadable)?;
// self.output
// .resolve_symbols(&self.objects, &mut self.loadable)?;
self.output
.resolve_symbols(&self.objects, &mut self.loadable)?;
self.output.finalize()
}

Loading…
Cancel
Save