Skip to content

Commit 2960b07

Browse files
committed
std_detect: Move cfgs into getauxval helper function
1 parent 6f1a72f commit 2960b07

File tree

1 file changed

+35
-94
lines changed
  • crates/std_detect/src/detect/os/linux

1 file changed

+35
-94
lines changed

Diff for: crates/std_detect/src/detect/os/linux/auxvec.rs

+35-94
Original file line numberDiff line numberDiff line change
@@ -74,68 +74,8 @@ pub(crate) struct AuxVec {
7474
/// [auxvec_h]: https://github.com/torvalds/linux/blob/master/include/uapi/linux/auxvec.h
7575
/// [auxv_docs]: https://docs.rs/auxv/0.3.3/auxv/
7676
pub(crate) fn auxv() -> Result<AuxVec, ()> {
77-
#[cfg(all(
78-
feature = "std_detect_dlsym_getauxval",
79-
not(all(
80-
target_os = "linux",
81-
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
82-
)),
83-
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
84-
not(all(target_os = "android", target_pointer_width = "64")),
85-
))]
86-
{
87-
// Try to call a dynamically-linked getauxval function.
88-
if let Ok(hwcap) = getauxval(AT_HWCAP) {
89-
// Targets with only AT_HWCAP:
90-
#[cfg(any(
91-
target_arch = "riscv32",
92-
target_arch = "riscv64",
93-
target_arch = "mips",
94-
target_arch = "mips64"
95-
))]
96-
{
97-
// Zero could indicate that no features were detected, but it's also used to
98-
// indicate an error. In either case, try the fallback.
99-
if hwcap != 0 {
100-
return Ok(AuxVec { hwcap });
101-
}
102-
}
103-
104-
// Targets with AT_HWCAP and AT_HWCAP2:
105-
#[cfg(any(
106-
target_arch = "aarch64",
107-
target_arch = "arm",
108-
target_arch = "powerpc",
109-
target_arch = "powerpc64",
110-
target_arch = "s390x",
111-
))]
112-
{
113-
if let Ok(hwcap2) = getauxval(AT_HWCAP2) {
114-
// Zero could indicate that no features were detected, but it's also used to
115-
// indicate an error. In particular, on many platforms AT_HWCAP2 will be
116-
// legitimately zero, since it contains the most recent feature flags. Use the
117-
// fallback only if no features were detected at all.
118-
if hwcap != 0 || hwcap2 != 0 {
119-
return Ok(AuxVec { hwcap, hwcap2 });
120-
}
121-
}
122-
}
123-
124-
// Intentionnaly not used
125-
let _ = hwcap;
126-
}
127-
}
128-
129-
#[cfg(not(all(
130-
feature = "std_detect_dlsym_getauxval",
131-
not(all(
132-
target_os = "linux",
133-
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
134-
)),
135-
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
136-
not(all(target_os = "android", target_pointer_width = "64")),
137-
)))]
138-
{
77+
// Try to call a getauxval function.
78+
if let Ok(hwcap) = getauxval(AT_HWCAP) {
13979
// Targets with only AT_HWCAP:
14080
#[cfg(any(
14181
target_arch = "riscv32",
@@ -145,7 +85,6 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
14585
target_arch = "loongarch64",
14686
))]
14787
{
148-
let hwcap = unsafe { libc::getauxval(AT_HWCAP as libc::c_ulong) as usize };
14988
// Zero could indicate that no features were detected, but it's also used to indicate
15089
// an error. In either case, try the fallback.
15190
if hwcap != 0 {
@@ -162,16 +101,19 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
162101
target_arch = "s390x",
163102
))]
164103
{
165-
let hwcap = unsafe { libc::getauxval(AT_HWCAP as libc::c_ulong) as usize };
166-
let hwcap2 = unsafe { libc::getauxval(AT_HWCAP2 as libc::c_ulong) as usize };
167-
// Zero could indicate that no features were detected, but it's also used to indicate
168-
// an error. In particular, on many platforms AT_HWCAP2 will be legitimately zero,
169-
// since it contains the most recent feature flags. Use the fallback only if no
170-
// features were detected at all.
171-
if hwcap != 0 || hwcap2 != 0 {
172-
return Ok(AuxVec { hwcap, hwcap2 });
104+
if let Ok(hwcap2) = getauxval(AT_HWCAP2) {
105+
// Zero could indicate that no features were detected, but it's also used to indicate
106+
// an error. In particular, on many platforms AT_HWCAP2 will be legitimately zero,
107+
// since it contains the most recent feature flags. Use the fallback only if no
108+
// features were detected at all.
109+
if hwcap != 0 || hwcap2 != 0 {
110+
return Ok(AuxVec { hwcap, hwcap2 });
111+
}
173112
}
174113
}
114+
115+
// Intentionnaly not used
116+
let _ = hwcap;
175117
}
176118

177119
#[cfg(feature = "std_detect_file_io")]
@@ -187,32 +129,31 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
187129
}
188130

189131
/// Tries to read the `key` from the auxiliary vector by calling the
190-
/// dynamically-linked `getauxval` function. If the function is not linked,
191-
/// this function return `Err`.
192-
#[cfg(any(
193-
test,
194-
all(
195-
feature = "std_detect_dlsym_getauxval",
196-
not(all(
197-
target_os = "linux",
198-
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
199-
)),
200-
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
201-
not(all(target_os = "android", target_pointer_width = "64")),
202-
)
203-
))]
132+
/// `getauxval` function. If the function is not linked, this function return `Err`.
204133
fn getauxval(key: usize) -> Result<usize, ()> {
205-
use libc;
206-
pub type F = unsafe extern "C" fn(usize) -> usize;
207-
unsafe {
208-
let ptr = libc::dlsym(libc::RTLD_DEFAULT, c"getauxval".as_ptr());
209-
if ptr.is_null() {
210-
return Err(());
134+
type F = unsafe extern "C" fn(libc::c_ulong) -> libc::c_ulong;
135+
cfg_if::cfg_if! {
136+
if #[cfg(all(
137+
feature = "std_detect_dlsym_getauxval",
138+
not(all(
139+
target_os = "linux",
140+
any(target_env = "gnu", target_env = "musl", target_env = "ohos"),
141+
)),
142+
// TODO: libc crate currently doesn't provide getauxval on 32-bit Android.
143+
not(all(target_os = "android", target_pointer_width = "64")),
144+
))] {
145+
let ffi_getauxval: F = unsafe {
146+
let ptr = libc::dlsym(libc::RTLD_DEFAULT, c"getauxval".as_ptr());
147+
if ptr.is_null() {
148+
return Err(());
149+
}
150+
core::mem::transmute(ptr)
151+
};
152+
} else {
153+
let ffi_getauxval: F = libc::getauxval;
211154
}
212-
213-
let ffi_getauxval: F = core::mem::transmute(ptr);
214-
Ok(ffi_getauxval(key))
215155
}
156+
Ok(unsafe { ffi_getauxval(key as libc::c_ulong) as usize })
216157
}
217158

218159
/// Tries to read the auxiliary vector from the `file`. If this fails, this

0 commit comments

Comments
 (0)