dynlink/
symbol.rs

1//! Definitions for symbols in the dynamic linker.
2
3use crate::library::Library;
4
5/// A (relocated) symbol. Contains information about the symbol itself, like value and size, along
6/// with a reference to the library that it comes from.
7pub struct RelocatedSymbol<'lib> {
8    sym: Option<elf::symbol::Symbol>,
9    pub(crate) lib: &'lib Library,
10}
11
12impl<'lib> RelocatedSymbol<'lib> {
13    pub(crate) fn new(sym: elf::symbol::Symbol, lib: &'lib Library) -> Self {
14        Self {
15            sym: Some(sym),
16            lib,
17        }
18    }
19
20    #[allow(dead_code)]
21    pub(crate) fn new_zero(lib: &'lib Library) -> Self {
22        Self { sym: None, lib }
23    }
24
25    /// Returns the relocated address of the symbol, i.e. the value of the symbol added to the base
26    /// address of the library it comes from.
27    pub fn reloc_value(&self) -> u64 {
28        if self.sym.is_none() {
29            return 0;
30        }
31        self.raw_value() + self.lib.base_addr() as u64
32    }
33
34    /// Returns the raw symbol value (unrelocated).
35    pub fn raw_value(&self) -> u64 {
36        self.sym.as_ref().map_or(0, |v| v.st_value)
37    }
38
39    /// Returns the symbol's size.
40    pub fn size(&self) -> u64 {
41        self.sym.as_ref().map_or(0, |v| v.st_size)
42    }
43}
44
45bitflags::bitflags! {
46    #[derive(Copy, Clone)]
47    /// Options for use during symbol lookup. If all of these flags are specified together, the search will fail.
48    pub struct LookupFlags : u32 {
49        /// Look elsewhere first. Note that the symbol may still bind to us if the dep graph has a cycle.
50        const SKIP_SELF = 1;
51        /// Don't look through dependencies, go straight to global search.
52        const SKIP_DEPS = 2;
53        /// Don't do a global search.
54        const SKIP_GLOBAL = 4;
55        /// Allow any symbols, not just secgates.
56        const SKIP_SECGATE_CHECK = 8;
57        /// Allow lookup to include weak symbols.
58        const ALLOW_WEAK = 0x10;
59    }
60}