dlopen_rs/loader/
mod.rs

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
mod arch;
mod builtin;
#[cfg(feature = "debug")]
pub(crate) mod debug;
pub mod dso;
#[cfg(feature = "ldso")]
mod ldso;
mod relocation;

use core::{
    ffi::CStr,
    fmt::Debug,
    marker::{self, PhantomData},
    ops,
    sync::atomic::{AtomicBool, Ordering},
};

use alloc::{ffi::CString, format, sync::Arc, vec::Vec};
use dso::{
    symbol::{SymbolData, SymbolInfo},
    ExtraData,
};

use crate::{find_symbol_error, Result};
pub use dso::ELFLibrary;

#[allow(unused)]
#[derive(Debug)]
enum LibraryExtraData {
    Internal(ExtraData),
    #[cfg(feature = "ldso")]
    External(ldso::ExtraData),
}

impl Drop for LibraryExtraData {
    fn drop(&mut self) {
        #[allow(irrefutable_let_patterns)]
        if let LibraryExtraData::Internal(extra) = self {
            if let Some(fini) = extra.fini_fn() {
                fini();
            }
            if let Some(fini_array) = extra.fini_array_fn() {
                for fini in fini_array {
                    fini();
                }
            }
        }
    }
}

#[allow(unused)]
pub(crate) struct RelocatedLibraryInner {
    name: CString,
    base: usize,
    symbols: SymbolData,
    #[cfg(feature = "tls")]
    tls: Option<usize>,
    #[cfg(feature = "debug")]
    link_map: debug::DebugInfo,
    extra: LibraryExtraData,
}

impl Debug for RelocatedLibraryInner {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RelocatedLibrary")
            .field("name", &self.name)
            .field("base", &self.base)
            .field("extra", &self.extra)
            .finish()
    }
}

impl RelocatedLibraryInner {
    #[inline]
    pub(crate) fn symbols(&self) -> &SymbolData {
        &self.symbols
    }

    #[cfg(feature = "tls")]
    pub(crate) fn tls(&self) -> Option<usize> {
        self.tls
    }
}

#[derive(Clone)]
pub struct RelocatedLibrary {
    pub(crate) inner: Arc<(AtomicBool, RelocatedLibraryInner)>,
}

impl Debug for RelocatedLibrary {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.inner().fmt(f)
    }
}

unsafe impl Send for RelocatedLibrary {}
unsafe impl Sync for RelocatedLibrary {}

impl RelocatedLibrary {
    /// Retrieves the list of dependent libraries.
    ///
    /// This method returns an optional reference to a vector of `RelocatedLibrary` instances,
    /// which represent the libraries that the current dynamic library depends on.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// if let Some(dependencies) = library.dep_libs() {
    ///     for lib in dependencies {
    ///         println!("Dependency: {:?}", lib);
    ///     }
    /// } else {
    ///     println!("No dependencies found.");
    /// }
    /// ```
    pub fn dep_libs(&self) -> Option<&Vec<RelocatedLibrary>> {
        match &self.inner().extra {
            LibraryExtraData::Internal(extra_data) => extra_data.get_dep_libs(),
            #[cfg(feature = "ldso")]
            LibraryExtraData::External(_) => None,
        }
    }

    /// Retrieves the name of the dynamic library.
    ///
    /// This method returns a string slice that represents the name of the dynamic library.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let library_name = library.name();
    /// println!("The dynamic library name is: {}", library_name);
    /// ```
    pub fn name(&self) -> &str {
        self.inner().name.to_str().unwrap()
    }

    pub fn cname(&self) -> &CStr {
        &self.inner().name
    }

    #[allow(unused)]
    pub(crate) fn is_register(&self) -> bool {
        self.inner.0.load(Ordering::Relaxed)
    }

    #[allow(unused)]
    pub(crate) fn set_register(&self) {
        self.inner.0.store(true, Ordering::Relaxed);
    }

    #[cfg(feature = "std")]
    pub(crate) fn get_extra_data(&self) -> Option<&ExtraData> {
        match &self.inner().extra {
            LibraryExtraData::Internal(lib) => Some(lib),
            #[cfg(feature = "ldso")]
            LibraryExtraData::External(_) => None,
        }
    }

    pub(crate) fn inner(&self) -> &RelocatedLibraryInner {
        &self.inner.1
    }

    pub fn base(&self) -> usize {
        self.inner().base
    }

