Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatter options to control SCALE=1 and DISP=0 behavior #476

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/Zydis/DecoderTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ typedef struct ZydisDecodedOperandMem_
/**
* The scale factor.
*/
ZyanU8 scale;
ZyanU8 scale; // TODO: This is current 0 if no SIB byte is present. Should we use 1 as the default?
/**
* Extended info for memory-operands with displacement.
*/
Expand Down
18 changes: 13 additions & 5 deletions include/Zydis/Formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,19 @@ typedef enum ZydisFormatterProperty_
/**
* Controls the printing of the scale-factor component for memory operands.
*
* Pass `ZYAN_TRUE` as value to force the formatter to always print the scale-factor component
* of memory operands or `ZYAN_FALSE` to omit the scale factor for values of `1`.
* Pass `ZYDIS_OPTION_NEVER` to never print the scale-factor 1, `ZYDIS_OPTION_ALWAYS` to always
* print the scale-factor 1 or `ZYDIS_OPTION_AUTO` to print the scale-factor 1 only if it's
* present in the physical instruction encoding (if the instruction has a `SIB` byte).
*/
ZYDIS_FORMATTER_PROP_FORCE_SCALE_ONE,
ZYDIS_FORMATTER_PROP_SCALE_ONE,
/**
* Controls the printing of the displacement component for memory operands.
*
* Pass `ZYDIS_OPTION_NEVER` to never print the displacement 0, `ZYDIS_OPTION_ALWAYS` to always
* print the displacement 0 or `ZYDIS_OPTION_AUTO` to print the displacement 0 only if it's
* present in the physical instruction encoding.
*/
ZYDIS_FORMATTER_PROP_DISP_ZERO,
/**
* Controls the printing of branch addresses.
*
Expand Down Expand Up @@ -169,8 +178,7 @@ typedef enum ZydisFormatterProperty_
*/
ZYDIS_FORMATTER_PROP_ADDR_BASE,
/**
* Controls the signedness of relative addresses. Absolute addresses are
* always unsigned.
* Controls the signedness of relative addresses. Absolute addresses are always unsigned.
*/
ZYDIS_FORMATTER_PROP_ADDR_SIGNEDNESS,
/**
Expand Down
7 changes: 5 additions & 2 deletions src/FormatterATT.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,12 @@ ZyanStatus ZydisFormatterATTFormatOperandMEM(const ZydisFormatter* formatter,
if (neither_reg_nor_idx)
{
ZYAN_CHECK(formatter->func_print_address_abs(formatter, buffer, context));
} else if (context->operand->mem.disp.has_displacement && context->operand->mem.disp.value)
} else if (context->operand->mem.disp.has_displacement /* && context->operand->mem.disp.value */)
{
// TODO: Add formatter option to control printing of scale1/disp0 with options
// - conditional (default) = print scale1 if SIB is present in physical encoding, print disp0 if DISP is present in physical encoding
// - always
// - never
ZYAN_CHECK(formatter->func_print_disp(formatter, buffer, context));
}

Expand All @@ -252,7 +256,6 @@ ZyanStatus ZydisFormatterATTFormatOperandMEM(const ZydisFormatter* formatter,
(context->operand->mem.type != ZYDIS_MEMOP_TYPE_MIB) &&
((context->operand->mem.scale > 1) || formatter->force_memory_scale))
{
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_DELIMITER);
ZYDIS_BUFFER_APPEND(buffer, DELIM_MEMORY);
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_IMMEDIATE);
ZYAN_CHECK(ZydisStringAppendDecU(&buffer->string, context->operand->mem.scale, 0,
Expand Down
6 changes: 5 additions & 1 deletion src/FormatterIntel.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,12 @@ ZyanStatus ZydisFormatterIntelFormatOperandMEM(const ZydisFormatter* formatter,
if (neither_reg_nor_idx)
{
ZYAN_CHECK(formatter->func_print_address_abs(formatter, buffer, context));
} else if (context->operand->mem.disp.has_displacement && context->operand->mem.disp.value)
} else if (context->operand->mem.disp.has_displacement /* && context->operand->mem.disp.value */)
{
// TODO: Add formatter option to control printing of scale1/disp0 with options
// - conditional (default) = print scale1 if SIB is present in physical encoding, print disp0 if DISP is present in physical encoding
// - always
// - never
ZYAN_CHECK(formatter->func_print_disp(formatter, buffer, context));
}
}
Expand Down
Loading