Move Symbol type definitions into symbol.rs

Also, remove types.rs now that its empty! Yay!
This commit is contained in:
Christopher Cole 2022-10-06 13:35:10 -07:00
parent c6e4073af4
commit 8427ef124e
2 changed files with 42 additions and 43 deletions

View File

@ -5,9 +5,9 @@ use std::io::{Read, Seek};
pub mod file;
pub mod gabi;
pub mod types;
pub mod segment;
pub mod section;
pub mod symbol;
pub mod parse;
use parse::Parse;
@ -122,7 +122,7 @@ impl File {
})
}
pub fn get_symbols(&self, section: &Section) -> Result<Vec<types::Symbol>, ParseError> {
pub fn get_symbols(&self, section: &Section) -> Result<Vec<symbol::Symbol>, ParseError> {
let mut symbols = Vec::new();
if section.shdr.sh_type == section::SectionType(gabi::SHT_SYMTAB) || section.shdr.sh_type == section::SectionType(gabi::SHT_DYNSYM) {
let link = &self.sections[section.shdr.sh_link as usize].data;
@ -134,7 +134,7 @@ impl File {
Ok(symbols)
}
fn parse_symbol<T: Read + Seek>(&self, io_section: &mut T, symbols: &mut Vec<types::Symbol>, link: &[u8]) -> Result<(), ParseError> {
fn parse_symbol<T: Read + Seek>(&self, io_section: &mut T, symbols: &mut Vec<symbol::Symbol>, link: &[u8]) -> Result<(), ParseError> {
let name: u32;
let value: u64;
let size: u64;
@ -158,14 +158,14 @@ impl File {
size = utils::read_u64(self.ehdr.endianness, io_section)?;
}
symbols.push(types::Symbol {
symbols.push(symbol::Symbol {
name: utils::get_string(link, name as usize)?,
value: value,
size: size,
shndx: shndx,
symtype: types::SymbolType(info[0] & 0xf),
bind: types::SymbolBind(info[0] >> 4),
vis: types::SymbolVis(other[0] & 0x3),
symtype: symbol::SymbolType(info[0] & 0xf),
bind: symbol::SymbolBind(info[0] >> 4),
vis: symbol::SymbolVis(other[0] & 0x3),
});
Ok(())
}

View File

@ -1,4 +1,31 @@
use std::fmt;
#[derive(Clone, PartialEq, Eq)]
pub struct Symbol {
/// Symbol name
pub name: String,
/// Symbol value
pub value: u64,
/// Symbol size
pub size: u64,
/// Section index
pub shndx: u16,
/// Symbol type
pub symtype: SymbolType,
/// Symbol binding
pub bind: SymbolBind,
/// Symbol visibility
pub vis: SymbolVis,
}
impl std::fmt::Display for Symbol {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"Symbol: Value: {:#010x} Size: {:#06x} Type: {} Bind: {} Vis: {} Section: {} Name: {}",
self.value, self.size, self.symtype, self.bind, self.vis, self.shndx, self.name
)
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct SymbolType(pub u8);
@ -19,8 +46,8 @@ pub const STT_TLS: SymbolType = SymbolType(6);
/// Indirect code object symbol
pub const STT_GNU_IFUNC: SymbolType = SymbolType(10);
impl fmt::Display for SymbolType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl std::fmt::Display for SymbolType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let str = match *self {
STT_NOTYPE => "unspecified",
STT_OBJECT => "data object",
@ -47,8 +74,8 @@ pub const STB_WEAK: SymbolBind = SymbolBind(2);
/// Unique symbol
pub const STB_GNU_UNIQUE: SymbolBind = SymbolBind(10);
impl fmt::Display for SymbolBind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl std::fmt::Display for SymbolBind {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let str = match *self {
STB_LOCAL => "local",
STB_GLOBAL => "global",
@ -71,8 +98,8 @@ pub const STV_HIDDEN: SymbolVis = SymbolVis(2);
/// Protected visibility
pub const STV_PROTECTED: SymbolVis = SymbolVis(3);
impl fmt::Display for SymbolVis {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl std::fmt::Display for SymbolVis {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let str = match *self {
STV_DEFAULT => "default",
STV_INTERNAL => "internal",
@ -82,32 +109,4 @@ impl fmt::Display for SymbolVis {
};
write!(f, "{}", str)
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct Symbol {
/// Symbol name
pub name: String,
/// Symbol value
pub value: u64,
/// Symbol size
pub size: u64,
/// Section index
pub shndx: u16,
/// Symbol type
pub symtype: SymbolType,
/// Symbol binding
pub bind: SymbolBind,
/// Symbol visibility
pub vis: SymbolVis,
}
impl fmt::Display for Symbol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Symbol: Value: {:#010x} Size: {:#06x} Type: {} Bind: {} Vis: {} Section: {} Name: {}",
self.value, self.size, self.symtype, self.bind, self.vis, self.shndx, self.name
)
}
}
}