supporting self segments loading

This commit is contained in:
georgemoralis 2023-06-13 19:05:06 +03:00
parent cfa68f2f40
commit bcc6288e9e
3 changed files with 48 additions and 4 deletions

View File

@ -5,6 +5,9 @@
#include <spdlog/pattern_formatter.h>
#include <magic_enum.hpp>
#include <fmt/core.h>
#include "../Util/Log.h"
constexpr bool debug_elf = true;
template <>
struct magic_enum::customize::enum_range<e_type_s> {
@ -515,8 +518,25 @@ void Elf::LoadSegment(u64 virtual_addr, u64 file_offset, u64 size)
{
if (m_self!=nullptr)
{
//it is self file //TODO
__debugbreak();
for (uint16_t i = 0; i < m_self->segment_count; i++)
{
const auto& seg = m_self_segments[i];
if (seg.IsBlocked())
{
auto phdr_id = seg.GetId();
const auto& phdr = m_elf_phdr[phdr_id];
if (file_offset >= phdr.p_offset && file_offset < phdr.p_offset + phdr.p_filesz)
{
auto offset = file_offset - phdr.p_offset;
m_f->Seek(offset + seg.file_offset,fsSeekMode::fsSeekSet);
m_f->Read(reinterpret_cast<void*>(static_cast<uintptr_t>(virtual_addr)), size);
return;
}
}
}
__debugbreak();//hmm we didn't return something...
}
else
{

View File

@ -27,10 +27,34 @@ struct self_header
struct self_segment_header
{
bool IsBlocked() const {
return (flags & 0x800) != 0;//0 or 0x800
}
u32 GetId() const {
return (flags >> 20u) & 0xFFFu;
}
bool IsOrdered() const {
return (flags & 1) != 0;//0 or 1
}
bool IsEncrypted() const {
return (flags & 2) != 0;//0 or 2
}
bool IsSigned() const {
return (flags & 4) != 0;//0 or 4
}
bool IsCompressed() const {
return (flags & 8) != 0;//0 or 8
}
u64 flags;
u64 file_offset;
u64 file_size;
u64 memory_size;
u64 memory_size;
};

View File

@ -36,7 +36,7 @@ int main(int argc, char* argv[])
logging::init(true);//init logging
const char* const path = "videoout_basic.elf";// argv[1]; //argument 1 is the path of self file to boot
const char* const path = argv[1]; //argument 1 is the path of self file to boot
auto* linker = Singleton<Linker>::Instance();
auto *module =linker->LoadModule(path);//load main executable