From c672c7a9fca9a5c970f3f757d6a08b2637c62965 Mon Sep 17 00:00:00 2001 From: Ende! Date: Sat, 7 Feb 2015 01:09:27 +0100 Subject: [PATCH] implemented CBindings/test.c fixed a few bugs in the C bindings encountered while implementing the test --- Bindings/C/VXDisassemblerC.h | 8 +-- Bindings/C/VXDisassemblerTypesC.h | 1 + Bindings/C/VXInstructionDecoderC.h | 6 +- Bindings/C/VXInstructionFormatterC.cpp | 18 +++--- Bindings/C/VXInstructionFormatterC.h | 8 ++- Bindings/C/VXOpcodeTableC.h | 18 +++--- CMakeLists.txt | 2 + Examples/CBindings/test.c | 84 +++++++++++++++++++++++++- 8 files changed, 117 insertions(+), 28 deletions(-) diff --git a/Bindings/C/VXDisassemblerC.h b/Bindings/C/VXDisassemblerC.h index 57cf832..73f4ec1 100644 --- a/Bindings/C/VXDisassemblerC.h +++ b/Bindings/C/VXDisassemblerC.h @@ -35,9 +35,9 @@ #ifndef _VDE_VXDISASSEMBLERC_H_ #define _VDE_VXDISASSEMBLERC_H_ -#include "VXDisassemblerTypes.h" -#include "VXInstructionDecoder.h" -#include "VXInstructionFormatter.h" -#include "VXDisassemblerUtils.h" +#include "VXDisassemblerTypesC.h" +#include "VXInstructionDecoderC.h" +#include "VXInstructionFormatterC.h" +#include "VXDisassemblerUtilsC.h" #endif /* _VDE_VXDISASSEMBLERC_H_ */ \ No newline at end of file diff --git a/Bindings/C/VXDisassemblerTypesC.h b/Bindings/C/VXDisassemblerTypesC.h index b7fcd75..a70f369 100644 --- a/Bindings/C/VXDisassemblerTypesC.h +++ b/Bindings/C/VXDisassemblerTypesC.h @@ -36,6 +36,7 @@ #define _VDE_VXDISASSEMBLERTYPESC_H_ #include +#include #include "VXOpcodeTableC.h" #ifdef __cplusplus diff --git a/Bindings/C/VXInstructionDecoderC.h b/Bindings/C/VXInstructionDecoderC.h index e40e528..bda21ae 100644 --- a/Bindings/C/VXInstructionDecoderC.h +++ b/Bindings/C/VXInstructionDecoderC.h @@ -109,9 +109,9 @@ VXInstructionDecoderContext* VXInstructionDecoder_Create(void); VXInstructionDecoderContext* VXInstructionDecoder_CreateEx( VXBaseDataSourceContext *input, - VXDisassemblerMode disassemblerMode = DM_M32BIT, - VXInstructionSetVendor preferredVendor = ISV_ANY, - uint64_t instructionPointer = 0); + VXDisassemblerMode disassemblerMode, + VXInstructionSetVendor preferredVendor, + uint64_t instructionPointer); void VXInstructionDecoder_Release( VXInstructionDecoderContext *ctx); diff --git a/Bindings/C/VXInstructionFormatterC.cpp b/Bindings/C/VXInstructionFormatterC.cpp index 1083354..13920b3 100644 --- a/Bindings/C/VXInstructionFormatterC.cpp +++ b/Bindings/C/VXInstructionFormatterC.cpp @@ -113,13 +113,13 @@ inline const Verteron::VXBaseInstructionFormatter* VXBaseInstructionFormatter_Cp } inline Verteron::VXBaseInstructionFormatter* VXIntelInstructionFormatter_CppPtr( - VXIntelInstructionFormatterContext *ctx) + VXBaseInstructionFormatterContext *ctx) { return reinterpret_cast(ctx); } inline const Verteron::VXBaseInstructionFormatter* VXIntelInstructionFormatter_CppPtr( - const VXIntelInstructionFormatterContext *ctx) + const VXBaseInstructionFormatterContext *ctx) { return reinterpret_cast(ctx); } @@ -136,7 +136,6 @@ inline const VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_CPtr return reinterpret_cast(ctx); } - } /* VXBaseSymbolResolver ======================================================================== */ @@ -201,6 +200,14 @@ void VXExactSymbolResolverContext_Clear( /* VXBaseInstructionFormatter ================================================================== */ +const char* VXBaseInstructionFormatter_FormatInstruction( + VXBaseInstructionFormatterContext *ctx, + const VXInstructionInfo *info) +{ + return VXBaseInstructionFormatter_CppPtr(ctx)->formatInstruction( + *VXInstructionInfo_CppPtr(info)); +} + VXBaseSymbolResolverContext* VXBaseInstructionFormatter_GetSymbolResolver( const VXBaseInstructionFormatterContext *ctx) { @@ -236,9 +243,4 @@ VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_CreateEx( VXBaseSymbolResolver_CppPtr(resolver))); } -void VXIntelInstructionFormatter_Release(VXIntelInstructionFormatterContext *ctx) -{ - delete VXIntelInstructionFormatter_CppPtr(ctx); -} - /* ============================================================================================= */ \ No newline at end of file diff --git a/Bindings/C/VXInstructionFormatterC.h b/Bindings/C/VXInstructionFormatterC.h index 66319ec..66608d0 100644 --- a/Bindings/C/VXInstructionFormatterC.h +++ b/Bindings/C/VXInstructionFormatterC.h @@ -85,7 +85,11 @@ typedef const char* (*VXResolveSymbol_t)( /* VXBaseInstructionFormatter ================================================================== */ - typedef struct _VXBaseInstructionFormatterContext {int a;} VXBaseInstructionFormatterContext; +typedef struct _VXBaseInstructionFormatterContext {int a;} VXBaseInstructionFormatterContext; + +const char* VXBaseInstructionFormatter_FormatInstruction( + VXBaseInstructionFormatterContext *ctx, + const VXInstructionInfo *info); VXBaseSymbolResolverContext* VXBaseInstructionFormatter_GetSymbolResolver( const VXBaseInstructionFormatterContext *ctx); @@ -98,8 +102,6 @@ void VXBaseInstructionFormatter_Release(VXBaseInstructionFormatterContext *ctx); /* VXIntelInstructionFormatter ================================================================ */ - typedef struct _VXIntelInstructionFormatterContext {int a;} VXIntelInstructionFormatterContext; - VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_Create(void); VXBaseInstructionFormatterContext* VXIntelInstructionFormatter_CreateEx( diff --git a/Bindings/C/VXOpcodeTableC.h b/Bindings/C/VXOpcodeTableC.h index 34e422f..c6d3bf2 100644 --- a/Bindings/C/VXOpcodeTableC.h +++ b/Bindings/C/VXOpcodeTableC.h @@ -1354,39 +1354,39 @@ typedef enum _VXDefinedOperandSize /* : uint8_t */ /** * @brief B sized register or D sized memory operand. */ - DOS_BD = (uint8_t(DOS_B) << 4) | uint8_t(DOS_D), + DOS_BD = (DOS_B << 4) | DOS_D, /** * @brief B sized register or V sized memory operand. */ - DOS_BV = (uint8_t(DOS_B) << 4) | uint8_t(DOS_V), + DOS_BV = (DOS_B << 4) | DOS_V, /** * @brief W sized register or D sized memory operand. */ - DOS_WD = (uint8_t(DOS_W) << 4) | uint8_t(DOS_D), + DOS_WD = (DOS_W << 4) | DOS_D, /** * @brief W sized register or V sized memory operand. */ - DOS_WV = (uint8_t(DOS_W) << 4) | uint8_t(DOS_V), + DOS_WV = (DOS_W << 4) | DOS_V, /** * @brief W sized register or Y sized memory operand. */ - DOS_WY = (uint8_t(DOS_W) << 4) | uint8_t(DOS_Y), + DOS_WY = (DOS_W << 4) | DOS_Y, /** * @brief D sized register or Y sized memory operand. */ - DOS_DY = (uint8_t(DOS_D) << 4) | uint8_t(DOS_Y), + DOS_DY = (DOS_D << 4) | DOS_Y, /** * @brief W sized register or O sized memory operand. */ - DOS_WO = (uint8_t(DOS_W) << 4) | uint8_t(DOS_O), + DOS_WO = (DOS_W << 4) | DOS_O, /** * @brief D sized register or O sized memory operand. */ - DOS_DO = (uint8_t(DOS_D) << 4) | uint8_t(DOS_O), + DOS_DO = (DOS_D << 4) | DOS_O, /** * @brief Q sized register or O sized memory operand. */ - DOS_QO = (uint8_t(DOS_Q) << 4) | uint8_t(DOS_O), + DOS_QO = (DOS_Q << 4) | DOS_O, } VXDefinedOperandSize; /** diff --git a/CMakeLists.txt b/CMakeLists.txt index b7ef67b..099d968 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ if (BUILD_C_BINDINGS) "Bindings/C/VXOpcodeTableC.cpp" "Bindings/C/VXInstructionDecoderC.cpp") add_library("VerteronDisassemblerEngineC" ${vdec_headers} ${vdec_sources}) + target_link_libraries("VerteronDisassemblerEngineC" "VerteronDisassemblerEngine") endif () # Examples @@ -68,6 +69,7 @@ if (BUILD_EXAMPLES) if (BUILD_C_BINDINGS) add_executable("CBindingsTest" "Examples/CBindings/test.c") + include_directories("Bindings/C") target_link_libraries("CBindingsTest" "VerteronDisassemblerEngineC") endif () diff --git a/Examples/CBindings/test.c b/Examples/CBindings/test.c index 4ab7b90..3cfa534 100644 --- a/Examples/CBindings/test.c +++ b/Examples/CBindings/test.c @@ -30,7 +30,89 @@ **************************************************************************************************/ +#include + +#include +#include + int main() { - + uint8_t data32[] = + { + 0x8B, 0xFF, 0x55, 0x8B, 0xEC, 0x6A, 0xFE, 0x68, 0xD8, 0x18, 0x09, 0x77, 0x68, 0x85, 0xD2, + 0x09, 0x77, 0x64, 0xA1, 0x00, 0x00, 0x00, 0x00, 0x50, 0x83, 0xEC, 0x14, 0x53, 0x56, 0x57, + 0xA1, 0x68, 0xEE, 0x13, 0x77, 0x31, 0x45, 0xF8, 0x33, 0xC5, 0x50, 0x8D, 0x45, 0xF0, 0x64, + 0xA3, 0x00, 0x00, 0x00, 0x00, 0x89, 0x65, 0xE8, 0xC7, 0x45, 0xFC, 0x00, 0x00, 0x00, 0x00, + 0x8B, 0x5D, 0x08, 0xF6, 0xC3, 0x04, 0x0F, 0x85, 0x57, 0x74, 0x00, 0x00, 0x53, 0x6A, 0x00, + 0xFF, 0x35, 0xA0, 0xE3, 0x13, 0x77, 0xFF, 0x15, 0x00, 0x10, 0x14, 0x77, 0x85, 0xC0, 0x0F, + 0x84, 0xC6, 0x48, 0x04, 0x00, 0xC7, 0x45, 0x08, 0x00, 0x00, 0x00, 0x00, 0xC7, 0x45, 0xFC, + 0xFE, 0xFF, 0xFF, 0xFF, 0x33, 0xC0, 0x8B, 0x4D, 0xF0, 0x64, 0x89, 0x0D, 0x00, 0x00, 0x00, + 0x00, 0x59, 0x5F, 0x5E, 0x5B, 0x8B, 0xE5, 0x5D, 0xC2, 0x04, 0x00 + }; + uint8_t data64[] = + { + 0x48, 0x89, 0x5C, 0x24, 0x10, 0x48, 0x89, 0x74, 0x24, 0x18, 0x89, 0x4C, 0x24, 0x08, 0x57, + 0x41, 0x54, 0x41, 0x55, 0x41, 0x56, 0x41, 0x57, 0x48, 0x83, 0xEC, 0x40, 0x4C, 0x8B, 0xF2, + 0x8B, 0xD9, 0x48, 0xC7, 0x44, 0x24, 0x20, 0x00, 0x00, 0x00, 0x00, 0x33, 0xF6, 0x48, 0x89, + 0x74, 0x24, 0x30, 0x45, 0x33, 0xFF, 0xF7, 0xC1, 0x8D, 0xF0, 0xFF, 0xFF, 0x0F, 0x85, 0xAA, + 0x53, 0x08, 0x00, 0xF6, 0xC1, 0x40, 0x8B, 0xFE, 0x41, 0xBD, 0x08, 0x00, 0x00, 0x00, 0x41, + 0x0F, 0x45, 0xFD, 0xF6, 0xC1, 0x02, 0x48, 0x8B, 0x0D, 0x10, 0xD4, 0x0E, 0x00, 0x0F, 0x85, + 0x40, 0xE1, 0x01, 0x00, 0x8B, 0x15, 0x4C, 0xD5, 0x0E, 0x00, 0x81, 0xC2, 0x00, 0x00, 0x14, + 0x00, 0x0B, 0xD7, 0x4D, 0x8B, 0xC6, 0xFF, 0x15, 0x3B, 0x2F, 0x10, 0x00, 0x48, 0x8B, 0xD8, + 0x48, 0x85, 0xC0, 0x0F, 0x84, 0x93, 0x78, 0x0A, 0x00, 0x48, 0x8B, 0xC3, 0x48, 0x8B, 0x5C, + 0x24, 0x78, 0x48, 0x8B, 0xB4, 0x24, 0x80, 0x00, 0x00, 0x00, 0x48, 0x83, 0xC4, 0x40, 0x41, + 0x5F, 0x41, 0x5E, 0x41, 0x5D, 0x41, 0x5C, 0x5F, 0xC3 + }; + + VXInstructionInfo info; + VXInstructionDecoderContext* decoder = NULL; + VXBaseInstructionFormatterContext* formatter = NULL; + VXBaseDataSourceContext* input32 = NULL; + VXBaseDataSourceContext* input64 = NULL; + + decoder = VXInstructionDecoder_Create(); + formatter = VXIntelInstructionFormatter_Create(); + + input32 = VXMemoryDataSource_Create(&data32[0], sizeof(data32)); + input64 = VXMemoryDataSource_Create(&data64[0], sizeof(data64)); + + VXInstructionDecoder_SetDisassemblerMode(decoder, DM_M32BIT); + VXInstructionDecoder_SetDataSource(decoder, input32); + VXInstructionDecoder_SetInstructionPointer(decoder, 0x77091852); + + puts("32 bit test ...\n\n"); + while (VXInstructionDecoder_DecodeInstruction(decoder, &info)) + { + printf("%08X ", (uint32_t)(info.instrAddress & 0xFFFFFFFF)); + if (info.flags & IF_ERROR_MASK) + { + printf("db %02X\n", info.data[0]); + } + else + { + printf("%s\n", VXBaseInstructionFormatter_FormatInstruction(formatter, &info)); + } + } + + puts("\n"); + + VXInstructionDecoder_SetDisassemblerMode(decoder, DM_M64BIT); + VXInstructionDecoder_SetDataSource(decoder, input64); + VXInstructionDecoder_SetInstructionPointer(decoder, 0x00007FFA39A81930ull); + puts("64 bit test ...\n\n"); + while (VXInstructionDecoder_DecodeInstruction(decoder, &info)) + { + printf("%016llX ", info.instrAddress); + if (info.flags & IF_ERROR_MASK) + { + printf("db %02X", info.data[0]); + } + else + { + printf("%s\n", VXBaseInstructionFormatter_FormatInstruction(formatter, &info)); + } + } + + getchar(); + return 0; } \ No newline at end of file