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 ()
 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_theader () const
 Gets the header of the elf file.
 
elf_64_program_header_tget_program_header (size_t index) const
 Gets a program header from the elf file.
 
elf_64_section_header_tget_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 23 of file elf.cpp.

24: m_elf_header_address(elf_header_address)
25{
26}

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 63 of file elf.cpp.

63 {
64
65 // Check bounds
66 if (index >= header()->program_header_count)
67 return nullptr;
68
69 // Find the program headers and return the item
70 auto* program_headers = (elf_64_program_header_t*) (m_elf_header_address + header()->program_header_offset);
71 return &program_headers[index];
72
73}
elf_64_header_t * header() const
Gets the header of the elf file.
Definition elf.cpp:52
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 81 of file elf.cpp.

81 {
82
83 // Check bound
84 if (index >= header()->section_header_count)
85 return nullptr;
86
87 // Find the section headers and return the item
88 auto* section_headers = (elf_64_section_header_t*) (m_elf_header_address + header()->section_header_offset);
89 return &section_headers[index];
90
91}
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 52 of file elf.cpp.

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

Referenced by get_program_header(), get_section_header(), and is_valid().

◆ 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 100 of file elf.cpp.

100 {
101
102 // Validate the magic number
103 for (size_t i = 0; i < 4; i++)
104 if (header()->identification[i] != ELF_MAGIC[i])
105 return false;
106
107 // Check if the elf is 64 bit
108 if (header()->identification[(int) ELFIdentification::Class] != (int) ELFClass::Bits64)
109 return false;
110
111 // Check if the elf is little endian
112 if (header()->identification[(int) ELFIdentification::Data] != (int) ELFData::LittleEndian)
113 return false;
114
115 // Check if the elf is version 1
116 if (header()->identification[(int) ELFIdentification::Version] != (int) ELFVersion::Current)
117 return false;
118
119 // Check if the elf is for the MaxOS platform
120 // if(header() -> identification[OSABI] != MaxOSABI)
121 // return false;
122
123 // Check if the elf is executable
124 if (header()->type != (int) ELFType::Executable)
125 return false;
126
127 // Check if the elf is for the x86_64 platform
128 if (header()->machine != (int) ELFMachine::x86_64)
129 return false;
130
131 // LGTM
132 return true;
133
134}

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

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

◆ load()

void ELF64::load ( )

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

Todo:
Error handling

Definition at line 38 of file elf.cpp.

38 {
39
40 if (!is_valid())
41 return;
42
43 load_program_headers();
44
45}
bool is_valid() const
Checks if the elf file is valid for MaxOS runtime.
Definition elf.cpp:100

References is_valid().

◆ 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 173 of file elf.cpp.

173 {
174
175 // Conversion
176 // ELF | VMM
177 // 0x0 | Executable
178 // 0x1 | Write
179 // 0x2 | Read
180
181 uint64_t flags = PRESENT | USER | NO_EXECUTE;
182
183 // Enable write
184 if (type & ELFWrite)
185 flags |= WRITE;
186
187 // Disable no execute
188 if (type & ELFExecute)
189 flags &= ~NO_EXECUTE;
190
191 return flags;
192}
@ PRESENT
The page is present in memory.
Definition physical.h:43
@ WRITE
Memory in this page is writable.
Definition physical.h:44
@ USER
This page is accessible from user mode.
Definition physical.h:45
@ NO_EXECUTE
Dont let the CPU execute code on this page.
Definition physical.h:52

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: