chaud_hot/dylib.rs
1use crate::util::etx;
2use anyhow::{Context as _, Result};
3use camino::Utf8Path;
4use core::ffi::c_int;
5use core::mem;
6use libloading::os::unix as ll;
7
8// "deep bind" ensure that a dylib being loaded will prefer its own symbols
9// over symbols available at the global scope.
10#[cfg(target_os = "linux")]
11const DEEP_BIND: c_int = libc::RTLD_DEEPBIND;
12
13#[cfg(not(target_os = "linux"))]
14const DEEP_BIND: c_int = 0;
15
16pub fn load(path: &Utf8Path) -> Result<()> {
17 // SAFETY: We cannot guarantee anything about the initialization routines.
18 // This is covered under the `unsafe-hot-reload` feature opt-in. We'll never
19 // run termination routines.
20 let lib = unsafe { ll::Library::open(Some(path), ll::RTLD_GLOBAL | ll::RTLD_NOW | DEEP_BIND) };
21
22 let lib = lib.with_context(etx!("Failed to load {path:?}"))?;
23
24 mem::forget(lib);
25
26 Ok(())
27}