Debian Codegraph @

debian/missing-sources/rustix/src/backend/linux_raw/param/libc_auxv.rs

raw
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 const AT_MINSIGSTKSZ: c::c_ulong = 51;
43
44 // Declare `sysconf` ourselves so that we don't depend on all of libc just for
45 // this.
46 extern "C" {
47 fn sysconf(name: c::c_int) -> c::c_long;
48 }
49
50 #[cfg(target_os = "android")]
51 const _SC_PAGESIZE: c::c_int = 39;
52 #[cfg(target_os = "linux")]
53 const _SC_PAGESIZE: c::c_int = 30;
54 #[cfg(target_os = "android")]
55 const _SC_CLK_TCK: c::c_int = 6;
56 #[cfg(target_os = "linux")]
57 const _SC_CLK_TCK: c::c_int = 2;
58
59 #[cfg(feature = "param")]
60 #[inline]
61 pub(crate) fn page_size() -> usize {
62 unsafe { sysconf(_SC_PAGESIZE) as usize }
63 }
64
65 #[cfg(feature = "param")]
66 #[inline]
67 pub(crate) fn clock_ticks_per_second() -> u64 {
68 unsafe { sysconf(_SC_CLK_TCK) as u64 }
69 }
70
71 #[cfg(feature = "param")]
72 #[inline]
73 pub(crate) fn linux_hwcap() -> (usize, usize) {
74 #[cfg(not(feature = "runtime"))]
75 unsafe {
76 if let Some(libc_getauxval) = getauxval.get() {
77 let hwcap = libc_getauxval(AT_HWCAP) as usize;
78 let hwcap2 = libc_getauxval(AT_HWCAP2) as usize;
79 (hwcap, hwcap2)
80 } else {
81 (0, 0)
82 }
83 }
84
85 #[cfg(feature = "runtime")]
86 unsafe {
87 let hwcap = getauxval(AT_HWCAP) as usize;
88 let hwcap2 = getauxval(AT_HWCAP2) as usize;
89 (hwcap, hwcap2)
90 }
91 }
92
93 #[cfg(feature = "param")]
94 #[inline]
95 pub(crate) fn linux_minsigstksz() -> usize {
96 #[cfg(not(feature = "runtime"))]
97 if let Some(libc_getauxval) = getauxval.get() {
98 unsafe { libc_getauxval(AT_MINSIGSTKSZ) as usize }
99 } else {
100 0
101 }
102
103 #[cfg(feature = "runtime")]
104 unsafe {
105 getauxval(AT_MINSIGSTKSZ) as usize
106 }
107 }
108
109 #[cfg(feature = "param")]
110 #[inline]
111 pub(crate) fn linux_execfn() -> &'static CStr {
112 #[cfg(not(feature = "runtime"))]
113 unsafe {
114 if let Some(libc_getauxval) = getauxval.get() {
115 CStr::from_ptr(libc_getauxval(AT_EXECFN).cast())
116 } else {
117 cstr!("")
118 }
119 }
120
121 #[cfg(feature = "runtime")]
122 unsafe {
123 CStr::from_ptr(getauxval(AT_EXECFN).cast())
124 }
125 }
126
127 #[cfg(feature = "runtime")]
128 #[inline]
129 pub(crate) fn linux_secure() -> bool {
130 unsafe { getauxval(AT_SECURE) as usize != 0 }
131 }
132
133 #[cfg(feature = "runtime")]
134 #[inline]
135 pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) {
136 unsafe {
137 let phdr: *const c::c_void = getauxval(AT_PHDR);
138 let phent = getauxval(AT_PHENT) as usize;
139 let phnum = getauxval(AT_PHNUM) as usize;
140 (phdr, phent, phnum)
141 }
142 }
143
144 /// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so
145 /// if we don't see it, this function returns a null pointer.
146 #[inline]
147 pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
148 #[cfg(not(feature = "runtime"))]
149 unsafe {
150 if let Some(libc_getauxval) = getauxval.get() {
151 libc_getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
152 } else {
153 null()
154 }
155 }
156
157 #[cfg(feature = "runtime")]
158 unsafe {
159 getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
160 }
161 }
162
163 #[cfg(feature = "runtime")]
164 #[inline]
165 pub(crate) fn entry() -> usize {
166 unsafe { getauxval(AT_ENTRY) as usize }
167 }
168
169 #[cfg(feature = "runtime")]
170 #[inline]
171 pub(crate) fn random() -> *const [u8; 16] {
172 unsafe { getauxval(AT_RANDOM) as *const [u8; 16] }
173 }
174
175 #[cfg(test)]
176 mod tests {
177 use super::*;
178
179 #[test]
180 fn test_abi() {
181 const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
182 const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
183 const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
184 const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
185 const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
186 const_assert_eq!(self::AT_SECURE, ::libc::AT_SECURE);
187 const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
188 const_assert_eq!(self::AT_MINSIGSTKSZ, ::libc::AT_MINSIGSTKSZ);
189 #[cfg(feature = "runtime")]
190 const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
191 #[cfg(feature = "runtime")]
192 const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
193 #[cfg(feature = "runtime")]
194 const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY);
195 #[cfg(feature = "runtime")]
196 const_assert_eq!(self::AT_RANDOM, ::libc::AT_RANDOM);
197 }
198 }