1 --- src/stdsimd/crates/std_detect/src/detect/os/freebsd/auxvec.rs.orig 2019-04-25 17:33:21 UTC
2 +++ src/stdsimd/crates/std_detect/src/detect/os/freebsd/auxvec.rs
4 +//! Parses ELF auxiliary vectors.
5 +#![cfg_attr(any(target_arch = "arm", target_arch = "powerpc64"), allow(dead_code))]
7 +/// Key to access the CPU Hardware capabilities bitfield.
8 +pub(crate) const AT_HWCAP: usize = 25;
9 +/// Key to access the CPU Hardware capabilities 2 bitfield.
10 +pub(crate) const AT_HWCAP2: usize = 26;
12 +/// Cache HWCAP bitfields of the ELF Auxiliary Vector.
14 +/// If an entry cannot be read all the bits in the bitfield are set to zero.
15 +/// This should be interpreted as all the features being disabled.
16 +#[derive(Debug, Copy, Clone)]
17 +pub(crate) struct AuxVec {
22 +/// ELF Auxiliary Vector
24 +/// The auxiliary vector is a memory region in a running ELF program's stack
25 +/// composed of (key: usize, value: usize) pairs.
27 +/// The keys used in the aux vector are platform dependent. For FreeBSD, they are
28 +/// defined in [sys/elf_common.h][elf_common_h]. The hardware capabilities of a given
29 +/// CPU can be queried with the `AT_HWCAP` and `AT_HWCAP2` keys.
31 +/// Note that run-time feature detection is not invoked for features that can
32 +/// be detected at compile-time.
34 +/// [elf_common.h]: https://svnweb.freebsd.org/base/release/12.0.0/sys/sys/elf_common.h?revision=341707
35 +pub(crate) fn auxv() -> Result<AuxVec, ()> {
36 + if let Ok(hwcap) = archauxv(AT_HWCAP) {
37 + if let Ok(hwcap2) = archauxv(AT_HWCAP2) {
38 + if hwcap != 0 && hwcap2 != 0 {
39 + return Ok(AuxVec { hwcap, hwcap2 });
46 +/// Tries to read the `key` from the auxiliary vector.
47 +fn archauxv(key: usize) -> Result<usize, ()> {
50 + #[derive (Copy, Clone)]
52 + pub struct Elf_Auxinfo {
56 + #[derive (Copy, Clone)]
59 + pub a_val: libc::c_long,
60 + pub a_ptr: *mut libc::c_void,
61 + pub a_fcn: Option<unsafe extern "C" fn() -> ()>,
64 + let mut auxv: [Elf_Auxinfo; 27] =
65 + [Elf_Auxinfo{a_type: 0, a_un: unnamed{a_val: 0,},}; 27];
67 + let mut len: libc::c_uint = mem::size_of_val(&auxv) as libc::c_uint;
70 + let mut mib = [libc::CTL_KERN, libc::KERN_PROC, libc::KERN_PROC_AUXV, libc::getpid()];
72 + let ret = libc::sysctl(mib.as_mut_ptr(),
74 + &mut auxv as *mut _ as *mut _,
75 + &mut len as *mut _ as *mut _,
76 + 0 as *mut libc::c_void,
81 + for i in 0..auxv.len() {
82 + if auxv[i].a_type == key {
83 + return Ok(auxv[i].a_un.a_val as usize);