]>
Commit | Line | Data |
---|---|---|
7453a54e SL |
1 | //! Support for "weak linkage" to symbols on Unix |
2 | //! | |
9c376795 FG |
3 | //! Some I/O operations we do in std require newer versions of OSes but we need |
4 | //! to maintain binary compatibility with older releases for now. In order to | |
5 | //! use the new functionality when available we use this module for detection. | |
7453a54e SL |
6 | //! |
7 | //! One option to use here is weak linkage, but that is unfortunately only | |
a2a8927a | 8 | //! really workable with ELF. Otherwise, use dlsym to get the symbol value at |
7453a54e SL |
9 | //! runtime. This is also done for compatibility with older versions of glibc, |
10 | //! and to avoid creating dependencies on GLIBC_PRIVATE symbols. It assumes that | |
11 | //! we've been dynamically linked to the library the symbol comes from, but that | |
12 | //! is currently always the case for things like libpthread/libc. | |
13 | //! | |
14 | //! A long time ago this used weak linkage for the __pthread_get_minstack | |
15 | //! symbol, but that caused Debian to detect an unnecessarily strict versioned | |
a2a8927a XL |
16 | //! dependency on libc6 (#23628) because it is GLIBC_PRIVATE. We now use `dlsym` |
17 | //! for a runtime lookup of that symbol to avoid the ELF versioned dependency. | |
7453a54e | 18 | |
3dfed10e XL |
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 | ||
532ac7d7 | 24 | use crate::ffi::CStr; |
a2a8927a | 25 | use crate::marker::PhantomData; |
532ac7d7 | 26 | use crate::mem; |
04454e1e FG |
27 | use crate::ptr; |
28 | use crate::sync::atomic::{self, AtomicPtr, Ordering}; | |
7453a54e | 29 | |
a2a8927a | 30 | // We can use true weak linkage on ELF targets. |
fe692bf9 | 31 | #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "tvos")))] |
487cf647 FG |
32 | pub(crate) macro weak { |
33 | (fn $name:ident($($t:ty),*) -> $ret:ty) => ( | |
34 | let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = { | |
35 | extern "C" { | |
36 | #[linkage = "extern_weak"] | |
37 | static $name: Option<unsafe extern "C" fn($($t),*) -> $ret>; | |
38 | } | |
39 | #[allow(unused_unsafe)] | |
40 | ExternWeak::new(unsafe { $name }) | |
41 | }; | |
42 | ) | |
43 | } | |
44 | ||
a2a8927a | 45 | // On non-ELF targets, use the dlsym approximation of weak linkage. |
fe692bf9 | 46 | #[cfg(any(target_os = "macos", target_os = "ios", target_os = "tvos"))] |
a2a8927a XL |
47 | pub(crate) use self::dlsym as weak; |
48 | ||
487cf647 FG |
49 | pub(crate) struct ExternWeak<F: Copy> { |
50 | weak_ptr: Option<F>, | |
51 | } | |
52 | ||
487cf647 FG |
53 | impl<F: Copy> ExternWeak<F> { |
54 | #[inline] | |
55 | pub(crate) fn new(weak_ptr: Option<F>) -> Self { | |
56 | ExternWeak { weak_ptr } | |
57 | } | |
58 | ||
59 | #[inline] | |
60 | pub(crate) fn get(&self) -> Option<F> { | |
61 | self.weak_ptr | |
62 | } | |
63 | } | |
64 | ||
a2a8927a XL |
65 | pub(crate) macro dlsym { |
66 | (fn $name:ident($($t:ty),*) -> $ret:ty) => ( | |
67 | dlsym!(fn $name($($t),*) -> $ret, stringify!($name)); | |
3c0e092e XL |
68 | ), |
69 | (fn $name:ident($($t:ty),*) -> $ret:ty, $sym:expr) => ( | |
a2a8927a XL |
70 | static DLSYM: DlsymWeak<unsafe extern "C" fn($($t),*) -> $ret> = |
71 | DlsymWeak::new(concat!($sym, '\0')); | |
72 | let $name = &DLSYM; | |
7453a54e SL |
73 | ) |
74 | } | |
a2a8927a | 75 | pub(crate) struct DlsymWeak<F> { |
7453a54e | 76 | name: &'static str, |
04454e1e | 77 | func: AtomicPtr<libc::c_void>, |
a2a8927a | 78 | _marker: PhantomData<F>, |
7453a54e SL |
79 | } |
80 | ||
a2a8927a XL |
81 | impl<F> DlsymWeak<F> { |
82 | pub(crate) const fn new(name: &'static str) -> Self { | |
04454e1e | 83 | DlsymWeak { name, func: AtomicPtr::new(ptr::invalid_mut(1)), _marker: PhantomData } |
7453a54e SL |
84 | } |
85 | ||
a2a8927a XL |
86 | #[inline] |
87 | pub(crate) fn get(&self) -> Option<F> { | |
7453a54e | 88 | unsafe { |
fc512014 XL |
89 | // Relaxed is fine here because we fence before reading through the |
90 | // pointer (see the comment below). | |
04454e1e FG |
91 | match self.func.load(Ordering::Relaxed) { |
92 | func if func.addr() == 1 => self.initialize(), | |
93 | func if func.is_null() => None, | |
94 | func => { | |
95 | let func = mem::transmute_copy::<*mut libc::c_void, F>(&func); | |
fc512014 XL |
96 | // The caller is presumably going to read through this value |
97 | // (by calling the function we've dlsymed). This means we'd | |
98 | // need to have loaded it with at least C11's consume | |
99 | // ordering in order to be guaranteed that the data we read | |
100 | // from the pointer isn't from before the pointer was | |
101 | // stored. Rust has no equivalent to memory_order_consume, | |
102 | // so we use an acquire fence (sorry, ARM). | |
103 | // | |
104 | // Now, in practice this likely isn't needed even on CPUs | |
105 | // where relaxed and consume mean different things. The | |
106 | // symbols we're loading are probably present (or not) at | |
107 | // init, and even if they aren't the runtime dynamic loader | |
108 | // is extremely likely have sufficient barriers internally | |
109 | // (possibly implicitly, for example the ones provided by | |
110 | // invoking `mprotect`). | |
111 | // | |
112 | // That said, none of that's *guaranteed*, and so we fence. | |
113 | atomic::fence(Ordering::Acquire); | |
114 | Some(func) | |
115 | } | |
7453a54e SL |
116 | } |
117 | } | |
118 | } | |
fc512014 | 119 | |
a2a8927a | 120 | // Cold because it should only happen during first-time initialization. |
fc512014 XL |
121 | #[cold] |
122 | unsafe fn initialize(&self) -> Option<F> { | |
04454e1e | 123 | assert_eq!(mem::size_of::<F>(), mem::size_of::<*mut libc::c_void>()); |
a2a8927a | 124 | |
fc512014 XL |
125 | let val = fetch(self.name); |
126 | // This synchronizes with the acquire fence in `get`. | |
04454e1e | 127 | self.func.store(val, Ordering::Release); |
fc512014 | 128 | |
04454e1e | 129 | if val.is_null() { None } else { Some(mem::transmute_copy::<*mut libc::c_void, F>(&val)) } |
fc512014 | 130 | } |
7453a54e SL |
131 | } |
132 | ||
04454e1e | 133 | unsafe fn fetch(name: &str) -> *mut libc::c_void { |
9fa01778 | 134 | let name = match CStr::from_bytes_with_nul(name.as_bytes()) { |
7453a54e | 135 | Ok(cstr) => cstr, |
04454e1e | 136 | Err(..) => return ptr::null_mut(), |
7453a54e | 137 | }; |
04454e1e | 138 | libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) |
7453a54e | 139 | } |
0731742a | 140 | |
fc512014 | 141 | #[cfg(not(any(target_os = "linux", target_os = "android")))] |
136023e0 | 142 | pub(crate) macro syscall { |
0731742a XL |
143 | (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( |
144 | unsafe fn $name($($arg_name: $t),*) -> $ret { | |
0731742a XL |
145 | weak! { fn $name($($t),*) -> $ret } |
146 | ||
147 | if let Some(fun) = $name.get() { | |
148 | fun($($arg_name),*) | |
149 | } else { | |
a2a8927a | 150 | super::os::set_errno(libc::ENOSYS); |
0731742a XL |
151 | -1 |
152 | } | |
153 | } | |
154 | ) | |
155 | } | |
156 | ||
fc512014 | 157 | #[cfg(any(target_os = "linux", target_os = "android"))] |
136023e0 | 158 | pub(crate) macro syscall { |
0731742a XL |
159 | (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( |
160 | unsafe fn $name($($arg_name:$t),*) -> $ret { | |
fc512014 XL |
161 | weak! { fn $name($($t),*) -> $ret } |
162 | ||
163 | // Use a weak symbol from libc when possible, allowing `LD_PRELOAD` | |
164 | // interposition, but if it's not found just use a raw syscall. | |
165 | if let Some(fun) = $name.get() { | |
166 | fun($($arg_name),*) | |
167 | } else { | |
a2a8927a XL |
168 | // This looks like a hack, but concat_idents only accepts idents |
169 | // (not paths). | |
170 | use libc::*; | |
171 | ||
fc512014 XL |
172 | syscall( |
173 | concat_idents!(SYS_, $name), | |
174 | $($arg_name),* | |
175 | ) as $ret | |
176 | } | |
0731742a XL |
177 | } |
178 | ) | |
179 | } | |
a2a8927a XL |
180 | |
181 | #[cfg(any(target_os = "linux", target_os = "android"))] | |
182 | pub(crate) macro raw_syscall { | |
183 | (fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => ( | |
184 | unsafe fn $name($($arg_name:$t),*) -> $ret { | |
185 | // This looks like a hack, but concat_idents only accepts idents | |
186 | // (not paths). | |
187 | use libc::*; | |
188 | ||
189 | syscall( | |
190 | concat_idents!(SYS_, $name), | |
191 | $($arg_name),* | |
192 | ) as $ret | |
193 | } | |
194 | ) | |
195 | } |