mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2025-05-28 14:23:18 +00:00
more symbols work and refactoring
This commit is contained in:
parent
11bf9d7928
commit
cc34a85c54
8 changed files with 38 additions and 10 deletions
|
@ -1,4 +1,4 @@
|
|||
#include "../Loader/Elf.h"
|
||||
#include "../Core/PS4/Loader/Elf.h"
|
||||
#include <windows.h>
|
||||
#include "../Util/Log.h"
|
||||
|
||||
|
|
|
@ -427,6 +427,34 @@ void Linker::LoadSymbols(Module* m)
|
|||
auto visibility = sym->GetVisibility();
|
||||
if (library != nullptr || module != nullptr)
|
||||
{
|
||||
switch (bind)
|
||||
{
|
||||
case STB_GLOBAL:
|
||||
case STB_WEAK:
|
||||
break;
|
||||
default:
|
||||
LOG_INFO_IF(debug_loader, "Unsupported bind {} for name symbol {} \n", bind,ids.at(0));
|
||||
continue;
|
||||
}
|
||||
switch (type)
|
||||
{
|
||||
case STT_OBJECT:
|
||||
case STT_FUN:
|
||||
break;
|
||||
default:
|
||||
LOG_INFO_IF(debug_loader, "Unsupported type {} for name symbol {} \n", type, ids.at(0));
|
||||
continue;
|
||||
}
|
||||
switch (visibility)
|
||||
{
|
||||
case STV_DEFAULT:
|
||||
break;
|
||||
default:
|
||||
LOG_INFO_IF(debug_loader, "Unsupported visibility {} for name symbol {} \n", visibility, ids.at(0));
|
||||
continue;
|
||||
}
|
||||
//if st_value!=0 then it's export symbol
|
||||
bool is_sym_export = sym->st_value != 0;
|
||||
LOG_INFO_IF(debug_loader, "name {} library {} module {} bind {} type {} visibility {}\n", ids.at(0),library->name,module->name,bind,type,visibility);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
#include "../../Loader/Elf.h"
|
||||
|
||||
#include <vector>
|
||||
#include "Loader/Elf.h"
|
||||
|
||||
struct DynamicModuleInfo;
|
||||
|
||||
|
|
551
src/Core/PS4/Loader/Elf.cpp
Normal file
551
src/Core/PS4/Loader/Elf.cpp
Normal file
|
@ -0,0 +1,551 @@
|
|||
#include "Elf.h"
|
||||
#include "spdlog/spdlog.h"
|
||||
#include "spdlog/sinks/basic_file_sink.h"
|
||||
#include <spdlog/sinks/stdout_color_sinks.h>
|
||||
#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> {
|
||||
static constexpr int min = 0xfe00;
|
||||
static constexpr int max = 0xfe18;
|
||||
// (max - min) must be less than UINT16_MAX.
|
||||
};
|
||||
|
||||
Elf::~Elf()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
static self_header* load_self(FsFile& f)
|
||||
{
|
||||
//read self header
|
||||
auto* self = new self_header;
|
||||
f.Read(self, sizeof(self_header));
|
||||
return self;
|
||||
}
|
||||
|
||||
|
||||
static self_segment_header* load_self_segments(FsFile& f, u16 num)
|
||||
{
|
||||
auto* segs = new self_segment_header[num];
|
||||
|
||||
f.Read(segs, sizeof(self_segment_header) * num);
|
||||
|
||||
return segs;
|
||||
}
|
||||
|
||||
|
||||
static elf_header* load_elf_header(FsFile& f)
|
||||
{
|
||||
auto* m_elf_header = new elf_header;
|
||||
|
||||
f.Read(m_elf_header, sizeof(elf_header));
|
||||
|
||||
return m_elf_header;
|
||||
}
|
||||
|
||||
static elf_program_header* load_program_header(FsFile& f, u64 offset, u16 num)
|
||||
{
|
||||
auto* phdr = new elf_program_header[num];
|
||||
|
||||
f.Seek(offset,fsSeekMode::fsSeekSet);
|
||||
f.Read(phdr, sizeof(elf_program_header) * num);
|
||||
|
||||
return phdr;
|
||||
}
|
||||
|
||||
static elf_section_header* load_section_header(FsFile& f, u64 offset, u16 num)
|
||||
{
|
||||
if (num == 0)//just in case we don't have section headers
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto* shdr = new elf_section_header[num];
|
||||
|
||||
f.Seek(offset,fsSeekMode::fsSeekSet);
|
||||
f.Read(shdr, sizeof(elf_section_header) * num);
|
||||
|
||||
return shdr;
|
||||
}
|
||||
|
||||
void Elf::Reset()//reset all variables
|
||||
{
|
||||
if (m_f != nullptr)
|
||||
{
|
||||
m_f->Close();
|
||||
delete m_f;
|
||||
}
|
||||
delete m_self;
|
||||
delete m_elf_header;
|
||||
delete m_self_id_header;
|
||||
delete[] m_self_segments;
|
||||
delete[] m_elf_phdr;
|
||||
delete[] m_elf_shdr;
|
||||
|
||||
m_self = nullptr;
|
||||
m_self_segments = nullptr;
|
||||
m_elf_header = nullptr;
|
||||
m_elf_phdr = nullptr;
|
||||
m_elf_shdr = nullptr;
|
||||
m_self_id_header = nullptr;
|
||||
}
|
||||
void Elf::Open(const std::string& file_name)
|
||||
{
|
||||
Reset();//reset all variables
|
||||
|
||||
m_f = new FsFile;
|
||||
m_f->Open(file_name, fsOpenMode::fsRead);
|
||||
|
||||
m_self = load_self(*m_f);
|
||||
|
||||
bool isself = isSelfFile();
|
||||
if (!isself)
|
||||
{
|
||||
delete m_self;
|
||||
m_self = nullptr;
|
||||
m_f->Seek(0,fsSeekMode::fsSeekSet); //it is not an self file move to the start of file
|
||||
}
|
||||
else
|
||||
{
|
||||
m_self_segments = load_self_segments(*m_f, m_self->segment_count);
|
||||
}
|
||||
|
||||
auto elfheader_pos = m_f->Tell();//get the entry pos for elf file
|
||||
|
||||
m_elf_header = load_elf_header(*m_f);
|
||||
|
||||
if (!isElfFile())
|
||||
{
|
||||
delete m_elf_header;
|
||||
m_elf_header = nullptr;
|
||||
}
|
||||
|
||||
if (m_elf_header != nullptr)
|
||||
{
|
||||
m_elf_phdr = load_program_header(*m_f, elfheader_pos + m_elf_header->e_phoff, m_elf_header->e_phnum);
|
||||
m_elf_shdr = load_section_header(*m_f, elfheader_pos + m_elf_header->e_shoff, m_elf_header->e_shnum);
|
||||
}
|
||||
if (isself && m_elf_header != nullptr)
|
||||
{
|
||||
u64 header_size = 0;
|
||||
header_size += sizeof(self_header);
|
||||
header_size += sizeof(self_segment_header) * m_self->segment_count;
|
||||
header_size += sizeof(elf_header);
|
||||
header_size += m_elf_header->e_phnum * m_elf_header->e_phentsize;
|
||||
header_size += m_elf_header->e_shnum * m_elf_header->e_shentsize;
|
||||
header_size += 15; header_size &= ~15; // align
|
||||
|
||||
if (m_elf_header->e_ehsize - header_size >= sizeof(elf_program_id_header))
|
||||
{
|
||||
m_f->Seek(header_size, fsSeekMode::fsSeekSet);
|
||||
|
||||
m_self_id_header = new elf_program_id_header;
|
||||
m_f->Read(m_self_id_header, sizeof(elf_program_id_header));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DebugDump();
|
||||
}
|
||||
|
||||
bool Elf::isSelfFile() const
|
||||
{
|
||||
if (m_f == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_self == nullptr)
|
||||
{
|
||||
return false;//if we can't load self header return false
|
||||
}
|
||||
|
||||
if (m_self->magic != self_header::signature)
|
||||
{
|
||||
printf("Not a SELF file. Magic file mismatched! current = 0x%08X required = 0x%08X\n", m_self->magic, self_header::signature);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_self->version != 0x00 || m_self->mode != 0x01 || m_self->endian != 0x01 || m_self->attributes != 0x12)
|
||||
{
|
||||
printf("Unknown SELF file\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_self->category != 0x01 || m_self->program_type != 0x01)
|
||||
{
|
||||
printf("Unknown SELF file\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Elf::isElfFile() const
|
||||
{
|
||||
if (m_f == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_ident.magic[EI_MAG0] != ELFMAG0 || m_elf_header->e_ident.magic[EI_MAG1] != ELFMAG1 || m_elf_header->e_ident.magic[EI_MAG2] != ELFMAG2 ||
|
||||
m_elf_header->e_ident.magic[EI_MAG3] != ELFMAG3)
|
||||
{
|
||||
printf("ERROR:Not an ELF file magic is wrong!\n");
|
||||
return false;
|
||||
}
|
||||
if (m_elf_header->e_ident.ei_class != ELF_CLASS_64)
|
||||
{
|
||||
printf("ERROR:e_ident[EI_CLASS] expected 0x02 is (0x%x)\n", m_elf_header->e_ident.ei_class);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_ident.ei_data != ELF_DATA_2LSB)
|
||||
{
|
||||
printf("ERROR:e_ident[EI_DATA] expected 0x01 is (0x%x)\n", m_elf_header->e_ident.ei_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_ident.ei_version != ELF_VERSION_CURRENT)
|
||||
{
|
||||
printf("ERROR:e_ident[EI_VERSION] expected 0x01 is (0x%x)\n", m_elf_header->e_ident.ei_version);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_ident.ei_osabi != ELF_OSABI_FREEBSD)
|
||||
{
|
||||
printf("ERROR:e_ident[EI_OSABI] expected 0x09 is (0x%x)\n", m_elf_header->e_ident.ei_osabi);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_ident.ei_abiversion != ELF_ABI_VERSION_AMDGPU_HSA_V2)
|
||||
{
|
||||
printf("ERROR:e_ident[EI_ABIVERSION] expected 0x00 is (0x%x)\n", m_elf_header->e_ident.ei_abiversion);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_type != ET_SCE_DYNEXEC && m_elf_header->e_type != ET_SCE_DYNAMIC)
|
||||
{
|
||||
printf("ERROR:e_type expected 0xFE10 OR 0xFE18 is (%04x)\n", m_elf_header->e_type);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_machine != EM_X86_64)
|
||||
{
|
||||
printf("ERROR:e_machine expected 0x3E is (%04x)\n", m_elf_header->e_machine);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_version != EV_CURRENT)
|
||||
{
|
||||
printf("ERROR:m_elf_header->e_version expected 0x01 is (0x%x)\n", m_elf_header->e_version);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_phentsize != sizeof(elf_program_header))
|
||||
{
|
||||
printf("ERROR:e_phentsize (%d) != sizeof(elf_program_header)\n", m_elf_header->e_phentsize);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_elf_header->e_shentsize > 0 && m_elf_header->e_shentsize != sizeof(elf_section_header)) //commercial games doesn't appear to have section headers
|
||||
{
|
||||
printf("ERROR:e_shentsize (%d) != sizeof(elf_section_header)\n", m_elf_header->e_shentsize);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Elf::DebugDump() {
|
||||
std::vector<spdlog::sink_ptr> sinks;
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>(L"output.log", true)); //this might work only in windows ;/
|
||||
spdlog::set_default_logger(std::make_shared<spdlog::logger>("shadps4 logger", begin(sinks), end(sinks)));
|
||||
auto f = std::make_unique<spdlog::pattern_formatter>("%v", spdlog::pattern_time_type::local, std::string("")); // disable eol
|
||||
spdlog::set_formatter(std::move(f));
|
||||
if (m_self != nullptr) {//if we load elf instead
|
||||
spdlog::info(SElfHeaderStr());
|
||||
spdlog::info("\n");
|
||||
for (u16 i = 0; i < m_self->segment_count; i++)
|
||||
{
|
||||
spdlog::info(SELFSegHeader(i));
|
||||
}
|
||||
spdlog::info("\n");
|
||||
}
|
||||
|
||||
spdlog::info(ElfHeaderStr());
|
||||
|
||||
if (m_elf_header->e_phentsize > 0)
|
||||
{
|
||||
spdlog::info("Program headers:\n");
|
||||
for (u16 i = 0; i < m_elf_header->e_phnum; i++)
|
||||
{
|
||||
spdlog::info(ElfPHeaderStr(i));
|
||||
}
|
||||
}
|
||||
if (m_elf_header->e_shentsize > 0)
|
||||
{
|
||||
spdlog::info("Section headers:\n");
|
||||
for (u16 i = 0; i < m_elf_header->e_shnum; i++)
|
||||
{
|
||||
spdlog::info("--- shdr {} --\n", i);
|
||||
spdlog::info("sh_name ........: {}\n", (m_elf_shdr + i)->sh_name);
|
||||
spdlog::info("sh_type ........: {:#010x}\n", (m_elf_shdr + i)->sh_type);
|
||||
spdlog::info("sh_flags .......: {:#018x}\n", (m_elf_shdr + i)->sh_flags);
|
||||
spdlog::info("sh_addr ........: {:#018x}\n", (m_elf_shdr + i)->sh_addr);
|
||||
spdlog::info("sh_offset ......: {:#018x}\n", (m_elf_shdr + i)->sh_offset);
|
||||
spdlog::info("sh_size ........: {:#018x}\n", (m_elf_shdr + i)->sh_size);
|
||||
spdlog::info("sh_link ........: {:#010x}\n", (m_elf_shdr + i)->sh_link);
|
||||
spdlog::info("sh_info ........: {:#010x}\n", (m_elf_shdr + i)->sh_info);
|
||||
spdlog::info("sh_addralign ...: {:#018x}\n", (m_elf_shdr + i)->sh_addralign);
|
||||
spdlog::info("sh_entsize .....: {:#018x}\n", (m_elf_shdr + i)->sh_entsize);
|
||||
}
|
||||
}
|
||||
if (m_self_id_header != nullptr)
|
||||
{
|
||||
spdlog::info("SELF info:\n");
|
||||
spdlog::info("auth id ............: {:#018x}\n", m_self_id_header->authid);
|
||||
auto program_type = magic_enum::enum_cast<program_type_es>(m_self_id_header->program_type);
|
||||
if (program_type.has_value())
|
||||
{
|
||||
spdlog::info("program type .......: {}\n", magic_enum::enum_name(program_type.value()));
|
||||
}
|
||||
else
|
||||
{
|
||||
spdlog::info("program type UNK....: {:#018x}\n", (int)m_self_id_header->program_type);
|
||||
}
|
||||
spdlog::info("app version ........: {:#018x}\n", m_self_id_header->appver);
|
||||
spdlog::info("fw version .........: {:#018x}\n", m_self_id_header->firmver);
|
||||
spdlog::info("digest..............: 0x");
|
||||
for (int i = 0; i < 32; i++) spdlog::info("{:02x}", m_self_id_header->digest[i]);
|
||||
spdlog::info("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::string Elf::SElfHeaderStr() {
|
||||
std::string header = fmt::format("======= SELF HEADER =========\n", m_self->magic);
|
||||
header+= fmt::format("magic ..............: 0x{:X}\n", m_self->magic);
|
||||
header+= fmt::format("version ............: {}\n", m_self->version);
|
||||
header+= fmt::format("mode ...............: {:#04x}\n", m_self->mode);
|
||||
header+= fmt::format("endian .............: {}\n", m_self->endian);
|
||||
header+= fmt::format("attributes .........: {:#04x}\n", m_self->attributes);
|
||||
header+= fmt::format("category ...........: {:#04x}\n", m_self->category);
|
||||
header+= fmt::format("program_type........: {:#04x}\n", m_self->program_type);
|
||||
header+= fmt::format("padding1 ...........: {:#06x}\n", m_self->padding1);
|
||||
header+= fmt::format("header size ........: {}\n", m_self->header_size);
|
||||
header+= fmt::format("meta size ..........: {}\n", m_self->meta_size);
|
||||
header+= fmt::format("file size ..........: {}\n", m_self->file_size);
|
||||
header+= fmt::format("padding2 ...........: {:#010x}\n", m_self->padding2);
|
||||
header+= fmt::format("segment count ......: {}\n", m_self->segment_count);
|
||||
header+= fmt::format("unknown 1A .........: {:#06x}\n", m_self->unknown1A);
|
||||
header+= fmt::format("padding3 ...........: {:#010x}\n", m_self->padding3);
|
||||
return header;
|
||||
}
|
||||
std::string Elf::SELFSegHeader(u16 no)
|
||||
{
|
||||
auto segment_header = m_self_segments[no];
|
||||
std::string header = fmt::format("====== SEGMENT HEADER {} ========\n", no);
|
||||
header += fmt::format("flags ............: {:#018x}\n", segment_header.flags);
|
||||
header += fmt::format("file offset ......: {:#018x}\n", segment_header.file_offset);
|
||||
header += fmt::format("file size ........: {}\n", segment_header.file_size);
|
||||
header += fmt::format("memory size ......: {}\n", segment_header.memory_size);
|
||||
return header;
|
||||
}
|
||||
std::string Elf::ElfHeaderStr()
|
||||
{
|
||||
std::string header = fmt::format("======= Elf header ===========\n");
|
||||
header+= fmt::format("ident ............: 0x");
|
||||
for (auto i : m_elf_header->e_ident.magic)
|
||||
{
|
||||
header += fmt::format("{:02X}", i);
|
||||
}
|
||||
header += fmt::format("\n");
|
||||
|
||||
auto ident_class = magic_enum::enum_cast<ident_class_es>(m_elf_header->e_ident.ei_class);
|
||||
if (ident_class.has_value())
|
||||
{
|
||||
header += fmt::format("ident class.......: {}\n", magic_enum::enum_name(ident_class.value()));
|
||||
}
|
||||
|
||||
auto ident_data = magic_enum::enum_cast<ident_endian_es>(m_elf_header->e_ident.ei_data);
|
||||
if (ident_data.has_value())
|
||||
{
|
||||
header += fmt::format("ident data .......: {}\n", magic_enum::enum_name(ident_data.value()));
|
||||
}
|
||||
|
||||
auto ident_version = magic_enum::enum_cast<ident_version_es>(m_elf_header->e_ident.ei_version);
|
||||
if (ident_version.has_value())
|
||||
{
|
||||
header += fmt::format("ident version.....: {}\n", magic_enum::enum_name(ident_version.value()));
|
||||
}
|
||||
|
||||
auto ident_osabi = magic_enum::enum_cast<ident_osabi_es>(m_elf_header->e_ident.ei_osabi);
|
||||
if (ident_osabi.has_value())
|
||||
{
|
||||
header += fmt::format("ident osabi .....: {}\n", magic_enum::enum_name(ident_osabi.value()));
|
||||
}
|
||||
|
||||
auto ident_abiversion = magic_enum::enum_cast<ident_abiversion_es>(m_elf_header->e_ident.ei_abiversion);
|
||||
if (ident_abiversion.has_value())
|
||||
{
|
||||
header += fmt::format("ident abiversion..: {}\n", magic_enum::enum_name(ident_abiversion.value()));
|
||||
}
|
||||
|
||||
header += fmt::format("ident UNK ........: 0x");
|
||||
for (auto i : m_elf_header->e_ident.pad)
|
||||
{
|
||||
header += fmt::format("{:02X}", i);
|
||||
}
|
||||
header += fmt::format("\n");
|
||||
|
||||
auto type = magic_enum::enum_cast<e_type_s>(m_elf_header->e_type);
|
||||
if (type.has_value())
|
||||
{
|
||||
header += fmt::format("type ............: {}\n", magic_enum::enum_name(type.value()));
|
||||
}
|
||||
|
||||
auto machine = magic_enum::enum_cast<e_machine_es>(m_elf_header->e_machine);
|
||||
if (machine.has_value())
|
||||
{
|
||||
header += fmt::format("machine ..........: {}\n", magic_enum::enum_name(machine.value()));
|
||||
}
|
||||
auto version = magic_enum::enum_cast<e_version_es>(m_elf_header->e_version);
|
||||
if (version.has_value())
|
||||
{
|
||||
header += fmt::format("version ..........: {}\n", magic_enum::enum_name(version.value()));
|
||||
}
|
||||
header += fmt::format("entry ............: {:#018x}\n", m_elf_header->e_entry);
|
||||
header += fmt::format("phoff ............: {:#018x}\n", m_elf_header->e_phoff);
|
||||
header += fmt::format("shoff ............: {:#018x}\n", m_elf_header->e_shoff);
|
||||
header += fmt::format("flags ............: {:#010x}\n", m_elf_header->e_flags);
|
||||
header += fmt::format("ehsize ...........: {}\n", m_elf_header->e_ehsize);
|
||||
header += fmt::format("phentsize ........: {}\n", m_elf_header->e_phentsize);
|
||||
header += fmt::format("phnum ............: {}\n", m_elf_header->e_phnum);
|
||||
header += fmt::format("shentsize ........: {}\n", m_elf_header->e_shentsize);
|
||||
header += fmt::format("shnum ............: {}\n", m_elf_header->e_shnum);
|
||||
header += fmt::format("shstrndx .........: {}\n", m_elf_header->e_shstrndx);
|
||||
return header;
|
||||
}
|
||||
|
||||
std::string Elf::ElfPheaderTypeStr(u32 type) {
|
||||
switch (type) {
|
||||
case PT_NULL:
|
||||
return "Null";
|
||||
case PT_LOAD:
|
||||
return "Loadable";
|
||||
case PT_DYNAMIC:
|
||||
return "Dynamic";
|
||||
case PT_INTERP:
|
||||
return "Interpreter Path";
|
||||
case PT_NOTE:
|
||||
return "Note";
|
||||
case PT_SHLIB:
|
||||
return "Section Header Library";
|
||||
case PT_PHDR:
|
||||
return "Program Header";
|
||||
case PT_TLS:
|
||||
return "Thread-Local Storage";
|
||||
case PT_NUM:
|
||||
return "Defined Sections Number";
|
||||
case PT_SCE_RELA:
|
||||
return "SCE Relative";
|
||||
case PT_SCE_DYNLIBDATA:
|
||||
return "SCE Dynamic Library Data";
|
||||
case PT_SCE_PROCPARAM:
|
||||
return "SCE Processor Parameters";
|
||||
case PT_SCE_MODULE_PARAM:
|
||||
return "SCE Module Parameters";
|
||||
case PT_SCE_RELRO:
|
||||
return "SCE Read-Only After Relocation";
|
||||
case PT_GNU_EH_FRAME:
|
||||
return "GNU Entry Header Frame";
|
||||
case PT_GNU_STACK:
|
||||
return "GNU Stack (executability)";
|
||||
case PT_GNU_RELRO:
|
||||
return "GNU Read-Only After Relocation";
|
||||
case PT_SCE_COMMENT:
|
||||
return "SCE Comment";
|
||||
case PT_SCE_LIBVERSION:
|
||||
return "SCE Library Version";
|
||||
default:
|
||||
return "Unknown Section";
|
||||
}
|
||||
}
|
||||
|
||||
std::string Elf::ElfPheaderFlagsStr(u32 flags) {
|
||||
std::string flg = "(";
|
||||
flg += (flags & PF_READ) ? "R" : "_";
|
||||
flg += (flags & PF_WRITE) ? "W" : "_";
|
||||
flg += (flags & PF_EXEC) ? "X" : "_";
|
||||
flg += ")";
|
||||
return flg;
|
||||
}
|
||||
|
||||
std::string Elf::ElfPHeaderStr(u16 no)
|
||||
{
|
||||
std::string header = fmt::format("====== PROGRAM HEADER {} ========\n", no);
|
||||
header += fmt::format("p_type ....: {}\n", ElfPheaderTypeStr((m_elf_phdr + no)->p_type));
|
||||
|
||||
auto flags = magic_enum::enum_cast<elf_program_flags>((m_elf_phdr + no)->p_flags);
|
||||
if (flags.has_value())
|
||||
{
|
||||
header += fmt::format("p_flags ...: {}\n", magic_enum::enum_name(flags.value()));
|
||||
}
|
||||
// header += fmt::format("p_flags ...: {:#010x}\n", (m_elf_phdr + no)->p_flags);
|
||||
header += fmt::format("p_offset ..: {:#018x}\n", (m_elf_phdr + no)->p_offset);
|
||||
header += fmt::format("p_vaddr ...: {:#018x}\n", (m_elf_phdr + no)->p_vaddr);
|
||||
header += fmt::format("p_paddr ...: {:#018x}\n", (m_elf_phdr + no)->p_paddr);
|
||||
header += fmt::format("p_filesz ..: {:#018x}\n", (m_elf_phdr + no)->p_filesz);
|
||||
header += fmt::format("p_memsz ...: {:#018x}\n", (m_elf_phdr + no)->p_memsz);
|
||||
header += fmt::format("p_align ...: {:#018x}\n", (m_elf_phdr + no)->p_align);
|
||||
return header;
|
||||
}
|
||||
void Elf::LoadSegment(u64 virtual_addr, u64 file_offset, u64 size)
|
||||
{
|
||||
if (m_self!=nullptr)
|
||||
{
|
||||
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
|
||||
{
|
||||
//it's elf file
|
||||
m_f->Seek(file_offset, fsSeekMode::fsSeekSet);
|
||||
m_f->Read(reinterpret_cast<void*>(static_cast<uintptr_t>(virtual_addr)), size);
|
||||
}
|
||||
}
|
||||
u64 Elf::GetElfEntry()
|
||||
{
|
||||
return m_elf_header->e_entry;
|
||||
}
|
478
src/Core/PS4/Loader/Elf.h
Normal file
478
src/Core/PS4/Loader/Elf.h
Normal file
|
@ -0,0 +1,478 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
#include "../../../types.h"
|
||||
#include "../../FsFile.h"
|
||||
|
||||
struct self_header
|
||||
{
|
||||
static const u32 signature = 0x1D3D154Fu;
|
||||
|
||||
u32 magic;
|
||||
u08 version;
|
||||
u08 mode;
|
||||
u08 endian;// 1 is little endian
|
||||
u08 attributes;
|
||||
u08 category;
|
||||
u08 program_type;
|
||||
u16 padding1;
|
||||
u16 header_size;
|
||||
u16 meta_size;
|
||||
u32 file_size;
|
||||
u32 padding2;
|
||||
u16 segment_count;
|
||||
u16 unknown1A; //always 0x22
|
||||
u32 padding3;
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
constexpr u08 EI_MAG0 = 0;/* e_ident[] indexes */
|
||||
constexpr u08 EI_MAG1 = 1;
|
||||
constexpr u08 EI_MAG2 = 2;
|
||||
constexpr u08 EI_MAG3 = 3;
|
||||
constexpr u08 EI_CLASS = 4;
|
||||
constexpr u08 EI_DATA = 5;
|
||||
constexpr u08 EI_VERSION = 6;
|
||||
constexpr u08 EI_OSABI = 7;
|
||||
constexpr u08 EI_ABIVERSION = 8;
|
||||
|
||||
// Magic number
|
||||
constexpr u08 ELFMAG0 = 0x7F;
|
||||
constexpr u08 ELFMAG1 = 'E';
|
||||
constexpr u08 ELFMAG2 = 'L';
|
||||
constexpr u08 ELFMAG3 = 'F';
|
||||
|
||||
typedef enum : u16 {
|
||||
ET_NONE = 0x0,
|
||||
ET_REL = 0x1,
|
||||
ET_EXEC = 0x2,
|
||||
ET_DYN = 0x3,
|
||||
ET_CORE = 0x4,
|
||||
ET_SCE_EXEC = 0xfe00,
|
||||
ET_SCE_STUBLIB = 0xfe0c,
|
||||
ET_SCE_DYNEXEC = 0xfe10,
|
||||
ET_SCE_DYNAMIC = 0xfe18
|
||||
} e_type_s;
|
||||
|
||||
typedef enum : u16 {
|
||||
EM_NONE = 0, /* No machine */
|
||||
EM_M32 = 1, /* AT&T WE 32100 */
|
||||
EM_SPARC = 2, /* SPARC */
|
||||
EM_386 = 3, /* Intel 80386 */
|
||||
EM_68K = 4, /* Motorola 68000 */
|
||||
EM_88K = 5, /* Motorola 88000 */
|
||||
EM_860 = 7, /* Intel 80860 */
|
||||
EM_MIPS = 8, /* MIPS I Architecture */
|
||||
EM_S370 = 9, /* IBM System/370 Processor */
|
||||
EM_MIPS_RS3_LE = 10, /* MIPS RS3000 Little-endian */
|
||||
EM_PARISC = 15, /* Hewlett-Packard PA-RISC */
|
||||
EM_VPP500 = 17, /* Fujitsu VPP500 */
|
||||
EM_SPARC32PLUS = 18, /* Enhanced instruction set SPARC */
|
||||
EM_960 = 19, /* Intel 80960 */
|
||||
EM_PPC = 20, /* PowerPC */
|
||||
EM_PPC64 = 21, /* 64-bit PowerPC */
|
||||
EM_S390 = 22, /* IBM System/390 Processor */
|
||||
EM_V800 = 36, /* NEC V800 */
|
||||
EM_FR20 = 37, /* Fujitsu FR20 */
|
||||
EM_RH32 = 38, /* TRW RH-32 */
|
||||
EM_RCE = 39, /* Motorola RCE */
|
||||
EM_ARM = 40, /* Advanced RISC Machines ARM */
|
||||
EM_ALPHA = 41, /* Digital Alpha */
|
||||
EM_SH = 42, /* Hitachi SH */
|
||||
EM_SPARCV9 = 43, /* SPARC Version 9 */
|
||||
EM_TRICORE = 44, /* Siemens TriCore embedded processor */
|
||||
EM_ARC = 45, /* Argonaut RISC Core, Argonaut Technologies Inc. */
|
||||
EM_H8_300 = 46, /* Hitachi H8/300 */
|
||||
EM_H8_300H = 47, /* Hitachi H8/300H */
|
||||
EM_H8S = 48, /* Hitachi H8S */
|
||||
EM_H8_500 = 49, /* Hitachi H8/500 */
|
||||
EM_IA_64 = 50, /* Intel IA-64 processor architecture */
|
||||
EM_MIPS_X = 51, /* Stanford MIPS-X */
|
||||
EM_COLDFIRE = 52, /* Motorola ColdFire */
|
||||
EM_68HC12 = 53, /* Motorola M68HC12 */
|
||||
EM_MMA = 54, /* Fujitsu MMA Multimedia Accelerator */
|
||||
EM_PCP = 55, /* Siemens PCP */
|
||||
EM_NCPU = 56, /* Sony nCPU embedded RISC processor */
|
||||
EM_NDR1 = 57, /* Denso NDR1 microprocessor */
|
||||
EM_STARCORE = 58, /* Motorola Star*Core processor */
|
||||
EM_ME16 = 59, /* Toyota ME16 processor */
|
||||
EM_ST100 = 60, /* STMicroelectronics ST100 processor */
|
||||
EM_TINYJ = 61, /* Advanced Logic Corp. TinyJ embedded processor family */
|
||||
EM_X86_64 = 62, /* AMD x86-64 architecture (PS4) */
|
||||
EM_PDSP = 63, /* Sony DSP Processor */
|
||||
EM_PDP10 = 64, /* Digital Equipment Corp. PDP-10 */
|
||||
EM_PDP11 = 65, /* Digital Equipment Corp. PDP-11 */
|
||||
EM_FX66 = 66, /* Siemens FX66 microcontroller */
|
||||
EM_ST9PLUS = 67, /* STMicroelectronics ST9+ 8/16 bit microcontroller */
|
||||
EM_ST7 = 68, /* STMicroelectronics ST7 8-bit microcontroller */
|
||||
EM_68HC16 = 69, /* Motorola MC68HC16 Microcontroller */
|
||||
EM_68HC11 = 70, /* Motorola MC68HC11 Microcontroller */
|
||||
EM_68HC08 = 71, /* Motorola MC68HC08 Microcontroller */
|
||||
EM_68HC05 = 72, /* Motorola MC68HC05 Microcontroller */
|
||||
EM_SVX = 73, /* Silicon Graphics SVx */
|
||||
EM_ST19 = 75, /* Digital VAX */
|
||||
EM_CRIS = 76, /* Axis Communications 32-bit embedded processor */
|
||||
EM_JAVELIN = 77, /* Infineon Technologies 32-bit embedded processor */
|
||||
EM_FIREPATH = 78, /* Element 14 64-bit DSP Processor */
|
||||
EM_ZSP = 79, /* LSI Logic 16-bit DSP Processor */
|
||||
EM_MMIX = 80, /* Donald Knuth's educational 64-bit processor */
|
||||
EM_HUANY = 81, /* Harvard University machine-independent object files */
|
||||
EM_PRISM = 82, /* SiTera Prism */
|
||||
EM_AVR = 83, /* Atmel AVR 8-bit microcontroller */
|
||||
EM_FR30 = 84, /* Fujitsu FR30 */
|
||||
EM_D10V = 85, /* Mitsubishi D10V */
|
||||
EM_D30V = 86, /* Mitsubishi D30V */
|
||||
EM_V850 = 87, /* NEC v850 */
|
||||
EM_M32R = 88, /* Mitsubishi M32R */
|
||||
EM_MN10300 = 89, /* Matsushita MN10300 */
|
||||
EM_MN10200 = 90, /* Matsushita MN10200 */
|
||||
EM_PJ = 91, /* PicoJava */
|
||||
EM_OPENRISC = 92, /* OpenRISC 32-bit embedded processor */
|
||||
EM_ARC_A5 = 93, /* ARC Cores Tangent-A5 */
|
||||
EM_XTENSA = 94, /* Tensilica Xtensa Architecture */
|
||||
EM_VIDEOCORE = 95, /* Alphamosaic VideoCore processor */
|
||||
EM_TMM_GPP = 96, /* Thompson Multimedia General Purpose Processor */
|
||||
EM_NS32K = 97, /* National Semiconductor 32000 series */
|
||||
EM_TPC = 98, /* Tenor Network TPC processor */
|
||||
EM_SNP1K = 99, /* Trebia SNP 1000 processor */
|
||||
EM_ST200 = 100, /* STMicroelectronics (www.st.com) ST200 microcontroller */
|
||||
EM_IP2K = 101, /* Ubicom IP2xxx microcontroller family */
|
||||
EM_MAX = 102, /* MAX Processor */
|
||||
EM_CR = 103, /* National Semiconductor CompactRISC microprocessor */
|
||||
EM_F2MC16 = 104, /* Fujitsu F2MC16 */
|
||||
EM_MSP430 = 105, /* Texas Instruments embedded microcontroller msp430 */
|
||||
EM_BLACKFIN = 106, /* Analog Devices Blackfin (DSP) processor */
|
||||
EM_SE_C33 = 107, /* S1C33 Family of Seiko Epson processors */
|
||||
EM_SEP = 108, /* Sharp embedded microprocessor */
|
||||
EM_ARCA = 109, /* Arca RISC Microprocessor */
|
||||
EM_UNICORE = 110 /* Microprocessor series from PKU-Unity Ltd. and MPRC */
|
||||
} e_machine_es;
|
||||
|
||||
typedef enum :u32 {
|
||||
EV_NONE = 0x0,
|
||||
EV_CURRENT = 0x1
|
||||
} e_version_es;
|
||||
|
||||
typedef enum : u08 {
|
||||
ELF_CLASS_NONE =0x0,
|
||||
ELF_CLASS_32 =0x1,
|
||||
ELF_CLASS_64 =0x2,
|
||||
ELF_CLASS_NUM =0x3
|
||||
} ident_class_es;
|
||||
|
||||
typedef enum : u08 {
|
||||
ELF_DATA_NONE = 0x0,
|
||||
ELF_DATA_2LSB = 0x1,
|
||||
ELF_DATA_2MSB = 0x2,
|
||||
ELF_DATA_NUM = 0x3
|
||||
} ident_endian_es;
|
||||
|
||||
typedef enum :u08 {
|
||||
ELF_VERSION_NONE = 0x0,
|
||||
ELF_VERSION_CURRENT = 0x1,
|
||||
ELF_VERSION_NUM = 0x2
|
||||
} ident_version_es;
|
||||
|
||||
typedef enum :u08 {
|
||||
ELF_OSABI_NONE = 0x0, /* No extensions or unspecified */
|
||||
ELF_OSABI_HPUX = 0x1, /* Hewlett-Packard HP-UX */
|
||||
ELF_OSABI_NETBSD = 0x2, /* NetBSD */
|
||||
ELF_OSABI_LINUX = 0x3, /* Linux */
|
||||
ELF_OSABI_SOLARIS = 0x6, /* Sun Solaris */
|
||||
ELF_OSABI_AIX = 0x7, /* AIX */
|
||||
ELF_OSABI_IRIX = 0x8, /* IRIX */
|
||||
ELF_OSABI_FREEBSD = 0x9, /* FreeBSD (PS4) */
|
||||
ELF_OSABI_TRU64 = 0xA, /* Compaq TRU64 UNIX */
|
||||
ELF_OSABI_MODESTO = 0xB, /* Novell Modesto */
|
||||
ELF_OSABI_OPENBSD = 0xC, /* Open BSD */
|
||||
ELF_OSABI_OPENVMS = 0xD, /* Open VMS */
|
||||
ELF_OSABI_NSK = 0xE, /* Hewlett-Packard Non-Stop Kernel */
|
||||
ELF_OSABI_AROS = 0xF, /* Amiga Research OS */
|
||||
ELF_OSABI_ARM_AEABI = 0x40, /* ARM EABI */
|
||||
ELF_OSABI_ARM = 0x61, /* ARM */
|
||||
ELF_OSABI_STANDALONE = 0xFF /* Standalone (embedded applications) */
|
||||
} ident_osabi_es;
|
||||
|
||||
typedef enum :u08 {
|
||||
ELF_ABI_VERSION_AMDGPU_HSA_V2=0x0,
|
||||
ELF_ABI_VERSION_AMDGPU_HSA_V3=0x1,
|
||||
ELF_ABI_VERSION_AMDGPU_HSA_V4=0x2,
|
||||
ELF_ABI_VERSION_AMDGPU_HSA_V5=0x3
|
||||
} ident_abiversion_es;
|
||||
|
||||
struct elf_ident {
|
||||
u08 magic[4];
|
||||
ident_class_es ei_class;
|
||||
ident_endian_es ei_data;
|
||||
ident_version_es ei_version;
|
||||
ident_osabi_es ei_osabi;
|
||||
ident_abiversion_es ei_abiversion;
|
||||
u08 pad[6];
|
||||
};
|
||||
|
||||
struct elf_header
|
||||
{
|
||||
static const u32 signature = 0x7F454C46u;
|
||||
|
||||
elf_ident e_ident; /* ELF identification */
|
||||
e_type_s e_type; /* Object file type */
|
||||
e_machine_es e_machine; /* Machine type */
|
||||
e_version_es e_version; /* Object file version */
|
||||
u64 e_entry; /* Entry point address */
|
||||
u64 e_phoff; /* Program header offset */
|
||||
u64 e_shoff; /* Section header offset */
|
||||
u32 e_flags; /* Processor-specific flags */
|
||||
u16 e_ehsize; /* ELF header size */
|
||||
u16 e_phentsize; /* Size of program header entry */
|
||||
u16 e_phnum; /* Number of program header entries */
|
||||
u16 e_shentsize; /* Size of section header entry */
|
||||
u16 e_shnum; /* Number of section header entries */
|
||||
u16 e_shstrndx; /* Section name string table index */
|
||||
};
|
||||
|
||||
typedef enum : u32 {
|
||||
PT_NULL = 0x0,
|
||||
PT_LOAD = 0x1,
|
||||
PT_DYNAMIC = 0x2,
|
||||
PT_INTERP = 0x3,
|
||||
PT_NOTE = 0x4,
|
||||
PT_SHLIB = 0x5,
|
||||
PT_PHDR = 0x6,
|
||||
PT_TLS = 0x7,
|
||||
PT_NUM = 0x8,
|
||||
PT_SCE_RELA = 0x60000000,
|
||||
PT_SCE_DYNLIBDATA = 0x61000000,
|
||||
PT_SCE_PROCPARAM = 0x61000001,
|
||||
PT_SCE_MODULE_PARAM = 0x61000002,
|
||||
PT_SCE_RELRO = 0x61000010,
|
||||
PT_GNU_EH_FRAME = 0x6474e550,
|
||||
PT_GNU_STACK = 0x6474e551,
|
||||
PT_GNU_RELRO = 0x6474e552,
|
||||
PT_SCE_COMMENT = 0x6fffff00,
|
||||
PT_SCE_LIBVERSION = 0x6fffff01,
|
||||
PT_LOSUNW = 0x6ffffffa,
|
||||
PT_SUNWBSS = 0x6ffffffa,
|
||||
PT_SUNWSTACK = 0x6ffffffb,
|
||||
PT_HISUNW = 0x6fffffff,
|
||||
PT_HIOS = 0x6fffffff,
|
||||
PT_LOPROC = 0x70000000,
|
||||
PT_HIPROC = 0x7fffffff
|
||||
} elf_program_type;
|
||||
|
||||
typedef enum : u32 {
|
||||
PF_NONE = 0x0,
|
||||
PF_EXEC = 0x1,
|
||||
PF_WRITE = 0x2,
|
||||
PF_WRITE_EXEC = 0x3,
|
||||
PF_READ = 0x4,
|
||||
PF_READ_EXEC = 0x5,
|
||||
PF_READ_WRITE = 0x6,
|
||||
PF_READ_WRITE_EXEC = 0x7
|
||||
} elf_program_flags;
|
||||
|
||||
struct elf_program_header
|
||||
{
|
||||
elf_program_type p_type; /* Type of segment */
|
||||
elf_program_flags p_flags; /* Segment attributes */
|
||||
u64 p_offset; /* Offset in file */
|
||||
u64 p_vaddr; /* Virtual address in memory */
|
||||
u64 p_paddr; /* Reserved */
|
||||
u64 p_filesz; /* Size of segment in file */
|
||||
u64 p_memsz; /* Size of segment in memory */
|
||||
u64 p_align; /* Alignment of segment */
|
||||
};
|
||||
|
||||
struct elf_section_header
|
||||
{
|
||||
u32 sh_name; /* Section name */
|
||||
u32 sh_type; /* Section type */
|
||||
u64 sh_flags; /* Section attributes */
|
||||
u64 sh_addr; /* Virtual address in memory */
|
||||
u64 sh_offset; /* Offset in file */
|
||||
u64 sh_size; /* Size of section */
|
||||
u32 sh_link; /* Link to other section */
|
||||
u32 sh_info; /* Miscellaneous information */
|
||||
u64 sh_addralign; /* Address alignment boundary */
|
||||
u64 sh_entsize; /* Size of entries, if section has table */
|
||||
};
|
||||
|
||||
typedef enum :u64 {
|
||||
PT_FAKE = 0x1,
|
||||
PT_NPDRM_EXEC = 0x4,
|
||||
PT_NPDRM_DYNLIB = 0x5,
|
||||
PT_SYSTEM_EXEC = 0x8,
|
||||
PT_SYSTEM_DYNLIB = 0x9,
|
||||
PT_HOST_KERNEL = 0xC,
|
||||
PT_SECURE_MODULE = 0xE,
|
||||
PT_SECURE_KERNEL = 0xF
|
||||
} program_type_es;
|
||||
|
||||
struct elf_program_id_header
|
||||
{
|
||||
u64 authid;
|
||||
program_type_es program_type;
|
||||
u64 appver;
|
||||
u64 firmver;
|
||||
u08 digest[32];
|
||||
};
|
||||
|
||||
constexpr s64 DT_NULL = 0;
|
||||
constexpr s64 DT_NEEDED = 0x00000001;
|
||||
constexpr s64 DT_RELA = 0x00000007;
|
||||
constexpr s64 DT_INIT = 0x0000000c;
|
||||
constexpr s64 DT_FINI = 0x0000000d;
|
||||
constexpr s64 DT_DEBUG = 0x00000015;
|
||||
constexpr s64 DT_TEXTREL = 0x00000016;
|
||||
constexpr s64 DT_INIT_ARRAY = 0x00000019;
|
||||
constexpr s64 DT_FINI_ARRAY = 0x0000001a;
|
||||
constexpr s64 DT_INIT_ARRAYSZ = 0x0000001b;
|
||||
constexpr s64 DT_FINI_ARRAYSZ = 0x0000001c;
|
||||
constexpr s64 DT_FLAGS = 0x0000001e;
|
||||
constexpr s64 DT_PREINIT_ARRAY = 0x00000020;
|
||||
constexpr s64 DT_PREINIT_ARRAYSZ = 0x00000021;
|
||||
constexpr s64 DT_SCE_FINGERPRINT = 0x61000007;
|
||||
constexpr s64 DT_SCE_ORIGINAL_FILENAME = 0x61000009;
|
||||
constexpr s64 DT_SCE_MODULE_INFO = 0x6100000d;
|
||||
constexpr s64 DT_SCE_NEEDED_MODULE = 0x6100000f;
|
||||
constexpr s64 DT_SCE_MODULE_ATTR = 0x61000011;
|
||||
constexpr s64 DT_SCE_EXPORT_LIB = 0x61000013;
|
||||
constexpr s64 DT_SCE_IMPORT_LIB = 0x61000015;
|
||||
constexpr s64 DT_SCE_IMPORT_LIB_ATTR = 0x61000019;
|
||||
constexpr s64 DT_SCE_HASH = 0x61000025;
|
||||
constexpr s64 DT_SCE_PLTGOT = 0x61000027;
|
||||
constexpr s64 DT_SCE_JMPREL = 0x61000029;
|
||||
constexpr s64 DT_SCE_PLTREL = 0x6100002b;
|
||||
constexpr s64 DT_SCE_PLTRELSZ = 0x6100002d;
|
||||
constexpr s64 DT_SCE_RELA = 0x6100002f;
|
||||
constexpr s64 DT_SCE_RELASZ = 0x61000031;
|
||||
constexpr s64 DT_SCE_RELAENT = 0x61000033;
|
||||
constexpr s64 DT_SCE_SYMENT = 0x6100003b;
|
||||
constexpr s64 DT_SCE_HASHSZ = 0x6100003d;
|
||||
constexpr s64 DT_SCE_STRTAB = 0x61000035;
|
||||
constexpr s64 DT_SCE_STRSZ = 0x61000037;
|
||||
constexpr s64 DT_SCE_SYMTAB = 0x61000039;
|
||||
constexpr s64 DT_SCE_SYMTABSZ = 0x6100003f;
|
||||
|
||||
|
||||
struct elf_dynamic
|
||||
{
|
||||
s64 d_tag;
|
||||
union
|
||||
{
|
||||
u64 d_val;
|
||||
u64 d_ptr;
|
||||
} d_un;
|
||||
};
|
||||
|
||||
constexpr u08 STB_LOCAL = 0;
|
||||
constexpr u08 STB_GLOBAL = 1;
|
||||
constexpr u08 STB_WEAK = 2;
|
||||
|
||||
constexpr u08 STT_NOTYPE = 0;
|
||||
constexpr u08 STT_OBJECT = 1;
|
||||
constexpr u08 STT_FUN = 2;
|
||||
constexpr u08 STT_SECTION = 3;
|
||||
constexpr u08 STT_FILE = 4;
|
||||
constexpr u08 STT_COMMON = 5;
|
||||
constexpr u08 STT_TLS = 6;
|
||||
constexpr u08 STT_LOOS = 10;
|
||||
constexpr u08 STT_SCE = 11; //module_start/module_stop
|
||||
constexpr u08 STT_HIOS = 12;
|
||||
constexpr u08 STT_LOPRO = 13;
|
||||
constexpr u08 STT_SPARC_REGISTER = 13;
|
||||
constexpr u08 STT_HIPROC = 15;
|
||||
|
||||
constexpr u08 STV_DEFAULT = 0;
|
||||
constexpr u08 STV_INTERNAL = 1;
|
||||
constexpr u08 STV_HIDDEN = 2;
|
||||
constexpr u08 STV_PROTECTED = 3;
|
||||
|
||||
struct elf_symbol
|
||||
{
|
||||
u08 GetBind() const { return st_info >> 4u; }
|
||||
u08 GetType() const { return st_info & 0xfu; }
|
||||
u08 GetVisibility() const { return st_other & 3u; }
|
||||
|
||||
u32 st_name;
|
||||
u08 st_info;
|
||||
u08 st_other;
|
||||
u16 st_shndx;
|
||||
u64 st_value;
|
||||
u64 st_size;
|
||||
};
|
||||
|
||||
struct elf_relocation
|
||||
{
|
||||
u64 rel_offset;
|
||||
u64 rel_info;
|
||||
s64 rel_addend;
|
||||
};
|
||||
|
||||
class Elf
|
||||
{
|
||||
public:
|
||||
Elf() = default;
|
||||
virtual ~Elf();
|
||||
|
||||
void Open(const std::string & file_name);
|
||||
bool isSelfFile() const;
|
||||
bool isElfFile() const;
|
||||
void DebugDump();
|
||||
[[nodiscard]] const self_header* GetSElfHeader() const { return m_self; }
|
||||
[[nodiscard]] const elf_header* GetElfHeader() const { return m_elf_header; }
|
||||
[[nodiscard]] const elf_program_header* GetProgramHeader() const { return m_elf_phdr; }
|
||||
[[nodiscard]] const self_segment_header* GetSegmentHeader() const { return m_self_segments; }
|
||||
std::string SElfHeaderStr();
|
||||
std::string SELFSegHeader(u16 no);
|
||||
std::string ElfHeaderStr();
|
||||
std::string ElfPHeaderStr(u16 no);
|
||||
std::string ElfPheaderTypeStr(u32 type);
|
||||
std::string ElfPheaderFlagsStr(u32 flags);
|
||||
void LoadSegment(u64 virtual_addr, u64 file_offset, u64 size);
|
||||
u64 GetElfEntry();
|
||||
|
||||
private:
|
||||
|
||||
void Reset();
|
||||
|
||||
FsFile* m_f = nullptr;
|
||||
self_header* m_self = nullptr;
|
||||
self_segment_header* m_self_segments = nullptr;
|
||||
elf_header* m_elf_header = nullptr;
|
||||
elf_program_header* m_elf_phdr = nullptr;
|
||||
elf_section_header* m_elf_shdr = nullptr;
|
||||
elf_program_id_header* m_self_id_header = nullptr;
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue