| 1 |
//! Linux auxv support, using libc. |
| 2 |
//! |
| 3 |
//! # Safety |
| 4 |
//! |
| 5 |
//! This uses raw pointers to locate and read the kernel-provided auxv array. |
| 6 |
#![allow(unsafe_code)] |
| 7 |
|
| 8 |
use crate::backend::c; |
| 9 |
#[cfg(feature = "param")] |
| 10 |
use crate::ffi::CStr; |
| 11 |
#[cfg(not(feature = "runtime"))] |
| 12 |
use core::ptr::null; |
| 13 |
use linux_raw_sys::elf::*; |
| 14 |
|
| 15 |
// `getauxval` wasn't supported in glibc until 2.16. Also this lets us use |
| 16 |
// `*mut` as the return type to preserve strict provenance. |
| 17 |
#[cfg(not(feature = "runtime"))] |
| 18 |
weak!(fn getauxval(c::c_ulong) -> *mut c::c_void); |
| 19 |
|
| 20 |
// With the "runtime" feature, go ahead and depend on `getauxval` existing so |
| 21 |
// that we never fail. |
| 22 |
#[cfg(feature = "runtime")] |
| 23 |
extern "C" { |
| 24 |
fn getauxval(type_: c::c_ulong) -> *mut c::c_void; |
| 25 |
} |
| 26 |
|
| 27 |
#[cfg(feature = "runtime")] |
| 28 |
const AT_PHDR: c::c_ulong = 3; |
| 29 |
#[cfg(feature = "runtime")] |
| 30 |
const AT_PHENT: c::c_ulong = 4; |
| 31 |
#[cfg(feature = "runtime")] |
| 32 |
const AT_PHNUM: c::c_ulong = 5; |
| 33 |
#[cfg(feature = "runtime")] |
| 34 |
const AT_ENTRY: c::c_ulong = 9; |
| 35 |
const AT_HWCAP: c::c_ulong = 16; |
| 36 |
#[cfg(feature = "runtime")] |
| 37 |
const AT_RANDOM: c::c_ulong = 25; |
| 38 |
const AT_HWCAP2: c::c_ulong = 26; |
| 39 |
const AT_SECURE: c::c_ulong = 23; |
| 40 |
const AT_EXECFN: c::c_ulong = 31; |
| 41 |
const AT_SYSINFO_EHDR: c::c_ulong = 33; |
| 42 |
|
| 43 |
// Declare `sysconf` ourselves so that we don't depend on all of libc just for |
| 44 |
// this. |
| 45 |
extern "C" { |
| 46 |
fn sysconf(name: c::c_int) -> c::c_long; |
| 47 |
} |
| 48 |
|
| 49 |
#[cfg(target_os = "android")] |
| 50 |
const _SC_PAGESIZE: c::c_int = 39; |
| 51 |
#[cfg(target_os = "linux")] |
| 52 |
const _SC_PAGESIZE: c::c_int = 30; |
| 53 |
#[cfg(target_os = "android")] |
| 54 |
const _SC_CLK_TCK: c::c_int = 6; |
| 55 |
#[cfg(target_os = "linux")] |
| 56 |
const _SC_CLK_TCK: c::c_int = 2; |
| 57 |
|
| 58 |
#[test] |
| 59 |
fn test_abi() { |
| 60 |
const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE); |
| 61 |
const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK); |
| 62 |
const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP); |
| 63 |
const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2); |
| 64 |
const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN); |
| 65 |
const_assert_eq!(self::AT_SECURE, ::libc::AT_SECURE); |
| 66 |
const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR); |
| 67 |
#[cfg(feature = "runtime")] |
| 68 |
const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR); |
| 69 |
#[cfg(feature = "runtime")] |
| 70 |
const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM); |
| 71 |
#[cfg(feature = "runtime")] |
| 72 |
const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY); |
| 73 |
#[cfg(feature = "runtime")] |
| 74 |
const_assert_eq!(self::AT_RANDOM, ::libc::AT_RANDOM); |
| 75 |
} |
| 76 |
|
| 77 |
#[cfg(feature = "param")] |
| 78 |
#[inline] |
| 79 |
pub(crate) fn page_size() -> usize { |
| 80 |
unsafe { sysconf(_SC_PAGESIZE) as usize } |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(feature = "param")] |
| 84 |
#[inline] |
| 85 |
pub(crate) fn clock_ticks_per_second() -> u64 { |
| 86 |
unsafe { sysconf(_SC_CLK_TCK) as u64 } |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(feature = "param")] |
| 90 |
#[inline] |
| 91 |
pub(crate) fn linux_hwcap() -> (usize, usize) { |
| 92 |
#[cfg(not(feature = "runtime"))] |
| 93 |
unsafe { |
| 94 |
if let Some(libc_getauxval) = getauxval.get() { |
| 95 |
let hwcap = libc_getauxval(AT_HWCAP) as usize; |
| 96 |
let hwcap2 = libc_getauxval(AT_HWCAP2) as usize; |
| 97 |
(hwcap, hwcap2) |
| 98 |
} else { |
| 99 |
(0, 0) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
#[cfg(feature = "runtime")] |
| 104 |
unsafe { |
| 105 |
let hwcap = getauxval(AT_HWCAP) as usize; |
| 106 |
let hwcap2 = getauxval(AT_HWCAP2) as usize; |
| 107 |
(hwcap, hwcap2) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[cfg(feature = "param")] |
| 112 |
#[inline] |
| 113 |
pub(crate) fn linux_minsigstksz() -> usize { |
| 114 |
// FIXME: reuse const from libc when available? |
| 115 |
const AT_MINSIGSTKSZ: c::c_ulong = 51; |
| 116 |
|
| 117 |
#[cfg(not(feature = "runtime"))] |
| 118 |
if let Some(libc_getauxval) = getauxval.get() { |
| 119 |
unsafe { libc_getauxval(AT_MINSIGSTKSZ) as usize } |
| 120 |
} else { |
| 121 |
0 |
| 122 |
} |
| 123 |
|
| 124 |
#[cfg(feature = "runtime")] |
| 125 |
unsafe { |
| 126 |
getauxval(AT_MINSIGSTKSZ) as usize |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(feature = "param")] |
| 131 |
#[inline] |
| 132 |
pub(crate) fn linux_execfn() -> &'static CStr { |
| 133 |
#[cfg(not(feature = "runtime"))] |
| 134 |
unsafe { |
| 135 |
if let Some(libc_getauxval) = getauxval.get() { |
| 136 |
CStr::from_ptr(libc_getauxval(AT_EXECFN).cast()) |
| 137 |
} else { |
| 138 |
cstr!("") |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
#[cfg(feature = "runtime")] |
| 143 |
unsafe { |
| 144 |
CStr::from_ptr(getauxval(AT_EXECFN).cast()) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
#[cfg(feature = "runtime")] |
| 149 |
#[inline] |
| 150 |
pub(crate) fn linux_secure() -> bool { |
| 151 |
unsafe { getauxval(AT_SECURE) as usize != 0 } |
| 152 |
} |
| 153 |
|
| 154 |
#[cfg(feature = "runtime")] |
| 155 |
#[inline] |
| 156 |
pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) { |
| 157 |
unsafe { |
| 158 |
let phdr = getauxval(AT_PHDR) as *const c::c_void; |
| 159 |
let phent = getauxval(AT_PHENT) as usize; |
| 160 |
let phnum = getauxval(AT_PHNUM) as usize; |
| 161 |
(phdr, phent, phnum) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so |
| 166 |
/// if we don't see it, this function returns a null pointer. |
| 167 |
#[inline] |
| 168 |
pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr { |
| 169 |
#[cfg(not(feature = "runtime"))] |
| 170 |
unsafe { |
| 171 |
if let Some(libc_getauxval) = getauxval.get() { |
| 172 |
libc_getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr |
| 173 |
} else { |
| 174 |
null() |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
#[cfg(feature = "runtime")] |
| 179 |
unsafe { |
| 180 |
getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
#[cfg(feature = "runtime")] |
| 185 |
#[inline] |
| 186 |
pub(crate) fn entry() -> usize { |
| 187 |
unsafe { getauxval(AT_ENTRY) as usize } |
| 188 |
} |
| 189 |
|
| 190 |
#[cfg(feature = "runtime")] |
| 191 |
#[inline] |
| 192 |
pub(crate) fn random() -> *const [u8; 16] { |
| 193 |
unsafe { getauxval(AT_RANDOM) as *const [u8; 16] } |
| 194 |
} |