Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::processes::ELF64 Class Reference

Handles the loading and parsing of 64-bit ELF files. More...

#include <elf.h>

Public Member Functions

 ELF64 (uintptr_t elf_header_address)
 Constructor for the ELF64 class.
 
 ~ELF64 ()
 Destructor for the ELF64 class.
 
void load_into_memory (memory::VirtualMemoryManager *vmm) const
 Loads the elf program into memory if a valid elf file.
 
bool is_valid () const
 Checks if the elf file is valid for MaxOS runtime.
 
elf_64_header_t * header () const
 Gets the header of the elf file.
 
elf_64_program_header_t * get_program_header (size_t index) const
 Gets a program header from the elf file.
 
elf_64_section_header_t * get_section_header (size_t index) const
 Gets a section header from the elf file.
 

Static Public Member Functions

static uint64_t to_vmm_flags (uint32_t type)
 Converts elf flags to vmm flags.
 

Detailed Description

Handles the loading and parsing of 64-bit ELF files.

Definition at line 217 of file elf.h.

Constructor & Destructor Documentation

◆ ELF64()

ELF64::ELF64 ( uintptr_t  elf_header_address)
explicit

Constructor for the ELF64 class.

Parameters
elf_header_addressThe address of the elf header, this must be mapped to memory before uses

Definition at line 26 of file elf.cpp.

27: m_elf_header_address(elf_header_address)
28{
29}

Member Function Documentation

◆ get_program_header()

elf_64_program_header_t * ELF64::get_program_header ( size_t  index) const

Gets a program header from the elf file.

Parameters
indexThe index of the program header
Returns
The program header at that index or nullptr if out of bounds

Definition at line 65 of file elf.cpp.

65 {
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}
elf_64_header_t * header() const
Gets the header of the elf file.
Definition elf.cpp:54
uint64_t program_header_offset
The offset in the file where the program header table starts.
Definition elf.h:117
The header for the program segments in a 64-bit ELF file.
Definition elf.h:162

References header(), and MaxOS::processes::ELFHeader64::program_header_offset.

◆ get_section_header()

elf_64_section_header_t * ELF64::get_section_header ( size_t  index) const

Gets a section header from the elf file.

Parameters
indexThe index of the section header
Returns
The section header at that index or nullptr if out of bounds

Definition at line 83 of file elf.cpp.

83 {
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}
uint64_t section_header_offset
The offset in the file where the section header table starts.
Definition elf.h:118
The header for the sections in a 64-bit ELF file.
Definition elf.h:198

References header(), and MaxOS::processes::ELFHeader64::section_header_offset.

◆ header()

elf_64_header_t * ELF64::header ( ) const

Gets the header of the elf file.

Returns
The header of the elf file

Definition at line 54 of file elf.cpp.

54 {
55
56 return (elf_64_header_t*) m_elf_header_address;
57}
The header of a 64-bit ELF file.
Definition elf.h:110

Referenced by get_program_header(), get_section_header(), is_valid(), and MaxOS::processes::Process::Process().

◆ is_valid()

bool ELF64::is_valid ( ) const

Checks if the elf file is valid for MaxOS runtime.

Returns
True if the elf file is valid, false otherwise
Todo:
Add support for maxOS ABI

Definition at line 102 of file elf.cpp.

102 {
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}

References MaxOS::processes::ELF_MAGIC, and header().

Referenced by load_into_memory(), and MaxOS::processes::GlobalScheduler::load_multiboot_elfs().

◆ load_into_memory()

void ELF64::load_into_memory ( memory::VirtualMemoryManager *  vmm) const

Loads the elf program into memory if a valid elf file.

Parameters
vmmWhich address space to load the elf into
Todo:
Error handling

Definition at line 42 of file elf.cpp.

42 {
43
44 ASSERT(is_valid(), "Invaild ELF file loaded\n");
45 load_program_headers(vmm);
46
47}
bool is_valid() const
Checks if the elf file is valid for MaxOS runtime.
Definition elf.cpp:102

References is_valid().

Referenced by MaxOS::processes::Process::Process().

◆ to_vmm_flags()

uint64_t ELF64::to_vmm_flags ( uint32_t  type)
static

Converts elf flags to vmm flags.

Parameters
typeThe elf flags of the program header
Returns
The vmm flags

Definition at line 176 of file elf.cpp.

176 {
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}
@ 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

References MaxOS::memory::NO_EXECUTE, MaxOS::memory::PRESENT, MaxOS::memory::USER, and MaxOS::memory::WRITE.


The documentation for this class was generated from the following files: