diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d6fd9f..03ad5d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,26 +145,21 @@ endif () # zydis # if (DISASM_ZYDIS) - include(ExternalProject) - ExternalProject_Add(Zydis_src - GIT_REPOSITORY https://github.com/zyantific/zydis.git - GIT_TAG v3.1.0 - GIT_SHALLOW TRUE - CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} - -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR} - -DCMAKE_POSITION_INDEPENDENT_CODE=ON - -DZYDIS_BUILD_SHARED_LIB=OFF - -DZYDIS_BUILD_EXAMPLES=OFF - -DZYDIS_BUILD_TOOLS=OFF - INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install && ${CMAKE_COMMAND} --build zycore --target install + FetchContent_Declare( + Zydis + GIT_REPOSITORY https://github.com/zyantific/zydis.git + GIT_TAG v4.0.0 + GIT_SHALLOW TRUE ) - file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/include) - add_library(Zydis STATIC IMPORTED) - set_property(TARGET Zydis PROPERTY IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}Zydis${CMAKE_STATIC_LIBRARY_SUFFIX}) - set_property(TARGET Zydis PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_BINARY_DIR}/include) + FetchContent_GetProperties(Zydis) + if(NOT zydis_POPULATED) + FetchContent_Populate(Zydis) + set(ZYDIS_BUILD_SHARED_LIB OFF CACHE BOOL "") + set(ZYDIS_BUILD_EXAMPLES OFF CACHE BOOL "") + set(ZYDIS_BUILD_TOOLS OFF CACHE BOOL "") + add_subdirectory_pic(${zydis_SOURCE_DIR} ${zydis_BINARY_DIR}) + endif() + list(APPEND FUNCHOOK_DEPS Zydis) set(DISASM Zydis) endif () @@ -220,9 +215,6 @@ configure_file(src/cmake_config.h.in config.h) function (add_funchook_library target_name target_type) add_library(${target_name} ${target_type} ${FUNCHOOK_SOURCES}) - if (DISASM_ZYDIS) - add_dependencies(${target_name} Zydis_src) - endif () set_target_properties(${target_name} PROPERTIES ${FUNCHOOK_PROPERTIES}) target_include_directories(${target_name} PUBLIC include) target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # to include config.h diff --git a/src/disasm.h b/src/disasm.h index dd5fab5..d19a3f0 100644 --- a/src/disasm.h +++ b/src/disasm.h @@ -73,6 +73,7 @@ typedef cs_insn funchook_insn_t; typedef struct { ZydisDecodedInstruction insn; + ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]; size_t next_address; } funchook_insn_t; diff --git a/src/disasm_Zydis.c b/src/disasm_Zydis.c index 9ccf664..c2b585c 100644 --- a/src/disasm_Zydis.c +++ b/src/disasm_Zydis.c @@ -37,10 +37,10 @@ #ifdef CPU_X86_64 #define MACHINE_MODE ZYDIS_MACHINE_MODE_LONG_64 -#define ADDRESS_WIDTH ZYDIS_ADDRESS_WIDTH_64 +#define STACK_WIDTH ZYDIS_STACK_WIDTH_64 #else #define MACHINE_MODE ZYDIS_MACHINE_MODE_LONG_COMPAT_32 -#define ADDRESS_WIDTH ZYDIS_ADDRESS_WIDTH_32 +#define STACK_WIDTH ZYDIS_STACK_WIDTH_32 #endif #define HEX(x) ((x) < 10 ? (x) + '0' : (x) - 10 + 'A') @@ -54,7 +54,7 @@ int funchook_disasm_init(funchook_disasm_t *disasm, funchook_t *funchook, const } disasm->funchook = funchook; - ZydisDecoderInit(&disasm->decoder, MACHINE_MODE, ADDRESS_WIDTH); + ZydisDecoderInit(&disasm->decoder, MACHINE_MODE, STACK_WIDTH); ZydisFormatterInit(&disasm->formatter, ZYDIS_FORMATTER_STYLE_INTEL); disasm->insn.next_address = address; disasm->code = code; @@ -70,7 +70,8 @@ void funchook_disasm_cleanup(funchook_disasm_t *disasm) int funchook_disasm_next(funchook_disasm_t *disasm, const funchook_insn_t **next_insn) { size_t code_size = disasm->code_end - disasm->code; - ZyanStatus status = ZydisDecoderDecodeBuffer(&disasm->decoder, disasm->code, code_size, &disasm->insn.insn); + ZyanStatus status = ZydisDecoderDecodeFull(&disasm->decoder, disasm->code, code_size, + &disasm->insn.insn, disasm->insn.operands); if (ZYAN_SUCCESS(status)) { disasm->insn.next_address += disasm->insn.insn.length; @@ -97,7 +98,8 @@ void funchook_disasm_log_instruction(funchook_disasm_t *disasm, const funchook_i char hex[24 * 3]; size_t i; - ZydisFormatterFormatInstruction(&disasm->formatter, &insn->insn, buffer, sizeof(buffer), addr); + ZydisFormatterFormatInstruction(&disasm->formatter, &insn->insn, insn->operands, insn->insn.operand_count, + buffer, sizeof(buffer), addr, ZYAN_NULL); for (i = 0; i < size; i++) { hex[i * 3 + 0] = HEX(code[i] >> 4); @@ -127,7 +129,7 @@ void funchook_disasm_x86_rip_relative(funchook_disasm_t *disasm, const funchook_ if (insn->insn.raw.disp.offset != 0) { int i; for (i = 0; i < insn->insn.operand_count; i++) { - const ZydisDecodedOperand *op = &insn->insn.operands[i]; + const ZydisDecodedOperand *op = &insn->operands[i]; if (op->mem.disp.has_displacement && op->mem.base == ZYDIS_REGISTER_RIP) { // Fix IP-relative addressing such as: // mov eax, dword ptr [rip + 0x236eda]