Path: blob/master/dep/zydis/include/Zydis/Decoder.h
4216 views
/***************************************************************************************************12Zyan Disassembler Library (Zydis)34Original Author : Florian Bernd56* Permission is hereby granted, free of charge, to any person obtaining a copy7* of this software and associated documentation files (the "Software"), to deal8* in the Software without restriction, including without limitation the rights9* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10* copies of the Software, and to permit persons to whom the Software is11* furnished to do so, subject to the following conditions:12*13* The above copyright notice and this permission notice shall be included in all14* copies or substantial portions of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22* SOFTWARE.2324***************************************************************************************************/2526/**27* @file28* Functions for decoding instructions.29*/3031#ifndef ZYDIS_DECODER_H32#define ZYDIS_DECODER_H3334#include <Zycore/Types.h>35#include <Zycore/Defines.h>36#include <Zydis/DecoderTypes.h>37#include <Zydis/Status.h>3839#ifdef __cplusplus40extern "C" {41#endif4243/* ============================================================================================== */44/* Enums and types */45/* ============================================================================================== */4647/* ---------------------------------------------------------------------------------------------- */48/* Decoder mode */49/* ---------------------------------------------------------------------------------------------- */5051/**52* Defines the `ZydisDecoderMode` enum.53*/54typedef enum ZydisDecoderMode_55{56/**57* Enables minimal instruction decoding without semantic analysis.58*59* This mode provides access to the mnemonic, the instruction-length, the effective60* operand-size, the effective address-width, some attributes (e.g. `ZYDIS_ATTRIB_IS_RELATIVE`)61* and all of the information in the `raw` field of the `ZydisDecodedInstruction` struct.62*63* Operands, most attributes and other specific information (like `AVX` info) are not64* accessible in this mode.65*66* This mode is NOT enabled by default.67*/68ZYDIS_DECODER_MODE_MINIMAL,69/**70* Enables the `AMD`-branch mode.71*72* Intel ignores the operand-size override-prefix (`0x66`) for all branches with 32-bit73* immediates and forces the operand-size of the instruction to 64-bit in 64-bit mode.74* In `AMD`-branch mode `0x66` is not ignored and changes the operand-size and the size of the75* immediate to 16-bit.76*77* This mode is NOT enabled by default.78*/79ZYDIS_DECODER_MODE_AMD_BRANCHES,80/**81* Enables `KNC` compatibility-mode.82*83* `KNC` and `KNL+` chips are sharing opcodes and encodings for some mask-related instructions.84* Enable this mode to use the old `KNC` specifications (different mnemonics, operands, ..).85*86* This mode is NOT enabled by default.87*/88ZYDIS_DECODER_MODE_KNC,89/**90* Enables the `MPX` mode.91*92* The `MPX` isa-extension reuses (overrides) some of the widenop instruction opcodes.93*94* This mode is enabled by default.95*/96ZYDIS_DECODER_MODE_MPX,97/**98* Enables the `CET` mode.99*100* The `CET` isa-extension reuses (overrides) some of the widenop instruction opcodes.101*102* This mode is enabled by default.103*/104ZYDIS_DECODER_MODE_CET,105/**106* Enables the `LZCNT` mode.107*108* The `LZCNT` isa-extension reuses (overrides) some of the widenop instruction opcodes.109*110* This mode is enabled by default.111*/112ZYDIS_DECODER_MODE_LZCNT,113/**114* Enables the `TZCNT` mode.115*116* The `TZCNT` isa-extension reuses (overrides) some of the widenop instruction opcodes.117*118* This mode is enabled by default.119*/120ZYDIS_DECODER_MODE_TZCNT,121/**122* Enables the `WBNOINVD` mode.123*124* The `WBINVD` instruction is interpreted as `WBNOINVD` on ICL chips, if a `F3` prefix is125* used.126*127* This mode is disabled by default.128*/129ZYDIS_DECODER_MODE_WBNOINVD,130/**131* Enables the `CLDEMOTE` mode.132*133* The `CLDEMOTE` isa-extension reuses (overrides) some of the widenop instruction opcodes.134*135* This mode is enabled by default.136*/137ZYDIS_DECODER_MODE_CLDEMOTE,138139/**140* Maximum value of this enum.141*/142ZYDIS_DECODER_MODE_MAX_VALUE = ZYDIS_DECODER_MODE_CLDEMOTE,143/**144* The minimum number of bits required to represent all values of this enum.145*/146ZYDIS_DECODER_MODE_REQUIRED_BITS = ZYAN_BITS_TO_REPRESENT(ZYDIS_DECODER_MODE_MAX_VALUE)147} ZydisDecoderMode;148149/* ---------------------------------------------------------------------------------------------- */150/* Decoder struct */151/* ---------------------------------------------------------------------------------------------- */152153/**154* Defines the `ZydisDecoder` struct.155*156* All fields in this struct should be considered as "private". Any changes may lead to unexpected157* behavior.158*/159typedef struct ZydisDecoder_160{161/**162* The machine mode.163*/164ZydisMachineMode machine_mode;165/**166* The stack width.167*/168ZydisStackWidth stack_width;169/**170* The decoder mode array.171*/172ZyanBool decoder_mode[ZYDIS_DECODER_MODE_MAX_VALUE + 1];173} ZydisDecoder;174175/* ---------------------------------------------------------------------------------------------- */176177/* ============================================================================================== */178/* Exported functions */179/* ============================================================================================== */180181/**182* @addtogroup decoder Decoder183* Functions allowing decoding of instruction bytes to a machine interpretable struct.184* @{185*/186187/**188* Initializes the given `ZydisDecoder` instance.189*190* @param decoder A pointer to the `ZydisDecoder` instance.191* @param machine_mode The machine mode.192* @param stack_width The stack width.193*194* @return A zyan status code.195*/196ZYDIS_EXPORT ZyanStatus ZydisDecoderInit(ZydisDecoder* decoder, ZydisMachineMode machine_mode,197ZydisStackWidth stack_width);198199/**200* Enables or disables the specified decoder-mode.201*202* @param decoder A pointer to the `ZydisDecoder` instance.203* @param mode The decoder mode.204* @param enabled `ZYAN_TRUE` to enable, or `ZYAN_FALSE` to disable the specified decoder-mode.205*206* @return A zyan status code.207*/208ZYDIS_EXPORT ZyanStatus ZydisDecoderEnableMode(ZydisDecoder* decoder, ZydisDecoderMode mode,209ZyanBool enabled);210211/**212* Decodes the instruction in the given input `buffer` and returns all details (e.g. operands).213*214* @param decoder A pointer to the `ZydisDecoder` instance.215* @param buffer A pointer to the input buffer.216* @param length The length of the input buffer. Note that this can be bigger than the217* actual size of the instruction -- you don't have to know the size up218* front. This length is merely used to prevent Zydis from doing219* out-of-bounds reads on your buffer.220* @param instruction A pointer to the `ZydisDecodedInstruction` struct receiving the details221* about the decoded instruction.222* @param operands A pointer to an array with `ZYDIS_MAX_OPERAND_COUNT` entries that223* receives the decoded operands. The number of operands decoded is224* determined by the `instruction.operand_count` field. Excess entries are225* zeroed.226*227* This is a convenience function that combines the following functions into one call:228*229* - `ZydisDecoderDecodeInstruction`230* - `ZydisDecoderDecodeOperands`231*232* Please refer to `ZydisDecoderDecodeInstruction` if operand decoding is not required or should233* be done separately (`ZydisDecoderDecodeOperands`).234*235* This function is not available in MINIMAL_MODE.236*237* @return A zyan status code.238*/239ZYDIS_EXPORT ZyanStatus ZydisDecoderDecodeFull(const ZydisDecoder* decoder,240const void* buffer, ZyanUSize length, ZydisDecodedInstruction* instruction,241ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT]);242243/**244* Decodes the instruction in the given input `buffer`.245*246* @param decoder A pointer to the `ZydisDecoder` instance.247* @param context A pointer to a decoder context struct which is required for further248* decoding (e.g. operand decoding using `ZydisDecoderDecodeOperands`) or249* `ZYAN_NULL` if not needed.250* @param buffer A pointer to the input buffer.251* @param length The length of the input buffer. Note that this can be bigger than the252* actual size of the instruction -- you don't have to know the size up253* front. This length is merely used to prevent Zydis from doing254* out-of-bounds reads on your buffer.255* @param instruction A pointer to the `ZydisDecodedInstruction` struct, that receives the256* details about the decoded instruction.257*258* @return A zyan status code.259*/260ZYDIS_EXPORT ZyanStatus ZydisDecoderDecodeInstruction(const ZydisDecoder* decoder,261ZydisDecoderContext* context, const void* buffer, ZyanUSize length,262ZydisDecodedInstruction* instruction);263264/**265* Decodes the instruction operands.266*267* @param decoder A pointer to the `ZydisDecoder` instance.268* @param context A pointer to the `ZydisDecoderContext` struct.269* @param instruction A pointer to the `ZydisDecodedInstruction` struct.270* @param operands The array that receives the decoded operands.271* Refer to `ZYDIS_MAX_OPERAND_COUNT` or `ZYDIS_MAX_OPERAND_COUNT_VISIBLE`272* when allocating space for the array to ensure that the buffer size is273* sufficient to always fit all instruction operands.274* Refer to `instruction.operand_count` or275* `instruction.operand_count_visible' when allocating space for the array276* to ensure that the buffer size is sufficient to fit all operands of277* the given instruction.278* @param operand_count The length of the `operands` array.279* This argument as well limits the maximum amount of operands to decode.280* If this value is `0`, no operands will be decoded and `ZYAN_NULL` will281* be accepted for the `operands` argument.282*283* This function fails, if `operand_count` is larger than the total number of operands for the284* given instruction (`instruction.operand_count`).285*286* This function is not available in MINIMAL_MODE.287*288* @return A zyan status code.289*/290ZYDIS_EXPORT ZyanStatus ZydisDecoderDecodeOperands(const ZydisDecoder* decoder,291const ZydisDecoderContext* context, const ZydisDecodedInstruction* instruction,292ZydisDecodedOperand* operands, ZyanU8 operand_count);293294/** @} */295296/* ============================================================================================== */297298#ifdef __cplusplus299}300#endif301302#endif /* ZYDIS_DECODER_H */303304305