Add Symbol.undefined() helper to test if a symbol is defined in this object or not

This commit is contained in:
Christopher Cole 2022-10-27 14:45:33 -07:00
parent 7513214a6a
commit e1d71d9447
No known key found for this signature in database
GPG Key ID: 0AC856975983E9DB

View File

@ -53,6 +53,14 @@ pub struct Symbol {
}
impl Symbol {
/// Returns true if a symbol is undefined in this ELF object.
///
/// When linking and loading, undefined symbols in this object get linked to
/// a defined symbol in another object.
pub fn undefined(&self) -> bool {
self.st_shndx == gabi::SHN_UNDEF
}
pub fn st_symtype(&self) -> SymbolType {
SymbolType(self.st_info & 0xf)
}
@ -182,6 +190,29 @@ mod table_tests {
const ELF32SYMSIZE: usize = 16;
const ELF64SYMSIZE: usize = 24;
#[test]
fn symbol_undefined() {
let undef_sym = Symbol {
st_name: 0,
st_value: 0,
st_size: 0,
st_shndx: 0,
st_info: 0,
st_other: 0,
};
assert!(undef_sym.undefined());
let def_sym = Symbol {
st_name: 0,
st_value: 0,
st_size: 0,
st_shndx: 42,
st_info: 0,
st_other: 0,
};
assert!(!def_sym.undefined());
}
#[test]
fn get_32_lsb() {
// All symbol tables are defined to have a zeroed out symbol at index 0.