Finish ELF loading, running actual code now

This commit is contained in:
wheremyfoodat
2022-09-15 17:35:59 +03:00
parent 51689af51f
commit 275c6dfd0c
7 changed files with 135 additions and 59 deletions

View File

@@ -1,4 +1,6 @@
#include "memory.hpp"
#include <cstring>
#include "elfio/elfio.hpp"
using namespace ELFIO;
@@ -10,14 +12,35 @@ std::optional<u32> Memory::loadELF(std::filesystem::path& path) {
return std::nullopt;
}
auto seg_num = reader.segments.size();
printf("Number of segments: %d\n", seg_num);
for (int i = 0; i < seg_num; ++i) {
const auto pseg = reader.segments[i];
std::cout << " [" << i << "] 0x" << std::hex << pseg->get_flags()
<< "\t0x" << pseg->get_virtual_address() << "\t0x"
<< pseg->get_file_size() << "\t0x" << pseg->get_memory_size()
<< std::endl;
auto segNum = reader.segments.size();
printf("Number of segments: %d\n", segNum);
for (int i = 0; i < segNum; ++i) {
const auto seg = reader.segments[i];
const auto flags = seg->get_flags();
const u32 vaddr = static_cast<u32>(seg->get_virtual_address()); // Vaddr the segment is loaded in
u32 fileSize = static_cast<u32>(seg->get_file_size()); // Size of segment in file
u32 memorySize = static_cast<u32>(seg->get_memory_size()); // Size of segment in memory
u8* data = (u8*)seg->get_data();
// Get read/write/execute permissions for segment
const bool r = (flags & 0b100) != 0;
const bool w = (flags & 0b010) != 0;
const bool x = (flags & 0b001) != 0;
printf(" # Perms Vaddr File Size Mem Size\n");
printf("[%d] (%c%c%c)\t%08X\t%08X\t%08X\n", i, r ? 'r' : '-', w ? 'w' : '-', x ? 'x' : '-', vaddr, fileSize, memorySize);
// Assert that the segment will be loaded in the executable region. If it isn't then panic.
// The executable region starts at 0x00100000 and has a maximum size of 0x03F00000
u64 endAddress = (u64)vaddr + (u64)fileSize;
const bool isGood = vaddr >= VirtualAddrs::ExecutableStart && endAddress < VirtualAddrs::ExecutableEnd;
if (!isGood) {
Helpers::panic("ELF is loaded at invalid place");
return std::nullopt;
}
u32 fcramAddr = vaddr - VirtualAddrs::ExecutableStart;
std::memcpy(&fcram[fcramAddr], data, fileSize);
}
return static_cast<u32>(reader.get_entry());