From b75bc7ccc67917f3cfa937822aca0ecae673395b Mon Sep 17 00:00:00 2001 From: flobernd Date: Tue, 4 Sep 2018 02:33:59 +0200 Subject: [PATCH] Minor refactorings and bugfixes --- include/Zydis/Utils.h | 4 ++-- src/Formatter.c | 52 +++++++++++++++++++++++++++++++++++++++++-- tools/ZydisInfo.c | 14 +++++++++--- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/include/Zydis/Utils.h b/include/Zydis/Utils.h index 902d822d..6d07ae3a 100644 --- a/include/Zydis/Utils.h +++ b/include/Zydis/Utils.h @@ -51,11 +51,11 @@ typedef enum ZydisInstructionSegment_ { ZYDIS_INSTR_SEGMENT_NONE, /** - * @brief The legacy prefixes. + * @brief The legacy prefixes (including ignored `REX` prefixes). */ ZYDIS_INSTR_SEGMENT_PREFIXES, /** - * @brief The `REX` prefix byte. + * @brief The effective `REX` prefix byte. */ ZYDIS_INSTR_SEGMENT_REX, /** diff --git a/src/Formatter.c b/src/Formatter.c index 9f2e866b..e3ffbf61 100644 --- a/src/Formatter.c +++ b/src/Formatter.c @@ -468,8 +468,56 @@ ZyanStatus ZydisFormatterSetHook(ZydisFormatter* formatter, ZydisFormatterFuncti const void* const temp = *callback; - // This code relies on the order of the enum values and the function fields inside the - // `ZydisFormatter` struct + // The following code relies on the order of the enum values and the function fields inside + // the `ZydisFormatter` struct + +#ifdef ZYAN_DEBUG + const ZyanUPointer* test = (ZyanUPointer*)(&formatter->func_pre_instruction + type); + switch (type) + { + case ZYDIS_FORMATTER_FUNC_PRE_INSTRUCTION: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_pre_instruction ); break; + case ZYDIS_FORMATTER_FUNC_POST_INSTRUCTION: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_post_instruction ); break; + case ZYDIS_FORMATTER_FUNC_FORMAT_INSTRUCTION: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_format_instruction); break; + case ZYDIS_FORMATTER_FUNC_PRE_OPERAND: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_pre_operand ); break; + case ZYDIS_FORMATTER_FUNC_POST_OPERAND: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_post_operand ); break; + case ZYDIS_FORMATTER_FUNC_FORMAT_OPERAND_REG: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_format_operand_reg); break; + case ZYDIS_FORMATTER_FUNC_FORMAT_OPERAND_MEM: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_format_operand_mem); break; + case ZYDIS_FORMATTER_FUNC_FORMAT_OPERAND_PTR: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_format_operand_ptr); break; + case ZYDIS_FORMATTER_FUNC_FORMAT_OPERAND_IMM: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_format_operand_imm); break; + case ZYDIS_FORMATTER_FUNC_PRINT_MNEMONIC: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_mnemonic ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_REGISTER: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_register ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_ADDRESS_ABS: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_address_abs ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_ADDRESS_REL: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_address_rel ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_DISP: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_disp ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_IMM: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_imm ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_MEMSIZE: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_mem_size ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_MEMSEG: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_mem_seg ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_PREFIXES: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_prefixes ); break; + case ZYDIS_FORMATTER_FUNC_PRINT_DECORATOR: + ZYAN_ASSERT(test == (ZyanUPointer*)&formatter->func_print_decorator ); break; + default: + ZYAN_UNREACHABLE; + } +#endif + *callback = *(const void**)(&formatter->func_pre_instruction + type); if (!temp) { diff --git a/tools/ZydisInfo.c b/tools/ZydisInfo.c index 7f69cc42..e1afd2a7 100644 --- a/tools/ZydisInfo.c +++ b/tools/ZydisInfo.c @@ -925,12 +925,20 @@ static void PrintInstruction(const ZydisDecodedInstruction* instruction) CVT100_OUT(COLOR_ERROR), CVT100_OUT(ZYAN_VT100SGR_RESET)); exit(status); } - char buffer[256]; - ZydisFormatterFormatInstruction(&formatter, instruction, &buffer[0], sizeof(buffer), 0); + char buffer_abs[256]; + ZydisFormatterFormatInstruction(&formatter, instruction, &buffer_abs[0], + sizeof(buffer_abs), 0); + char buffer_rel[256]; + ZydisFormatterFormatInstruction(&formatter, instruction, &buffer_rel[0], + sizeof(buffer_rel), ZYDIS_RUNTIME_ADDRESS_NONE); ZYAN_PUTS(""); PrintSectionHeader("DISASM"); - ZYAN_PRINTF(" %s%s%s\n", CVT100_OUT(ZYAN_VT100SGR_FG_BRIGHT_BLACK), &buffer[0], + PrintValueLabel("ABSOLUTE"); + ZYAN_PRINTF("%s%s%s\n", CVT100_OUT(ZYAN_VT100SGR_FG_BRIGHT_BLACK), &buffer_abs[0], + CVT100_OUT(COLOR_DEFAULT)); + PrintValueLabel("RELATIVE"); + ZYAN_PRINTF("%s%s%s\n", CVT100_OUT(ZYAN_VT100SGR_FG_BRIGHT_BLACK), &buffer_rel[0], CVT100_OUT(COLOR_DEFAULT)); }