X86 Encoder Decoder
Patching instructions

Patching decoded instructions

XED_DLL_EXPORT xed_bool_t xed_patch_disp (xed_decoded_inst_t *xedd, xed_uint8_t *itext, xed_enc_displacement_t disp)
 Replace a memory displacement. More...
 
XED_DLL_EXPORT xed_bool_t xed_patch_relbr (xed_decoded_inst_t *xedd, xed_uint8_t *itext, xed_encoder_operand_t disp)
 Replace a branch displacement. More...
 
XED_DLL_EXPORT xed_bool_t xed_patch_imm0 (xed_decoded_inst_t *xedd, xed_uint8_t *itext, xed_encoder_operand_t imm0)
 Replace an imm0 immediate value. More...
 

Detailed Description

These functions are useful for JITs and other uses where one must modify certain fields of instructions after encoding. To modify an instruction, one must encode it (creating an itext array of bytes) and then decode it (so that the patching routines know where the various fields are located.). Once the itext and the decoded instruction are available, certain fields can be modified.

The decode step required to create patchable instructions obviously takes additional time so it is suggested one only create patchable instructions once as templates and re-use them as needed.

See examples/xed-ex9-patch.c for an example.

Function Documentation

◆ xed_patch_disp()

XED_DLL_EXPORT xed_bool_t xed_patch_disp ( xed_decoded_inst_t xedd,
xed_uint8_t *  itext,
xed_enc_displacement_t  disp 
)

Replace a memory displacement.

The widths of original displacement and replacement must match.

Parameters
xeddA decoded instruction.
itextThe corresponding encoder output, byte array.
dispA xed_enc_displacement_t object describing the new displacement.
Returns
xed_bool_t 1=success, 0=failure

◆ xed_patch_imm0()

XED_DLL_EXPORT xed_bool_t xed_patch_imm0 ( xed_decoded_inst_t xedd,
xed_uint8_t *  itext,
xed_encoder_operand_t  imm0 
)

Replace an imm0 immediate value.

The widths of original immediate and replacement must match.

Parameters
xeddA decoded instruction.
itextThe corresponding encoder output, byte array.
imm0A xed_encoder_operand_t object describing the new immediate.
Returns
xed_bool_t 1=success, 0=failure

◆ xed_patch_relbr()

XED_DLL_EXPORT xed_bool_t xed_patch_relbr ( xed_decoded_inst_t xedd,
xed_uint8_t *  itext,
xed_encoder_operand_t  disp 
)

Replace a branch displacement.

The widths of original displacement and replacement must match.

Parameters
xeddA decoded instruction.
itextThe corresponding encoder output, byte array.
dispA xed_encoder_operand_t object describing the new displacement.
Returns
xed_bool_t 1=success, 0=failure