Merge pull request #9 from georgemoralis/magic_enum

Added elf enums using magic enum library
This commit is contained in:
georgemoralis 2023-04-18 19:46:36 +03:00 committed by GitHub
commit 5b692d4864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 285 additions and 48 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "third_party/spdlog"]
path = third_party/spdlog
url = https://github.com/gabime/spdlog
[submodule "third_party/magic_enum"]
path = third_party/magic_enum
url = https://github.com/Neargye/magic_enum.git

View File

@ -2,6 +2,14 @@
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
#include <spdlog/sinks/stdout_color_sinks.h>
#include <magic_enum.hpp>
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()
{
@ -186,43 +194,44 @@ bool Elf::isElfFile() const
{
return false;
}
if (m_elf_header->e_ident[EI_MAG0] != ELFMAG0 || m_elf_header->e_ident[EI_MAG1] != ELFMAG1 || m_elf_header->e_ident[EI_MAG2] != ELFMAG2 ||
m_elf_header->e_ident[EI_MAG3] != ELFMAG3)
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] != ELFCLASS64)
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]);
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] != ELFDATA2LSB)
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]);
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] != EV_CURRENT)
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]);
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] != ELFOSABI_FREEBSD)
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]);
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] != ELFABIVERSION_AMDGPU_HSA_V2)
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]);
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_DYNEXEC && m_elf_header->e_type != ET_DYNAMIC)
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;
@ -264,7 +273,7 @@ void Elf::DebugDump() {
spdlog::set_formatter(std::move(f));
spdlog::info("SELF header:\n");
spdlog::info(" magic ..............: 0x{:x}\n", m_self->magic);
spdlog::info(" magic ..............: 0x{:X}\n", m_self->magic);
spdlog::info(" version .........: {}\n", m_self->version);
spdlog::info(" mode .........: {:#04x}\n", m_self->mode);
spdlog::info(" endian .........: {}\n", m_self->endian);
@ -295,27 +304,76 @@ void Elf::DebugDump() {
spdlog::info("\n");
spdlog::info("Elf header:\n");
spdlog::info(" ident .........: 0x");
for (auto i : m_elf_header->e_ident)
spdlog::info(" ident ............: 0x");
for (auto i : m_elf_header->e_ident.magic)
{
spdlog::info("{:02x}", i);
spdlog::info("{:02X}", i);
}
spdlog::info("\n");
spdlog::info(" type .........: {:#06x}\n", m_elf_header->e_type);
spdlog::info(" machine .......: {:#06x}\n", m_elf_header->e_machine);
spdlog::info(" version .......: {:#010x}\n", m_elf_header->e_version);
auto ident_class = magic_enum::enum_cast<ident_class_es>(m_elf_header->e_ident.ei_class);
if (ident_class.has_value())
{
spdlog::info(" ident class.......: {}\n", magic_enum::enum_name(ident_class.value()));
}
spdlog::info(" entry .........: {:#018x}\n", m_elf_header->e_entry);
spdlog::info(" phoff .........: {:#018x}\n", m_elf_header->e_phoff);
spdlog::info(" shoff .........: {:#018x}\n", m_elf_header->e_shoff);
spdlog::info(" flags .........: {:#010x}\n", m_elf_header->e_flags);
spdlog::info(" ehsize ........: {}\n", m_elf_header->e_ehsize);
spdlog::info(" phentsize .....: {}\n", m_elf_header->e_phentsize);
spdlog::info(" phnum .........: {}\n", m_elf_header->e_phnum);
spdlog::info(" shentsize .....: {}\n", m_elf_header->e_shentsize);
spdlog::info(" shnum .........: {}\n", m_elf_header->e_shnum);
spdlog::info(" shstrndx ......: {}\n", m_elf_header->e_shstrndx);
auto ident_data = magic_enum::enum_cast<ident_endian_es>(m_elf_header->e_ident.ei_data);
if (ident_data.has_value())
{
spdlog::info(" 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())
{
spdlog::info(" 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())
{
spdlog::info(" 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())
{
spdlog::info(" ident abiversion..: {}\n", magic_enum::enum_name(ident_abiversion.value()));
}
spdlog::info(" ident UNK ........: 0x");
for (auto i : m_elf_header->e_ident.pad)
{
spdlog::info("{:02X}", i);
}
spdlog::info("\n");
auto type = magic_enum::enum_cast<e_type_s>(m_elf_header->e_type);
if (type.has_value())
{
spdlog::info(" 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())
{
spdlog::info(" 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())
{
spdlog::info(" version ..........: {}\n", magic_enum::enum_name(version.value()));
}
spdlog::info(" entry ............: {:#018x}\n", m_elf_header->e_entry);
spdlog::info(" phoff ............: {:#018x}\n", m_elf_header->e_phoff);
spdlog::info(" shoff ............: {:#018x}\n", m_elf_header->e_shoff);
spdlog::info(" flags ............: {:#010x}\n", m_elf_header->e_flags);
spdlog::info(" ehsize ...........: {}\n", m_elf_header->e_ehsize);
spdlog::info(" phentsize ........: {}\n", m_elf_header->e_phentsize);
spdlog::info(" phnum ............: {}\n", m_elf_header->e_phnum);
spdlog::info(" shentsize ........: {}\n", m_elf_header->e_shentsize);
spdlog::info(" shnum ............: {}\n", m_elf_header->e_shnum);
spdlog::info(" shstrndx .........: {}\n", m_elf_header->e_shstrndx);
if (m_elf_header->e_phentsize > 0)
{
@ -355,7 +413,15 @@ void Elf::DebugDump() {
{
spdlog::info("SELF info:\n");
spdlog::info("auth id ............: {:#018x}\n", m_self_id_header->authid);
spdlog::info("program type .......: {:#018x}\n", m_self_id_header->program_type);
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");

View File

@ -50,26 +50,182 @@ constexpr u08 ELFMAG1 = 'E';
constexpr u08 ELFMAG2 = 'L';
constexpr u08 ELFMAG3 = 'F';
//other ident fields , only ps4 neccesary ones
constexpr u08 ELFCLASS64 = 2;
constexpr u08 ELFDATA2LSB = 1;
constexpr u08 ELFOSABI_FREEBSD = 9; // FreeBSD
constexpr u08 EV_CURRENT = 1;
constexpr u08 ELFABIVERSION_AMDGPU_HSA_V2 = 0;
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;
//type fields PS4 specific
constexpr u16 ET_DYNEXEC = 0xFE10; // Executable file
constexpr u16 ET_DYNAMIC = 0xFE18; // Shared
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;
//machine field
constexpr u16 EM_X86_64 = 62; // Advanced Micro Devices X86-64 processor
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
{
u08 e_ident[16]; /* ELF identification */
u16 e_type; /* Object file type */
u16 e_machine; /* Machine type */
u32 e_version; /* Object file version */
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 */
@ -108,10 +264,21 @@ struct elf_section_header
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;
u64 program_type;
program_type_es program_type;
u64 appver;
u64 firmver;
u08 digest[32];

1
third_party/magic_enum vendored Submodule

@ -0,0 +1 @@
Subproject commit 95c71dab42acb62bf5d4a781e8f29982f7f3046e

View File

@ -8,7 +8,7 @@
<ItemDefinitionGroup>
<ClCompile>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
<AdditionalIncludeDirectories>$(SolutionDir)\third_party\fmt\include;$(SolutionDir)\third_party\spdlog\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)\third_party\fmt\include;$(SolutionDir)\third_party\magic_enum\include;$(SolutionDir)\third_party\spdlog\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard>
<PreprocessorDefinitions>SPDLOG_FMT_EXTERNAL;_CONSOLE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;WIN32;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BufferSecurityCheck>false</BufferSecurityCheck>