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