1use crate::{HEADER_SIZE, SECTION_SIZE};
3
4#[repr(C)]
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
24impl Section {
25 #[inline]
27 pub const fn to_array(&self) -> [u8; SECTION_SIZE] {
28 unsafe { core::ptr::read(self as *const Self as *const [u8; SECTION_SIZE]) }
30 }
31
32 #[inline]
34 pub fn validate(&self) -> bool {
35 let align_ok = if self.is_loadable {
38 (self.base & 0xfff) == 0
39 } else {
40 true
41 };
42
43 let perm_ok = !(self.is_execable && !self.is_loadable);
45
46 align_ok && perm_ok
47 }
48}
49
50#[derive(Debug, Clone, Copy)]
52pub(crate) struct SectionIter {
53 pub buf: &'static [u8],
54 pub total: u16,
55 pub current: u16,
56}
57
58impl Iterator for SectionIter {
60 type Item = Section;
61
62 fn next(&mut self) -> Option<Self::Item> {
63 let base = HEADER_SIZE + self.current as usize * SECTION_SIZE;
64 let buf = &self.buf[base..base + SECTION_SIZE];
65
66 if self.current >= self.total {
68 return None;
69 }
70
71 let section = unsafe { *(buf.as_ptr() as *const Section) };
73 Some(section)
74 }
75}