Zydis  v4.1.0
Encoder.h
Go to the documentation of this file.
1 /***************************************************************************************************
2 
3  Zyan Disassembler Library (Zydis)
4 
5  Original Author : Mappa
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
32 #ifndef ZYDIS_ENCODER_H
33 #define ZYDIS_ENCODER_H
34 
35 #include <Zycore/Types.h>
36 #include <Zydis/MetaInfo.h>
37 #include <Zydis/Register.h>
38 #include <Zydis/DecoderTypes.h>
39 #include <Zydis/Mnemonic.h>
40 #include <Zydis/Status.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* ============================================================================================== */
47 /* Macros */
48 /* ============================================================================================== */
49 
50 /* ---------------------------------------------------------------------------------------------- */
51 /* Constants */
52 /* ---------------------------------------------------------------------------------------------- */
53 
57 #define ZYDIS_ENCODER_MAX_OPERANDS 5
58 
59 // If asserts are failing here remember to update encoder table generator before fixing asserts
60 ZYAN_STATIC_ASSERT(ZYAN_BITS_TO_REPRESENT(ZYDIS_ENCODER_MAX_OPERANDS) == 3);
61 
65 #define ZYDIS_ENCODABLE_PREFIXES (ZYDIS_ATTRIB_HAS_LOCK | \
66  ZYDIS_ATTRIB_HAS_REP | \
67  ZYDIS_ATTRIB_HAS_REPE | \
68  ZYDIS_ATTRIB_HAS_REPNE | \
69  ZYDIS_ATTRIB_HAS_BND | \
70  ZYDIS_ATTRIB_HAS_XACQUIRE | \
71  ZYDIS_ATTRIB_HAS_XRELEASE | \
72  ZYDIS_ATTRIB_HAS_BRANCH_NOT_TAKEN | \
73  ZYDIS_ATTRIB_HAS_BRANCH_TAKEN | \
74  ZYDIS_ATTRIB_HAS_NOTRACK | \
75  ZYDIS_ATTRIB_HAS_SEGMENT_CS | \
76  ZYDIS_ATTRIB_HAS_SEGMENT_SS | \
77  ZYDIS_ATTRIB_HAS_SEGMENT_DS | \
78  ZYDIS_ATTRIB_HAS_SEGMENT_ES | \
79  ZYDIS_ATTRIB_HAS_SEGMENT_FS | \
80  ZYDIS_ATTRIB_HAS_SEGMENT_GS)
81 
82 /* ---------------------------------------------------------------------------------------------- */
83 
84 /* ============================================================================================== */
85 /* Enums and types */
86 /* ============================================================================================== */
87 
93 {
101 
111  ZYAN_BITS_TO_REPRESENT(ZYDIS_ENCODABLE_ENCODING_MAX_VALUE)
113 
118 typedef enum ZydisBranchWidth_
119 {
125 
135 
141 {
146 
155  ZYAN_BITS_TO_REPRESENT(ZYDIS_ADDRESS_SIZE_HINT_MAX_VALUE)
157 
163 {
169 
178  ZYAN_BITS_TO_REPRESENT(ZYDIS_OPERAND_SIZE_HINT_MAX_VALUE)
180 
184 typedef struct ZydisEncoderOperand_
185 {
194  {
204  ZyanBool is4;
205  } reg;
210  {
222  ZyanU8 scale;
230  ZyanI64 displacement;
234  ZyanU16 size;
235  } mem;
240  {
244  ZyanU16 segment;
248  ZyanU32 offset;
249  } ptr;
254  {
258  ZyanU64 u;
262  ZyanI64 s;
263  } imm;
265 
269 typedef struct ZydisEncoderRequest_
270 {
337  {
351  ZyanBool sae;
356  ZyanBool zeroing_mask;
357  } evex;
362  {
383  ZyanBool sae;
387  ZyanBool eviction_hint;
388  } mvex;
390 
391 /* ============================================================================================== */
392 /* Exported functions */
393 /* ============================================================================================== */
394 
412  void *buffer, ZyanUSize *length);
413 
430  void *buffer, ZyanUSize *length, ZyanU64 runtime_address);
431 
450  const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operands,
451  ZyanU8 operand_count, ZydisEncoderRequest* request);
452 
461 ZYDIS_EXPORT ZyanStatus ZydisEncoderNopFill(void *buffer, ZyanUSize length);
462 
465 /* ============================================================================================== */
466 
467 #ifdef __cplusplus
468 }
469 #endif
470 
471 #endif /* ZYDIS_ENCODER_H */
Defines the basic ZydisDecodedInstruction and ZydisDecodedOperand structs.
enum ZydisConversionMode_ ZydisConversionMode
Defines the ZydisConversionMode enum.
enum ZydisRoundingMode_ ZydisRoundingMode
Defines the ZydisRoundingMode enum.
enum ZydisSwizzleMode_ ZydisSwizzleMode
Defines the ZydisSwizzleMode enum.
enum ZydisBroadcastMode_ ZydisBroadcastMode
Defines the ZydisBroadcastMode enum.
enum ZydisBranchType_ ZydisBranchType
Defines the ZydisBranchType enum.
#define ZYDIS_EXPORT
Symbol is exported in shared library builds.
Definition: Defines.h:67
#define ZYDIS_ENCODER_MAX_OPERANDS
Maximum number of encodable (explicit and implicit) operands.
Definition: Encoder.h:57
enum ZydisAddressSizeHint_ ZydisAddressSizeHint
Defines possible values for address size hints.
enum ZydisBranchWidth_ ZydisBranchWidth
Defines encodable physical/effective sizes of relative immediate operands.
ZydisAddressSizeHint_
Defines possible values for address size hints.
Definition: Encoder.h:141
@ ZYDIS_ADDRESS_SIZE_HINT_REQUIRED_BITS
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:154
@ ZYDIS_ADDRESS_SIZE_HINT_NONE
Definition: Encoder.h:142
@ ZYDIS_ADDRESS_SIZE_HINT_16
Definition: Encoder.h:143
@ ZYDIS_ADDRESS_SIZE_HINT_MAX_VALUE
Maximum value of this enum.
Definition: Encoder.h:150
@ ZYDIS_ADDRESS_SIZE_HINT_32
Definition: Encoder.h:144
@ ZYDIS_ADDRESS_SIZE_HINT_64
Definition: Encoder.h:145
struct ZydisEncoderOperand_ ZydisEncoderOperand
Describes explicit or implicit instruction operand.
ZYAN_STATIC_ASSERT(ZYAN_BITS_TO_REPRESENT(ZYDIS_ENCODER_MAX_OPERANDS)==3)
ZydisOperandSizeHint_
Defines possible values for operand size hints.
Definition: Encoder.h:163
@ ZYDIS_OPERAND_SIZE_HINT_16
Definition: Encoder.h:166
@ ZYDIS_OPERAND_SIZE_HINT_32
Definition: Encoder.h:167
@ ZYDIS_OPERAND_SIZE_HINT_REQUIRED_BITS
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:177
@ ZYDIS_OPERAND_SIZE_HINT_NONE
Definition: Encoder.h:164
@ ZYDIS_OPERAND_SIZE_HINT_8
Definition: Encoder.h:165
@ ZYDIS_OPERAND_SIZE_HINT_MAX_VALUE
Maximum value of this enum.
Definition: Encoder.h:173
@ ZYDIS_OPERAND_SIZE_HINT_64
Definition: Encoder.h:168
enum ZydisEncodableEncoding_ ZydisEncodableEncoding
Defines possible physical instruction encodings as bit flags, so multiple acceptable encodings can be...
enum ZydisOperandSizeHint_ ZydisOperandSizeHint
Defines possible values for operand size hints.
ZydisBranchWidth_
Defines encodable physical/effective sizes of relative immediate operands.
Definition: Encoder.h:119
@ ZYDIS_BRANCH_WIDTH_NONE
Definition: Encoder.h:120
@ ZYDIS_BRANCH_WIDTH_MAX_VALUE
Maximum value of this enum.
Definition: Encoder.h:129
@ ZYDIS_BRANCH_WIDTH_8
Definition: Encoder.h:121
@ ZYDIS_BRANCH_WIDTH_32
Definition: Encoder.h:123
@ ZYDIS_BRANCH_WIDTH_REQUIRED_BITS
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:133
@ ZYDIS_BRANCH_WIDTH_16
Definition: Encoder.h:122
@ ZYDIS_BRANCH_WIDTH_64
Definition: Encoder.h:124
ZydisEncodableEncoding_
Defines possible physical instruction encodings as bit flags, so multiple acceptable encodings can be...
Definition: Encoder.h:93
@ ZYDIS_ENCODABLE_ENCODING_REQUIRED_BITS
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:110
@ ZYDIS_ENCODABLE_ENCODING_MAX_VALUE
Maximum value of this enum.
Definition: Encoder.h:105
@ ZYDIS_ENCODABLE_ENCODING_DEFAULT
Definition: Encoder.h:94
@ ZYDIS_ENCODABLE_ENCODING_VEX
Definition: Encoder.h:98
@ ZYDIS_ENCODABLE_ENCODING_MVEX
Definition: Encoder.h:100
@ ZYDIS_ENCODABLE_ENCODING_EVEX
Definition: Encoder.h:99
@ ZYDIS_ENCODABLE_ENCODING_XOP
Definition: Encoder.h:97
@ ZYDIS_ENCODABLE_ENCODING_LEGACY
Definition: Encoder.h:95
@ ZYDIS_ENCODABLE_ENCODING_3DNOW
Definition: Encoder.h:96
struct ZydisEncoderRequest_ ZydisEncoderRequest
Main structure consumed by the encoder.
enum ZydisMnemonic_ ZydisMnemonic
Defines the ZydisMnemonic enum.
enum ZydisRegister_ ZydisRegister
Defines the ZydisRegister enum.
Mnemonic constant definitions and helper functions.
Utility functions and constants for registers.
enum ZydisOperandType_ ZydisOperandType
Defines the ZydisOperandType enum.
enum ZydisMachineMode_ ZydisMachineMode
Defines the ZydisMachineMode enum.
Status code definitions and check macros.
ZYDIS_EXPORT ZyanStatus ZydisEncoderDecodedInstructionToEncoderRequest(const ZydisDecodedInstruction *instruction, const ZydisDecodedOperand *operands, ZyanU8 operand_count, ZydisEncoderRequest *request)
Converts decoded instruction to encoder request that can be passed to ZydisEncoderEncodeInstruction.
ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstruction(const ZydisEncoderRequest *request, void *buffer, ZyanUSize *length)
Encodes instruction with semantics specified in encoder request structure.
ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstructionAbsolute(ZydisEncoderRequest *request, void *buffer, ZyanUSize *length, ZyanU64 runtime_address)
Encodes instruction with semantics specified in encoder request structure.
ZYDIS_EXPORT ZyanStatus ZydisEncoderNopFill(void *buffer, ZyanUSize length)
Fills provided buffer with NOP instructions using longest possible multi-byte instructions.
ZyanU64 ZydisInstructionAttributes
Defines the ZydisInstructionAttributes data-type.
Definition: SharedTypes.h:501
Information about a decoded instruction.
Definition: DecoderTypes.h:1238
Defines the ZydisDecodedOperand struct.
Definition: DecoderTypes.h:200
Extended info for memory-operands.
Definition: Encoder.h:210
ZyanU16 size
Size of this operand in bytes.
Definition: Encoder.h:234
ZyanI64 displacement
The displacement value.
Definition: Encoder.h:230
ZydisRegister base
The base register.
Definition: Encoder.h:214
ZyanU8 scale
The scale factor.
Definition: Encoder.h:222
ZydisRegister index
The index register.
Definition: Encoder.h:218
Extended info for pointer-operands.
Definition: Encoder.h:240
ZyanU32 offset
The offset value.
Definition: Encoder.h:248
ZyanU16 segment
The segment value.
Definition: Encoder.h:244
Extended info for register-operands.
Definition: Encoder.h:194
ZyanBool is4
Is this 4th operand (VEX/XOP).
Definition: Encoder.h:204
ZydisRegister value
The register value.
Definition: Encoder.h:198
Describes explicit or implicit instruction operand.
Definition: Encoder.h:185
struct ZydisEncoderOperand_::ZydisEncoderOperandPtr_ ptr
struct ZydisEncoderOperand_::ZydisEncoderOperandReg_ reg
union ZydisEncoderOperand_::ZydisEncoderOperandImm_ imm
struct ZydisEncoderOperand_::ZydisEncoderOperandMem_ mem
ZydisOperandType type
The type of the operand.
Definition: Encoder.h:189
Extended info for EVEX instructions.
Definition: Encoder.h:337
ZydisRoundingMode rounding
The rounding-mode.
Definition: Encoder.h:346
ZyanBool zeroing_mask
Signals, if the zeroing-mask functionality should be enabled for the instruction.
Definition: Encoder.h:356
ZyanBool sae
Signals, if the SAE (suppress-all-exceptions) functionality should be enabled for the instruction.
Definition: Encoder.h:351
ZydisBroadcastMode broadcast
The broadcast-mode.
Definition: Encoder.h:342
Extended info for MVEX instructions.
Definition: Encoder.h:362
ZydisRoundingMode rounding
The rounding-mode.
Definition: Encoder.h:374
ZydisConversionMode conversion
The data-conversion mode.
Definition: Encoder.h:370
ZyanBool eviction_hint
Signals, if the instruction has a memory-eviction-hint (KNC only).
Definition: Encoder.h:387
ZyanBool sae
Signals, if the SAE (suppress-all-exceptions) functionality is enabled for the instruction.
Definition: Encoder.h:383
ZydisSwizzleMode swizzle
The AVX register-swizzle mode.
Definition: Encoder.h:378
ZydisBroadcastMode broadcast
The broadcast-mode.
Definition: Encoder.h:366
Main structure consumed by the encoder.
Definition: Encoder.h:270
ZydisBranchType branch_type
Branch type (required for branching instructions only).
Definition: Encoder.h:296
ZydisMnemonic mnemonic
The instruction-mnemonic.
Definition: Encoder.h:285
ZydisOperandSizeHint operand_size_hint
Optional operand size hint used to resolve ambiguities for some instructions.
Definition: Encoder.h:324
ZydisInstructionAttributes prefixes
A combination of requested encodable prefixes (ZYDIS_ATTRIB_HAS_* flags) for desired instruction.
Definition: Encoder.h:290
ZydisEncodableEncoding allowed_encodings
This optional field can be used to restrict allowed physical encodings for desired instruction.
Definition: Encoder.h:281
ZydisMachineMode machine_mode
The machine mode used to encode this instruction.
Definition: Encoder.h:274
struct ZydisEncoderRequest_::ZydisEncoderRequestEvexFeatures_ evex
struct ZydisEncoderRequest_::ZydisEncoderRequestMvexFeatures_ mvex
ZydisEncoderOperand operands[ZYDIS_ENCODER_MAX_OPERANDS]
Detailed info for all explicit and implicit instruction operands.
Definition: Encoder.h:332
ZyanU8 operand_count
The number of instruction-operands.
Definition: Encoder.h:328
ZydisBranchWidth branch_width
Specifies physical size for relative immediate operands.
Definition: Encoder.h:303
ZydisAddressSizeHint address_size_hint
Optional address size hint used to resolve ambiguities for some instructions.
Definition: Encoder.h:317
Extended info for immediate-operands.
Definition: Encoder.h:254
ZyanU64 u
The unsigned immediate value.
Definition: Encoder.h:258
ZyanI64 s
The signed immediate value.
Definition: Encoder.h:262