1 //! Linux auxv support, using libc.
5 //! This uses raw pointers to locate and read the kernel-provided auxv array.
9 #[cfg(feature = "param")]
11 #[cfg(not(feature = "runtime"))]
13 use linux_raw_sys
::elf
::*;
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
);
20 // With the "runtime" feature, go ahead and depend on `getauxval` existing so
21 // that we never fail.
22 #[cfg(feature = "runtime")]
24 fn getauxval(type_
: c
::c_ulong
) -> *mut c
::c_void
;
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;
40 // Declare `sysconf` ourselves so that we don't depend on all of libc just for
43 fn sysconf(name
: c
::c_int
) -> c
::c_long
;
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;
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
);
71 #[cfg(feature = "param")]
73 pub(crate) fn page_size() -> usize {
74 unsafe { sysconf(_SC_PAGESIZE) as usize }
77 #[cfg(feature = "param")]
79 pub(crate) fn clock_ticks_per_second() -> u64 {
80 unsafe { sysconf(_SC_CLK_TCK) as u64 }
83 #[cfg(feature = "param")]
85 pub(crate) fn linux_hwcap() -> (usize, usize) {
86 #[cfg(not(feature = "runtime"))]
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;
97 #[cfg(feature = "runtime")]
99 let hwcap
= getauxval(AT_HWCAP
) as usize;
100 let hwcap2
= getauxval(AT_HWCAP2
) as usize;
105 #[cfg(feature = "param")]
107 pub(crate) fn linux_execfn() -> &'
static CStr
{
108 #[cfg(not(feature = "runtime"))]
110 if let Some(libc_getauxval
) = getauxval
.get() {
111 CStr
::from_ptr(libc_getauxval(AT_EXECFN
).cast())
117 #[cfg(feature = "runtime")]
119 CStr
::from_ptr(getauxval(AT_EXECFN
).cast())
123 #[cfg(feature = "runtime")]
125 pub(crate) fn exe_phdrs() -> (*const c
::c_void
, usize, usize) {
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;
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.
137 pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr
{
138 #[cfg(not(feature = "runtime"))]
140 if let Some(libc_getauxval
) = getauxval
.get() {
141 libc_getauxval(AT_SYSINFO_EHDR
) as *const Elf_Ehdr
147 #[cfg(feature = "runtime")]
149 getauxval(AT_SYSINFO_EHDR
) as *const Elf_Ehdr
153 #[cfg(feature = "runtime")]
155 pub(crate) fn entry() -> usize {
156 unsafe { getauxval(AT_ENTRY) as usize }