]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/weak.rs
Rollup merge of #76635 - scottmcm:slice-as-chunks, r=LukasKalbertodt
[rust.git] / library / std / src / sys / unix / weak.rs
1 //! Support for "weak linkage" to symbols on Unix
2 //!
3 //! Some I/O operations we do in libstd require newer versions of OSes but we
4 //! need to maintain binary compatibility with older releases for now. In order
5 //! to use the new functionality when available we use this module for
6 //! detection.
7 //!
8 //! One option to use here is weak linkage, but that is unfortunately only
9 //! really workable on Linux. Hence, use dlsym to get the symbol value at
10 //! runtime. This is also done for compatibility with older versions of glibc,
11 //! and to avoid creating dependencies on GLIBC_PRIVATE symbols. It assumes that
12 //! we've been dynamically linked to the library the symbol comes from, but that
13 //! is currently always the case for things like libpthread/libc.
14 //!
15 //! A long time ago this used weak linkage for the __pthread_get_minstack
16 //! symbol, but that caused Debian to detect an unnecessarily strict versioned
17 //! dependency on libc6 (#23628).
18
19 // There are a variety of `#[cfg]`s controlling which targets are involved in
20 // each instance of `weak!` and `syscall!`. Rather than trying to unify all of
21 // that, we'll just allow that some unix targets don't use this module at all.
22 #![allow(dead_code, unused_macros)]
23
24 use crate::ffi::CStr;
25 use crate::marker;
26 use crate::mem;
27 use crate::sync::atomic::{AtomicUsize, Ordering};
28
29 macro_rules! weak {
30     (fn $name:ident($($t:ty),*) -> $ret:ty) => (
31         static $name: crate::sys::weak::Weak<unsafe extern fn($($t),*) -> $ret> =
32             crate::sys::weak::Weak::new(concat!(stringify!($name), '\0'));
33     )
34 }
35
36 pub struct Weak<F> {
37     name: &'static str,
38     addr: AtomicUsize,
39     _marker: marker::PhantomData<F>,
40 }
41
42 impl<F> Weak<F> {
43     pub const fn new(name: &'static str) -> Weak<F> {
44         Weak { name, addr: AtomicUsize::new(1), _marker: marker::PhantomData }
45     }
46
47     pub fn get(&self) -> Option<F> {
48         assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
49         unsafe {
50             if self.addr.load(Ordering::SeqCst) == 1 {
51                 self.addr.store(fetch(self.name), Ordering::SeqCst);
52             }
53             match self.addr.load(Ordering::SeqCst) {
54                 0 => None,
55                 addr => Some(mem::transmute_copy::<usize, F>(&addr)),
56             }
57         }
58     }
59 }
60
61 unsafe fn fetch(name: &str) -> usize {
62     let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
63         Ok(cstr) => cstr,
64         Err(..) => return 0,
65     };
66     libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize
67 }
68
69 #[cfg(not(target_os = "linux"))]
70 macro_rules! syscall {
71     (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
72         unsafe fn $name($($arg_name: $t),*) -> $ret {
73             use super::os;
74
75             weak! { fn $name($($t),*) -> $ret }
76
77             if let Some(fun) = $name.get() {
78                 fun($($arg_name),*)
79             } else {
80                 os::set_errno(libc::ENOSYS);
81                 -1
82             }
83         }
84     )
85 }
86
87 #[cfg(target_os = "linux")]
88 macro_rules! syscall {
89     (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
90         unsafe fn $name($($arg_name:$t),*) -> $ret {
91             // This looks like a hack, but concat_idents only accepts idents
92             // (not paths).
93             use libc::*;
94
95             syscall(
96                 concat_idents!(SYS_, $name),
97                 $($arg_name as c_long),*
98             ) as $ret
99         }
100     )
101 }