Clean up symbol displaying

This commit is contained in:
pjht 2023-12-20 13:11:16 -06:00
parent 849f3c7dd2
commit 7f987f2077
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
2 changed files with 19 additions and 7 deletions

View File

@ -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<ElfSymbol> 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<ElfSymbol> 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
))
}
}
}

View File

@ -60,6 +60,9 @@ impl SymbolTable {
pub fn read_from_file(path: &str) -> anyhow::Result<Self> {
let mut file = ElfStream::<AnyEndian, _>::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::<Vec<_>>();
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::<HashMap<_, _>>();