]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/weak.rs
Rollup merge of #58802 - nnethercote:inline-record_layout, r=oli-obk
[rust.git] / src / libstd / 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 use crate::ffi::CStr;
20 use crate::marker;
21 use crate::mem;
22 use crate::sync::atomic::{AtomicUsize, Ordering};
23
24 macro_rules! weak {
25     (fn $name:ident($($t:ty),*) -> $ret:ty) => (
26         static $name: crate::sys::weak::Weak<unsafe extern fn($($t),*) -> $ret> =
27             crate::sys::weak::Weak::new(concat!(stringify!($name), '\0'));
28     )
29 }
30
31 pub struct Weak<F> {
32     name: &'static str,
33     addr: AtomicUsize,
34     _marker: marker::PhantomData<F>,
35 }
36
37 impl<F> Weak<F> {
38     pub const fn new(name: &'static str) -> Weak<F> {
39         Weak {
40             name,
41             addr: AtomicUsize::new(1),
42             _marker: marker::PhantomData,
43         }
44     }
45
46     pub fn get(&self) -> Option<F> {
47         assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
48         unsafe {
49             if self.addr.load(Ordering::SeqCst) == 1 {
50                 self.addr.store(fetch(self.name), Ordering::SeqCst);
51             }
52             match self.addr.load(Ordering::SeqCst) {
53                 0 => None,
54                 addr => Some(mem::transmute_copy::<usize, F>(&addr)),
55             }
56         }
57     }
58 }
59
60 unsafe fn fetch(name: &str) -> usize {
61     let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
62         Ok(cstr) => cstr,
63         Err(..) => return 0,
64     };
65     libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize
66 }
67
68 #[cfg(not(target_os = "linux"))]
69 macro_rules! syscall {
70     (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
71         unsafe fn $name($($arg_name: $t),*) -> $ret {
72             use super::os;
73
74             weak! { fn $name($($t),*) -> $ret }
75
76             if let Some(fun) = $name.get() {
77                 fun($($arg_name),*)
78             } else {
79                 os::set_errno(libc::ENOSYS);
80                 -1
81             }
82         }
83     )
84 }
85
86 #[cfg(target_os = "linux")]
87 macro_rules! syscall {
88     (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
89         unsafe fn $name($($arg_name:$t),*) -> $ret {
90             // This looks like a hack, but concat_idents only accepts idents
91             // (not paths).
92             use libc::*;
93
94             syscall(
95                 concat_idents!(SYS_, $name),
96                 $($arg_name as c_long),*
97             ) as $ret
98         }
99     )
100 }