| 1 |
//! Linux auxv `init` function, for "use-explicitly-provided-auxv" mode. |
| 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 |
use core::ffi::c_void; |
| 12 |
use core::ptr::{null_mut, read, NonNull}; |
| 13 |
#[cfg(feature = "runtime")] |
| 14 |
use core::sync::atomic::AtomicBool; |
| 15 |
use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; |
| 16 |
use linux_raw_sys::elf::*; |
| 17 |
use linux_raw_sys::general::{ |
| 18 |
AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_MINSIGSTKSZ, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR, |
| 19 |
}; |
| 20 |
#[cfg(feature = "runtime")] |
| 21 |
use linux_raw_sys::general::{AT_ENTRY, AT_PHDR, AT_PHENT, AT_PHNUM, AT_RANDOM, AT_SECURE}; |
| 22 |
|
| 23 |
#[cfg(feature = "param")] |
| 24 |
#[inline] |
| 25 |
pub(crate) fn page_size() -> usize { |
| 26 |
unsafe { PAGE_SIZE.load(Ordering::Relaxed) } |
| 27 |
} |
| 28 |
|
| 29 |
#[cfg(feature = "param")] |
| 30 |
#[inline] |
| 31 |
pub(crate) fn clock_ticks_per_second() -> u64 { |
| 32 |
unsafe { CLOCK_TICKS_PER_SECOND.load(Ordering::Relaxed) as u64 } |
| 33 |
} |
| 34 |
|
| 35 |
#[cfg(feature = "param")] |
| 36 |
#[inline] |
| 37 |
pub(crate) fn linux_hwcap() -> (usize, usize) { |
| 38 |
unsafe { |
| 39 |
( |
| 40 |
HWCAP.load(Ordering::Relaxed), |
| 41 |
HWCAP2.load(Ordering::Relaxed), |
| 42 |
) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg(feature = "param")] |
| 47 |
#[inline] |
| 48 |
pub(crate) fn linux_minsigstksz() -> usize { |
| 49 |
unsafe { MINSIGSTKSZ.load(Ordering::Relaxed) } |
| 50 |
} |
| 51 |
|
| 52 |
#[cfg(feature = "param")] |
| 53 |
#[inline] |
| 54 |
pub(crate) fn linux_execfn() -> &'static CStr { |
| 55 |
let execfn = unsafe { EXECFN.load(Ordering::Relaxed) }; |
| 56 |
|
| 57 |
// SAFETY: We initialize `EXECFN` to a valid `CStr` pointer, and we assume |
| 58 |
// the `AT_EXECFN` value provided by the kernel points to a valid C string. |
| 59 |
unsafe { CStr::from_ptr(execfn.cast()) } |
| 60 |
} |
| 61 |
|
| 62 |
#[cfg(feature = "runtime")] |
| 63 |
#[inline] |
| 64 |
pub(crate) fn linux_secure() -> bool { |
| 65 |
unsafe { SECURE.load(Ordering::Relaxed) } |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(feature = "runtime")] |
| 69 |
#[inline] |
| 70 |
pub(crate) fn exe_phdrs() -> (*const c_void, usize, usize) { |
| 71 |
unsafe { |
| 72 |
( |
| 73 |
PHDR.load(Ordering::Relaxed).cast(), |
| 74 |
PHENT.load(Ordering::Relaxed), |
| 75 |
PHNUM.load(Ordering::Relaxed), |
| 76 |
) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/// `AT_SYSINFO_EHDR` isn't present on all platforms in all configurations, so |
| 81 |
/// if we don't see it, this function returns a null pointer. |
| 82 |
#[inline] |
| 83 |
pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr { |
| 84 |
unsafe { SYSINFO_EHDR.load(Ordering::Relaxed) } |
| 85 |
} |
| 86 |
|
| 87 |
#[cfg(feature = "runtime")] |
| 88 |
#[inline] |
| 89 |
pub(crate) fn entry() -> usize { |
| 90 |
unsafe { ENTRY.load(Ordering::Relaxed) } |
| 91 |
} |
| 92 |
|
| 93 |
#[cfg(feature = "runtime")] |
| 94 |
#[inline] |
| 95 |
pub(crate) fn random() -> *const [u8; 16] { |
| 96 |
unsafe { RANDOM.load(Ordering::Relaxed) } |
| 97 |
} |
| 98 |
|
| 99 |
static mut PAGE_SIZE: AtomicUsize = AtomicUsize::new(0); |
| 100 |
static mut CLOCK_TICKS_PER_SECOND: AtomicUsize = AtomicUsize::new(0); |
| 101 |
static mut HWCAP: AtomicUsize = AtomicUsize::new(0); |
| 102 |
static mut HWCAP2: AtomicUsize = AtomicUsize::new(0); |
| 103 |
static mut MINSIGSTKSZ: AtomicUsize = AtomicUsize::new(0); |
| 104 |
static mut SYSINFO_EHDR: AtomicPtr<Elf_Ehdr> = AtomicPtr::new(null_mut()); |
| 105 |
// Initialize `EXECFN` to a valid `CStr` pointer so that we don't need to check |
| 106 |
// for null on every `execfn` call. |
| 107 |
static mut EXECFN: AtomicPtr<c::c_char> = AtomicPtr::new(b"\0".as_ptr() as _); |
| 108 |
#[cfg(feature = "runtime")] |
| 109 |
static mut SECURE: AtomicBool = AtomicBool::new(false); |
| 110 |
// Use `dangling` so that we can always treat it like an empty slice. |
| 111 |
#[cfg(feature = "runtime")] |
| 112 |
static mut PHDR: AtomicPtr<Elf_Phdr> = AtomicPtr::new(NonNull::dangling().as_ptr()); |
| 113 |
#[cfg(feature = "runtime")] |
| 114 |
static mut PHENT: AtomicUsize = AtomicUsize::new(0); |
| 115 |
#[cfg(feature = "runtime")] |
| 116 |
static mut PHNUM: AtomicUsize = AtomicUsize::new(0); |
| 117 |
#[cfg(feature = "runtime")] |
| 118 |
static mut ENTRY: AtomicUsize = AtomicUsize::new(0); |
| 119 |
#[cfg(feature = "runtime")] |
| 120 |
static mut RANDOM: AtomicPtr<[u8; 16]> = AtomicPtr::new(NonNull::dangling().as_ptr()); |
| 121 |
|
| 122 |
/// When "use-explicitly-provided-auxv" is enabled, we export a function to be |
| 123 |
/// called during initialization, and passed a pointer to the original |
| 124 |
/// environment variable block set up by the OS. |
| 125 |
pub(crate) unsafe fn init(envp: *mut *mut u8) { |
| 126 |
init_from_envp(envp); |
| 127 |
} |
| 128 |
|
| 129 |
/// # Safety |
| 130 |
/// |
| 131 |
/// This must be passed a pointer to the environment variable buffer |
| 132 |
/// provided by the kernel, which is followed in memory by the auxv array. |
| 133 |
unsafe fn init_from_envp(mut envp: *mut *mut u8) { |
| 134 |
while !(*envp).is_null() { |
| 135 |
envp = envp.add(1); |
| 136 |
} |
| 137 |
init_from_auxp(envp.add(1).cast()) |
| 138 |
} |
| 139 |
|
| 140 |
/// Process auxv entries from the auxv array pointed to by `auxp`. |
| 141 |
/// |
| 142 |
/// # Safety |
| 143 |
/// |
| 144 |
/// This must be passed a pointer to an auxv array. |
| 145 |
/// |
| 146 |
/// The buffer contains `Elf_aux_t` elements, though it need not be aligned; |
| 147 |
/// function uses `read_unaligned` to read from it. |
| 148 |
unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) { |
| 149 |
loop { |
| 150 |
let Elf_auxv_t { a_type, a_val } = read(auxp); |
| 151 |
|
| 152 |
match a_type as _ { |
| 153 |
AT_PAGESZ => PAGE_SIZE.store(a_val as usize, Ordering::Relaxed), |
| 154 |
AT_CLKTCK => CLOCK_TICKS_PER_SECOND.store(a_val as usize, Ordering::Relaxed), |
| 155 |
AT_HWCAP => HWCAP.store(a_val as usize, Ordering::Relaxed), |
| 156 |
AT_HWCAP2 => HWCAP2.store(a_val as usize, Ordering::Relaxed), |
| 157 |
AT_MINSIGSTKSZ => MINSIGSTKSZ.store(a_val as usize, Ordering::Relaxed), |
| 158 |
AT_EXECFN => EXECFN.store(a_val.cast::<c::c_char>(), Ordering::Relaxed), |
| 159 |
AT_SYSINFO_EHDR => SYSINFO_EHDR.store(a_val.cast::<Elf_Ehdr>(), Ordering::Relaxed), |
| 160 |
|
| 161 |
#[cfg(feature = "runtime")] |
| 162 |
AT_SECURE => SECURE.store(a_val as usize != 0, Ordering::Relaxed), |
| 163 |
#[cfg(feature = "runtime")] |
| 164 |
AT_PHDR => PHDR.store(a_val.cast::<Elf_Phdr>(), Ordering::Relaxed), |
| 165 |
#[cfg(feature = "runtime")] |
| 166 |
AT_PHNUM => PHNUM.store(a_val as usize, Ordering::Relaxed), |
| 167 |
#[cfg(feature = "runtime")] |
| 168 |
AT_PHENT => PHENT.store(a_val as usize, Ordering::Relaxed), |
| 169 |
#[cfg(feature = "runtime")] |
| 170 |
AT_ENTRY => ENTRY.store(a_val as usize, Ordering::Relaxed), |
| 171 |
#[cfg(feature = "runtime")] |
| 172 |
AT_RANDOM => RANDOM.store(a_val.cast::<[u8; 16]>(), Ordering::Relaxed), |
| 173 |
|
| 174 |
AT_NULL => break, |
| 175 |
_ => (), |
| 176 |
} |
| 177 |
auxp = auxp.add(1); |
| 178 |
} |
| 179 |
} |