Skip to main content

kernel/sched/loader/defs/
header.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! ELF header definitions
3//!
4//! Authors: MarioS271
5
6#[repr(C)]
7pub struct ElfHeader {
8    pub e_ident: [u8; 16],
9    pub e_type: u16,
10    pub e_machine: u16,
11    pub e_version: u32,
12    pub e_entry: u64,
13    pub e_phoff: u64,
14    pub e_shoff: u64,
15    pub e_flags: u32,
16    pub e_ehsize: u16,
17    pub e_phentsize: u16,
18    pub e_phnum: u16,
19    pub e_shentsize: u16,
20    pub e_shnum: u16,
21    pub e_shstrndx: u16,
22}
23
24impl ElfHeader {
25    pub const ELF_MAGIC: [u8; 4] = [0x7F, b'E', b'L', b'F'];
26    pub const ET_EXEC: u16 = 2;
27    pub const ET_DYN: u16 = 3;
28    pub const EM_X86_64: u16 = 0x3E;
29
30    /// Takes an ELF byte slice and returns it as a reference to an [`ElfHeader`]
31    ///
32    /// # Safety
33    /// The caller should make sure that the given byte slice is ELF data to avoid undefined behavior
34    pub fn new(elf: &[u8]) -> &Self {
35        unsafe { &*(elf.as_ptr() as *const Self) }
36    }
37}