1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Linux auxv support, using libc.
//!
//! # Safety
//!
//! This uses raw pointers to locate and read the kernel-provided auxv array.
#![allow(unsafe_code)]

use crate::backend::c;
#[cfg(feature = "param")]
use crate::ffi::CStr;
#[cfg(not(feature = "runtime"))]
use core::ptr::null;
use linux_raw_sys::elf::*;

// `getauxval` wasn't supported in glibc until 2.16. Also this lets us use
// `*mut` as the return type to preserve strict provenance.
#[cfg(not(feature = "runtime"))]
weak!(fn getauxval(c::c_ulong) -> *mut c::c_void);

// With the "runtime" feature, go ahead and depend on `getauxval` existing so
// that we never fail.
#[cfg(feature = "runtime")]
extern "C" {
    fn getauxval(type_: c::c_ulong) -> *mut c::c_void;
}

#[cfg(feature = "runtime")]
const AT_PHDR: c::c_ulong = 3;
#[cfg(feature = "runtime")]
const AT_PHENT: c::c_ulong = 4;
#[cfg(feature = "runtime")]
const AT_PHNUM: c::c_ulong = 5;
#[cfg(feature = "runtime")]
const AT_ENTRY: c::c_ulong = 9;
const AT_HWCAP: c::c_ulong = 16;
#[cfg(feature = "runtime")]
const AT_RANDOM: c::c_ulong = 25;
const AT_HWCAP2: c::c_ulong = 26;
const AT_SECURE: c::c_ulong = 23;
const AT_EXECFN: c::c_ulong = 31;
const AT_SYSINFO_EHDR: c::c_ulong = 33;
const AT_MINSIGSTKSZ: c::c_ulong = 51;

// Declare `sysconf` ourselves so that we don't depend on all of libc just for
// this.
extern "C" {
    fn sysconf(name: c::c_int) -> c::c_long;
}

#[cfg(target_os = "android")]
const _SC_PAGESIZE: c::c_int = 39;
#[cfg(target_os = "linux")]
const _SC_PAGESIZE: c::c_int = 30;
#[cfg(target_os = "android")]
const _SC_CLK_TCK: c::c_int = 6;
#[cfg(target_os = "linux")]
const _SC_CLK_TCK: c::c_int = 2;

#[test]
fn test_abi() {
    const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
    const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
    const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
    const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
    const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
    const_assert_eq!(self::AT_SECURE, ::libc::AT_SECURE);
    const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
    const_assert_eq!(self::AT_MINSIGSTKSZ, ::libc::AT_MINSIGSTKSZ);
    #[cfg(feature = "runtime")]
    const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
    #[cfg(feature = "runtime")]
    const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
    #[cfg(feature = "runtime")]
    const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY);
    #[cfg(feature = "runtime")]
    const_assert_eq!(self::AT_RANDOM, ::libc::AT_RANDOM);
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn page_size() -> usize {
    unsafe { sysconf(_SC_PAGESIZE) as usize }
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn clock_ticks_per_second() -> u64 {
    unsafe { sysconf(_SC_CLK_TCK) as u64 }
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_hwcap() -> (usize, usize) {
    #[cfg(not(feature = "runtime"))]
    unsafe {
        if let Some(libc_getauxval) = getauxval.get() {
            let hwcap = libc_getauxval(AT_HWCAP) as usize;
            let hwcap2 = libc_getauxval(AT_HWCAP2) as usize;
            (hwcap, hwcap2)
        } else {
            (0, 0)
        }
    }

    #[cfg(feature = "runtime")]
    unsafe {
        let hwcap = getauxval(AT_HWCAP) as usize;
        let hwcap2 = getauxval(AT_HWCAP2) as usize;
        (hwcap, hwcap2)
    }
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_minsigstksz() -> usize {
    #[cfg(not(feature = "runtime"))]
    if let Some(libc_getauxval) = getauxval.get() {
        unsafe { libc_getauxval(AT_MINSIGSTKSZ) as usize }
    } else {
        0
    }

    #[cfg(feature = "runtime")]
    unsafe {
        getauxval(AT_MINSIGSTKSZ) as usize
    }
}

#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_execfn() -> &'static CStr {
    #[cfg(not(feature = "runtime"))]
    unsafe {
        if let Some(libc_getauxval) = getauxval.get() {
            CStr::from_ptr(libc_getauxval(AT_EXECFN).cast())
        } else {
            cstr!("")
        }
    }

    #[cfg(feature = "runtime")]
    unsafe {
        CStr::from_ptr(getauxval(AT_EXECFN).cast())
    }
}

#[cfg(feature = "runtime")]
#[inline]
pub(crate) fn linux_secure() -> bool {
    unsafe { getauxval(AT_SECURE) as usize != 0 }
}

#[cfg(feature = "runtime")]
#[inline]
pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) {
    unsafe {
        let phdr = getauxval(AT_PHDR) as *const c::c_void;
        let phent = getauxval(AT_PHENT) as usize;
        let phnum = getauxval(AT_PHNUM) as usize;
        (phdr, phent, phnum)
    }
}

/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so
/// if we don't see it, this function returns a null pointer.
#[inline]
pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
    #[cfg(not(feature = "runtime"))]
    unsafe {
        if let Some(libc_getauxval) = getauxval.get() {
            libc_getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
        } else {
            null()
        }
    }

    #[cfg(feature = "runtime")]
    unsafe {
        getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
    }
}

#[cfg(feature = "runtime")]
#[inline]
pub(crate) fn entry() -> usize {
    unsafe { getauxval(AT_ENTRY) as usize }
}

#[cfg(feature = "runtime")]
#[inline]
pub(crate) fn random() -> *const [u8; 16] {
    unsafe { getauxval(AT_RANDOM) as *const [u8; 16] }
}