Zydis  v4.0.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 {
94  ZYDIS_ENCODABLE_ENCODING_DEFAULT = 0x00000000,
95  ZYDIS_ENCODABLE_ENCODING_LEGACY = 0x00000001,
96  ZYDIS_ENCODABLE_ENCODING_3DNOW = 0x00000002,
97  ZYDIS_ENCODABLE_ENCODING_XOP = 0x00000004,
98  ZYDIS_ENCODABLE_ENCODING_VEX = 0x00000008,
99  ZYDIS_ENCODABLE_ENCODING_EVEX = 0x00000010,
100  ZYDIS_ENCODABLE_ENCODING_MVEX = 0x00000020,
101 
105  ZYDIS_ENCODABLE_ENCODING_MAX_VALUE = (ZYDIS_ENCODABLE_ENCODING_MVEX |
106  (ZYDIS_ENCODABLE_ENCODING_MVEX - 1)),
111  ZYAN_BITS_TO_REPRESENT(ZYDIS_ENCODABLE_ENCODING_MAX_VALUE)
113 
118 typedef enum ZydisBranchWidth_
119 {
120  ZYDIS_BRANCH_WIDTH_NONE,
121  ZYDIS_BRANCH_WIDTH_8,
122  ZYDIS_BRANCH_WIDTH_16,
123  ZYDIS_BRANCH_WIDTH_32,
124  ZYDIS_BRANCH_WIDTH_64,
125 
129  ZYDIS_BRANCH_WIDTH_MAX_VALUE = ZYDIS_BRANCH_WIDTH_64,
135 
141 {
142  ZYDIS_ADDRESS_SIZE_HINT_NONE,
143  ZYDIS_ADDRESS_SIZE_HINT_16,
144  ZYDIS_ADDRESS_SIZE_HINT_32,
145  ZYDIS_ADDRESS_SIZE_HINT_64,
146 
150  ZYDIS_ADDRESS_SIZE_HINT_MAX_VALUE = ZYDIS_ADDRESS_SIZE_HINT_64,
155  ZYAN_BITS_TO_REPRESENT(ZYDIS_ADDRESS_SIZE_HINT_MAX_VALUE)
157 
163 {
164  ZYDIS_OPERAND_SIZE_HINT_NONE,
165  ZYDIS_OPERAND_SIZE_HINT_8,
166  ZYDIS_OPERAND_SIZE_HINT_16,
167  ZYDIS_OPERAND_SIZE_HINT_32,
168  ZYDIS_OPERAND_SIZE_HINT_64,
169 
173  ZYDIS_OPERAND_SIZE_HINT_MAX_VALUE = ZYDIS_OPERAND_SIZE_HINT_64,
178  ZYAN_BITS_TO_REPRESENT(ZYDIS_OPERAND_SIZE_HINT_MAX_VALUE)
180 
184 typedef struct ZydisEncoderOperand_
185 {
194  {
198  ZydisRegister value;
204  ZyanBool is4;
205  } reg;
210  {
214  ZydisRegister base;
218  ZydisRegister index;
222  ZyanU8 scale;
226  ZyanI64 displacement;
230  ZyanU16 size;
231  } mem;
236  {
240  ZyanU16 segment;
244  ZyanU32 offset;
245  } ptr;
250  {
254  ZyanU64 u;
258  ZyanI64 s;
259  } imm;
261 
265 typedef struct ZydisEncoderRequest_
266 {
281  ZydisMnemonic mnemonic;
326  {
340  ZyanBool sae;
345  ZyanBool zeroing_mask;
346  } evex;
351  {
372  ZyanBool sae;
376  ZyanBool eviction_hint;
377  } mvex;
379 
380 /* ============================================================================================== */
381 /* Exported functions */
382 /* ============================================================================================== */
383 
401  void *buffer, ZyanUSize *length);
402 
419  void *buffer, ZyanUSize *length, ZyanU64 runtime_address);
420 
439  const ZydisDecodedInstruction* instruction, const ZydisDecodedOperand* operands,
440  ZyanU8 operand_count, ZydisEncoderRequest* request);
441 
450 ZYDIS_EXPORT ZyanStatus ZydisEncoderNopFill(void *buffer, ZyanUSize length);
451 
454 /* ============================================================================================== */
455 
456 #ifdef __cplusplus
457 }
458 #endif
459 
460 #endif /* ZYDIS_ENCODER_H */
ZydisRoundingMode rounding
The rounding-mode.
Definition: Encoder.h:363
enum ZydisMachineMode_ ZydisMachineMode
Defines the ZydisMachineMode enum.
Extended info for register-operands.
Definition: Encoder.h:193
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:110
enum ZydisRoundingMode_ ZydisRoundingMode
Defines the ZydisRoundingMode enum.
Information about a decoded instruction.
Definition: DecoderTypes.h:1237
ZydisEncodableEncoding_
Defines possible physical instruction encodings as bit flags, so multiple acceptable encodings can be...
Definition: Encoder.h:92
enum ZydisOperandSizeHint_ ZydisOperandSizeHint
Defines possible values for operand size hints.
ZydisAddressSizeHint_
Defines possible values for address size hints.
Definition: Encoder.h:140
ZyanU16 size
Size of this operand in bytes.
Definition: Encoder.h:230
ZyanU64 u
The unsigned immediate value.
Definition: Encoder.h:254
ZydisEncoderOperand operands[ZYDIS_ENCODER_MAX_OPERANDS]
Detailed info for all explicit and implicit instruction operands.
Definition: Encoder.h:321
Describes explicit or implicit instruction operand.
Definition: Encoder.h:184
enum ZydisBranchType_ ZydisBranchType
Defines the ZydisBranchType enum.
ZyanBool sae
Signals, if the SAE (suppress-all-exceptions) functionality is enabled for the instruction.
Definition: Encoder.h:372
ZydisRegister index
The index register.
Definition: Encoder.h:218
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.
ZyanI64 s
The signed immediate value.
Definition: Encoder.h:258
ZydisOperandSizeHint operand_size_hint
Optional operand size hint used to resolve ambiguities for some instructions.
Definition: Encoder.h:313
Extended info for immediate-operands.
Definition: Encoder.h:249
ZyanBool sae
Signals, if the SAE (suppress-all-exceptions) functionality should be enabled for the instruction...
Definition: Encoder.h:340
Utility functions and constants for registers.
ZydisBranchType branch_type
Branch type (required for branching instructions only).
Definition: Encoder.h:292
ZyanU16 segment
The segment value.
Definition: Encoder.h:240
enum ZydisBroadcastMode_ ZydisBroadcastMode
Defines the ZydisBroadcastMode enum.
ZyanBool zeroing_mask
Signals, if the zeroing-mask functionality should be enabled for the instruction. ...
Definition: Encoder.h:345
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:133
Extended info for EVEX instructions.
Definition: Encoder.h:325
ZYDIS_EXPORT ZyanStatus ZydisEncoderNopFill(void *buffer, ZyanUSize length)
Fills provided buffer with NOP instructions using longest possible multi-byte instructions.
ZydisOperandSizeHint_
Defines possible values for operand size hints.
Definition: Encoder.h:162
#define ZYDIS_EXPORT
Symbol is exported in shared library builds.
Definition: Defines.h:67
ZydisRoundingMode rounding
The rounding-mode.
Definition: Encoder.h:335
struct ZydisEncoderRequest_ ZydisEncoderRequest
Main structure consumed by the encoder.
#define ZYDIS_ENCODER_MAX_OPERANDS
Maximum number of encodable (explicit and implicit) operands.
Definition: Encoder.h:57
Extended info for pointer-operands.
Definition: Encoder.h:235
ZyanU8 scale
The scale factor.
Definition: Encoder.h:222
ZydisRegister base
The base register.
Definition: Encoder.h:214
ZydisBroadcastMode broadcast
The broadcast-mode.
Definition: Encoder.h:331
enum ZydisEncodableEncoding_ ZydisEncodableEncoding
Defines possible physical instruction encodings as bit flags, so multiple acceptable encodings can be...
ZydisAddressSizeHint address_size_hint
Optional address size hint used to resolve ambiguities for some instructions.
Definition: Encoder.h:306
ZydisEncodableEncoding allowed_encodings
This optional field can be used to restrict allowed physical encodings for desired instruction...
Definition: Encoder.h:277
enum ZydisAddressSizeHint_ ZydisAddressSizeHint
Defines possible values for address size hints.
ZyanU32 offset
The offset value.
Definition: Encoder.h:244
ZyanU8 operand_count
The number of instruction-operands.
Definition: Encoder.h:317
Maximum value of this enum.
Definition: Encoder.h:173
ZydisInstructionAttributes prefixes
A combination of requested encodable prefixes (ZYDIS_ATTRIB_HAS_* flags) for desired instruction...
Definition: Encoder.h:286
ZydisMachineMode machine_mode
The machine mode used to encode this instruction.
Definition: Encoder.h:270
struct ZydisEncoderOperand_ ZydisEncoderOperand
Describes explicit or implicit instruction operand.
ZydisRegister value
The register value.
Definition: Encoder.h:198
ZydisBroadcastMode broadcast
The broadcast-mode.
Definition: Encoder.h:355
ZydisSwizzleMode swizzle
The AVX register-swizzle mode.
Definition: Encoder.h:367
Maximum value of this enum.
Definition: Encoder.h:150
ZydisConversionMode conversion
The data-conversion mode.
Definition: Encoder.h:359
Extended info for MVEX instructions.
Definition: Encoder.h:350
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:154
ZydisMnemonic mnemonic
The instruction-mnemonic.
Definition: Encoder.h:281
Maximum value of this enum.
Definition: Encoder.h:105
enum ZydisConversionMode_ ZydisConversionMode
Defines the ZydisConversionMode enum.
ZydisBranchWidth_
Defines encodable physical/effective sizes of relative immediate operands.
Definition: Encoder.h:118
ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstructionAbsolute(ZydisEncoderRequest *request, void *buffer, ZyanUSize *length, ZyanU64 runtime_address)
Encodes instruction with semantics specified in encoder request structure.
ZydisBranchWidth branch_width
Specifies physical size for relative immediate operands.
Definition: Encoder.h:299
ZyanBool eviction_hint
Signals, if the instruction has a memory-eviction-hint (KNC only).
Definition: Encoder.h:376
ZydisOperandType type
The type of the operand.
Definition: Encoder.h:189
enum ZydisSwizzleMode_ ZydisSwizzleMode
Defines the ZydisSwizzleMode enum.
ZyanBool is4
Is this 4th operand (VEX/XOP).
Definition: Encoder.h:204
Defines the basic ZydisDecodedInstruction and ZydisDecodedOperand structs.
enum ZydisBranchWidth_ ZydisBranchWidth
Defines encodable physical/effective sizes of relative immediate operands.
The minimum number of bits required to represent all values of this enum.
Definition: Encoder.h:177
ZYDIS_EXPORT ZyanStatus ZydisEncoderEncodeInstruction(const ZydisEncoderRequest *request, void *buffer, ZyanUSize *length)
Encodes instruction with semantics specified in encoder request structure.
Defines the ZydisDecodedOperand struct.
Definition: DecoderTypes.h:199
ZyanI64 displacement
The displacement value.
Definition: Encoder.h:226
Mnemonic constant definitions and helper functions.
Status code definitions and check macros.
ZyanU64 ZydisInstructionAttributes
Defines the ZydisInstructionAttributes data-type.
Definition: SharedTypes.h:497
Main structure consumed by the encoder.
Definition: Encoder.h:265
enum ZydisOperandType_ ZydisOperandType
Defines the ZydisOperandType enum.
Extended info for memory-operands.
Definition: Encoder.h:209
Maximum value of this enum.
Definition: Encoder.h:129