From 7f987f2077f1c57250a610589827090d8ac59681 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 20 Dec 2023 13:11:16 -0600 Subject: [PATCH] Clean up symbol displaying --- src/symbol.rs | 21 +++++++++++++++------ src/symbol_table.rs | 5 ++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/symbol.rs b/src/symbol.rs index 4768502..f8bc521 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -4,6 +4,7 @@ use elf::symbol::Symbol as ElfSymbol; #[derive(Debug)] pub struct Symbol { + sect_name: String, section: u16, value: u32, size: u32, @@ -25,9 +26,10 @@ impl Symbol { } } -impl From for Symbol { - fn from(sym: ElfSymbol) -> Self { +impl Symbol { + pub fn new(sym: &ElfSymbol, sect_name: String) -> Self { Self { + sect_name, section: sym.st_shndx, value: sym.st_value as u32, size: sym.st_size as u32, @@ -37,9 +39,16 @@ impl From for Symbol { impl Display for Symbol { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!( - "Value: {:#010x} Size: {:#06x} Section: {}", - self.value, self.size, self.section - )) + if self.size == 0 { + f.write_fmt(format_args!( + "{:#010x} ({})", + self.value, self.sect_name + )) + } else { + f.write_fmt(format_args!( + "{:#010x}..{:#010x} ({})", + self.value, self.value+self.size, self.sect_name + )) + } } } diff --git a/src/symbol_table.rs b/src/symbol_table.rs index d78cef7..18f088e 100644 --- a/src/symbol_table.rs +++ b/src/symbol_table.rs @@ -60,6 +60,9 @@ impl SymbolTable { pub fn read_from_file(path: &str) -> anyhow::Result { let mut file = ElfStream::::open_stream(File::open(path)?)?; + let (sect_headers, shstrtab) = file.section_headers_with_strtab()?; + let shstrtab = shstrtab.unwrap(); + let sect_names = sect_headers.iter().map(|sect| shstrtab.get(sect.sh_name as usize).unwrap().to_string()).collect::>(); let (symtab, symstrtab) = file .symbol_table()? .ok_or_else(|| anyhow!("No symbol table in {}", path))?; @@ -70,7 +73,7 @@ impl SymbolTable { .map(|sym| { ( symstrtab.get(sym.st_name as usize).unwrap().to_string(), - Symbol::from(sym), + Symbol::new(&sym, sect_names[sym.st_shndx as usize].to_string()), ) }) .collect::>();