1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use tracing::trace;

use super::{Context, LoadedOrUnloaded};
use crate::{
    library::{Library, LibraryId},
    symbol::{LookupFlags, RelocatedSymbol},
    DynlinkError, DynlinkErrorKind,
};

impl Context {
    /// Search for a symbol, starting from library denoted by start_id. For normal symbol lookup,
    /// this should be the ID of the library that needs a symbol looked up. Flags can be
    /// specified which allow control over where to look for the symbol.
    pub fn lookup_symbol<'a>(
        &'a self,
        start_id: LibraryId,
        name: &str,
        lookup_flags: LookupFlags,
    ) -> Result<RelocatedSymbol<'a>, DynlinkError> {
        let start_lib = self.get_library(start_id)?;
        // First try looking up within ourselves.
        if !lookup_flags.contains(LookupFlags::SKIP_SELF) {
            if let Ok(sym) = start_lib.lookup_symbol(name) {
                return Ok(sym);
            }
        }

        // Next, try all of our transitive dependencies.
        if !lookup_flags.contains(LookupFlags::SKIP_DEPS) {
            let mut visit = petgraph::visit::Bfs::new(&self.library_deps, start_id.0);
            while let Some(node) = visit.next(&self.library_deps) {
                let dep = &self.library_deps[node];
                if node != start_id.0 {
                    match dep {
                        LoadedOrUnloaded::Unloaded(_) => {}
                        LoadedOrUnloaded::Loaded(dep) => {
                            if lookup_flags.contains(LookupFlags::SKIP_SECGATE_CHECK)
                                || dep.is_local_or_secgate_from(start_lib, name)
                            {
                                if let Ok(sym) = dep.lookup_symbol(name) {
                                    return Ok(sym);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Fall back to global search.
        if !lookup_flags.contains(LookupFlags::SKIP_GLOBAL) {
            trace!("falling back to global search for {}", name);
            self.lookup_symbol_global(start_lib, name, lookup_flags)
        } else {
            Err(DynlinkErrorKind::NameNotFound {
                name: name.to_string(),
            }
            .into())
        }
    }

    pub(crate) fn lookup_symbol_global<'a>(
        &'a self,
        start_lib: &Library,
        name: &str,
        lookup_flags: LookupFlags,
    ) -> Result<RelocatedSymbol<'a>, DynlinkError> {
        for idx in self.library_deps.node_indices() {
            let dep = &self.library_deps[idx];
            match dep {
                LoadedOrUnloaded::Unloaded(_) => {}
                LoadedOrUnloaded::Loaded(dep) => {
                    if lookup_flags.contains(LookupFlags::SKIP_SECGATE_CHECK)
                        || dep.is_local_or_secgate_from(start_lib, name)
                    {
                        if let Ok(sym) = dep.lookup_symbol(name) {
                            return Ok(sym);
                        }
                    }
                }
            }
        }
        Err(DynlinkErrorKind::NameNotFound {
            name: name.to_string(),
        }
        .into())
    }
}