From dfc234f363123cf511adff8c6539404d753bb498 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Tue, 30 May 2023 13:33:52 +0300 Subject: [PATCH] initial disasm class using zydis --- CMakeLists.txt | 2 +- src/Util/Disassembler.cpp | 33 +++++++++++++++++++++++++++++++++ src/Util/Disassembler.h | 16 ++++++++++++++++ third-party/CMakeLists.txt | 11 ++++++++++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/Util/Disassembler.cpp create mode 100644 src/Util/Disassembler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 71e31bbe..ec0be38c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ add_executable(shadps4 src/Core/Memory.h src/Core/PS4/Linker.cpp src/Core/PS4/Linker.h - "src/Util/Singleton.h") + "src/Util/Singleton.h" "src/Util/Disassembler.cpp" "src/Util/Disassembler.h") find_package(OpenGL REQUIRED) diff --git a/src/Util/Disassembler.cpp b/src/Util/Disassembler.cpp new file mode 100644 index 00000000..bc59968f --- /dev/null +++ b/src/Util/Disassembler.cpp @@ -0,0 +1,33 @@ +#include "Disassembler.h" +#include + + +Disassembler::Disassembler() +{ + ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64); + ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL); +} + +Disassembler::~Disassembler() +{ +} + +void Disassembler::printInstruction(void* code)//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); + if (!ZYAN_SUCCESS(status)) + { + printf("decode instruction failed at %p\n", code); + printInst(instruction, operands); + } +} + +void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands) +{ + const int bufLen = 256; + char szBuffer[bufLen]; + ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible, szBuffer, sizeof(szBuffer), 0,NULL); + printf("instruction: %s\n", szBuffer); +} \ No newline at end of file diff --git a/src/Util/Disassembler.h b/src/Util/Disassembler.h new file mode 100644 index 00000000..038e9202 --- /dev/null +++ b/src/Util/Disassembler.h @@ -0,0 +1,16 @@ +#pragma once + +#include "zydis/Zydis.h" + +class Disassembler +{ +public: + Disassembler(); + ~Disassembler(); + void printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands); + void printInstruction(void* code); + +private: + ZydisDecoder m_decoder; + ZydisFormatter m_formatter; +}; \ No newline at end of file diff --git a/third-party/CMakeLists.txt b/third-party/CMakeLists.txt index f0d5d668..7272a238 100644 --- a/third-party/CMakeLists.txt +++ b/third-party/CMakeLists.txt @@ -19,11 +19,20 @@ target_include_directories(stb INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/stb") set(SDL3_DIR ${CMAKE_CURRENT_SOURCE_DIR}/SDL) add_subdirectory(${SDL3_DIR}) +#================ Zydis ======================== +# Register Zydis dependency. +# Disable build of tools and examples. +option(ZYDIS_BUILD_TOOLS "" OFF) +option(ZYDIS_BUILD_EXAMPLES "" OFF) +set(zydis_DIR ${CMAKE_CURRENT_SOURCE_DIR}/zydis) +add_subdirectory(${zydis_DIR}) + #=================== IMGUI =================== set(IMGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/imgui) add_library(IMGUI STATIC) + target_sources( IMGUI PRIVATE ${IMGUI_DIR}/imgui_demo.cpp @@ -47,6 +56,6 @@ find_package(OpenGL REQUIRED) target_link_libraries(IMGUI PUBLIC ${OPENGL_LIBRARIES}) -target_link_libraries(IMGUI PUBLIC SDL3-shared ${CMAKE_DL_LIBS}) +target_link_libraries(IMGUI PUBLIC SDL3-shared ${CMAKE_DL_LIBS} Zydis)