Max OS 0.3
Loading...
Searching...
No Matches
elf.cpp
Go to the documentation of this file.
1
9#include <libkpi/syscalls.h>
10
11#include <processes/elf.h>
12#include <common/logger.h>
13
14using namespace MaxOS;
15using namespace MaxOS::processes;
16using namespace MaxOS::memory;
17using namespace MaxOS::common;
18
19
20
26ELF64::ELF64(uintptr_t elf_header_address)
27: m_elf_header_address(elf_header_address)
28{
29}
30
34ELF64::~ELF64() = default;
35
43
44 ASSERT(is_valid(), "Invaild ELF file loaded\n");
45 load_program_headers(vmm);
46
47}
48
55
56 return (elf_64_header_t*) m_elf_header_address;
57}
58
66
67 // Check bounds
68 if (index >= header()->program_header_count)
69 return nullptr;
70
71 // Find the program headers and return the item
72 auto* program_headers = (elf_64_program_header_t*) (m_elf_header_address + header()->program_header_offset);
73 return &program_headers[index];
74
75}
76
84
85 // Check bound
86 if (index >= header()->section_header_count)
87 return nullptr;
88
89 // Find the section headers and return the item
90 auto* section_headers = (elf_64_section_header_t*) (m_elf_header_address + header()->section_header_offset);
91 return &section_headers[index];
92
93}
94
102bool ELF64::is_valid() const {
103
104 // Validate the magic number
105 for (size_t i = 0; i < 4; i++)
106 if (header()->identification[i] != ELF_MAGIC[i])
107 return false;
108
109 // Check if the elf is 64 bit
110 if (header()->identification[(int) ELFIdentification::Class] != (int) ELFClass::Bits64)
111 return false;
112
113 // Check if the elf is little endian
114 if (header()->identification[(int) ELFIdentification::Data] != (int) ELFData::LittleEndian)
115 return false;
116
117 // Check if the elf is version 1
118 if (header()->identification[(int) ELFIdentification::Version] != (int) ELFVersion::Current)
119 return false;
120
121 // Check if the elf is for the MaxOS platform
122 // if(header() -> identification[OSABI] != MaxOSABI)
123 // return false;
124
125 // Check if the elf is executable
126 if (header()->type != (int) ELFType::Executable)
127 return false;
128
129 // Check if the elf is for the x86_64 platform
130 if (header()->machine != (int) ELFMachine::x86_64)
131 return false;
132
133 // LGTM
134 return true;
135
136}
137
141void ELF64::load_program_headers(VirtualMemoryManager* vmm) const {
142
143 size_t actual_size;
144 for (size_t i = 0; i < header()->program_header_count; i++) {
145
146 // Get the header information
147 elf_64_program_header_t* program_header = get_program_header(i);
148
149 // Only load headers that actually need loading
150 if (program_header->type != (int) ELFProgramType::Load)
151 continue;
152
153 // Allocate space at the requested address
154 void* address = vmm->allocate(program_header->virtual_address, program_header->memory_size, actual_size, PRESENT | WRITE);
155 ASSERT(address != nullptr, "Failed to allocate memory for program header\n");
156
157 // Copy the program into memory at that address
158 memcpy(address, (void*) (m_elf_header_address + program_header->offset), program_header->file_size);
159
160 // Zero the rest of the memory if needed
161 size_t zero_size = program_header->memory_size - program_header->file_size;
162 memset((void*) ((uintptr_t) address + program_header->file_size), 0, zero_size);
163
164 // Once the memory has been copied can now mark the pages as read only etc
165 uint64_t flags = to_vmm_flags(program_header->flags);
167 }
168}
169
176uint64_t ELF64::to_vmm_flags(uint32_t type) {
177
178 // Conversion
179 // ELF | VMM
180 // 0x0 | Executable
181 // 0x1 | Write
182 // 0x2 | Read
183
184 uint64_t flags = PRESENT | USER | NO_EXECUTE;
185
186 // Enable write
187 if (type & ELFWrite)
188 flags |= WRITE;
189
190 // Disable no execute
191 if (type & ELFExecute)
192 flags &= ~NO_EXECUTE;
193
194 return flags;
195}
void change_page_flags(virtual_address_t *virtual_address, size_t flags, uint64_t *pml4_root)
Changes the flags of a page.
Definition physical.cpp:846
static PhysicalMemoryManager * s_current_manager
The current physical memory manager in use.
Definition physical.h:183
Manages the virtual memory of the system and provides functions to allocate and free memory in the vi...
Definition virtual.h:104
void * allocate(size_t size, size_t flags)
Allocate a new chunk of virtual memory.
Definition virtual.cpp:113
uint64_t * pml4_root_address()
Get the virtual address of the PML4 root.
Definition virtual.cpp:691
elf_64_header_t * header() const
Gets the header of the elf file.
Definition elf.cpp:54
static uint64_t to_vmm_flags(uint32_t type)
Converts elf flags to vmm flags.
Definition elf.cpp:176
ELF64(uintptr_t elf_header_address)
Constructor for the ELF64 class.
Definition elf.cpp:26
~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:65
elf_64_section_header_t * get_section_header(size_t index) const
Gets a section header from the elf file.
Definition elf.cpp:83
bool is_valid() const
Checks if the elf file is valid for MaxOS runtime.
Definition elf.cpp:102
void load_into_memory(memory::VirtualMemoryManager *vmm) const
Loads the elf program into memory if a valid elf file.
Definition elf.cpp:42
Defines structures and enums for handling ELF (Executable and Linkable Format) files in MaxOS.
constexpr char ELF_MAGIC[4]
The expected ELF magic number at the start of the file.
Definition elf.h:22
Defines a Logger class for logging messages with different severity levels to multiple output streams...
@ PRESENT
The page is present in memory.
Definition physical.h:44
@ WRITE
Memory in this page is writable.
Definition physical.h:45
@ USER
This page is accessible from user mode.
Definition physical.h:46
@ NO_EXECUTE
Dont let the CPU execute code on this page.
Definition physical.h:53
The header of a 64-bit ELF file.
Definition elf.h:110
uint64_t section_header_offset
The offset in the file where the section header table starts.
Definition elf.h:118
uint64_t program_header_offset
The offset in the file where the program header table starts.
Definition elf.h:117
uint16_t program_header_count
The number of entries in the program header table.
Definition elf.h:122
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 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 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