Clean up symbol displaying
This commit is contained in:
parent
849f3c7dd2
commit
7f987f2077
@ -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
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<_, _>>();
|
||||
|
Loading…
Reference in New Issue
Block a user