2023-10-27 15:28:05 +00:00
|
|
|
#include "Elf.h"
|
2023-08-18 08:22:01 +00:00
|
|
|
|
2023-10-27 15:28:05 +00:00
|
|
|
#include <Util/log.h>
|
2023-10-26 19:55:13 +00:00
|
|
|
#include <debug.h>
|
2023-10-27 15:28:05 +00:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
|
|
|
constexpr bool log_file_loader = true; // disable it to disable logging
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
static std::string_view getProgramTypeName(program_type_es type) {
|
|
|
|
switch (type) {
|
2023-10-27 15:28:05 +00:00
|
|
|
case PT_FAKE: return "PT_FAKE";
|
|
|
|
case PT_NPDRM_EXEC: return "PT_NPDRM_EXEC";
|
|
|
|
case PT_NPDRM_DYNLIB: return "PT_NPDRM_DYNLIB";
|
|
|
|
case PT_SYSTEM_EXEC: return "PT_SYSTEM_EXEC";
|
|
|
|
case PT_SYSTEM_DYNLIB: return "PT_SYSTEM_DYNLIB";
|
|
|
|
case PT_HOST_KERNEL: return "PT_HOST_KERNEL";
|
|
|
|
case PT_SECURE_MODULE: return "PT_SECURE_MODULE";
|
|
|
|
case PT_SECURE_KERNEL: return "PT_SECURE_KERNEL";
|
|
|
|
default: return "INVALID";
|
2023-10-26 19:55:13 +00:00
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
static std::string_view getIdentClassName(ident_class_es elf_class) {
|
|
|
|
switch (elf_class) {
|
2023-10-27 15:28:05 +00:00
|
|
|
case ELF_CLASS_NONE: return "ELF_CLASS_NONE";
|
|
|
|
case ELF_CLASS_32: return "ELF_CLASS_32";
|
|
|
|
case ELF_CLASS_64: return "ELF_CLASS_64";
|
|
|
|
case ELF_CLASS_NUM: return "ELF_CLASS_NUM";
|
|
|
|
default: return "INVALID";
|
2023-10-26 19:55:13 +00:00
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
static std::string_view getIdentEndianName(ident_endian_es endian) {
|
|
|
|
switch (endian) {
|
2023-10-27 15:28:05 +00:00
|
|
|
case ELF_DATA_NONE: return "ELF_DATA_NONE";
|
|
|
|
case ELF_DATA_2LSB: return "ELF_DATA_2LSB";
|
|
|
|
case ELF_DATA_2MSB: return "ELF_DATA_2MSB";
|
|
|
|
case ELF_DATA_NUM: return "ELF_DATA_NUM";
|
|
|
|
default: return "INVALID";
|
2023-10-26 19:55:13 +00:00
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
static std::string_view getIdentVersionName(ident_version_es version) {
|
|
|
|
switch (version) {
|
2023-10-27 15:28:05 +00:00
|
|
|
case ELF_VERSION_NONE: return "ELF_VERSION_NONE";
|
|
|
|
case ELF_VERSION_CURRENT: return "ELF_VERSION_CURRENT";
|
|
|
|
case ELF_VERSION_NUM: return "ELF_VERSION_NUM";
|
|
|
|
default: return "INVALID";
|
2023-10-26 19:55:13 +00:00
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
static std::string_view getIdentOsabiName(ident_osabi_es osabi) {
|
|
|
|
switch (osabi) {
|
2023-10-27 15:28:05 +00:00
|
|
|
case ELF_OSABI_NONE: return "ELF_OSABI_NONE";
|
|
|
|
case ELF_OSABI_HPUX: return "ELF_OSABI_HPUX";
|
|
|
|
case ELF_OSABI_NETBSD: return "ELF_OSABI_NETBSD";
|
|
|
|
case ELF_OSABI_LINUX: return "ELF_OSABI_LINUX";
|
|
|
|
case ELF_OSABI_SOLARIS: return "ELF_OSABI_SOLARIS";
|
|
|
|
case ELF_OSABI_AIX: return "ELF_OSABI_AIX";
|
|
|
|
case ELF_OSABI_IRIX: return "ELF_OSABI_IRIX";
|
|
|
|
case ELF_OSABI_FREEBSD: return "ELF_OSABI_FREEBSD";
|
|
|
|
case ELF_OSABI_TRU64: return "ELF_OSABI_TRU64";
|
|
|
|
case ELF_OSABI_MODESTO: return "ELF_OSABI_MODESTO";
|
|
|
|
case ELF_OSABI_OPENBSD: return "ELF_OSABI_OPENBSD";
|
|
|
|
case ELF_OSABI_OPENVMS: return "ELF_OSABI_OPENVMS";
|
|
|
|
case ELF_OSABI_NSK: return "ELF_OSABI_NSK";
|
|
|
|
case ELF_OSABI_AROS: return "ELF_OSABI_AROS";
|
|
|
|
case ELF_OSABI_ARM_AEABI: return "ELF_OSABI_ARM_AEABI";
|
|
|
|
case ELF_OSABI_ARM: return "ELF_OSABI_ARM";
|
|
|
|
case ELF_OSABI_STANDALONE: return "ELF_OSABI_STANDALONE";
|
|
|
|
default: return "INVALID";
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
2023-10-26 19:55:13 +00:00
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
static std::string_view getIdentAbiversionName(ident_abiversion_es version) {
|
|
|
|
switch (version) {
|
2023-10-27 15:28:05 +00:00
|
|
|
case ELF_ABI_VERSION_AMDGPU_HSA_V2: return "ELF_ABI_VERSION_AMDGPU_HSA_V2";
|
|
|
|
case ELF_ABI_VERSION_AMDGPU_HSA_V3: return "ELF_ABI_VERSION_AMDGPU_HSA_V3";
|
|
|
|
case ELF_ABI_VERSION_AMDGPU_HSA_V4: return "ELF_ABI_VERSION_AMDGPU_HSA_V4";
|
|
|
|
case ELF_ABI_VERSION_AMDGPU_HSA_V5: return "ELF_ABI_VERSION_AMDGPU_HSA_V5";
|
|
|
|
default: return "INVALID";
|
2023-10-26 19:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-27 15:28:05 +00:00
|
|
|
Elf::~Elf() { Reset(); }
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-27 15:28:05 +00:00
|
|
|
void Elf::Reset() { m_f.close(); }
|
2023-06-23 01:48:55 +00:00
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
void Elf::Open(const std::string& file_name) {
|
2023-10-26 19:55:13 +00:00
|
|
|
Reset();
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
m_f.open(file_name, Common::FS::OpenMode::Read);
|
|
|
|
m_f.read(m_self);
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (is_self = isSelfFile(); !is_self) {
|
|
|
|
m_f.seek(0, Common::FS::SeekMode::Set);
|
2023-08-18 08:22:01 +00:00
|
|
|
} else {
|
2023-10-26 19:55:13 +00:00
|
|
|
m_self_segments.resize(m_self.segment_count);
|
|
|
|
m_f.read(m_self_segments);
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
const u64 elf_header_pos = m_f.tell();
|
|
|
|
m_f.read(m_elf_header);
|
2023-08-18 08:22:01 +00:00
|
|
|
if (!isElfFile()) {
|
2023-10-26 19:55:13 +00:00
|
|
|
return;
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
const auto load_headers = [this]<typename T>(std::vector<T>& out, u64 offset, u16 num) {
|
|
|
|
if (!num) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.resize(num);
|
|
|
|
m_f.seek(offset, Common::FS::SeekMode::Set);
|
|
|
|
m_f.read(out);
|
|
|
|
};
|
|
|
|
|
|
|
|
load_headers(m_elf_phdr, elf_header_pos + m_elf_header.e_phoff, m_elf_header.e_phnum);
|
|
|
|
load_headers(m_elf_shdr, elf_header_pos + m_elf_header.e_shoff, m_elf_header.e_shnum);
|
|
|
|
|
|
|
|
if (is_self) {
|
2023-05-02 15:22:19 +00:00
|
|
|
u64 header_size = 0;
|
|
|
|
header_size += sizeof(self_header);
|
2023-10-26 19:55:13 +00:00
|
|
|
header_size += sizeof(self_segment_header) * m_self.segment_count;
|
2023-05-02 15:22:19 +00:00
|
|
|
header_size += sizeof(elf_header);
|
2023-10-26 19:55:13 +00:00
|
|
|
header_size += m_elf_header.e_phnum * m_elf_header.e_phentsize;
|
|
|
|
header_size += m_elf_header.e_shnum * m_elf_header.e_shentsize;
|
2023-08-18 08:22:01 +00:00
|
|
|
header_size += 15;
|
2023-10-26 19:55:13 +00:00
|
|
|
header_size &= ~15; // Align
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_ehsize - header_size >= sizeof(elf_program_id_header)) {
|
|
|
|
m_f.seek(header_size, Common::FS::SeekMode::Set);
|
|
|
|
m_f.read(m_self_id_header);
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 07:49:51 +00:00
|
|
|
DebugDump();
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
bool Elf::isSelfFile() const {
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_self.magic != self_header::signature) [[unlikely]] {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "Not a SELF file.Magic file mismatched !current = {} required = {}\n ", log_hex_full(m_self.magic),
|
|
|
|
log_hex_full(self_header::signature));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_self.version != 0x00 || m_self.mode != 0x01 || m_self.endian != 0x01 || m_self.attributes != 0x12) [[unlikely]] {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "Unknown SELF file\n");
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_self.category != 0x01 || m_self.program_type != 0x01) [[unlikely]] {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "Unknown SELF file\n");
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
bool Elf::isElfFile() const {
|
2023-10-26 19:55:13 +00:00
|
|
|
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) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "Not an ELF file magic is wrong!\n");
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_ident.ei_class != ELF_CLASS_64) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_ident[EI_CLASS] expected 0x02 is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_ident.ei_class)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_ident.ei_data != ELF_DATA_2LSB) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_ident[EI_DATA] expected 0x01 is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_ident.ei_data)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_ident.ei_version != ELF_VERSION_CURRENT) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_ident[EI_VERSION] expected 0x01 is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_ident.ei_version)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_ident.ei_osabi != ELF_OSABI_FREEBSD) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_ident[EI_OSABI] expected 0x09 is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_ident.ei_osabi)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_ident.ei_abiversion != ELF_ABI_VERSION_AMDGPU_HSA_V2) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_ident[EI_ABIVERSION] expected 0x00 is ({})\n",
|
|
|
|
log_hex_full(static_cast<u32>(m_elf_header.e_ident.ei_abiversion)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-27 15:28:05 +00:00
|
|
|
if (m_elf_header.e_type != ET_SCE_DYNEXEC && m_elf_header.e_type != ET_SCE_DYNAMIC && m_elf_header.e_type != ET_SCE_EXEC) {
|
|
|
|
LOG_ERROR_IF(log_file_loader, "e_type expected 0xFE10 OR 0xFE18 OR 0xfe00 is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_type)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_machine != EM_X86_64) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_machine expected 0x3E is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_machine)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_version != EV_CURRENT) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "m_elf_header.e_version expected 0x01 is ({})\n", log_hex_full(static_cast<u32>(m_elf_header.e_version)));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_phentsize != sizeof(elf_program_header)) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_phentsize ({}) != sizeof(elf_program_header)\n", static_cast<u32>(m_elf_header.e_phentsize));
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
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
|
2023-05-02 15:22:19 +00:00
|
|
|
{
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_ERROR_IF(log_file_loader, "e_shentsize ({}) != sizeof(elf_section_header)\n", m_elf_header.e_shentsize);
|
2023-05-02 15:22:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Elf::DebugDump() {
|
2023-10-26 19:55:13 +00:00
|
|
|
if (is_self) { // If we load elf instead
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader,(SElfHeaderStr()));
|
2023-10-26 19:55:13 +00:00
|
|
|
for (u16 i = 0; i < m_self.segment_count; i++) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, SELFSegHeader(i));
|
2023-05-22 10:58:41 +00:00
|
|
|
}
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, ElfHeaderStr());
|
2023-05-02 15:22:19 +00:00
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_phentsize > 0) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, "Program headers:\n");
|
2023-10-26 19:55:13 +00:00
|
|
|
for (u16 i = 0; i < m_elf_header.e_phnum; i++) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, ElfPHeaderStr(i));
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-26 19:55:13 +00:00
|
|
|
if (m_elf_header.e_shentsize > 0) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader,"Section headers:\n");
|
2023-10-26 19:55:13 +00:00
|
|
|
for (u16 i = 0; i < m_elf_header.e_shnum; i++) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, "--- shdr {} --\n", i);
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_name ........: {}\n", log_hex_full(m_elf_shdr[i].sh_name));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_type ........: {}\n", log_hex_full(m_elf_shdr[i].sh_type));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_flags .......: {}\n", log_hex_full(m_elf_shdr[i].sh_flags));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_addr ........: {}\n", log_hex_full(m_elf_shdr[i].sh_addr));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_offset ......: {}\n", log_hex_full(m_elf_shdr[i].sh_offset));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_size ........: {}\n", log_hex_full(m_elf_shdr[i].sh_size));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_link ........: {}\n", log_hex_full(m_elf_shdr[i].sh_link));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_info ........: {}\n", log_hex_full(m_elf_shdr[i].sh_info));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_addralign ...: {}\n", log_hex_full(m_elf_shdr[i].sh_addralign));
|
|
|
|
LOG_INFO_IF(log_file_loader, "sh_entsize .....: {}\n", log_hex_full(m_elf_shdr[i].sh_entsize));
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-26 19:55:13 +00:00
|
|
|
|
|
|
|
if (is_self) {
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, "SELF info:\n");
|
|
|
|
LOG_INFO_IF(log_file_loader, "auth id ............: {}\n", log_hex_full(m_self_id_header.authid));
|
|
|
|
LOG_INFO_IF(log_file_loader, "program type .......: {}\n", getProgramTypeName(m_self_id_header.program_type));
|
|
|
|
LOG_INFO_IF(log_file_loader, "app version ........: {}\n", log_hex_full(m_self_id_header.appver));
|
|
|
|
LOG_INFO_IF(log_file_loader, "fw version .........: {}\n", log_hex_full(m_self_id_header.firmver));
|
2023-08-18 08:22:01 +00:00
|
|
|
std::string digest;
|
2023-10-26 19:55:13 +00:00
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
digest += fmt::format("{:02X}", m_self_id_header.digest[i]);
|
|
|
|
}
|
2023-10-27 15:28:05 +00:00
|
|
|
LOG_INFO_IF(log_file_loader, "digest..............: 0x{}\n", digest);
|
2023-05-02 15:22:19 +00:00
|
|
|
}
|
2023-05-06 08:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Elf::SElfHeaderStr() {
|
2023-10-26 19:55:13 +00:00
|
|
|
std::string header = fmt::format("======= SELF HEADER =========\n", m_self.magic);
|
2023-10-27 15:28:05 +00:00
|
|
|
header += fmt::format("magic ..............: {}\n", log_hex_full(m_self.magic));
|
2023-10-26 19:55:13 +00:00
|
|
|
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);
|
2023-10-27 15:28:05 +00:00
|
|
|
header += fmt::format("padding1 ...........: {}\n", log_hex_full(m_self.padding1));
|
2023-10-26 19:55:13 +00:00
|
|
|
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);
|
2023-10-27 15:28:05 +00:00
|
|
|
header += fmt::format("padding2 ...........: {}\n", log_hex_full(m_self.padding2));
|
2023-10-26 19:55:13 +00:00
|
|
|
header += fmt::format("segment count ......: {}\n", m_self.segment_count);
|
2023-10-27 15:28:05 +00:00
|
|
|
header += fmt::format("unknown 1A .........: {}\n", log_hex_full(m_self.unknown1A));
|
|
|
|
header += fmt::format("padding3 ...........: {}\n", log_hex_full(m_self.padding3));
|
2023-08-18 08:22:01 +00:00
|
|
|
return header;
|
2023-05-08 09:15:28 +00:00
|
|
|
}
|
2023-06-23 01:48:55 +00:00
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
std::string Elf::SELFSegHeader(u16 no) {
|
2023-10-26 19:55:13 +00:00
|
|
|
const auto segment_header = m_self_segments[no];
|
2023-05-10 19:21:34 +00:00
|
|
|
std::string header = fmt::format("====== SEGMENT HEADER {} ========\n", no);
|
2023-05-08 09:15:28 +00:00
|
|
|
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;
|
2023-05-10 19:21:34 +00:00
|
|
|
}
|
2023-06-23 01:48:55 +00:00
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
std::string Elf::ElfHeaderStr() {
|
2023-05-10 19:21:34 +00:00
|
|
|
std::string header = fmt::format("======= Elf header ===========\n");
|
2023-08-18 08:22:01 +00:00
|
|
|
header += fmt::format("ident ............: 0x");
|
2023-10-26 19:55:13 +00:00
|
|
|
for (auto i : m_elf_header.e_ident.magic) {
|
2023-05-10 19:21:34 +00:00
|
|
|
header += fmt::format("{:02X}", i);
|
|
|
|
}
|
|
|
|
header += fmt::format("\n");
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
header += fmt::format("ident class.......: {}\n", getIdentClassName(m_elf_header.e_ident.ei_class));
|
|
|
|
header += fmt::format("ident data .......: {}\n", getIdentEndianName(m_elf_header.e_ident.ei_data));
|
|
|
|
header += fmt::format("ident version.....: {}\n", getIdentVersionName(m_elf_header.e_ident.ei_version));
|
|
|
|
header += fmt::format("ident osabi .....: {}\n", getIdentOsabiName(m_elf_header.e_ident.ei_osabi));
|
|
|
|
header += fmt::format("ident abiversion..: {}\n", getIdentAbiversionName(m_elf_header.e_ident.ei_abiversion));
|
2023-05-10 19:21:34 +00:00
|
|
|
|
|
|
|
header += fmt::format("ident UNK ........: 0x");
|
2023-10-26 19:55:13 +00:00
|
|
|
for (auto i : m_elf_header.e_ident.pad) {
|
2023-05-10 19:21:34 +00:00
|
|
|
header += fmt::format("{:02X}", i);
|
|
|
|
}
|
|
|
|
header += fmt::format("\n");
|
|
|
|
|
2023-10-26 19:55:13 +00:00
|
|
|
header += fmt::format("type ............: {}\n", static_cast<u32>(m_elf_header.e_type));
|
|
|
|
header += fmt::format("machine ..........: {}\n", static_cast<u32>(m_elf_header.e_machine));
|
|
|
|
header += fmt::format("version ..........: {}\n", static_cast<u32>(m_elf_header.e_version));
|
2023-10-27 15:28:05 +00:00
|
|
|
header += fmt::format("entry ............: {}\n", log_hex_full(m_elf_header.e_entry));
|
|
|
|
header += fmt::format("phoff ............: {}\n", log_hex_full(m_elf_header.e_phoff));
|
|
|
|
header += fmt::format("shoff ............: {}\n", log_hex_full(m_elf_header.e_shoff));
|
|
|
|
header += fmt::format("flags ............: {}\n", log_hex_full(m_elf_header.e_flags));
|
2023-10-26 19:55:13 +00:00
|
|
|
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);
|
2023-05-10 19:21:34 +00:00
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
2023-05-13 07:00:22 +00:00
|
|
|
std::string Elf::ElfPheaderTypeStr(u32 type) {
|
|
|
|
switch (type) {
|
2023-08-18 08:22:01 +00:00
|
|
|
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";
|
2023-05-13 07:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
std::string Elf::ElfPHeaderStr(u16 no) {
|
2023-05-10 19:21:34 +00:00
|
|
|
std::string header = fmt::format("====== PROGRAM HEADER {} ========\n", no);
|
2023-10-26 19:55:13 +00:00
|
|
|
header += fmt::format("p_type ....: {}\n", ElfPheaderTypeStr(m_elf_phdr[no].p_type));
|
2023-10-27 15:28:05 +00:00
|
|
|
header += fmt::format("p_flags ...: {}\n", log_hex_full(static_cast<u32>(m_elf_phdr[no].p_flags)));
|
|
|
|
header += fmt::format("p_offset ..: {}\n", log_hex_full(m_elf_phdr[no].p_offset));
|
|
|
|
header += fmt::format("p_vaddr ...: {}\n", log_hex_full(m_elf_phdr[no].p_vaddr));
|
|
|
|
header += fmt::format("p_paddr ...: {}\n", log_hex_full(m_elf_phdr[no].p_paddr));
|
|
|
|
header += fmt::format("p_filesz ..: {}\n", log_hex_full(m_elf_phdr[no].p_filesz));
|
|
|
|
header += fmt::format("p_memsz ...: {}\n", log_hex_full(m_elf_phdr[no].p_memsz));
|
|
|
|
header += fmt::format("p_align ...: {}\n", log_hex_full(m_elf_phdr[no].p_align));
|
2023-05-10 19:21:34 +00:00
|
|
|
return header;
|
|
|
|
}
|
2023-06-23 01:48:55 +00:00
|
|
|
|
2023-08-18 08:22:01 +00:00
|
|
|
void Elf::LoadSegment(u64 virtual_addr, u64 file_offset, u64 size) {
|
2023-10-26 19:55:13 +00:00
|
|
|
if (!is_self) {
|
|
|
|
// It's elf file
|
|
|
|
m_f.seek(file_offset, Common::FS::SeekMode::Set);
|
|
|
|
m_f.read(reinterpret_cast<void*>(static_cast<uintptr_t>(virtual_addr)), size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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, Common::FS::SeekMode::Set);
|
|
|
|
m_f.read(reinterpret_cast<void*>(static_cast<uintptr_t>(virtual_addr)), size);
|
|
|
|
return;
|
2023-06-13 16:05:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-25 15:08:10 +00:00
|
|
|
}
|
2023-10-26 19:55:13 +00:00
|
|
|
BREAKPOINT(); // Hmm we didn't return something...
|
2023-05-25 15:08:10 +00:00
|
|
|
}
|