]> git.proxmox.com Git - rustc.git/blob - vendor/rustix/src/backend/linux_raw/param/libc_auxv.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / vendor / rustix / src / backend / linux_raw / param / libc_auxv.rs
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 const AT_HWCAP2: c::c_ulong = 26;
37 const AT_EXECFN: c::c_ulong = 31;
38 const AT_SYSINFO_EHDR: c::c_ulong = 33;
39
40 // Declare `sysconf` ourselves so that we don't depend on all of libc just for
41 // this.
42 extern "C" {
43 fn sysconf(name: c::c_int) -> c::c_long;
44 }
45
46 #[cfg(target_os = "android")]
47 const _SC_PAGESIZE: c::c_int = 39;
48 #[cfg(target_os = "linux")]
49 const _SC_PAGESIZE: c::c_int = 30;
50 #[cfg(target_os = "android")]
51 const _SC_CLK_TCK: c::c_int = 6;
52 #[cfg(target_os = "linux")]
53 const _SC_CLK_TCK: c::c_int = 2;
54
55 #[test]
56 fn test_abi() {
57 const_assert_eq!(self::_SC_PAGESIZE, ::libc::_SC_PAGESIZE);
58 const_assert_eq!(self::_SC_CLK_TCK, ::libc::_SC_CLK_TCK);
59 const_assert_eq!(self::AT_HWCAP, ::libc::AT_HWCAP);
60 const_assert_eq!(self::AT_HWCAP2, ::libc::AT_HWCAP2);
61 const_assert_eq!(self::AT_EXECFN, ::libc::AT_EXECFN);
62 const_assert_eq!(self::AT_SYSINFO_EHDR, ::libc::AT_SYSINFO_EHDR);
63 #[cfg(feature = "runtime")]
64 const_assert_eq!(self::AT_PHDR, ::libc::AT_PHDR);
65 #[cfg(feature = "runtime")]
66 const_assert_eq!(self::AT_PHNUM, ::libc::AT_PHNUM);
67 #[cfg(feature = "runtime")]
68 const_assert_eq!(self::AT_ENTRY, ::libc::AT_ENTRY);
69 }
70
71 #[cfg(feature = "param")]
72 #[inline]
73 pub(crate) fn page_size() -> usize {
74 unsafe { sysconf(_SC_PAGESIZE) as usize }
75 }
76
77 #[cfg(feature = "param")]
78 #[inline]
79 pub(crate) fn clock_ticks_per_second() -> u64 {
80 unsafe { sysconf(_SC_CLK_TCK) as u64 }
81 }
82
83 #[cfg(feature = "param")]
84 #[inline]
85 pub(crate) fn linux_hwcap() -> (usize, usize) {
86 #[cfg(not(feature = "runtime"))]
87 unsafe {
88 if let Some(libc_getauxval) = getauxval.get() {
89 let hwcap = libc_getauxval(AT_HWCAP) as usize;
90 let hwcap2 = libc_getauxval(AT_HWCAP2) as usize;
91 (hwcap, hwcap2)
92 } else {
93 (0, 0)
94 }
95 }
96
97 #[cfg(feature = "runtime")]
98 unsafe {
99 let hwcap = getauxval(AT_HWCAP) as usize;
100 let hwcap2 = getauxval(AT_HWCAP2) as usize;
101 (hwcap, hwcap2)
102 }
103 }
104
105 #[cfg(feature = "param")]
106 #[inline]
107 pub(crate) fn linux_execfn() -> &'static CStr {
108 #[cfg(not(feature = "runtime"))]
109 unsafe {
110 if let Some(libc_getauxval) = getauxval.get() {
111 CStr::from_ptr(libc_getauxval(AT_EXECFN).cast())
112 } else {
113 cstr!("")
114 }
115 }
116
117 #[cfg(feature = "runtime")]
118 unsafe {
119 CStr::from_ptr(getauxval(AT_EXECFN).cast())
120 }
121 }
122
123 #[cfg(feature = "runtime")]
124 #[inline]
125 pub(crate) fn exe_phdrs() -> (*const c::c_void, usize, usize) {
126 unsafe {
127 let phdr = getauxval(AT_PHDR) as *const c::c_void;
128 let phent = getauxval(AT_PHENT) as usize;
129 let phnum = getauxval(AT_PHNUM) as usize;
130 (phdr, phent, phnum)
131 }
132 }
133
134 /// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations,
135 /// so if we don't see it, this function returns a null pointer.
136 #[inline]
137 pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
138 #[cfg(not(feature = "runtime"))]
139 unsafe {
140 if let Some(libc_getauxval) = getauxval.get() {
141 libc_getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
142 } else {
143 null()
144 }
145 }
146
147 #[cfg(feature = "runtime")]
148 unsafe {
149 getauxval(AT_SYSINFO_EHDR) as *const Elf_Ehdr
150 }
151 }
152
153 #[cfg(feature = "runtime")]
154 #[inline]
155 pub(crate) fn entry() -> usize {
156 unsafe { getauxval(AT_ENTRY) as usize }
157 }