some cleanups

This commit is contained in:
georgemoralis 2023-11-21 16:54:10 +02:00
parent 4f6dc8fddb
commit b6d98d4b72
3 changed files with 216 additions and 245 deletions

View File

@ -1,15 +1,14 @@
#pragma once #pragma once
#include <string>
#include <cinttypes> #include <cinttypes>
#include <span> #include <span>
#include <string>
#include <vector> #include <vector>
#include "common/types.h"
#include "common/fs_file.h" #include "common/fs_file.h"
#include "common/types.h"
struct self_header struct self_header {
{
static const u32 signature = 0x1D3D154Fu; static const u32 signature = 0x1D3D154Fu;
u32 magic; u32 magic;
@ -29,15 +28,12 @@ struct self_header
u32 padding3; u32 padding3;
}; };
struct self_segment_header struct self_segment_header {
{
bool IsBlocked() const { bool IsBlocked() const {
return (flags & 0x800) != 0; // 0 or 0x800 return (flags & 0x800) != 0; // 0 or 0x800
} }
u32 GetId() const { u32 GetId() const { return (flags >> 20u) & 0xFFFu; }
return (flags >> 20u) & 0xFFFu;
}
bool IsOrdered() const { bool IsOrdered() const {
return (flags & 1) != 0; // 0 or 1 return (flags & 1) != 0; // 0 or 1
@ -61,7 +57,6 @@ struct self_segment_header
u64 memory_size; u64 memory_size;
}; };
constexpr u08 EI_MAG0 = 0; /* e_ident[] indexes */ constexpr u08 EI_MAG0 = 0; /* e_ident[] indexes */
constexpr u08 EI_MAG1 = 1; constexpr u08 EI_MAG1 = 1;
constexpr u08 EI_MAG2 = 2; constexpr u08 EI_MAG2 = 2;
@ -184,30 +179,13 @@ typedef enum : u16 {
EM_UNICORE = 110 /* Microprocessor series from PKU-Unity Ltd. and MPRC */ EM_UNICORE = 110 /* Microprocessor series from PKU-Unity Ltd. and MPRC */
} e_machine_es; } e_machine_es;
typedef enum :u32 { typedef enum : u32 { EV_NONE = 0x0, EV_CURRENT = 0x1 } e_version_es;
EV_NONE = 0x0,
EV_CURRENT = 0x1
} e_version_es;
typedef enum : u08 { typedef enum : u08 { ELF_CLASS_NONE = 0x0, ELF_CLASS_32 = 0x1, ELF_CLASS_64 = 0x2, ELF_CLASS_NUM = 0x3 } ident_class_es;
ELF_CLASS_NONE =0x0,
ELF_CLASS_32 =0x1,
ELF_CLASS_64 =0x2,
ELF_CLASS_NUM =0x3
} ident_class_es;
typedef enum : u08 { typedef enum : u08 { ELF_DATA_NONE = 0x0, ELF_DATA_2LSB = 0x1, ELF_DATA_2MSB = 0x2, ELF_DATA_NUM = 0x3 } ident_endian_es;
ELF_DATA_NONE = 0x0,
ELF_DATA_2LSB = 0x1,
ELF_DATA_2MSB = 0x2,
ELF_DATA_NUM = 0x3
} ident_endian_es;
typedef enum :u08 { typedef enum : u08 { ELF_VERSION_NONE = 0x0, ELF_VERSION_CURRENT = 0x1, ELF_VERSION_NUM = 0x2 } ident_version_es;
ELF_VERSION_NONE = 0x0,
ELF_VERSION_CURRENT = 0x1,
ELF_VERSION_NUM = 0x2
} ident_version_es;
typedef enum : u08 { typedef enum : u08 {
ELF_OSABI_NONE = 0x0, /* No extensions or unspecified */ ELF_OSABI_NONE = 0x0, /* No extensions or unspecified */
@ -246,8 +224,7 @@ struct elf_ident {
u08 pad[6]; u08 pad[6];
}; };
struct elf_header struct elf_header {
{
static const u32 signature = 0x7F454C46u; static const u32 signature = 0x7F454C46u;
elf_ident e_ident; /* ELF identification */ elf_ident e_ident; /* ELF identification */
@ -306,8 +283,7 @@ typedef enum : u32 {
PF_READ_WRITE_EXEC = 0x7 PF_READ_WRITE_EXEC = 0x7
} elf_program_flags; } elf_program_flags;
struct elf_program_header struct elf_program_header {
{
elf_program_type p_type; /* Type of segment */ elf_program_type p_type; /* Type of segment */
elf_program_flags p_flags; /* Segment attributes */ elf_program_flags p_flags; /* Segment attributes */
u64 p_offset; /* Offset in file */ u64 p_offset; /* Offset in file */
@ -318,8 +294,7 @@ struct elf_program_header
u64 p_align; /* Alignment of segment */ u64 p_align; /* Alignment of segment */
}; };
struct elf_section_header struct elf_section_header {
{
u32 sh_name; /* Section name */ u32 sh_name; /* Section name */
u32 sh_type; /* Section type */ u32 sh_type; /* Section type */
u64 sh_flags; /* Section attributes */ u64 sh_flags; /* Section attributes */
@ -343,8 +318,7 @@ typedef enum :u64 {
PT_SECURE_KERNEL = 0xF PT_SECURE_KERNEL = 0xF
} program_type_es; } program_type_es;
struct elf_program_id_header struct elf_program_id_header {
{
u64 authid; u64 authid;
program_type_es program_type; program_type_es program_type;
u64 appver; u64 appver;
@ -389,12 +363,9 @@ constexpr s64 DT_SCE_STRSZ = 0x61000037;
constexpr s64 DT_SCE_SYMTAB = 0x61000039; constexpr s64 DT_SCE_SYMTAB = 0x61000039;
constexpr s64 DT_SCE_SYMTABSZ = 0x6100003f; constexpr s64 DT_SCE_SYMTABSZ = 0x6100003f;
struct elf_dynamic {
struct elf_dynamic
{
s64 d_tag; s64 d_tag;
union union {
{
u64 d_val; u64 d_val;
u64 d_ptr; u64 d_ptr;
} d_un; } d_un;
@ -423,8 +394,7 @@ constexpr u08 STV_INTERNAL = 1;
constexpr u08 STV_HIDDEN = 2; constexpr u08 STV_HIDDEN = 2;
constexpr u08 STV_PROTECTED = 3; constexpr u08 STV_PROTECTED = 3;
struct elf_symbol struct elf_symbol {
{
u08 GetBind() const { return st_info >> 4u; } u08 GetBind() const { return st_info >> 4u; }
u08 GetType() const { return st_info & 0xfu; } u08 GetType() const { return st_info & 0xfu; }
u08 GetVisibility() const { return st_other & 3u; } u08 GetVisibility() const { return st_other & 3u; }
@ -437,8 +407,7 @@ struct elf_symbol
u64 st_size; u64 st_size;
}; };
struct elf_relocation struct elf_relocation {
{
u32 GetSymbol() const { return static_cast<u32>(rel_info >> 32u); } u32 GetSymbol() const { return static_cast<u32>(rel_info >> 32u); }
u32 GetType() const { return static_cast<u32>(rel_info & 0xffffffff); } u32 GetType() const { return static_cast<u32>(rel_info & 0xffffffff); }
@ -447,8 +416,10 @@ struct elf_relocation
s64 rel_addend; s64 rel_addend;
}; };
constexpr u32 R_X86_64_64 = 1; // Direct 64 bit constexpr u32 R_X86_64_64 = 1; // Direct 64 bit
constexpr u32 R_X86_64_GLOB_DAT = 6;
constexpr u32 R_X86_64_JUMP_SLOT = 7; // Create PLT entry constexpr u32 R_X86_64_JUMP_SLOT = 7; // Create PLT entry
constexpr u32 R_X86_64_RELATIVE = 8; // Adjust by program base constexpr u32 R_X86_64_RELATIVE = 8; // Adjust by program base
constexpr u32 R_X86_64_DTPMOD64 = 16;
namespace Core::Loader { namespace Core::Loader {
@ -462,25 +433,15 @@ class Elf {
bool isElfFile() const; bool isElfFile() const;
void DebugDump(); void DebugDump();
[[nodiscard]] self_header GetSElfHeader() const { [[nodiscard]] self_header GetSElfHeader() const { return m_self; }
return m_self;
}
[[nodiscard]] elf_header GetElfHeader() const { [[nodiscard]] elf_header GetElfHeader() const { return m_elf_header; }
return m_elf_header;
}
[[nodiscard]] std::span<const elf_program_header> GetProgramHeader() const { [[nodiscard]] std::span<const elf_program_header> GetProgramHeader() const { return m_elf_phdr; }
return m_elf_phdr;
}
[[nodiscard]] std::span<const self_segment_header> GetSegmentHeader() const { [[nodiscard]] std::span<const self_segment_header> GetSegmentHeader() const { return m_self_segments; }
return m_self_segments;
}
[[nodiscard]] u64 GetElfEntry() const { [[nodiscard]] u64 GetElfEntry() const { return m_elf_header.e_entry; }
return m_elf_header.e_entry;
}
std::string SElfHeaderStr(); std::string SElfHeaderStr();
std::string SELFSegHeader(u16 no); std::string SELFSegHeader(u16 no);

View File

@ -1,6 +1,7 @@
#include "common/log.h"
#include "core/virtual_memory.h" #include "core/virtual_memory.h"
#include "common/log.h"
#ifdef _WIN64 #ifdef _WIN64
#include <windows.h> #include <windows.h>
#else #else
@ -107,8 +108,7 @@ bool memory_patch(u64 vaddr, u64 value) {
memory_protect(vaddr, 8, old_mode, nullptr); memory_protect(vaddr, 8, old_mode, nullptr);
// if mode is executable flush it so insure that cpu finds it // if mode is executable flush it so insure that cpu finds it
if ((old_mode == MemoryMode::Execute || old_mode == MemoryMode::ExecuteRead || old_mode == MemoryMode::ExecuteWrite || if (containsExecuteMode(old_mode)) {
old_mode == MemoryMode::ExecuteReadWrite)) {
memory_flush(vaddr, 8); memory_flush(vaddr, 8);
} }

View File

@ -25,4 +25,14 @@ bool memory_protect(u64 address, u64 size, MemoryMode mode, MemoryMode* old_mode
bool memory_flush(u64 address, u64 size); bool memory_flush(u64 address, u64 size);
bool memory_patch(u64 vaddr, u64 value); bool memory_patch(u64 vaddr, u64 value);
inline bool containsExecuteMode(MemoryMode mode) {
switch (mode) {
case MemoryMode::Execute: return true;
case MemoryMode::ExecuteRead: return true;
case MemoryMode::ExecuteWrite: return true;
case MemoryMode::ExecuteReadWrite: return true;
default: return false;
}
}
} // namespace VirtualMemory } // namespace VirtualMemory