#[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,
}Expand description
One symbol: a name, what it names, where it is defined, and who can see it.
value is an offset within the symbol’s section in a relocatable object, and an
address in an executable. For a Common symbol it is the
required alignment. size is the size in bytes of what the symbol names, or zero
when unknown.
The fields are public; the constructors cover the common shapes. The struct is
#[non_exhaustive], so build one with a constructor and adjust fields or chain the
with_* methods.
§Examples
use object_lang::{
Architecture, Binding, Object, Section, SectionKind, Symbol, SymbolKind, Visibility,
};
let mut obj = Object::relocatable(Architecture::X86_64);
let text = obj.add_section(Section::new(".text", SectionKind::Text).with_data(vec![0xc3; 8]));
let helper = Symbol::function("helper", text, 4, 4)
.with_binding(Binding::Local);
let api = Symbol::function("api", text, 0, 4).with_visibility(Visibility::Protected);
obj.add_symbol(helper);
obj.add_symbol(api);
assert!(obj.validate().is_ok());
assert_eq!(obj.symbols()[0].kind, SymbolKind::Function);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.name: StringThe symbol’s name. Must not contain a NUL byte.
kind: SymbolKindWhat the symbol names.
binding: BindingWho can see the symbol.
visibility: VisibilityHow far a non-local symbol is exported.
section: SymbolSectionWhere the symbol is defined.
value: u64The offset in the section (relocatable), the address (executable), or the
alignment (Common).
size: u64The size in bytes of what the symbol names, or zero.
Implementations§
Source§impl Symbol
impl Symbol
Sourcepub fn new(
name: impl Into<String>,
kind: SymbolKind,
binding: Binding,
section: SymbolSection,
value: u64,
size: u64,
) -> Self
pub fn new( name: impl Into<String>, kind: SymbolKind, binding: Binding, section: SymbolSection, value: u64, size: u64, ) -> Self
A symbol with every property given; the other constructors are shorthands.
Visibility is Default.
§Examples
use object_lang::{Binding, Symbol, SymbolKind, SymbolSection};
let sym = Symbol::new("x", SymbolKind::Data, Binding::Weak, SymbolSection::Undefined, 0, 0);
assert_eq!(sym.binding, Binding::Weak);Sourcepub fn function(
name: impl Into<String>,
section: SectionId,
value: u64,
size: u64,
) -> Self
pub fn function( name: impl Into<String>, section: SectionId, value: u64, size: u64, ) -> Self
A global function defined at value in section, size bytes long.
§Examples
use object_lang::{Architecture, Object, Section, SectionKind, Symbol, SymbolKind};
let mut obj = Object::relocatable(Architecture::X86_64);
let text = obj.add_section(Section::new(".text", SectionKind::Text).with_data(vec![0xc3]));
let main = Symbol::function("main", text, 0, 1);
assert_eq!(main.kind, SymbolKind::Function);Sourcepub fn data(
name: impl Into<String>,
section: SectionId,
value: u64,
size: u64,
) -> Self
pub fn data( name: impl Into<String>, section: SectionId, value: u64, size: u64, ) -> Self
A global data object defined at value in section, size bytes long.
§Examples
use object_lang::{Architecture, Object, Section, SectionKind, Symbol, SymbolKind};
let mut obj = Object::relocatable(Architecture::X86_64);
let data = obj.add_section(Section::new(".data", SectionKind::Data).with_data(vec![0; 4]));
assert_eq!(Symbol::data("counter", data, 0, 4).kind, SymbolKind::Data);Sourcepub fn undefined(name: impl Into<String>) -> Self
pub fn undefined(name: impl Into<String>) -> Self
A global reference to a symbol another object defines.
§Examples
use object_lang::{Symbol, SymbolSection};
assert_eq!(Symbol::undefined("malloc").section, SymbolSection::Undefined);Sourcepub fn absolute(name: impl Into<String>, value: u64) -> Self
pub fn absolute(name: impl Into<String>, value: u64) -> Self
A global constant value that relocation does not change.
§Examples
use object_lang::Symbol;
assert_eq!(Symbol::absolute("STACK_SIZE", 0x10_0000).value, 0x10_0000);Sourcepub fn with_binding(self, binding: Binding) -> Self
pub fn with_binding(self, binding: Binding) -> Self
self with a different binding.
§Examples
use object_lang::{Binding, Symbol};
assert_eq!(Symbol::undefined("f").with_binding(Binding::Weak).binding, Binding::Weak);Sourcepub fn with_visibility(self, visibility: Visibility) -> Self
pub fn with_visibility(self, visibility: Visibility) -> Self
self with a different visibility.
§Examples
use object_lang::{Symbol, Visibility};
let s = Symbol::undefined("f").with_visibility(Visibility::Hidden);
assert_eq!(s.visibility, Visibility::Hidden);Sourcepub fn with_kind(self, kind: SymbolKind) -> Self
pub fn with_kind(self, kind: SymbolKind) -> Self
self with a different kind.
§Examples
use object_lang::{Symbol, SymbolKind};
assert_eq!(Symbol::undefined("f").with_kind(SymbolKind::Function).kind, SymbolKind::Function);