use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use crate::error::{Error, Result};
use crate::ident::{ElfClass, Endian};
use crate::parse_utils::{array2, array4, array8};
use crate::strtab::StringTable;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SymbolBind {
Local,
Global,
Weak,
Other(u8),
}
impl SymbolBind {
#[must_use]
pub const fn from_u8(value: u8) -> Self {
match value {
0 => Self::Local,
1 => Self::Global,
2 => Self::Weak,
other => Self::Other(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::Local => 0,
Self::Global => 1,
Self::Weak => 2,
Self::Other(value) => value,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SymbolType {
NoType,
Object,
Func,
Section,
File,
Tls,
GnuIfunc,
Other(u8),
}
impl SymbolType {
#[must_use]
pub const fn from_u8(value: u8) -> Self {
match value {
0 => Self::NoType,
1 => Self::Object,
2 => Self::Func,
3 => Self::Section,
4 => Self::File,
6 => Self::Tls,
10 => Self::GnuIfunc,
other => Self::Other(other),
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::NoType => 0,
Self::Object => 1,
Self::Func => 2,
Self::Section => 3,
Self::File => 4,
Self::Tls => 6,
Self::GnuIfunc => 10,
Self::Other(value) => value,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SymbolVisibility {
Default,
Internal,
Hidden,
Protected,
}
impl SymbolVisibility {
#[must_use]
pub const fn from_u8(value: u8) -> Self {
match value & 0x03 {
0 => Self::Default,
1 => Self::Internal,
2 => Self::Hidden,
_ => Self::Protected,
}
}
#[must_use]
pub const fn to_u8(self) -> u8 {
match self {
Self::Default => 0,
Self::Internal => 1,
Self::Hidden => 2,
Self::Protected => 3,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Symbol {
pub name: String,
pub value: u64,
pub size: u64,
pub bind: SymbolBind,
pub sym_type: SymbolType,
pub visibility: SymbolVisibility,
pub shndx: u16,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SymbolTable {
pub symbols: Vec<Symbol>,
}
impl SymbolTable {
pub fn parse(
data: &[u8],
strtab: StringTable<'_>,
endian: Endian,
class: ElfClass,
) -> Result<Self> {
let entry_size = match class {
ElfClass::Elf64 => 24usize,
ElfClass::Elf32 => 16usize,
};
if !data.len().is_multiple_of(entry_size) {
return Err(Error::invalid_section(
"symbol table is not a whole number of entries",
));
}
let mut symbols = Vec::with_capacity(data.len() / entry_size);
for entry in data.chunks_exact(entry_size) {
let name_index = endian.u32(array4(entry, 0)?);
let (value, size, info, other, shndx) = match class {
ElfClass::Elf64 => (
endian.u64(array8(entry, 8)?),
endian.u64(array8(entry, 16)?),
entry[4],
entry[5],
endian.u16(array2(entry, 6)?),
),
ElfClass::Elf32 => (
u64::from(endian.u32(array4(entry, 4)?)),
u64::from(endian.u32(array4(entry, 8)?)),
entry[12],
entry[13],
endian.u16(array2(entry, 14)?),
),
};
symbols.push(Symbol {
name: strtab
.get(usize::try_from(name_index).unwrap_or(0))
.unwrap_or_default()
.to_string(),
value,
size,
bind: SymbolBind::from_u8(info >> 4),
sym_type: SymbolType::from_u8(info & 0x0f),
visibility: SymbolVisibility::from_u8(other & 0x03),
shndx,
});
}
Ok(Self { symbols })
}
#[must_use]
pub fn symbols(&self) -> &[Symbol] {
&self.symbols
}
#[must_use]
pub fn len(&self) -> usize {
self.symbols.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.symbols.is_empty()
}
}