nm_binutils/object/
symbol.rs

1use goblin::elf::{Elf, sym::*};
2
3/// Struct Symbol contains properties of object file's symbols.
4/// 
5/// ## Range
6/// - Binding: LOCAL, GLOBAL, WEAK, ...
7/// - Typ: NOTYPE, OBJECT, FUNC, SECTION, FILE, COMMON, TLS, ...
8/// 
9#[derive(Debug, Clone)]
10pub struct Symbol<'a> {
11    /// Symbol's name
12    pub name: &'a str,
13    /// Symbol's binding
14    pub binding: &'static str,
15    /// Symbol's type
16    pub typ: &'static str,
17    /// Symbol's visibility
18    pub visibility: &'static str,
19    /// Symbol's virtual address
20    pub address: u64,
21    /// Symbol's size
22    pub size: u64,
23}
24
25impl<'a> Symbol<'a> {
26    /// Construct a Symbol using given Elf and Sym.
27    pub fn new<'b> (elf: &'a Elf, sym: &'b Sym) -> Self {
28        Symbol {
29            name: Self::name(elf, sym),
30            binding: bind_to_str(sym.st_bind()),
31            typ: type_to_str(sym.st_type()),
32            visibility: visibility_to_str(sym.st_visibility()),
33            address: sym.st_value,
34            size: sym.st_size,
35        }
36    }
37
38    pub fn name<'b> (elf: &'b Elf, sym: &Sym) -> &'b str {
39        elf.strtab.get_at(sym.st_name).unwrap()
40    }
41}