//! Structural ELF64/AArch64 header gate. Segment permission, payload and //! entry-point policy belong to the next loader stage, before publication. pub const HEADER_BYTES: usize = 64; pub const PROGRAM_BYTES: usize = 56; pub const MAX_PROGRAM_HEADERS: usize = 4; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Truncated, Magic, Class, Endian, Version, Machine, Type, HeaderSize, ProgramCount, ProgramStride, ProgramRange, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Program { pub kind: u32, pub flags: u32, pub offset: u64, pub virtual_address: u64, pub physical_address: u64, pub file_size: u64, pub memory_size: u64, pub alignment: u64, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Headers<'a> { file: &'a [u8], start: usize, count: usize, entry: u64, } fn u16_at(bytes: &[u8], at: usize) -> u16 { u16::from_le_bytes(bytes[at..at + 2].try_into().expect("bounded ELF field")) } fn u32_at(bytes: &[u8], at: usize) -> u32 { u32::from_le_bytes(bytes[at..at + 4].try_into().expect("bounded ELF field")) } fn u64_at(bytes: &[u8], at: usize) -> u64 { u64::from_le_bytes(bytes[at..at + 8].try_into().expect("bounded ELF field")) } impl<'a> Headers<'a> { pub fn parse(file: &'a [u8]) -> Result { let header = file.get(..HEADER_BYTES).ok_or(Error::Truncated)?; if header[..4] != [0x7f, b'E', b'L', b'F'] { return Err(Error::Magic); } if header[4] != 2 { return Err(Error::Class); } if header[5] != 1 { return Err(Error::Endian); } if header[6] != 1 || u32_at(header, 20) != 1 { return Err(Error::Version); } if u16_at(header, 18) != 183 { return Err(Error::Machine); } if u16_at(header, 16) != 2 { return Err(Error::Type); } if usize::from(u16_at(header, 52)) != HEADER_BYTES { return Err(Error::HeaderSize); } let count = usize::from(u16_at(header, 56)); if !(1..=MAX_PROGRAM_HEADERS).contains(&count) { return Err(Error::ProgramCount); } let stride = usize::from(u16_at(header, 54)); if stride != PROGRAM_BYTES { return Err(Error::ProgramStride); } let table_offset = u64_at(header, 32); let span = (count as u64).checked_mul(stride as u64).ok_or(Error::ProgramRange)?; let table_end = table_offset.checked_add(span).ok_or(Error::ProgramRange)?; let start = usize::try_from(table_offset).map_err(|_| Error::ProgramRange)?; let end = usize::try_from(table_end).map_err(|_| Error::ProgramRange)?; if start < HEADER_BYTES || file.get(start..end).is_none() { return Err(Error::ProgramRange); } Ok(Self { file, start, count, entry: u64_at(header, 24) }) } pub fn count(self) -> usize { self.count } pub fn entry(self) -> u64 { self.entry } pub fn table_start(self) -> usize { self.start } pub fn table_end(self) -> usize { self.start + self.count * PROGRAM_BYTES } pub fn program(self, index: usize) -> Option { if index >= self.count { return None; } // The whole span was checked before constructing this private view. let at = self.start + index * PROGRAM_BYTES; Some(Program { kind: u32_at(self.file, at), flags: u32_at(self.file, at + 4), offset: u64_at(self.file, at + 8), virtual_address: u64_at(self.file, at + 16), physical_address: u64_at(self.file, at + 24), file_size: u64_at(self.file, at + 32), memory_size: u64_at(self.file, at + 40), alignment: u64_at(self.file, at + 48) }) } } #[derive(Debug, PartialEq, Eq)] pub enum PrepareError { Header(Error), Resource(E) } /// Acquire private loader resources only after the entire header table is /// in bounds. This does not publish mappings or validate segment payloads. pub fn prepare<'a, T, E>(file: &'a [u8], acquire: impl FnOnce(Headers<'a>) -> Result) -> Result> { let headers = Headers::parse(file).map_err(PrepareError::Header)?; acquire(headers).map_err(PrepareError::Resource) }