From 66ea5329d21e3b10a3f77ad8a08c602b20978cc4 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Tue, 30 May 2023 16:27:11 +0300 Subject: [PATCH] some more playing with zydis --- src/Core/PS4/Linker.cpp | 21 ++++++++++++++++++++- src/Loader/Elf.cpp | 2 +- src/Loader/Elf.h | 2 +- src/Util/Disassembler.cpp | 10 +++++----- src/Util/Disassembler.h | 5 +++-- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/Core/PS4/Linker.cpp b/src/Core/PS4/Linker.cpp index 7d2d65ee..0cc7e646 100644 --- a/src/Core/PS4/Linker.cpp +++ b/src/Core/PS4/Linker.cpp @@ -1,6 +1,7 @@ #include "Linker.h" #include "../Memory.h" #include "../../Util/Log.h" +#include "../../Util/Disassembler.h" constexpr bool debug_loader = true; @@ -101,7 +102,7 @@ void Linker::LoadModuleToMemory(Module* m) LOG_INFO_IF(debug_loader, "segment_file_size .....: {}\n", segment_file_size); LOG_INFO_IF(debug_loader, "segment_memory_size ...: {}\n", segment_memory_size); LOG_INFO_IF(debug_loader, "segment_mode ..........: {}\n", segment_mode); - + m->elf->LoadSegment(segment_addr, elf_pheader[i].p_offset, segment_file_size); } else @@ -138,4 +139,22 @@ void Linker::LoadModuleToMemory(Module* m) } } LOG_INFO_IF(debug_loader, "program entry addr ..........: {:#018x}\n", m->elf->GetElfEntry() + m->base_virtual_addr); + + auto* rt1 = reinterpret_cast(m->elf->GetElfEntry() + m->base_virtual_addr); + ZyanU64 runtime_address = m->elf->GetElfEntry() + m->base_virtual_addr; + + // Loop over the instructions in our buffer. + ZyanUSize offset = 0; + ZydisDisassembledInstruction instruction; + while (ZYAN_SUCCESS(ZydisDisassembleIntel( + /* machine_mode: */ ZYDIS_MACHINE_MODE_LONG_64, + /* runtime_address: */ runtime_address, + /* buffer: */ rt1 + offset, + /* length: */ sizeof(rt1) - offset, + /* instruction: */ &instruction + ))) { + printf("%016" PRIX64 " %s\n", runtime_address, instruction.text); + offset += instruction.info.length; + runtime_address += instruction.info.length; + } } \ No newline at end of file diff --git a/src/Loader/Elf.cpp b/src/Loader/Elf.cpp index be8a83fb..357e3b07 100644 --- a/src/Loader/Elf.cpp +++ b/src/Loader/Elf.cpp @@ -446,7 +446,7 @@ std::string Elf::ElfPheaderTypeStr(u32 type) { return "Loadable"; case PT_DYNAMIC: return "Dynamic"; - case PT_INERP: + case PT_INTERP: return "Interpreter Path"; case PT_NOTE: return "Note"; diff --git a/src/Loader/Elf.h b/src/Loader/Elf.h index e842fbb6..dbe7c9c3 100644 --- a/src/Loader/Elf.h +++ b/src/Loader/Elf.h @@ -242,7 +242,7 @@ typedef enum : u32 { PT_NULL = 0x0, PT_LOAD = 0x1, PT_DYNAMIC = 0x2, - PT_INERP = 0x3, + PT_INTERP = 0x3, PT_NOTE = 0x4, PT_SHLIB = 0x5, PT_PHDR = 0x6, diff --git a/src/Util/Disassembler.cpp b/src/Util/Disassembler.cpp index 72292b95..83b9289f 100644 --- a/src/Util/Disassembler.cpp +++ b/src/Util/Disassembler.cpp @@ -12,25 +12,25 @@ Disassembler::~Disassembler() { } -void Disassembler::printInstruction(void* code)//print a single instruction +void Disassembler::printInstruction(void* code,u64 address)//print a single instruction { ZydisDecodedInstruction instruction; ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE]; - ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, ZYDIS_MAX_INSTRUCTION_LENGTH,&instruction, operands); + ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands); if (!ZYAN_SUCCESS(status)) { printf("decode instruction failed at %p\n", code); } else { - printInst(instruction, operands); + printInst(instruction, operands,address); } } -void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands) +void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,u64 address) { const int bufLen = 256; char szBuffer[bufLen]; - ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible, szBuffer, sizeof(szBuffer), 0,NULL); + ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible, szBuffer, sizeof(szBuffer), address, ZYAN_NULL); printf("instruction: %s\n", szBuffer); } \ No newline at end of file diff --git a/src/Util/Disassembler.h b/src/Util/Disassembler.h index 038e9202..896a7632 100644 --- a/src/Util/Disassembler.h +++ b/src/Util/Disassembler.h @@ -1,14 +1,15 @@ #pragma once #include "zydis/Zydis.h" +#include "../types.h" class Disassembler { public: Disassembler(); ~Disassembler(); - void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands); - void printInstruction(void* code); + void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,u64 address); + void printInstruction(void* code,u64 address); private: ZydisDecoder m_decoder;