2016-02-04 13:56:59 -08:00
|
|
|
//! Support for "weak linkage" to symbols on Unix
|
|
|
|
|
//!
|
|
|
|
|
//! Some I/O operations we do in libstd require newer versions of OSes but we
|
|
|
|
|
//! need to maintain binary compatibility with older releases for now. In order
|
|
|
|
|
//! to use the new functionality when available we use this module for
|
|
|
|
|
//! detection.
|
|
|
|
|
//!
|
|
|
|
|
//! One option to use here is weak linkage, but that is unfortunately only
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
//! really workable with ELF. Otherwise, use dlsym to get the symbol value at
|
2016-02-04 13:56:59 -08:00
|
|
|
//! runtime. This is also done for compatibility with older versions of glibc,
|
|
|
|
|
//! and to avoid creating dependencies on GLIBC_PRIVATE symbols. It assumes that
|
|
|
|
|
//! we've been dynamically linked to the library the symbol comes from, but that
|
|
|
|
|
//! is currently always the case for things like libpthread/libc.
|
|
|
|
|
//!
|
|
|
|
|
//! A long time ago this used weak linkage for the __pthread_get_minstack
|
|
|
|
|
//! symbol, but that caused Debian to detect an unnecessarily strict versioned
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
//! dependency on libc6 (#23628) because it is GLIBC_PRIVATE. We now use `dlsym`
|
|
|
|
|
//! for a runtime lookup of that symbol to avoid the ELF versioned dependency.
|
2016-02-04 13:56:59 -08:00
|
|
|
|
2020-07-22 16:38:58 -07:00
|
|
|
// There are a variety of `#[cfg]`s controlling which targets are involved in
|
|
|
|
|
// each instance of `weak!` and `syscall!`. Rather than trying to unify all of
|
|
|
|
|
// that, we'll just allow that some unix targets don't use this module at all.
|
|
|
|
|
#![allow(dead_code, unused_macros)]
|
|
|
|
|
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::ffi::CStr;
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
use crate::marker::PhantomData;
|
2019-02-11 04:23:21 +09:00
|
|
|
use crate::mem;
|
2020-11-13 19:15:51 -08:00
|
|
|
use crate::sync::atomic::{self, AtomicUsize, Ordering};
|
2016-02-04 13:56:59 -08:00
|
|
|
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
// We can use true weak linkage on ELF targets.
|
|
|
|
|
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
|
2021-07-05 20:28:10 -07:00
|
|
|
pub(crate) macro weak {
|
2016-02-04 13:56:59 -08:00
|
|
|
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
let ref $name: ExternWeak<unsafe extern "C" fn($($t),*) -> $ret> = {
|
|
|
|
|
extern "C" {
|
|
|
|
|
#[linkage = "extern_weak"]
|
|
|
|
|
static $name: *const libc::c_void;
|
|
|
|
|
}
|
|
|
|
|
#[allow(unused_unsafe)]
|
|
|
|
|
ExternWeak::new(unsafe { $name })
|
|
|
|
|
};
|
2016-02-04 13:56:59 -08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
// On non-ELF targets, use the dlsym approximation of weak linkage.
|
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
|
|
|
|
pub(crate) use self::dlsym as weak;
|
|
|
|
|
|
|
|
|
|
pub(crate) struct ExternWeak<F> {
|
|
|
|
|
weak_ptr: *const libc::c_void,
|
|
|
|
|
_marker: PhantomData<F>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F> ExternWeak<F> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn new(weak_ptr: *const libc::c_void) -> Self {
|
|
|
|
|
ExternWeak { weak_ptr, _marker: PhantomData }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<F> ExternWeak<F> {
|
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn get(&self) -> Option<F> {
|
|
|
|
|
unsafe {
|
|
|
|
|
if self.weak_ptr.is_null() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(mem::transmute_copy::<*const libc::c_void, F>(&self.weak_ptr))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) macro dlsym {
|
|
|
|
|
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
|
|
|
|
|
static DLSYM: DlsymWeak<unsafe extern "C" fn($($t),*) -> $ret> =
|
|
|
|
|
DlsymWeak::new(concat!(stringify!($name), '\0'));
|
|
|
|
|
let $name = &DLSYM;
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) struct DlsymWeak<F> {
|
2016-02-04 13:56:59 -08:00
|
|
|
name: &'static str,
|
|
|
|
|
addr: AtomicUsize,
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
_marker: PhantomData<F>,
|
2016-02-04 13:56:59 -08:00
|
|
|
}
|
|
|
|
|
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
impl<F> DlsymWeak<F> {
|
|
|
|
|
pub(crate) const fn new(name: &'static str) -> Self {
|
|
|
|
|
DlsymWeak { name, addr: AtomicUsize::new(1), _marker: PhantomData }
|
2016-02-04 13:56:59 -08:00
|
|
|
}
|
|
|
|
|
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn get(&self) -> Option<F> {
|
2016-02-04 13:56:59 -08:00
|
|
|
unsafe {
|
2020-11-13 19:15:51 -08:00
|
|
|
// Relaxed is fine here because we fence before reading through the
|
|
|
|
|
// pointer (see the comment below).
|
|
|
|
|
match self.addr.load(Ordering::Relaxed) {
|
|
|
|
|
1 => self.initialize(),
|
2019-02-13 14:07:08 -08:00
|
|
|
0 => None,
|
2020-11-13 19:15:51 -08:00
|
|
|
addr => {
|
|
|
|
|
let func = mem::transmute_copy::<usize, F>(&addr);
|
|
|
|
|
// The caller is presumably going to read through this value
|
|
|
|
|
// (by calling the function we've dlsymed). This means we'd
|
|
|
|
|
// need to have loaded it with at least C11's consume
|
|
|
|
|
// ordering in order to be guaranteed that the data we read
|
|
|
|
|
// from the pointer isn't from before the pointer was
|
|
|
|
|
// stored. Rust has no equivalent to memory_order_consume,
|
|
|
|
|
// so we use an acquire fence (sorry, ARM).
|
|
|
|
|
//
|
|
|
|
|
// Now, in practice this likely isn't needed even on CPUs
|
|
|
|
|
// where relaxed and consume mean different things. The
|
|
|
|
|
// symbols we're loading are probably present (or not) at
|
|
|
|
|
// init, and even if they aren't the runtime dynamic loader
|
|
|
|
|
// is extremely likely have sufficient barriers internally
|
|
|
|
|
// (possibly implicitly, for example the ones provided by
|
|
|
|
|
// invoking `mprotect`).
|
|
|
|
|
//
|
|
|
|
|
// That said, none of that's *guaranteed*, and so we fence.
|
|
|
|
|
atomic::fence(Ordering::Acquire);
|
|
|
|
|
Some(func)
|
|
|
|
|
}
|
2016-02-04 15:22:41 -08:00
|
|
|
}
|
2016-02-04 13:56:59 -08:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-13 19:15:51 -08:00
|
|
|
|
|
|
|
|
// Cold because it should only happen during first-time initalization.
|
|
|
|
|
#[cold]
|
|
|
|
|
unsafe fn initialize(&self) -> Option<F> {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
assert_eq!(mem::size_of::<F>(), mem::size_of::<usize>());
|
|
|
|
|
|
2020-11-13 19:15:51 -08:00
|
|
|
let val = fetch(self.name);
|
|
|
|
|
// This synchronizes with the acquire fence in `get`.
|
|
|
|
|
self.addr.store(val, Ordering::Release);
|
|
|
|
|
|
|
|
|
|
match val {
|
|
|
|
|
0 => None,
|
|
|
|
|
addr => Some(mem::transmute_copy::<usize, F>(&addr)),
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-04 13:56:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn fetch(name: &str) -> usize {
|
2019-02-13 13:46:45 -08:00
|
|
|
let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
|
2016-02-04 13:56:59 -08:00
|
|
|
Ok(cstr) => cstr,
|
|
|
|
|
Err(..) => return 0,
|
|
|
|
|
};
|
2016-03-21 16:54:53 -07:00
|
|
|
libc::dlsym(libc::RTLD_DEFAULT, name.as_ptr()) as usize
|
2016-02-04 13:56:59 -08:00
|
|
|
}
|
2018-12-19 16:13:43 +02:00
|
|
|
|
2020-11-05 12:38:09 -08:00
|
|
|
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
2021-07-05 20:28:10 -07:00
|
|
|
pub(crate) macro syscall {
|
2018-12-19 16:13:43 +02:00
|
|
|
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
|
|
|
|
|
unsafe fn $name($($arg_name: $t),*) -> $ret {
|
|
|
|
|
weak! { fn $name($($t),*) -> $ret }
|
|
|
|
|
|
|
|
|
|
if let Some(fun) = $name.get() {
|
|
|
|
|
fun($($arg_name),*)
|
|
|
|
|
} else {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
super::os::set_errno(libc::ENOSYS);
|
2018-12-21 15:53:37 +02:00
|
|
|
-1
|
2018-12-19 16:13:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 12:38:09 -08:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
2021-07-05 20:28:10 -07:00
|
|
|
pub(crate) macro syscall {
|
2018-12-19 16:13:43 +02:00
|
|
|
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
|
|
|
|
|
unsafe fn $name($($arg_name:$t),*) -> $ret {
|
2020-11-05 12:38:09 -08:00
|
|
|
weak! { fn $name($($t),*) -> $ret }
|
2020-11-05 14:08:42 -08:00
|
|
|
|
|
|
|
|
// Use a weak symbol from libc when possible, allowing `LD_PRELOAD`
|
|
|
|
|
// interposition, but if it's not found just use a raw syscall.
|
2020-11-05 12:38:09 -08:00
|
|
|
if let Some(fun) = $name.get() {
|
|
|
|
|
fun($($arg_name),*)
|
|
|
|
|
} else {
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
// This looks like a hack, but concat_idents only accepts idents
|
|
|
|
|
// (not paths).
|
|
|
|
|
use libc::*;
|
|
|
|
|
|
2020-11-05 14:08:42 -08:00
|
|
|
syscall(
|
|
|
|
|
concat_idents!(SYS_, $name),
|
2020-11-19 10:09:40 +01:00
|
|
|
$($arg_name),*
|
2020-11-05 14:08:42 -08:00
|
|
|
) as $ret
|
2020-11-05 12:38:09 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
Refactor weak symbols in std::sys::unix
This makes a few changes to the weak symbol macros in `sys::unix`:
- `dlsym!` is added to keep the functionality for runtime `dlsym`
lookups, like for `__pthread_get_minstack@GLIBC_PRIVATE` that we don't
want to show up in ELF symbol tables.
- `weak!` now uses `#[linkage = "extern_weak"]` symbols, so its runtime
behavior is just a simple null check. This is also used by `syscall!`.
- On non-ELF targets (macos/ios) where that linkage is not known to
behave, `weak!` is just an alias to `dlsym!` for the old behavior.
- `raw_syscall!` is added to always call `libc::syscall` on linux and
android, for cases like `clone3` that have no known libc wrapper.
The new `weak!` linkage does mean that you'll get versioned symbols if
you build with a newer glibc, like `WEAK DEFAULT UND statx@GLIBC_2.28`.
This might seem problematic, but old non-weak symbols can tie the build
to new versions too, like `dlsym@GLIBC_2.34` from their recent library
unification. If you build with an old glibc like `dist-x86_64-linux`
does, you'll still get unversioned `WEAK DEFAULT UND statx`, which may
be resolved based on the runtime glibc.
I also found a few functions that don't need to be weak anymore:
- Android can directly use `ftruncate64`, `pread64`, and `pwrite64`, as
these were added in API 12, and our baseline is API 14.
- Linux can directly use `splice`, added way back in glibc 2.5 and
similarly old musl. Android only added it in API 21 though.
2021-11-12 12:58:38 -08:00
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
|
|
|
|
pub(crate) macro raw_syscall {
|
|
|
|
|
(fn $name:ident($($arg_name:ident: $t:ty),*) -> $ret:ty) => (
|
|
|
|
|
unsafe fn $name($($arg_name:$t),*) -> $ret {
|
|
|
|
|
// This looks like a hack, but concat_idents only accepts idents
|
|
|
|
|
// (not paths).
|
|
|
|
|
use libc::*;
|
|
|
|
|
|
|
|
|
|
syscall(
|
|
|
|
|
concat_idents!(SYS_, $name),
|
|
|
|
|
$($arg_name),*
|
|
|
|
|
) as $ret
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|