2015-05-16 11:05:17 +08:00
|
|
|
/***************************************************************************************************
|
2014-10-30 08:00:05 +08:00
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
Zyan Disassembler Engine (Zydis)
|
2014-10-30 08:00:05 +08:00
|
|
|
|
|
|
|
Original Author : Florian Bernd
|
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2015-05-16 11:05:17 +08:00
|
|
|
*
|
2014-10-30 08:00:05 +08:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2015-05-16 11:05:17 +08:00
|
|
|
*
|
2014-10-30 08:00:05 +08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
|
2015-05-16 11:05:17 +08:00
|
|
|
***************************************************************************************************/
|
|
|
|
|
2016-09-13 11:26:55 +08:00
|
|
|
#include <assert.h>
|
2016-05-26 03:25:48 +08:00
|
|
|
#include <Zydis/Internal/InstructionTable.h>
|
|
|
|
|
2016-09-13 11:26:55 +08:00
|
|
|
/* ============================================================================================== */
|
|
|
|
/* Data tables */
|
|
|
|
/* ============================================================================================== */
|
2016-05-26 03:25:48 +08:00
|
|
|
|
2016-09-13 11:26:55 +08:00
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Forward declarations */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all opcode filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the opcode.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterOpcode[][256];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all vex-map filters.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = LES, LDS or BOUND instruction (default encoding)
|
|
|
|
* 1 = 0F
|
|
|
|
* 2 = 0F38
|
|
|
|
* 3 = 0F3A
|
|
|
|
* 4 = 66
|
|
|
|
* 5 = 66_0F
|
|
|
|
* 6 = 66_0F38
|
|
|
|
* 7 = 66_0F3A
|
|
|
|
* 8 = F3
|
|
|
|
* 9 = F3_0F
|
|
|
|
* A = F3_0F38
|
|
|
|
* B = F3_0F3A
|
|
|
|
* C = F2
|
|
|
|
* D = F2_0F
|
|
|
|
* E = F2_0F38
|
|
|
|
* F = F2_0F3A
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterVEX[][16];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all xop-map filters.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = POP instruction (default encoding)
|
|
|
|
* 1 = xop8
|
|
|
|
* 2 = xop9
|
|
|
|
* 3 = xopA
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterXOP[][4];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all instruction-mode filters.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = 64 bit mode required
|
|
|
|
* 1 = 64 bit mode excluded
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterMode[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all mandatory-prefix switch tables.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = none
|
|
|
|
* 1 = 66
|
|
|
|
* 2 = F3
|
|
|
|
* 3 = F2
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterMandatoryPrefix[][4];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all modrm.mod filters.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = [modrm_mod == !11] = memory
|
|
|
|
* 1 = [modrm_mod == 11] = register
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterModrmMod[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all modrm.reg filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the modrm_reg field.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterModrmReg[][8];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all modrm.rm filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the modrm_rm field.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterModrmRm[][8];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all operand-size filters.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = 16bit = 0x66 prefix in 32 bit mode
|
|
|
|
* 1 = 32bit = 0x66 prefix in 16 bit mode
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterOperandSize[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all address-size filters.
|
|
|
|
*
|
|
|
|
* Index values:
|
|
|
|
* 0 = 16
|
|
|
|
* 1 = 32
|
|
|
|
* 2 = 64
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterAddressSize[][3];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all rex/vex/evex.w filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the rex/vex/evex.w field.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterREXW[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all vex.l filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the vex/evex.l field.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterVEXL[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all evex.l' filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the evex.l' field.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterEVEXL2[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all evex.b filters.
|
|
|
|
*
|
|
|
|
* Indexed by the numeric value of the evex.b field.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionTableNode filterEVEXB[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all operand-definitions with 1 operand.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisOperandDefinition operandDefinitions1[][1];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all operand-definitions with 2 operands.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisOperandDefinition operandDefinitions2[][2];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all operand-definitions with 3 operands.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisOperandDefinition operandDefinitions3[][3];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all operand-definitions with 4 operands.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisOperandDefinition operandDefinitions4[][4];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all operand-definitions with 5 operands.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisOperandDefinition operandDefinitions5[][5];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Contains all instruction-definitions.
|
|
|
|
*/
|
2016-11-23 01:12:05 +08:00
|
|
|
extern const ZydisInstructionDefinition instructionDefinitions[];
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Functions */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
2016-11-23 01:12:05 +08:00
|
|
|
const ZydisInstructionTableNode* ZydisInstructionTableGetRootNode()
|
2016-09-13 11:26:55 +08:00
|
|
|
{
|
2016-11-23 01:12:05 +08:00
|
|
|
static const ZydisInstructionTableNode root = { ZYDIS_NODETYPE_FILTER_OPCODE, 0x00000000 };
|
|
|
|
return &root;
|
2016-09-13 11:26:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 01:12:05 +08:00
|
|
|
const ZydisInstructionTableNode* ZydisInstructionTableGetChildNode(
|
|
|
|
const ZydisInstructionTableNode* parent, uint16_t index)
|
2016-09-13 11:26:55 +08:00
|
|
|
{
|
2016-11-23 01:12:05 +08:00
|
|
|
switch (parent->type)
|
2016-09-13 11:26:55 +08:00
|
|
|
{
|
|
|
|
case ZYDIS_NODETYPE_FILTER_OPCODE:
|
|
|
|
ZYDIS_ASSERT(index < 256);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterOpcode[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_VEX:
|
|
|
|
ZYDIS_ASSERT(index < 16);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterVEX[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_XOP:
|
|
|
|
ZYDIS_ASSERT(index < 4);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterXOP[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_MODE:
|
|
|
|
ZYDIS_ASSERT(index < 3);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterMode[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_MANDATORYPREFIX:
|
|
|
|
ZYDIS_ASSERT(index < 4);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterMandatoryPrefix[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_MODRMMOD:
|
|
|
|
ZYDIS_ASSERT(index < 2);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterModrmMod[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_MODRMREG:
|
|
|
|
ZYDIS_ASSERT(index < 8);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterModrmReg[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_MODRMRM:
|
|
|
|
ZYDIS_ASSERT(index < 8);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterModrmRm[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_OPERANDSIZE:
|
|
|
|
ZYDIS_ASSERT(index < 2);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterOperandSize[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_ADDRESSSIZE:
|
|
|
|
ZYDIS_ASSERT(index < 3);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterAddressSize[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_REXW:
|
|
|
|
ZYDIS_ASSERT(index < 2);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterREXW[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_VEXL:
|
|
|
|
ZYDIS_ASSERT(index < 2);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterVEXL[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_EVEXL2:
|
|
|
|
ZYDIS_ASSERT(index < 2);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterEVEXL2[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
case ZYDIS_NODETYPE_FILTER_EVEXB:
|
|
|
|
ZYDIS_ASSERT(index < 2);
|
2016-11-23 01:12:05 +08:00
|
|
|
return &filterEVEXB[parent->value][index];
|
2016-09-13 11:26:55 +08:00
|
|
|
default:
|
|
|
|
ZYDIS_UNREACHABLE;
|
|
|
|
}
|
2016-11-23 01:12:05 +08:00
|
|
|
static const ZydisInstructionTableNode invalid = { ZYDIS_NODETYPE_INVALID, 0x00000000 };
|
|
|
|
return &invalid;
|
2016-09-13 11:26:55 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 01:12:05 +08:00
|
|
|
bool ZydisInstructionTableGetDefinition(const ZydisInstructionTableNode* node,
|
|
|
|
const ZydisInstructionDefinition** definition, const ZydisOperandDefinition** operands,
|
|
|
|
uint8_t* operandCount)
|
2016-09-13 11:26:55 +08:00
|
|
|
{
|
2016-11-23 01:12:05 +08:00
|
|
|
*definition = &instructionDefinitions[node->value];
|
|
|
|
switch (node->type)
|
2016-09-13 11:26:55 +08:00
|
|
|
{
|
|
|
|
case ZYDIS_NODETYPE_DEFINITION_0OP:
|
2016-11-23 01:12:05 +08:00
|
|
|
*operandCount = 0;
|
2016-09-13 11:26:55 +08:00
|
|
|
break;
|
|
|
|
case ZYDIS_NODETYPE_DEFINITION_1OP:
|
2016-11-23 01:12:05 +08:00
|
|
|
*operandCount = 1;
|
|
|
|
*operands = operandDefinitions1[(*definition)->operandsId];
|
2016-09-13 11:26:55 +08:00
|
|
|
break;
|
|
|
|
case ZYDIS_NODETYPE_DEFINITION_2OP:
|
2016-11-23 01:12:05 +08:00
|
|
|
*operandCount = 2;
|
|
|
|
*operands = operandDefinitions2[(*definition)->operandsId];
|
2016-09-13 11:26:55 +08:00
|
|
|
break;
|
|
|
|
case ZYDIS_NODETYPE_DEFINITION_3OP:
|
2016-11-23 01:12:05 +08:00
|
|
|
*operandCount = 3;
|
|
|
|
*operands = operandDefinitions3[(*definition)->operandsId];
|
2016-09-13 11:26:55 +08:00
|
|
|
break;
|
|
|
|
case ZYDIS_NODETYPE_DEFINITION_4OP:
|
2016-11-23 01:12:05 +08:00
|
|
|
*operandCount = 4;
|
|
|
|
*operands = operandDefinitions4[(*definition)->operandsId];
|
2016-09-13 11:26:55 +08:00
|
|
|
break;
|
|
|
|
case ZYDIS_NODETYPE_DEFINITION_5OP:
|
2016-11-23 01:12:05 +08:00
|
|
|
*operandCount = 5;
|
|
|
|
*operands = operandDefinitions5[(*definition)->operandsId];
|
2016-09-13 11:26:55 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ZYDIS_UNREACHABLE;
|
2016-11-23 01:12:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-09-13 11:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Main instruction-table */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define ZYDIS_INVALID \
|
2016-11-23 01:12:05 +08:00
|
|
|
{ ZYDIS_NODETYPE_INVALID, 0x00000000 }
|
2016-09-13 11:26:55 +08:00
|
|
|
#define ZYDIS_FILTER(type, id) \
|
|
|
|
{ type, id }
|
|
|
|
#define ZYDIS_DEFINITION_0OP(id) \
|
|
|
|
{ ZYDIS_NODETYPE_DEFINITION_0OP, id }
|
|
|
|
#define ZYDIS_DEFINITION_1OP(id) \
|
|
|
|
{ ZYDIS_NODETYPE_DEFINITION_1OP, id }
|
|
|
|
#define ZYDIS_DEFINITION_2OP(id) \
|
|
|
|
{ ZYDIS_NODETYPE_DEFINITION_2OP, id }
|
|
|
|
#define ZYDIS_DEFINITION_3OP(id) \
|
|
|
|
{ ZYDIS_NODETYPE_DEFINITION_3OP, id }
|
|
|
|
#define ZYDIS_DEFINITION_4OP(id) \
|
|
|
|
{ ZYDIS_NODETYPE_DEFINITION_4OP, id }
|
|
|
|
#define ZYDIS_DEFINITION_5OP(id) \
|
|
|
|
{ ZYDIS_NODETYPE_DEFINITION_5OP, id }
|
|
|
|
|
|
|
|
#include <Zydis/Internal/InstructionFilters.inc>
|
2015-05-22 23:23:32 +08:00
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
#undef ZYDIS_INVALID
|
|
|
|
#undef ZYDIS_FILTER
|
2016-09-13 11:26:55 +08:00
|
|
|
#undef ZYDIS_DEFINITION_0OP
|
|
|
|
#undef ZYDIS_DEFINITION_1OP
|
|
|
|
#undef ZYDIS_DEFINITION_2OP
|
|
|
|
#undef ZYDIS_DEFINITION_3OP
|
|
|
|
#undef ZYDIS_DEFINITION_4OP
|
|
|
|
#undef ZYDIS_DEFINITION_5OP
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Operand definitions */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2015-03-16 23:37:15 +08:00
|
|
|
|
2016-11-23 01:12:05 +08:00
|
|
|
#define ZYDIS_OPERAND_DEFINITION(type, encoding, access) \
|
|
|
|
{ type, encoding, access }
|
|
|
|
|
2016-09-13 11:26:55 +08:00
|
|
|
#include <Zydis/Internal/OperandDefinitions.inc>
|
2015-03-16 23:37:15 +08:00
|
|
|
|
2016-11-23 01:12:05 +08:00
|
|
|
#undef ZYDIS_OPERAND_DEFINITION
|
2016-09-13 11:26:55 +08:00
|
|
|
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
|
|
|
/* Instruction definitions */
|
|
|
|
/* ---------------------------------------------------------------------------------------------- */
|
2015-03-16 23:37:15 +08:00
|
|
|
|
2016-05-26 03:25:48 +08:00
|
|
|
#include <Zydis/Internal/InstructionDefinitions.inc>
|
2015-05-16 11:05:17 +08:00
|
|
|
|
2016-09-13 11:26:55 +08:00
|
|
|
/* ============================================================================================== */
|