Skip to content

Commit 75aa6f6

Browse files
authored
std_detect: Always avoid dlsym on *-linux-gnu* targets (#1375)
1 parent 3a96d41 commit 75aa6f6

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

crates/std_detect/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ run-time feature detection. When this feature is disabled, `std_detect` assumes
3030
that [`getauxval`] is linked to the binary. If that is not the case the behavior
3131
is undefined.
3232

33+
Note: This feature is ignored on `*-linux-gnu*` targets, since all `*-linux-gnu*` targets ([since Rust 1.64](https://blog.rust-lang.org/2022/08/01/Increasing-glibc-kernel-requirements.html)) have glibc requirements higher than [glibc 2.16 that added `getauxval`](https://sourceware.org/legacy-ml/libc-announce/2012/msg00000.html), and we can safely assume [`getauxval`] is linked to the binary.
34+
3335
* `std_detect_file_io` (enabled by default, requires `std`): Enable to perform run-time feature
3436
detection using file APIs (e.g. `/proc/cpuinfo`, etc.) if other more performant
3537
methods fail. This feature requires `libstd` as a dependency, preventing the

crates/std_detect/src/detect/os/linux/auxvec.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ pub(crate) struct AuxVec {
5454
/// error, cpuinfo still can (and will) be used to try to perform run-time
5555
/// feature detecton on some platforms.
5656
///
57+
/// Note: The `std_detect_dlsym_getauxval` cargo feature is ignored on `*-linux-gnu*` targets,
58+
/// since [all `*-linux-gnu*` targets ([since Rust 1.64](https://blog.rust-lang.org/2022/08/01/Increasing-glibc-kernel-requirements.html))
59+
/// have glibc requirements higher than [glibc 2.16 that added `getauxval`](https://sourceware.org/legacy-ml/libc-announce/2012/msg00000.html),
60+
/// and we can safely assume [`getauxval`] is linked to the binary.
61+
///
5762
/// For more information about when `getauxval` is available check the great
5863
/// [`auxv` crate documentation][auxv_docs].
5964
///
6065
/// [auxvec_h]: https://github.com/torvalds/linux/blob/master/include/uapi/linux/auxvec.h
6166
/// [auxv_docs]: https://docs.rs/auxv/0.3.3/auxv/
6267
pub(crate) fn auxv() -> Result<AuxVec, ()> {
63-
#[cfg(feature = "std_detect_dlsym_getauxval")]
68+
#[cfg(all(
69+
feature = "std_detect_dlsym_getauxval",
70+
not(all(target_os = "linux", target_env = "gnu"))
71+
))]
6472
{
6573
// Try to call a dynamically-linked getauxval function.
6674
if let Ok(hwcap) = getauxval(AT_HWCAP) {
@@ -101,7 +109,10 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
101109
}
102110
}
103111

104-
#[cfg(not(feature = "std_detect_dlsym_getauxval"))]
112+
#[cfg(any(
113+
not(feature = "std_detect_dlsym_getauxval"),
114+
all(target_os = "linux", target_env = "gnu")
115+
))]
105116
{
106117
// Targets with only AT_HWCAP:
107118
#[cfg(any(
@@ -154,7 +165,10 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
154165
/// Tries to read the `key` from the auxiliary vector by calling the
155166
/// dynamically-linked `getauxval` function. If the function is not linked,
156167
/// this function return `Err`.
157-
#[cfg(feature = "std_detect_dlsym_getauxval")]
168+
#[cfg(all(
169+
feature = "std_detect_dlsym_getauxval",
170+
not(all(target_os = "linux", target_env = "gnu"))
171+
))]
158172
fn getauxval(key: usize) -> Result<usize, ()> {
159173
use libc;
160174
pub type F = unsafe extern "C" fn(usize) -> usize;

0 commit comments

Comments
 (0)