From 87bceb3cb50bd108f060ef8f29ce077e28f8dbef Mon Sep 17 00:00:00 2001 From: Ales Katona Date: Wed, 31 Mar 2021 21:33:23 -0700 Subject: [PATCH] fix symbol name resolving --- src/formats/elf/object.rs | 13 +++++++++---- src/linker.rs | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/formats/elf/object.rs b/src/formats/elf/object.rs index 51b4158..30b6386 100644 --- a/src/formats/elf/object.rs +++ b/src/formats/elf/object.rs @@ -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) } diff --git a/src/linker.rs b/src/linker.rs index 9fd9735..e819a13 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -42,14 +42,14 @@ where } pub fn link(mut self) -> Result { - // 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() }