    /// Get a pointer to a function or static variable by symbol name.
    ///
    /// The symbol is interpreted as-is; no mangling is done. This means that symbols like `x::y` are
    /// most likely invalid.
    ///
    /// # Safety
    ///
    /// Users of this API must specify the correct type of the function or variable loaded.
    ///
    ///
    /// # Examples
    ///
    /// Given a loaded library:
    ///
    /// ```no_run
    /// # use ::dlopen_rs::ELFLibrary;
    /// let lib = ELFLibrary::from_file("/path/to/awesome.module")
    ///		.unwrap()
    ///		.relocate(&[])
    ///		.unwrap();
    /// ```
    ///
    /// Loading and using a function looks like this:
    ///
    /// ```no_run
    /// unsafe {
    ///     let awesome_function: Symbol<unsafe extern fn(f64) -> f64> =
    ///         lib.get("awesome_function").unwrap();
    ///     awesome_function(0.42);
    /// }
    /// ```
    ///
    /// A static variable may also be loaded and inspected:
    ///
    /// ```no_run
    /// unsafe {
    ///     let awesome_variable: Symbol<*mut f64> = lib.get("awesome_variable").unwrap();
    ///     **awesome_variable = 42.0;
    /// };
    /// ```
    pub unsafe fn get<'lib, T>(&'lib self, name: &str) -> Result<Symbol<'lib, T>> {
        self.inner()
            .symbols()
            .get_sym(&SymbolInfo::new(name))
            .map(|sym| Symbol {
                ptr: (self.base() + sym.st_value as usize) as _,
                pd: PhantomData,
            })
            .ok_or(find_symbol_error(format!("can not find symbol:{}", name)))
    }

    /// Attempts to load a versioned symbol from the dynamically-linked library.
    ///
    /// # Safety
    /// This function is unsafe because it involves raw pointer manipulation and
    /// dereferencing. The caller must ensure that the library handle is valid
    /// and that the symbol exists and has the correct type.
    ///
    /// # Parameters
    /// - `&'lib self`: A reference to the library instance from which the symbol will be loaded.
    /// - `name`: The name of the symbol to load.
    /// - `version`: The version of the symbol to load.
    ///
    /// # Returns
    /// If the symbol is found and has the correct type, this function returns
    /// `Ok(Symbol<'lib, T>)`, where `Symbol` is a wrapper around a raw function pointer.
    /// If the symbol cannot be found or an error occurs, it returns an `Err` with a message.
    ///
    /// # Examples
    /// ```
    /// let symbol = unsafe { lib.get_version::<fn()>>("function_name", "1.0").unwrap() };
    /// ```
    ///
    /// # Errors
    /// Returns a custom error if the symbol cannot be found, or if there is a problem
    /// retrieving the symbol.
    #[cfg(feature = "version")]
    pub unsafe fn get_version<'lib, T>(
        &'lib self,
        name: &str,
        version: &str,
    ) -> Result<Symbol<'lib, T>> {
        let version = dso::version::SymbolVersion::new(version);
        self.inner()
            .symbols()
            .get_sym(&SymbolInfo::new_with_version(name, version))
            .map(|sym| Symbol {
                ptr: (self.base() + sym.st_value as usize) as _,
                pd: PhantomData,
            })
            .ok_or(find_symbol_error(format!("can not find symbol:{}", name)))
    }
}

pub trait ExternLibrary {
    /// Get the symbol of the dynamic library, and the return value is the address of the symbol
    /// # Examples
    ///
    /// ```
    /// #[derive(Debug, Clone)]
    /// struct MyLib(Arc<Library>);
    ///
    /// impl ExternLibrary for MyLib {
    /// 	fn get_sym(&self, name: &str) -> Option<*const ()> {
    /// 		let sym: Option<*const ()> = unsafe {
    ///  			self.0.get::<*const usize>(name.as_bytes())
    ///				.map_or(None, |sym| Some(sym.into_raw().into_raw() as _))
    ///			};
    ///			sym
    /// 	}
    ///}
    /// ```
    fn get_sym(&self, name: &str) -> Option<*const ()>;
}

#[derive(Debug, Clone)]
pub struct Symbol<'lib, T: 'lib> {
    ptr: *mut (),
    pd: marker::PhantomData<&'lib T>,
}

impl<'lib, T> ops::Deref for Symbol<'lib, T> {
    type Target = T;
    fn deref(&self) -> &T {
        unsafe { &*(&self.ptr as *const *mut _ as *const T) }
    }
}