Skip to content

Commit

Permalink
Minor refactorings and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd committed Sep 4, 2018
1 parent 538a308 commit b75bc7c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
4 changes: 2 additions & 2 deletions include/Zydis/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
/**
Expand Down
52 changes: 50 additions & 2 deletions src/Formatter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
14 changes: 11 additions & 3 deletions tools/ZydisInfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit b75bc7c

Please sign in to comment.