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" {
getauxval(type_: c::c_ulong) -> *mut c::c_void24 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" {
sysconf(name: c::c_int) -> c::c_long46 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]
test_abi()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]
page_size() -> usize79 pub(crate) fn page_size() -> usize {
80 unsafe { sysconf(_SC_PAGESIZE) as usize }
81 }
82
83 #[cfg(feature = "param")]
84 #[inline]
clock_ticks_per_second() -> u6485 pub(crate) fn clock_ticks_per_second() -> u64 {
86 unsafe { sysconf(_SC_CLK_TCK) as u64 }
87 }
88
89 #[cfg(feature = "param")]
90 #[inline]
linux_hwcap() -> (usize, usize)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]
linux_execfn() -> &'static CStr113 pub(crate) fn linux_execfn() -> &'static CStr {
114 #[cfg(not(feature = "runtime"))]
115 unsafe {
116 if let Some(libc_getauxval) = getauxval.get() {
117 CStr::from_ptr(libc_getauxval(AT_EXECFN).cast())
118 } else {
119 cstr!("")
120 }
121 }
122
123 #[cfg(feature = "runtime")]
124 unsafe {
125 CStr::from_ptr(getauxval(AT_EXECFN).cast())
126 }
127 }
128
129 #[cfg(feature = "runtime")]
130 #[inline]
linux_secure() -> bool131 pub(crate) fn linux_secure() -> bool {
132 unsafe { getauxval(AT_SECURE) as usize != 0 }
133 }
134
135 #[cfg(feature = "runtime")]
136 #[inline]
exe_phdrs() -> (*const c::c_void, usize, usize)137 pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) {
138 unsafe {
139 let phdr = getauxval(AT_PHDR) as *const c::c_void;
140 let phent = getauxval(AT_PHENT) as usize;
141 let phnum = getauxval(AT_PHNUM) as usize;
142 (phdr, phent, phnum)
143 }
144 }
145
146 /// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so
147 /// if we don't see it, this function returns a null pointer.
148 #[inline]
sysinfo_ehdr() -> *const Elf_Ehdr149 pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
150 #[cfg(not(feature = "runtime"))]
151 unsafe {
152 if let Some(libc_getauxval) = getauxval.get() {
153 libc_getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
154 } else {
155 null()
156 }
157 }
158
159 #[cfg(feature = "runtime")]
160 unsafe {
161 getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
162 }
163 }
164
165 #[cfg(feature = "runtime")]
166 #[inline]
entry() -> usize167 pub(crate) fn entry() -> usize {
168 unsafe { getauxval(AT_ENTRY) as usize }
169 }
170
171 #[cfg(feature = "runtime")]
172 #[inline]
random() -> *const [u8; 16]173 pub(crate) fn random() -> *const [u8; 16] {
174 unsafe { getauxval(AT_RANDOM) as *const [u8; 16] }
175 }
176