From 56616f832e1e28f228f26e96028bc56b770224d5 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Wed, 5 Apr 2023 20:21:44 +0300 Subject: [PATCH] finished elf header validation --- emulator/Loader/Elf.cpp | 65 +++++++++++++++++++++++++++++++++++++++-- emulator/Loader/Elf.h | 37 ++++++++++++++++++----- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/emulator/Loader/Elf.cpp b/emulator/Loader/Elf.cpp index dc6c9c15..b783dc60 100644 --- a/emulator/Loader/Elf.cpp +++ b/emulator/Loader/Elf.cpp @@ -151,10 +151,69 @@ bool Elf::isElfFile() const { return false; } - if (m_elf_header->e_ident[0] != '\x7f' || m_elf_header->e_ident[1] != '\x45' || m_elf_header->e_ident[2] != '\x4c' || - m_elf_header->e_ident[3] != '\x46') + 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) { - printf("Not an ELF file magic is wrong!\n"); + printf("ERROR:Not an ELF file magic is wrong!\n"); + return false; + } + if (m_elf_header->e_ident[EI_CLASS] != ELFCLASS64) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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) + { + 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; } diff --git a/emulator/Loader/Elf.h b/emulator/Loader/Elf.h index 5740535f..bcc40d4c 100644 --- a/emulator/Loader/Elf.h +++ b/emulator/Loader/Elf.h @@ -34,14 +34,35 @@ struct self_segment_header }; -constexpr int EI_MAG0 = 0;/* e_ident[] indexes */ -constexpr int EI_MAG2 = 2; -constexpr int EI_MAG3 = 3; -constexpr int EI_CLASS = 4; -constexpr int EI_DATA = 5; -constexpr int EI_VERSION = 6; -constexpr int EI_OSABI = 7; -constexpr int EI_PAD = 8; +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'; + +//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; + +//type fields PS4 specific +constexpr u16 ET_DYNEXEC = 0xFE10; // Executable file +constexpr u16 ET_DYNAMIC = 0xFE18; // Shared + +//machine field +constexpr u16 EM_X86_64 = 62; // Advanced Micro Devices X86-64 processor struct elf_header {