⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

veridian_kernel/elf/
types.rs

1//! ELF64 type definitions
2//!
3//! Contains all ELF64 struct, enum, and error type definitions used by the
4//! loader. Separated from `mod.rs` for maintainability.
5
6use alloc::{string::String, vec::Vec};
7
8/// ELF magic number
9pub const ELF_MAGIC: [u8; 4] = [0x7f, b'E', b'L', b'F'];
10
11/// ELF class
12#[repr(u8)]
13#[derive(Debug, Clone, Copy, PartialEq)]
14pub enum ElfClass {
15    None = 0,
16    Elf32 = 1,
17    Elf64 = 2,
18}
19
20/// ELF data encoding
21#[repr(u8)]
22#[derive(Debug, Clone, Copy, PartialEq)]
23pub enum ElfData {
24    None = 0,
25    LittleEndian = 1,
26    BigEndian = 2,
27}
28
29/// ELF file type
30#[repr(u16)]
31#[derive(Debug, Clone, Copy, PartialEq)]
32pub enum ElfType {
33    None = 0,
34    Relocatable = 1,
35    Executable = 2,
36    SharedObject = 3,
37    Core = 4,
38}
39
40/// ELF machine type
41#[repr(u16)]
42#[derive(Debug, Clone, Copy, PartialEq)]
43pub enum ElfMachine {
44    None = 0,
45    X86_64 = 62,
46    AArch64 = 183,
47    RiscV = 243,
48}
49
50/// ELF header
51#[repr(C)]
52#[derive(Debug, Clone, Copy)]
53pub struct Elf64Header {
54    pub magic: [u8; 4],
55    pub class: u8,
56    pub data: u8,
57    pub version: u8,
58    pub os_abi: u8,
59    pub abi_version: u8,
60    pub padding: [u8; 7],
61    pub elf_type: u16,
62    pub machine: u16,
63    pub version2: u32,
64    pub entry: u64,
65    pub phoff: u64,
66    pub shoff: u64,
67    pub flags: u32,
68    pub ehsize: u16,
69    pub phentsize: u16,
70    pub phnum: u16,
71    pub shentsize: u16,
72    pub shnum: u16,
73    pub shstrndx: u16,
74}
75
76/// Program header type
77#[repr(u32)]
78#[derive(Debug, Clone, Copy, PartialEq)]
79pub enum ProgramType {
80    Null = 0,
81    Load = 1,
82    Dynamic = 2,
83    Interp = 3,
84    Note = 4,
85    Shlib = 5,
86    Phdr = 6,
87    Tls = 7,
88}
89
90/// Program header
91#[repr(C)]
92#[derive(Debug, Clone, Copy)]
93pub struct Elf64ProgramHeader {
94    pub p_type: u32,
95    pub p_flags: u32,
96    pub p_offset: u64,
97    pub p_vaddr: u64,
98    pub p_paddr: u64,
99    pub p_filesz: u64,
100    pub p_memsz: u64,
101    pub p_align: u64,
102}
103
104/// Section header
105#[repr(C)]
106#[derive(Debug, Clone, Copy)]
107pub struct Elf64SectionHeader {
108    pub sh_name: u32,
109    pub sh_type: u32,
110    pub sh_flags: u64,
111    pub sh_addr: u64,
112    pub sh_offset: u64,
113    pub sh_size: u64,
114    pub sh_link: u32,
115    pub sh_info: u32,
116    pub sh_addralign: u64,
117    pub sh_entsize: u64,
118}
119
120/// Dynamic entry
121#[repr(C)]
122#[derive(Debug, Clone, Copy)]
123pub struct Elf64Dynamic {
124    pub d_tag: i64,
125    pub d_val: u64,
126}
127
128/// Symbol table entry
129#[repr(C)]
130#[derive(Debug, Clone, Copy)]
131pub struct Elf64Symbol {
132    pub st_name: u32,
133    pub st_info: u8,
134    pub st_other: u8,
135    pub st_shndx: u16,
136    pub st_value: u64,
137    pub st_size: u64,
138}
139
140/// Relocation entry
141#[repr(C)]
142#[derive(Debug, Clone, Copy)]
143pub struct Elf64Rela {
144    pub r_offset: u64,
145    pub r_info: u64,
146    pub r_addend: i64,
147}
148
149/// ELF loader errors
150#[derive(Debug)]
151pub enum ElfError {
152    InvalidMagic,
153    InvalidClass,
154    InvalidData,
155    InvalidType,
156    UnsupportedMachine,
157    InvalidProgramHeader,
158    MemoryAllocationFailed,
159    FileReadFailed,
160    RelocationFailed,
161    InvalidSymbol,
162}
163
164/// ELF segment information
165#[derive(Debug, Clone)]
166pub struct ElfSegment {
167    pub segment_type: SegmentType,
168    pub virtual_addr: u64,
169    pub physical_addr: u64,
170    pub file_offset: u64,
171    pub file_size: u64,
172    pub memory_size: u64,
173    pub flags: u32,
174    pub alignment: u64,
175}
176
177/// Segment type
178#[derive(Debug, Clone, Copy, PartialEq)]
179pub enum SegmentType {
180    Null,
181    Load,
182    Dynamic,
183    Interp,
184    Note,
185    Shlib,
186    Phdr,
187    Tls,
188    Other(u32),
189}
190
191/// ELF binary information
192#[derive(Debug)]
193pub struct ElfBinary {
194    pub entry_point: u64,
195    pub load_base: u64,
196    pub load_size: usize,
197    pub segments: Vec<ElfSegment>,
198    pub interpreter: Option<String>,
199    pub dynamic: bool,
200}
201
202/// Dynamic linking information
203#[derive(Debug)]
204pub struct DynamicInfo {
205    pub needed: Vec<String>,              // Required shared libraries
206    pub soname: Option<String>,           // Library name
207    pub rpath: Option<String>,            // Runtime library search path
208    pub runpath: Option<String>,          // Runtime library search path (newer)
209    pub init: Option<u64>,                // Initialization function
210    pub fini: Option<u64>,                // Finalization function
211    pub init_array: Option<(u64, usize)>, // Init array (addr, count)
212    pub fini_array: Option<(u64, usize)>, // Fini array (addr, count)
213    pub hash: Option<u64>,                // Symbol hash table
214    pub strtab: Option<u64>,              // String table
215    pub symtab: Option<u64>,              // Symbol table
216    pub strsz: usize,                     // String table size
217    pub syment: usize,                    // Symbol table entry size
218    pub pltgot: Option<u64>,              // PLT/GOT address
219    pub pltrelsz: usize,                  // PLT relocation table size
220    pub pltrel: Option<u64>,              // PLT relocation type
221    pub jmprel: Option<u64>,              // PLT relocations
222    pub rel: Option<u64>,                 // Relocation table
223    pub relsz: usize,                     // Relocation table size
224    pub relent: usize,                    // Relocation entry size
225    pub rela: Option<u64>,                // Relocation table with addends
226    pub relasz: usize,                    // Rela table size
227    pub relaent: usize,                   // Rela entry size
228}
229
230/// Symbol information
231#[derive(Debug, Clone)]
232pub struct ElfSymbol {
233    pub name: String,
234    pub value: u64,
235    pub size: u64,
236    pub info: u8,
237    pub other: u8,
238    pub shndx: u16,
239}
240
241/// Relocation entry
242#[derive(Debug, Clone)]
243pub struct ElfRelocation {
244    pub offset: u64,
245    pub symbol: u32,
246    pub reloc_type: u32,
247    pub addend: i64,
248}