#![allow(unsafe_code)]
use super::super::c;
use super::super::elf::*;
#[cfg(feature = "param")]
use crate::ffi::CStr;
use core::ffi::c_void;
use core::mem::size_of;
use core::ptr::{null, read};
#[cfg(feature = "runtime")]
use core::slice;
use linux_raw_sys::general::{
AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_PHDR, AT_PHENT, AT_PHNUM,
AT_SYSINFO_EHDR,
};
#[cfg(feature = "param")]
#[inline]
pub(crate) fn page_size() -> usize {
unsafe { PAGE_SIZE }
}
#[cfg(feature = "param")]
#[inline]
pub(crate) fn clock_ticks_per_second() -> u64 {
unsafe { CLOCK_TICKS_PER_SECOND as u64 }
}
#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_hwcap() -> (usize, usize) {
unsafe { (HWCAP, HWCAP2) }
}
#[cfg(feature = "param")]
#[inline]
pub(crate) fn linux_execfn() -> &'static CStr {
unsafe { CStr::from_ptr(EXECFN.0.cast()) }
}
#[cfg(feature = "runtime")]
#[inline]
pub(crate) fn exe_phdrs() -> (*const c_void, usize) {
unsafe { (PHDR.0.cast(), PHNUM) }
}
#[cfg(feature = "runtime")]
#[inline]
pub(in super::super) fn exe_phdrs_slice() -> &'static [Elf_Phdr] {
let (phdr, phnum) = exe_phdrs();
unsafe { slice::from_raw_parts(phdr.cast(), phnum) }
}
#[inline]
pub(in super::super) fn sysinfo_ehdr() -> *const Elf_Ehdr {
unsafe { SYSINFO_EHDR.0 }
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SyncConstPtr<T>(*const T);
unsafe impl<T> Sync for SyncConstPtr<T> {}
impl<T> SyncConstPtr<T> {
pub const unsafe fn new(ptr: *const T) -> Self {
Self(ptr)
}
}
static mut PAGE_SIZE: usize = 0;
static mut CLOCK_TICKS_PER_SECOND: usize = 0;
static mut HWCAP: usize = 0;
static mut HWCAP2: usize = 0;
static mut SYSINFO_EHDR: SyncConstPtr<Elf_Ehdr> = unsafe { SyncConstPtr::new(null()) };
static mut PHDR: SyncConstPtr<Elf_Phdr> = unsafe { SyncConstPtr::new(null()) };
static mut PHNUM: usize = 0;
static mut EXECFN: SyncConstPtr<c::c_char> = unsafe { SyncConstPtr::new(null()) };
pub(crate) unsafe fn init(envp: *mut *mut u8) {
init_from_envp(envp);
}
unsafe fn init_from_envp(mut envp: *mut *mut u8) {
while !(*envp).is_null() {
envp = envp.add(1);
}
init_from_auxp(envp.add(1).cast())
}
unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) {
loop {
let Elf_auxv_t { a_type, a_val } = read(auxp);
match a_type as _ {
AT_PAGESZ => PAGE_SIZE = a_val as usize,
AT_CLKTCK => CLOCK_TICKS_PER_SECOND = a_val as usize,
AT_HWCAP => HWCAP = a_val as usize,
AT_HWCAP2 => HWCAP2 = a_val as usize,
AT_PHDR => PHDR = SyncConstPtr::new(a_val.cast::<Elf_Phdr>()),
AT_PHNUM => PHNUM = a_val as usize,
AT_PHENT => assert_eq!(a_val as usize, size_of::<Elf_Phdr>()),
AT_EXECFN => EXECFN = SyncConstPtr::new(a_val.cast::<c::c_char>()),
AT_SYSINFO_EHDR => SYSINFO_EHDR = SyncConstPtr::new(a_val.cast::<Elf_Ehdr>()),
AT_NULL => break,
_ => (),
}
auxp = auxp.add(1);
}
}
#[repr(C)]
#[derive(Copy, Clone)]
struct Elf_auxv_t {
a_type: usize,
a_val: *const c_void,
}