Skip to main content

proka_exec/
sections.rs

1//! The definitions of section entry.
2use crate::{HEADER_SIZE, SECTION_SIZE};
3
4/// A section entry.
5#[repr(C)]
6#[derive(Debug, Clone, Copy)]
7pub struct Section {
8    /// The section name (16 bytes max).
9    pub name: [u8; 16],
10
11    /// Assign is this section loadable
12    pub is_loadable: bool,
13
14    /// Assign is this section executable
15    pub is_execable: bool,
16
17    /// The offset of the section start.
18    pub base: u32,
19
20    /// The length of the section.
21    pub length: u32
22}
23
24/// The iterator of each sections
25#[derive(Debug, Clone, Copy)]
26pub(crate) struct SectionIter {
27    pub buf: &'static [u8],
28    pub total: u16,
29    pub current: u16,
30}
31
32// Iterator implementations
33impl Iterator for SectionIter {
34    type Item = Section;
35
36    fn next(&mut self) -> Option<Self::Item> {
37        let base = HEADER_SIZE + self.current as usize * SECTION_SIZE; 
38        let buf = &self.buf[base..base + SECTION_SIZE];
39
40        // Check: is current over than total
41        if self.current >= self.total {
42            return None;
43        }
44
45        // Now convert it
46        let section = unsafe { *(buf.as_ptr() as *const Section) };
47        Some(section)
48    }
49}