use alloc::string::String;
use crate::model::SectionId;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum SymbolKind {
NoType,
Function,
Data,
Tls,
File,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Binding {
Local,
Global,
Weak,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Visibility {
Default,
Hidden,
Protected,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum SymbolSection {
Undefined,
Absolute,
Common,
Section(SectionId),
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub struct Symbol {
pub name: String,
pub kind: SymbolKind,
pub binding: Binding,
pub visibility: Visibility,
pub section: SymbolSection,
pub value: u64,
pub size: u64,
}
impl Symbol {
#[must_use]
pub fn new(
name: impl Into<String>,
kind: SymbolKind,
binding: Binding,
section: SymbolSection,
value: u64,
size: u64,
) -> Self {
Symbol {
name: name.into(),
kind,
binding,
visibility: Visibility::Default,
section,
value,
size,
}
}
#[must_use]
pub fn function(name: impl Into<String>, section: SectionId, value: u64, size: u64) -> Self {
Symbol::new(
name,
SymbolKind::Function,
Binding::Global,
SymbolSection::Section(section),
value,
size,
)
}
#[must_use]
pub fn data(name: impl Into<String>, section: SectionId, value: u64, size: u64) -> Self {
Symbol::new(
name,
SymbolKind::Data,
Binding::Global,
SymbolSection::Section(section),
value,
size,
)
}
#[must_use]
pub fn undefined(name: impl Into<String>) -> Self {
Symbol::new(
name,
SymbolKind::NoType,
Binding::Global,
SymbolSection::Undefined,
0,
0,
)
}
#[must_use]
pub fn absolute(name: impl Into<String>, value: u64) -> Self {
Symbol::new(
name,
SymbolKind::NoType,
Binding::Global,
SymbolSection::Absolute,
value,
0,
)
}
#[must_use]
pub fn common(name: impl Into<String>, size: u64, align: u64) -> Self {
Symbol::new(
name,
SymbolKind::Data,
Binding::Global,
SymbolSection::Common,
align,
size,
)
}
#[must_use]
pub fn file(name: impl Into<String>) -> Self {
Symbol::new(
name,
SymbolKind::File,
Binding::Local,
SymbolSection::Absolute,
0,
0,
)
}
#[must_use]
pub fn with_binding(mut self, binding: Binding) -> Self {
self.binding = binding;
self
}
#[must_use]
pub fn with_visibility(mut self, visibility: Visibility) -> Self {
self.visibility = visibility;
self
}
#[must_use]
pub fn with_kind(mut self, kind: SymbolKind) -> Self {
self.kind = kind;
self
}
}