1use crate::{HEADER_SIZE, SECTION_SIZE};
3
4#[repr(C, packed)]
6#[derive(Debug, Clone, Copy)]
7pub struct Section {
8 pub name: [u8; 16],
10
11 pub is_loadable: bool,
13
14 pub is_execable: bool,
16
17 pub base: u32,
19
20 pub length: u32,
22
23 pub _reserved: [u8; 6]
25}
26
27impl Section {
28 #[inline]
30 pub const fn to_array(&self) -> [u8; SECTION_SIZE] {
31 unsafe { core::ptr::read(self as *const Self as *const [u8; SECTION_SIZE]) }
33 }
34
35 #[inline]
37 pub fn validate(&self) -> bool {
38 let align_ok = if self.is_loadable {
41 (self.base & 0xfff) == 0
42 } else {
43 true
44 };
45
46 let perm_ok = !(self.is_execable && !self.is_loadable);
48
49 align_ok && perm_ok
50 }
51}
52
53#[derive(Debug, Clone, Copy)]
55pub(crate) struct SectionIter {
56 pub buf: &'static [u8],
57 pub total: u16,
58 pub current: u16,
59}
60
61impl Iterator for SectionIter {
63 type Item = Section;
64
65 fn next(&mut self) -> Option<Self::Item> {
66 let base = HEADER_SIZE + self.current as usize * SECTION_SIZE;
67 let buf = &self.buf[base..base + SECTION_SIZE];
68
69 if self.current >= self.total {
71 return None;
72 }
73
74 let section = unsafe { *(buf.as_ptr() as *const Section) };
76 Some(section)
77 }
78}