Max OS 0.3
Loading...
Searching...
No Matches
elf.h
Go to the documentation of this file.
1
9#ifndef MAXOS_PROCESSES_ELF_H
10#define MAXOS_PROCESSES_ELF_H
11
12#include <cstdint>
13#include <cstddef>
14
16#include <memory/memoryIO.h>
17
18
19namespace MaxOS::processes {
20
22 constexpr char ELF_MAGIC[4] = { 0x7F, 'E', 'L', 'F' };
23
28 enum class ELFIdentification {
29 Magic0,
30 Magic1,
31 Magic2,
32 Magic3,
33 Class,
34 Data,
35 Version,
36 OSABI,
37 ABIVersion,
38 Padding,
39 };
40
45 enum class ELFClass {
46 Invalid,
47 Bits32,
48 Bits64
49 };
50
55 enum class ELFData {
56 InvalidData,
57 LittleEndian,
58 BigEndian
59 };
60
65 enum class ELFType {
66 None,
67 Relocatable,
68 Executable,
69 Shared,
70 Core,
71 ProcessorSpecificLow = 0xFF0,
72 ProcessorSpecificHigh = 0xFFF
73 };
74
79 enum class ELFMachine {
80 NoMachine,
81 ATnTWe32100,
82 SPARC,
83 x86,
84 MIPS = 0x8,
85 PowerPC = 0x14,
86 ARM = 0x28,
87 SuperH = 0x2A,
88 IA_64 = 0x32,
89 x86_64 = 0x3E,
90 AArch64 = 0xB7,
91 RISC_V = 0xF3
92 };
93
98 enum class ELFVersion {
99 Invalid,
100 Current
101 };
102
110 typedef struct ELFHeader64 {
111
112 unsigned char identification[16];
113 uint16_t type;
114 uint16_t machine;
115 uint32_t version;
116 uint64_t entry;
119 uint32_t flags;
126
127 } elf_64_header_t;
128
133 enum class ELFProgramType {
134 Null,
135 Load,
136 Dynamic,
137 Interpreter,
138 Note,
139 SharedLibrary,
140 HeaderTable,
141 TableProcessorSpecificLow = 0x70000000,
142 TableProcessorSpecificHigh = 0x7FFFFFFF
143 };
144
150 ELFExecute = (1 << 0),
151 ELFWrite = (1 << 1),
152 ELFRead = (1 << 2)
153 };
154
162 typedef struct ELFProgramHeader64 {
163
164 uint32_t type;
165 uint32_t flags;
166 uint64_t offset;
169 uint64_t file_size;
170 uint64_t memory_size;
171 uint64_t align;
172
173 } elf_64_program_header_t;
174
179 enum class ELFSectionType {
180 Null,
181 ProgramBits,
182 SymbolTable,
183 StringTable,
184 RelocationAddends,
185 SymbolHashTable,
186 DynamicLinkingTable,
187 SectionNote,
188 NoBits,
189 };
190
198 typedef struct ELFSectionHeader64 {
199
200 uint32_t name_index;
201 uint32_t type;
202 uint64_t sh_flags;
204 uint64_t offset;
205 uint64_t size;
206 uint32_t link;
207 uint32_t info;
209 uint64_t entry_size;
210
211 } elf_64_section_header_t;
212
217 class ELF64 {
218 private:
219 uintptr_t m_elf_header_address;
220
221 void load_program_headers() const;
222
223 public:
224 explicit ELF64(uintptr_t elf_header_address);
226
227 void load();
228 [[nodiscard]] bool is_valid() const;
229
230 [[nodiscard]] elf_64_header_t* header() const;
231 [[nodiscard]] elf_64_program_header_t* get_program_header(size_t index) const;
232 [[nodiscard]] elf_64_section_header_t* get_section_header(size_t index) const;
233
234 static uint64_t to_vmm_flags(uint32_t type);
235
236 };
237
238}
239
240
241#endif // MAXOS_PROCESSES_ELF_H
Handles the loading and parsing of 64-bit ELF files.
Definition elf.h:217
elf_64_header_t * header() const
Gets the header of the elf file.
Definition elf.cpp:52
static uint64_t to_vmm_flags(uint32_t type)
Converts elf flags to vmm flags.
Definition elf.cpp:173
~ELF64()
Destructor for the ELF64 class.
elf_64_program_header_t * get_program_header(size_t index) const
Gets a program header from the elf file.
Definition elf.cpp:63
void load()
Loads the elf program into memory if a valid elf file.
Definition elf.cpp:38
elf_64_section_header_t * get_section_header(size_t index) const
Gets a section header from the elf file.
Definition elf.cpp:81
bool is_valid() const
Checks if the elf file is valid for MaxOS runtime.
Definition elf.cpp:100
ELFType
The type of the ELF file (executable, shared object, etc.)
Definition elf.h:65
constexpr char ELF_MAGIC[4]
The expected ELF magic number at the start of the file.
Definition elf.h:22
ELFMachine
The target architecture of the ELF file.
Definition elf.h:79
ELFSectionType
The type of a section in the ELF file.
Definition elf.h:179
ELFProgramType
The type of a program header in the ELF file.
Definition elf.h:133
ELFProgramFlags
The flags of a program header in the ELF file.
Definition elf.h:149
ELFIdentification
The indexes of the ELF identification array.
Definition elf.h:28
ELFData
The data encoding of the ELF file (little-endian or big-endian)
Definition elf.h:55
ELFVersion
The version of the ELF file.
Definition elf.h:98
ELFClass
The class of the ELF file (32-bit or 64-bit)
Definition elf.h:45
Defines classes for memory input and output operations of various bit widths (8, 16,...
Defines a MemoryManager class for handling memory allocation and deallocation.
The header of a 64-bit ELF file.
Definition elf.h:110
uint16_t section_header_size
The size of each entry in the section header table.
Definition elf.h:123
uint64_t section_header_offset
The offset in the file where the section header table starts.
Definition elf.h:118
uint16_t type
The type of the ELF file (see ELFType)
Definition elf.h:113
uint64_t program_header_offset
The offset in the file where the program header table starts.
Definition elf.h:117
unsigned char identification[16]
Identifies the file as an ELF file and contains metadata about the file (class, data encoding,...
Definition elf.h:112
uint32_t version
The version of the ELF file (see ELFVersion)
Definition elf.h:115
uint16_t section_header_string_table_index
Where the section header string table is located (index into the section header table)
Definition elf.h:125
uint16_t machine
The target architecture of the ELF file (see ELFMachine)
Definition elf.h:114
uint16_t program_header_size
The size of each entry in the program header table.
Definition elf.h:121
uint16_t program_header_count
The number of entries in the program header table.
Definition elf.h:122
uint32_t flags
Architecture-specific flags.
Definition elf.h:119
uint16_t section_header_count
The number of entries in the section header table.
Definition elf.h:124
uint16_t elf_header_size
The size of this ELF header.
Definition elf.h:120
uint64_t entry
The starting RIP address where the program begins execution.
Definition elf.h:116
The header for the program segments in a 64-bit ELF file.
Definition elf.h:162
uint32_t type
The type of the program header (see ELFProgramType)
Definition elf.h:164
uint64_t memory_size
The size of the segment in memory (should be >= file_size)
Definition elf.h:170
uint64_t align
The required alignment of the segment in memory (normally 2^n)
Definition elf.h:171
uint64_t virtual_address
Where the segment is to be loaded in memory.
Definition elf.h:167
uint64_t file_size
The size of the segment in the file.
Definition elf.h:169
uint32_t flags
The flags of the program header (see ELFProgramFlags)
Definition elf.h:165
uint64_t physical_address
Reserved for physical address (not used in modern systems)
Definition elf.h:168
uint64_t offset
The offset in the file where the segment is located.
Definition elf.h:166
The header for the sections in a 64-bit ELF file.
Definition elf.h:198
uint64_t sh_flags
The flags of the section (see ELFSectionFlags)
Definition elf.h:202
uint64_t address_alignment
The required alignment of the section in memory (normally 2^n)
Definition elf.h:208
uint32_t type
The type of the section (see ELFSectionType)
Definition elf.h:201
uint64_t offset
The offset in the file where the section is located.
Definition elf.h:204
uint32_t name_index
The index into the section header string table where the name of this section is located.
Definition elf.h:200
uint64_t virtual_address
Where the section is to be loaded in memory.
Definition elf.h:203
uint64_t entry_size
The size of each entry in the section if it contains a table of fixed-size entries.
Definition elf.h:209
uint32_t info
Extra information (meaning depends on section type)
Definition elf.h:207
uint32_t link
Section index link (meaning depends on section type)
Definition elf.h:206
uint64_t size
The size of the section in the file.
Definition elf.h:205