2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2022-10-06 04:31:30 +08:00
|
|
|
*
|
2022-11-13 04:26:28 +08:00
|
|
|
* Copyright 1996-2022 The NASM Authors - All Rights Reserved
|
2009-06-29 08:13:04 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2002-05-01 04:51:32 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2022-10-06 04:31:30 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
2022-10-06 04:31:30 +08:00
|
|
|
/*
|
2009-06-29 08:13:04 +08:00
|
|
|
* disasm.c where all the _work_ gets done in the Netwide Disassembler
|
2024-07-24 03:47:25 +08:00
|
|
|
*
|
|
|
|
* See x86/bytecode.txt for the definition of the instruction encoding
|
|
|
|
* byte codes.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "disasm.h"
|
|
|
|
#include "sync.h"
|
|
|
|
#include "insns.h"
|
2008-05-21 05:21:29 +08:00
|
|
|
#include "tables.h"
|
|
|
|
#include "regdis.h"
|
2013-11-21 07:32:52 +08:00
|
|
|
#include "disp8.h"
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2018-08-15 06:12:19 +08:00
|
|
|
#define fetch_safe(_start, _ptr, _size, _need, _op) \
|
|
|
|
do { \
|
|
|
|
if (((_ptr) - (_start)) >= ((_size) - (_need))) \
|
|
|
|
_op; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define fetch_or_return(_start, _ptr, _size, _need) \
|
|
|
|
fetch_safe(_start, _ptr, _size, _need, return 0)
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Flags that go into the `segment' field of `insn' structures
|
|
|
|
* during disassembly.
|
|
|
|
*/
|
2013-11-09 09:22:24 +08:00
|
|
|
#define SEG_RELATIVE 1
|
|
|
|
#define SEG_32BIT 2
|
|
|
|
#define SEG_RMREG 4
|
|
|
|
#define SEG_DISP8 8
|
|
|
|
#define SEG_DISP16 16
|
|
|
|
#define SEG_DISP32 32
|
|
|
|
#define SEG_NODISP 64
|
|
|
|
#define SEG_SIGNED 128
|
|
|
|
#define SEG_64BIT 256
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2007-09-12 06:44:03 +08:00
|
|
|
/*
|
|
|
|
* Prefix information
|
|
|
|
*/
|
|
|
|
struct prefix_info {
|
2013-11-09 09:22:24 +08:00
|
|
|
uint8_t osize; /* Operand size */
|
|
|
|
uint8_t asize; /* Address size */
|
|
|
|
uint8_t osp; /* Operand size prefix present */
|
|
|
|
uint8_t asp; /* Address size prefix present */
|
|
|
|
uint8_t rep; /* Rep prefix present */
|
|
|
|
uint8_t seg; /* Segment override prefix present */
|
|
|
|
uint8_t wait; /* WAIT "prefix" present */
|
|
|
|
uint8_t lock; /* Lock prefix present */
|
|
|
|
uint8_t vex[3]; /* VEX prefix present */
|
|
|
|
uint8_t vex_c; /* VEX "class" (VEX, XOP, ...) */
|
|
|
|
uint8_t vex_m; /* VEX.M field */
|
2008-05-06 09:47:27 +08:00
|
|
|
uint8_t vex_v;
|
2013-11-09 09:22:24 +08:00
|
|
|
uint8_t vex_lp; /* VEX.LP fields */
|
|
|
|
uint32_t rex; /* REX prefix present */
|
2013-11-12 03:49:10 +08:00
|
|
|
uint8_t evex[3]; /* EVEX prefix present */
|
2007-09-12 06:44:03 +08:00
|
|
|
};
|
|
|
|
|
2007-04-16 09:18:30 +08:00
|
|
|
#define getu8(x) (*(uint8_t *)(x))
|
2007-11-14 01:37:59 +08:00
|
|
|
#if X86_MEMORY
|
2007-04-16 09:18:30 +08:00
|
|
|
/* Littleendian CPU which can handle unaligned references */
|
|
|
|
#define getu16(x) (*(uint16_t *)(x))
|
|
|
|
#define getu32(x) (*(uint32_t *)(x))
|
|
|
|
#define getu64(x) (*(uint64_t *)(x))
|
|
|
|
#else
|
|
|
|
static uint16_t getu16(uint8_t *data)
|
|
|
|
{
|
|
|
|
return (uint16_t)data[0] + ((uint16_t)data[1] << 8);
|
|
|
|
}
|
|
|
|
static uint32_t getu32(uint8_t *data)
|
|
|
|
{
|
|
|
|
return (uint32_t)getu16(data) + ((uint32_t)getu16(data+2) << 16);
|
|
|
|
}
|
|
|
|
static uint64_t getu64(uint8_t *data)
|
|
|
|
{
|
|
|
|
return (uint64_t)getu32(data) + ((uint64_t)getu32(data+4) << 32);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define gets8(x) ((int8_t)getu8(x))
|
|
|
|
#define gets16(x) ((int16_t)getu16(x))
|
|
|
|
#define gets32(x) ((int32_t)getu32(x))
|
|
|
|
#define gets64(x) ((int64_t)getu64(x))
|
|
|
|
|
|
|
|
/* Important: regval must already have been adjusted for rex extensions */
|
2009-10-14 03:28:14 +08:00
|
|
|
static enum reg_enum whichreg(opflags_t regflags, int regval, int rex)
|
2007-04-16 09:18:30 +08:00
|
|
|
{
|
2013-03-03 22:43:07 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
opflags_t flags;
|
|
|
|
enum reg_enum reg;
|
|
|
|
} specific_registers[] = {
|
|
|
|
{REG_AL, R_AL},
|
|
|
|
{REG_AX, R_AX},
|
|
|
|
{REG_EAX, R_EAX},
|
|
|
|
{REG_RAX, R_RAX},
|
|
|
|
{REG_DL, R_DL},
|
|
|
|
{REG_DX, R_DX},
|
|
|
|
{REG_EDX, R_EDX},
|
|
|
|
{REG_RDX, R_RDX},
|
|
|
|
{REG_CL, R_CL},
|
|
|
|
{REG_CX, R_CX},
|
|
|
|
{REG_ECX, R_ECX},
|
|
|
|
{REG_RCX, R_RCX},
|
|
|
|
{FPU0, R_ST0},
|
|
|
|
{XMM0, R_XMM0},
|
|
|
|
{YMM0, R_YMM0},
|
2013-11-12 03:49:10 +08:00
|
|
|
{ZMM0, R_ZMM0},
|
2013-03-03 22:43:07 +08:00
|
|
|
{REG_ES, R_ES},
|
|
|
|
{REG_CS, R_CS},
|
|
|
|
{REG_SS, R_SS},
|
|
|
|
{REG_DS, R_DS},
|
|
|
|
{REG_FS, R_FS},
|
2013-11-12 03:49:10 +08:00
|
|
|
{REG_GS, R_GS},
|
|
|
|
{OPMASK0, R_K0},
|
2013-03-03 22:43:07 +08:00
|
|
|
};
|
|
|
|
|
2007-09-13 11:32:39 +08:00
|
|
|
if (!(regflags & (REGISTER|REGMEM)))
|
2013-11-09 09:22:24 +08:00
|
|
|
return 0; /* Registers not permissible?! */
|
2007-09-13 11:32:39 +08:00
|
|
|
|
|
|
|
regflags |= REGISTER;
|
|
|
|
|
2013-03-03 22:43:07 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(specific_registers); i++)
|
|
|
|
if (!(specific_registers[i].flags & ~regflags))
|
|
|
|
return specific_registers[i].reg;
|
2002-06-06 10:41:20 +08:00
|
|
|
|
2007-04-16 09:18:30 +08:00
|
|
|
/* All the entries below look up regval in an 16-entry array */
|
2013-11-12 03:49:10 +08:00
|
|
|
if (regval < 0 || regval > (rex & REX_EV ? 31 : 15))
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2014-12-15 03:44:54 +08:00
|
|
|
#define GET_REGISTER(__array, __index) \
|
2014-12-20 16:40:36 +08:00
|
|
|
((size_t)(__index) < (size_t)ARRAY_SIZE(__array) ? __array[(__index)] : 0)
|
2014-12-15 03:44:54 +08:00
|
|
|
|
2007-09-13 11:32:39 +08:00
|
|
|
if (!(REG8 & ~regflags)) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (rex & (REX_P|REX_NH))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_reg8_rex, regval);
|
2013-11-09 09:22:24 +08:00
|
|
|
else
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_reg8, regval);
|
2007-04-16 09:18:30 +08:00
|
|
|
}
|
2007-09-13 11:32:39 +08:00
|
|
|
if (!(REG16 & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_reg16, regval);
|
2007-09-13 11:32:39 +08:00
|
|
|
if (!(REG32 & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_reg32, regval);
|
2007-09-13 11:32:39 +08:00
|
|
|
if (!(REG64 & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_reg64, regval);
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!(REG_SREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_sreg, regval & 7); /* Ignore REX */
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!(REG_CREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_creg, regval);
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!(REG_DREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_dreg, regval);
|
2007-04-16 09:18:30 +08:00
|
|
|
if (!(REG_TREG & ~regflags)) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (regval > 7)
|
|
|
|
return 0; /* TR registers are ill-defined with rex */
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_treg, regval);
|
2007-04-16 09:18:30 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!(FPUREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_fpureg, regval & 7); /* Ignore REX */
|
2002-05-01 04:51:32 +08:00
|
|
|
if (!(MMXREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_mmxreg, regval & 7); /* Ignore REX */
|
2002-05-01 04:56:43 +08:00
|
|
|
if (!(XMMREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_xmmreg, regval);
|
2008-05-06 09:47:27 +08:00
|
|
|
if (!(YMMREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_ymmreg, regval);
|
2013-11-12 03:49:10 +08:00
|
|
|
if (!(ZMMREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_zmmreg, regval);
|
2013-11-12 03:49:10 +08:00
|
|
|
if (!(OPMASKREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_opmaskreg, regval);
|
2013-11-20 10:44:13 +08:00
|
|
|
if (!(BNDREG & ~regflags))
|
2014-12-15 03:44:54 +08:00
|
|
|
return GET_REGISTER(nasm_rd_bndreg, regval);
|
2020-07-17 12:48:28 +08:00
|
|
|
if (!(TMMREG & ~regflags))
|
|
|
|
return GET_REGISTER(nasm_rd_tmmreg, regval);
|
2002-06-06 10:41:20 +08:00
|
|
|
|
2014-12-15 03:44:54 +08:00
|
|
|
#undef GET_REGISTER
|
2002-05-01 04:51:32 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-20 10:43:04 +08:00
|
|
|
static uint32_t append_evex_reg_deco(char *buf, uint32_t num,
|
|
|
|
decoflags_t deco, uint8_t *evex)
|
|
|
|
{
|
|
|
|
const char * const er_names[] = {"rn-sae", "rd-sae", "ru-sae", "rz-sae"};
|
|
|
|
uint32_t num_chars = 0;
|
|
|
|
|
|
|
|
if ((deco & MASK) && (evex[2] & EVEX_P2AAA)) {
|
|
|
|
enum reg_enum opmasknum = nasm_rd_opmaskreg[evex[2] & EVEX_P2AAA];
|
|
|
|
const char * regname = nasm_reg_names[opmasknum - EXPR_REG_START];
|
|
|
|
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
"{%s}", regname);
|
|
|
|
|
|
|
|
if ((deco & Z) && (evex[2] & EVEX_P2Z)) {
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
"{z}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evex[2] & EVEX_P2B) {
|
|
|
|
if (deco & ER) {
|
|
|
|
uint8_t er_type = (evex[2] & EVEX_P2LL) >> 5;
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
",{%s}", er_names[er_type]);
|
|
|
|
} else if (deco & SAE) {
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
",{sae}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t append_evex_mem_deco(char *buf, uint32_t num, opflags_t type,
|
|
|
|
decoflags_t deco, uint8_t *evex)
|
|
|
|
{
|
|
|
|
uint32_t num_chars = 0;
|
|
|
|
|
|
|
|
if ((evex[2] & EVEX_P2B) && (deco & BRDCAST_MASK)) {
|
|
|
|
decoflags_t deco_brsize = deco & BRSIZE_MASK;
|
2022-11-08 08:24:39 +08:00
|
|
|
opflags_t template_opsize = brsize_to_size(deco_brsize);
|
|
|
|
unsigned int br_num = (type & SIZE_MASK) / BITS128 *
|
|
|
|
BITS64 / template_opsize * 2;
|
2013-11-20 10:43:04 +08:00
|
|
|
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
"{1to%d}", br_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((deco & MASK) && (evex[2] & EVEX_P2AAA)) {
|
|
|
|
enum reg_enum opmasknum = nasm_rd_opmaskreg[evex[2] & EVEX_P2AAA];
|
|
|
|
const char * regname = nasm_reg_names[opmasknum - EXPR_REG_START];
|
|
|
|
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
"{%s}", regname);
|
|
|
|
|
|
|
|
if ((deco & Z) && (evex[2] & EVEX_P2Z)) {
|
|
|
|
num_chars += snprintf(buf + num_chars, num - num_chars,
|
|
|
|
"{z}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return num_chars;
|
|
|
|
}
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Process an effective address (ModRM) specification.
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint8_t *do_ea(uint8_t *data, int modrm, int asize,
|
2013-11-09 09:22:24 +08:00
|
|
|
int segsize, enum ea_type type,
|
2011-06-23 09:19:28 +08:00
|
|
|
operand *op, insn *ins)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2002-05-01 04:51:32 +08:00
|
|
|
int mod, rm, scale, index, base;
|
2007-09-18 09:45:44 +08:00
|
|
|
int rex;
|
2013-11-12 03:49:10 +08:00
|
|
|
uint8_t *evex;
|
2007-09-18 09:45:44 +08:00
|
|
|
uint8_t sib = 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
bool is_evex = !!(ins->rex & REX_EV);
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
mod = (modrm >> 6) & 03;
|
|
|
|
rm = modrm & 07;
|
|
|
|
|
2011-06-23 09:19:28 +08:00
|
|
|
if (mod != 3 && asize != 16 && rm == 4)
|
2013-11-09 09:22:24 +08:00
|
|
|
sib = *data++;
|
2007-09-18 09:45:44 +08:00
|
|
|
|
2013-11-12 03:49:10 +08:00
|
|
|
rex = ins->rex;
|
|
|
|
evex = ins->evex_p;
|
2007-09-18 09:45:44 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (mod == 3) { /* pure register version */
|
2007-04-16 10:39:56 +08:00
|
|
|
op->basereg = rm+(rex & REX_B ? 8 : 0);
|
2005-01-16 06:15:51 +08:00
|
|
|
op->segment |= SEG_RMREG;
|
2013-11-12 03:49:10 +08:00
|
|
|
if (is_evex && segsize == 64) {
|
|
|
|
op->basereg += (evex[0] & EVEX_P0X ? 0 : 16);
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
return data;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2007-10-02 06:41:25 +08:00
|
|
|
op->disp_size = 0;
|
2007-08-29 07:06:00 +08:00
|
|
|
op->eaflags = 0;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
|
|
|
if (asize == 16) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* <mod> specifies the displacement size (none, byte or
|
|
|
|
* word), and <rm> specifies the register combination.
|
|
|
|
* Exception: mod=0,rm=6 does not specify [BP] as one might
|
|
|
|
* expect, but instead specifies [disp16].
|
|
|
|
*/
|
2011-06-23 09:19:28 +08:00
|
|
|
|
|
|
|
if (type != EA_SCALAR)
|
|
|
|
return NULL;
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
op->indexreg = op->basereg = -1;
|
|
|
|
op->scale = 1; /* always, in 16 bits */
|
|
|
|
switch (rm) {
|
|
|
|
case 0:
|
|
|
|
op->basereg = R_BX;
|
|
|
|
op->indexreg = R_SI;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
op->basereg = R_BX;
|
|
|
|
op->indexreg = R_DI;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
op->basereg = R_BP;
|
|
|
|
op->indexreg = R_SI;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
op->basereg = R_BP;
|
|
|
|
op->indexreg = R_DI;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
op->basereg = R_SI;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
op->basereg = R_DI;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
op->basereg = R_BP;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
op->basereg = R_BX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rm == 6 && mod == 0) { /* special case */
|
|
|
|
op->basereg = -1;
|
|
|
|
if (segsize != 16)
|
2007-10-02 06:41:25 +08:00
|
|
|
op->disp_size = 16;
|
2005-01-16 06:15:51 +08:00
|
|
|
mod = 2; /* fake disp16 */
|
|
|
|
}
|
|
|
|
switch (mod) {
|
|
|
|
case 0:
|
|
|
|
op->segment |= SEG_NODISP;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
op->segment |= SEG_DISP8;
|
2013-11-16 05:18:32 +08:00
|
|
|
if (ins->evex_tuple != 0) {
|
|
|
|
op->offset = gets8(data) * get_disp8N(ins);
|
|
|
|
} else {
|
|
|
|
op->offset = gets8(data);
|
|
|
|
}
|
|
|
|
data++;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
op->segment |= SEG_DISP16;
|
|
|
|
op->offset = *data++;
|
|
|
|
op->offset |= ((unsigned)*data++) << 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return data;
|
2002-05-01 04:51:32 +08:00
|
|
|
} else {
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Once again, <mod> specifies displacement size (this time
|
|
|
|
* none, byte or *dword*), while <rm> specifies the base
|
|
|
|
* register. Again, [EBP] is missing, replaced by a pure
|
2007-04-16 09:18:30 +08:00
|
|
|
* disp32 (this time that's mod=0,rm=*5*) in 32-bit mode,
|
2013-11-09 09:22:24 +08:00
|
|
|
* and RIP-relative addressing in 64-bit mode.
|
|
|
|
*
|
|
|
|
* However, rm=4
|
2005-01-16 06:15:51 +08:00
|
|
|
* indicates not a single base register, but instead the
|
|
|
|
* presence of a SIB byte...
|
|
|
|
*/
|
2013-11-09 09:22:24 +08:00
|
|
|
int a64 = asize == 64;
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
op->indexreg = -1;
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
if (a64)
|
|
|
|
op->basereg = nasm_rd_reg64[rm | ((rex & REX_B) ? 8 : 0)];
|
|
|
|
else
|
|
|
|
op->basereg = nasm_rd_reg32[rm | ((rex & REX_B) ? 8 : 0)];
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (rm == 5 && mod == 0) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (segsize == 64) {
|
|
|
|
op->eaflags |= EAF_REL;
|
|
|
|
op->segment |= SEG_RELATIVE;
|
|
|
|
}
|
2007-08-29 07:06:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
if (asize != 64)
|
|
|
|
op->disp_size = asize;
|
2007-08-29 07:06:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
op->basereg = -1;
|
|
|
|
mod = 2; /* fake disp32 */
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2011-06-23 09:19:28 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
if (rm == 4) { /* process SIB */
|
2013-11-16 05:20:55 +08:00
|
|
|
uint8_t vsib_hi = 0;
|
2007-09-18 09:45:44 +08:00
|
|
|
scale = (sib >> 6) & 03;
|
|
|
|
index = (sib >> 3) & 07;
|
|
|
|
base = sib & 07;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
|
|
|
op->scale = 1 << scale;
|
|
|
|
|
2013-11-16 05:20:55 +08:00
|
|
|
if (segsize == 64) {
|
|
|
|
vsib_hi = (rex & REX_X ? 8 : 0) |
|
|
|
|
(evex[2] & EVEX_P2VP ? 0 : 16);
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
if (type == EA_XMMVSIB)
|
2013-11-16 05:20:55 +08:00
|
|
|
op->indexreg = nasm_rd_xmmreg[index | vsib_hi];
|
2013-11-09 09:22:24 +08:00
|
|
|
else if (type == EA_YMMVSIB)
|
2013-11-16 05:20:55 +08:00
|
|
|
op->indexreg = nasm_rd_ymmreg[index | vsib_hi];
|
2013-11-09 09:22:24 +08:00
|
|
|
else if (type == EA_ZMMVSIB)
|
2013-11-16 05:20:55 +08:00
|
|
|
op->indexreg = nasm_rd_zmmreg[index | vsib_hi];
|
2013-11-09 09:22:24 +08:00
|
|
|
else if (index == 4 && !(rex & REX_X))
|
|
|
|
op->indexreg = -1; /* ESP/RSP cannot be an index */
|
2011-06-23 09:19:28 +08:00
|
|
|
else if (a64)
|
2013-11-09 09:22:24 +08:00
|
|
|
op->indexreg = nasm_rd_reg64[index | ((rex & REX_X) ? 8 : 0)];
|
|
|
|
else
|
|
|
|
op->indexreg = nasm_rd_reg32[index | ((rex & REX_X) ? 8 : 0)];
|
|
|
|
|
|
|
|
if (base == 5 && mod == 0) {
|
|
|
|
op->basereg = -1;
|
|
|
|
mod = 2; /* Fake disp32 */
|
|
|
|
} else if (a64)
|
|
|
|
op->basereg = nasm_rd_reg64[base | ((rex & REX_B) ? 8 : 0)];
|
|
|
|
else
|
|
|
|
op->basereg = nasm_rd_reg32[base | ((rex & REX_B) ? 8 : 0)];
|
|
|
|
|
|
|
|
if (segsize == 16)
|
|
|
|
op->disp_size = 32;
|
2011-06-23 09:19:28 +08:00
|
|
|
} else if (type != EA_SCALAR) {
|
|
|
|
/* Can't have VSIB without SIB */
|
|
|
|
return NULL;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
switch (mod) {
|
|
|
|
case 0:
|
|
|
|
op->segment |= SEG_NODISP;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
op->segment |= SEG_DISP8;
|
2013-11-16 05:18:32 +08:00
|
|
|
if (ins->evex_tuple != 0) {
|
|
|
|
op->offset = gets8(data) * get_disp8N(ins);
|
|
|
|
} else {
|
|
|
|
op->offset = gets8(data);
|
|
|
|
}
|
2013-11-09 09:22:24 +08:00
|
|
|
data++;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
op->segment |= SEG_DISP32;
|
2008-01-03 04:19:41 +08:00
|
|
|
op->offset = gets32(data);
|
2013-11-09 09:22:24 +08:00
|
|
|
data += 4;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return data;
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-01 04:57:59 +08:00
|
|
|
* Determine whether the instruction template in t corresponds to the data
|
2002-05-01 04:51:32 +08:00
|
|
|
* stream in data. Return the number of bytes matched if so.
|
|
|
|
*/
|
2007-11-13 13:57:00 +08:00
|
|
|
#define case4(x) case (x): case (x)+1: case (x)+2: case (x)+3
|
|
|
|
|
2007-09-12 06:44:03 +08:00
|
|
|
static int matches(const struct itemplate *t, uint8_t *data,
|
2013-11-09 09:22:24 +08:00
|
|
|
const struct prefix_info *prefix, int segsize, insn *ins)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t *r = (uint8_t *)(t->code);
|
|
|
|
uint8_t *origdata = data;
|
2007-10-11 05:58:45 +08:00
|
|
|
bool a_used = false, o_used = false;
|
2007-09-12 06:44:03 +08:00
|
|
|
enum prefixes drep = 0;
|
2009-02-22 10:22:56 +08:00
|
|
|
enum prefixes dwait = 0;
|
2007-09-12 06:44:03 +08:00
|
|
|
uint8_t lock = prefix->lock;
|
|
|
|
int osize = prefix->osize;
|
|
|
|
int asize = prefix->asize;
|
2007-11-13 13:57:00 +08:00
|
|
|
int i, c;
|
2008-10-16 02:51:24 +08:00
|
|
|
int op1, op2;
|
2008-10-25 15:42:51 +08:00
|
|
|
struct operand *opx, *opy;
|
2008-10-16 02:51:24 +08:00
|
|
|
uint8_t opex = 0;
|
2008-05-21 14:36:36 +08:00
|
|
|
bool vex_ok = false;
|
2008-05-27 03:03:55 +08:00
|
|
|
int regmask = (segsize == 64) ? 15 : 7;
|
2011-06-23 09:19:28 +08:00
|
|
|
enum ea_type eat = EA_SCALAR;
|
2007-09-12 06:13:17 +08:00
|
|
|
|
2007-09-18 06:49:30 +08:00
|
|
|
for (i = 0; i < MAX_OPERANDS; i++) {
|
2013-11-09 09:22:24 +08:00
|
|
|
ins->oprs[i].segment = ins->oprs[i].disp_size =
|
|
|
|
(segsize == 64 ? SEG_64BIT : segsize == 32 ? SEG_32BIT : 0);
|
2007-09-18 06:49:30 +08:00
|
|
|
}
|
2013-11-16 05:18:32 +08:00
|
|
|
ins->evex_tuple = 0;
|
2007-09-12 06:44:03 +08:00
|
|
|
ins->rex = prefix->rex;
|
2007-11-13 14:55:27 +08:00
|
|
|
memset(ins->prefixes, 0, sizeof ins->prefixes);
|
2007-04-16 09:18:30 +08:00
|
|
|
|
2013-11-10 02:16:11 +08:00
|
|
|
if (itemp_has(t, (segsize == 64 ? IF_NOLONG : IF_LONG)))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-09-12 06:44:03 +08:00
|
|
|
if (prefix->rep == 0xF2)
|
2013-11-10 02:16:11 +08:00
|
|
|
drep = (itemp_has(t, IF_BND) ? P_BND : P_REPNE);
|
2007-09-12 06:44:03 +08:00
|
|
|
else if (prefix->rep == 0xF3)
|
2005-01-16 06:15:51 +08:00
|
|
|
drep = P_REP;
|
|
|
|
|
2009-02-22 10:22:56 +08:00
|
|
|
dwait = prefix->wait ? P_WAIT : 0;
|
|
|
|
|
2007-11-13 13:57:00 +08:00
|
|
|
while ((c = *r++) != 0) {
|
2013-11-09 09:22:24 +08:00
|
|
|
op1 = (c & 3) + ((opex & 1) << 2);
|
|
|
|
op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
|
|
|
|
opx = &ins->oprs[op1];
|
|
|
|
opy = &ins->oprs[op2];
|
|
|
|
opex = 0;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 01:
|
|
|
|
case 02:
|
|
|
|
case 03:
|
|
|
|
case 04:
|
2005-01-16 06:15:51 +08:00
|
|
|
while (c--)
|
|
|
|
if (*r++ != *data++)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 05:
|
|
|
|
case 06:
|
|
|
|
case 07:
|
|
|
|
opex = c;
|
|
|
|
break;
|
2008-10-16 02:51:24 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(010):
|
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
int t = *r++, d = *data++;
|
|
|
|
if (d < t || d > t + 7)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else {
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->basereg = (d-t)+
|
2013-11-09 09:22:24 +08:00
|
|
|
(ins->rex & REX_B ? 8 : 0);
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->segment |= SEG_RMREG;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-20 10:44:13 +08:00
|
|
|
case4(014):
|
|
|
|
/* this is an separate index reg position of MIB operand (ICC) */
|
|
|
|
/* Disassembler uses NASM's split EA form only */
|
|
|
|
break;
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(0274):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = (int8_t)*data++;
|
|
|
|
opx->segment |= SEG_SIGNED;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(020):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = *data++;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(024):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = *data++;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(030):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = getu16(data);
|
2013-11-09 09:22:24 +08:00
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case4(034):
|
|
|
|
if (osize == 32) {
|
|
|
|
opx->offset = getu32(data);
|
|
|
|
data += 4;
|
|
|
|
} else {
|
|
|
|
opx->offset = getu16(data);
|
|
|
|
data += 2;
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segsize != asize)
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->disp_size = asize;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(040):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = getu32(data);
|
2013-11-09 09:22:24 +08:00
|
|
|
data += 4;
|
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-03-01 14:28:32 +08:00
|
|
|
case4(0254):
|
|
|
|
opx->offset = gets32(data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(044):
|
|
|
|
switch (asize) {
|
|
|
|
case 16:
|
|
|
|
opx->offset = getu16(data);
|
|
|
|
data += 2;
|
|
|
|
if (segsize != 16)
|
|
|
|
opx->disp_size = 16;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
opx->offset = getu32(data);
|
|
|
|
data += 4;
|
|
|
|
if (segsize == 16)
|
|
|
|
opx->disp_size = 32;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
opx->offset = getu64(data);
|
|
|
|
opx->disp_size = 64;
|
|
|
|
data += 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case4(050):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = gets8(data++);
|
|
|
|
opx->segment |= SEG_RELATIVE;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(054):
|
|
|
|
opx->offset = getu64(data);
|
|
|
|
data += 8;
|
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(060):
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->offset = gets16(data);
|
2013-11-09 09:22:24 +08:00
|
|
|
data += 2;
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->segment |= SEG_RELATIVE;
|
|
|
|
opx->segment &= ~SEG_32BIT;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(064): /* rel */
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->segment |= SEG_RELATIVE;
|
2013-02-21 03:25:59 +08:00
|
|
|
/* In long mode rel is always 32 bits, sign extended. */
|
|
|
|
if (segsize == 64 || osize == 32) {
|
|
|
|
opx->offset = gets32(data);
|
|
|
|
data += 4;
|
|
|
|
if (segsize != 64)
|
|
|
|
opx->segment |= SEG_32BIT;
|
|
|
|
opx->type = (opx->type & ~SIZE_MASK)
|
|
|
|
| (segsize == 64 ? BITS64 : BITS32);
|
|
|
|
} else {
|
|
|
|
opx->offset = gets16(data);
|
|
|
|
data += 2;
|
|
|
|
opx->segment &= ~SEG_32BIT;
|
|
|
|
opx->type = (opx->type & ~SIZE_MASK) | BITS16;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(070):
|
2008-01-03 04:19:41 +08:00
|
|
|
opx->offset = gets32(data);
|
2013-11-09 09:22:24 +08:00
|
|
|
data += 4;
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->segment |= SEG_32BIT | SEG_RELATIVE;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case4(0100):
|
|
|
|
case4(0110):
|
|
|
|
case4(0120):
|
|
|
|
case4(0130):
|
|
|
|
{
|
|
|
|
int modrm = *data++;
|
2007-11-13 15:00:31 +08:00
|
|
|
opx->segment |= SEG_RMREG;
|
2011-06-23 09:19:28 +08:00
|
|
|
data = do_ea(data, modrm, asize, segsize, eat, opy, ins);
|
2013-11-09 09:22:24 +08:00
|
|
|
if (!data)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2008-10-16 02:51:24 +08:00
|
|
|
opx->basereg = ((modrm >> 3) & 7) + (ins->rex & REX_R ? 8 : 0);
|
2013-11-12 03:49:10 +08:00
|
|
|
if ((ins->rex & REX_EV) && (segsize == 64))
|
|
|
|
opx->basereg += (ins->evex_p[0] & EVEX_P0RP ? 0 : 16);
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-17 12:48:28 +08:00
|
|
|
case 0171:
|
|
|
|
{
|
|
|
|
uint8_t t = *r++;
|
|
|
|
uint8_t d = *data++;
|
|
|
|
if ((d ^ t) & ~070) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
op2 = (op2 & ~3) | ((t >> 3) & 3);
|
|
|
|
opy = &ins->oprs[op2];
|
|
|
|
opy->basereg = ((d >> 3) & 7) +
|
|
|
|
(ins->rex & REX_R ? 8 : 0);
|
|
|
|
opy->segment |= SEG_RMREG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0172:
|
|
|
|
{
|
|
|
|
uint8_t ximm = *data++;
|
|
|
|
c = *r++;
|
|
|
|
ins->oprs[c >> 3].basereg = (ximm >> 4) & regmask;
|
|
|
|
ins->oprs[c >> 3].segment |= SEG_RMREG;
|
|
|
|
ins->oprs[c & 7].offset = ximm & 15;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0173:
|
|
|
|
{
|
|
|
|
uint8_t ximm = *data++;
|
|
|
|
c = *r++;
|
|
|
|
|
|
|
|
if ((c ^ ximm) & 15)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
|
|
|
|
ins->oprs[c >> 4].basereg = (ximm >> 4) & regmask;
|
|
|
|
ins->oprs[c >> 4].segment |= SEG_RMREG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case4(0174):
|
|
|
|
{
|
|
|
|
uint8_t ximm = *data++;
|
|
|
|
|
|
|
|
opx->basereg = (ximm >> 4) & regmask;
|
|
|
|
opx->segment |= SEG_RMREG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case4(0200):
|
|
|
|
case4(0204):
|
|
|
|
case4(0210):
|
|
|
|
case4(0214):
|
|
|
|
case4(0220):
|
|
|
|
case4(0224):
|
|
|
|
case4(0230):
|
|
|
|
case4(0234):
|
|
|
|
{
|
2005-01-16 06:15:51 +08:00
|
|
|
int modrm = *data++;
|
|
|
|
if (((modrm >> 3) & 07) != (c & 07))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0; /* spare field doesn't match up */
|
2011-06-23 09:19:28 +08:00
|
|
|
data = do_ea(data, modrm, asize, segsize, eat, opy, ins);
|
2013-11-09 09:22:24 +08:00
|
|
|
if (!data)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-12 03:49:10 +08:00
|
|
|
case4(0240):
|
|
|
|
case 0250:
|
|
|
|
{
|
|
|
|
uint8_t evexm = *r++;
|
|
|
|
uint8_t evexwlp = *r++;
|
2014-05-06 04:58:51 +08:00
|
|
|
uint8_t modrm, valid_mask;
|
2013-11-12 03:49:10 +08:00
|
|
|
ins->evex_tuple = *r++ - 0300;
|
2014-05-06 04:58:51 +08:00
|
|
|
modrm = *(origdata + 1);
|
2013-11-12 03:49:10 +08:00
|
|
|
|
|
|
|
ins->rex |= REX_EV;
|
|
|
|
if ((prefix->rex & (REX_EV|REX_V|REX_P)) != REX_EV)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
|
|
|
|
if ((evexm & 0x1f) != prefix->vex_m)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
|
|
|
|
switch (evexwlp & 060) {
|
|
|
|
case 000:
|
|
|
|
if (prefix->rex & REX_W)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
break;
|
|
|
|
case 020:
|
|
|
|
if (!(prefix->rex & REX_W))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
ins->rex |= REX_W;
|
|
|
|
break;
|
|
|
|
case 040: /* VEX.W is a don't care */
|
|
|
|
ins->rex &= ~REX_W;
|
|
|
|
break;
|
|
|
|
case 060:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-06 04:58:51 +08:00
|
|
|
/* If EVEX.b is set with reg-reg op,
|
|
|
|
* EVEX.L'L contains embedded rounding control info
|
|
|
|
*/
|
|
|
|
if ((prefix->evex[2] & EVEX_P2B) && ((modrm >> 6) == 3)) {
|
|
|
|
valid_mask = 0x3; /* prefix only */
|
|
|
|
} else {
|
|
|
|
valid_mask = 0xf; /* vector length and prefix */
|
|
|
|
}
|
|
|
|
if ((evexwlp ^ prefix->vex_lp) & valid_mask)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
|
|
|
|
if (c == 0250) {
|
2013-11-16 05:20:55 +08:00
|
|
|
if ((prefix->vex_v != 0) ||
|
|
|
|
(!(prefix->evex[2] & EVEX_P2VP) &&
|
|
|
|
((eat < EA_XMMVSIB) || (eat > EA_ZMMVSIB))))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-12 03:49:10 +08:00
|
|
|
} else {
|
|
|
|
opx->segment |= SEG_RMREG;
|
|
|
|
opx->basereg = ((~prefix->evex[2] & EVEX_P2VP) << (4 - 3) ) |
|
|
|
|
prefix->vex_v;
|
|
|
|
}
|
|
|
|
vex_ok = true;
|
|
|
|
memcpy(ins->evex_p, prefix->evex, 3);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case4(0260):
|
|
|
|
case 0270:
|
|
|
|
{
|
|
|
|
int vexm = *r++;
|
|
|
|
int vexwlp = *r++;
|
|
|
|
|
|
|
|
ins->rex |= REX_V;
|
|
|
|
if ((prefix->rex & (REX_V|REX_P)) != REX_V)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
|
|
|
|
if ((vexm & 0x1f) != prefix->vex_m)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
|
|
|
|
switch (vexwlp & 060) {
|
|
|
|
case 000:
|
|
|
|
if (prefix->rex & REX_W)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
case 020:
|
|
|
|
if (!(prefix->rex & REX_W))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
ins->rex &= ~REX_W;
|
|
|
|
break;
|
|
|
|
case 040: /* VEX.W is a don't care */
|
|
|
|
ins->rex &= ~REX_W;
|
|
|
|
break;
|
|
|
|
case 060:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The 010 bit of vexwlp is set if VEX.L is ignored */
|
|
|
|
if ((vexwlp ^ prefix->vex_lp) & ((vexwlp & 010) ? 03 : 07))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
|
|
|
|
if (c == 0270) {
|
|
|
|
if (prefix->vex_v != 0)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
} else {
|
|
|
|
opx->segment |= SEG_RMREG;
|
|
|
|
opx->basereg = prefix->vex_v;
|
|
|
|
}
|
|
|
|
vex_ok = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-05-06 09:47:27 +08:00
|
|
|
|
2012-02-26 14:33:46 +08:00
|
|
|
case 0271:
|
2012-02-26 02:24:24 +08:00
|
|
|
if (prefix->rep == 0xF3)
|
|
|
|
drep = P_XRELEASE;
|
|
|
|
break;
|
|
|
|
|
2012-02-26 14:33:46 +08:00
|
|
|
case 0272:
|
2012-02-26 02:24:24 +08:00
|
|
|
if (prefix->rep == 0xF2)
|
|
|
|
drep = P_XACQUIRE;
|
|
|
|
else if (prefix->rep == 0xF3)
|
|
|
|
drep = P_XRELEASE;
|
|
|
|
break;
|
|
|
|
|
2012-02-26 14:33:46 +08:00
|
|
|
case 0273:
|
2012-02-26 02:24:24 +08:00
|
|
|
if (prefix->lock == 0xF0) {
|
|
|
|
if (prefix->rep == 0xF2)
|
|
|
|
drep = P_XACQUIRE;
|
|
|
|
else if (prefix->rep == 0xF3)
|
|
|
|
drep = P_XRELEASE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0310:
|
2007-04-16 09:18:30 +08:00
|
|
|
if (asize != 16)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
a_used = true;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0311:
|
2009-06-27 07:18:00 +08:00
|
|
|
if (asize != 32)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
a_used = true;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0312:
|
2005-01-16 06:15:51 +08:00
|
|
|
if (asize != segsize)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
a_used = true;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0313:
|
|
|
|
if (asize != 64)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
else
|
|
|
|
a_used = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0314:
|
|
|
|
if (prefix->rex & REX_B)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0315:
|
|
|
|
if (prefix->rex & REX_X)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0316:
|
|
|
|
if (prefix->rex & REX_R)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0317:
|
|
|
|
if (prefix->rex & REX_W)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0320:
|
2007-04-16 09:18:30 +08:00
|
|
|
if (osize != 16)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
o_used = true;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0321:
|
2007-04-16 10:02:06 +08:00
|
|
|
if (osize != 32)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
o_used = true;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0322:
|
2016-11-16 22:46:00 +08:00
|
|
|
if (osize != (segsize == 16 ? 16 : 32))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
o_used = true;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0323:
|
|
|
|
ins->rex |= REX_W; /* 64-bit only instruction */
|
|
|
|
osize = 64;
|
|
|
|
o_used = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0324:
|
|
|
|
if (osize != 64)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
o_used = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0325:
|
|
|
|
ins->rex |= REX_NH;
|
|
|
|
break;
|
|
|
|
|
2013-02-21 03:25:54 +08:00
|
|
|
case 0326:
|
|
|
|
if (prefix->rep == 0xF3)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-02-21 03:25:54 +08:00
|
|
|
break;
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0331:
|
2007-09-12 06:44:03 +08:00
|
|
|
if (prefix->rep)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0332:
|
|
|
|
if (prefix->rep != 0xF2)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
drep = 0;
|
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0333:
|
2007-09-12 06:44:03 +08:00
|
|
|
if (prefix->rep != 0xF3)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2005-01-16 06:15:51 +08:00
|
|
|
drep = 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0334:
|
|
|
|
if (lock) {
|
|
|
|
ins->rex |= REX_R;
|
|
|
|
lock = 0;
|
|
|
|
}
|
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0335:
|
2007-09-13 12:58:51 +08:00
|
|
|
if (drep == P_REP)
|
|
|
|
drep = P_REPE;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0336:
|
|
|
|
case 0337:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0340:
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
|
|
|
|
case 0341:
|
|
|
|
if (prefix->wait != 0x9B)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
dwait = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0360:
|
|
|
|
if (prefix->osp || prefix->rep)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0361:
|
|
|
|
if (!prefix->osp || prefix->rep)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
o_used = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0364:
|
|
|
|
if (prefix->osp)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0365:
|
|
|
|
if (prefix->asp)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0366:
|
|
|
|
if (!prefix->osp)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
o_used = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0367:
|
|
|
|
if (!prefix->asp)
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
a_used = true;
|
|
|
|
break;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2012-02-26 03:46:56 +08:00
|
|
|
case 0370:
|
|
|
|
case 0371:
|
|
|
|
break;
|
|
|
|
|
2011-06-23 09:19:28 +08:00
|
|
|
case 0374:
|
|
|
|
eat = EA_XMMVSIB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0375:
|
|
|
|
eat = EA_YMMVSIB;
|
|
|
|
break;
|
|
|
|
|
2013-08-16 10:01:25 +08:00
|
|
|
case 0376:
|
|
|
|
eat = EA_ZMMVSIB;
|
|
|
|
break;
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
default:
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0; /* Unknown code */
|
2013-11-09 09:22:24 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2013-11-12 03:49:10 +08:00
|
|
|
if (!vex_ok && (ins->rex & (REX_V | REX_EV)))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2008-05-21 14:36:36 +08:00
|
|
|
|
2011-07-08 07:58:22 +08:00
|
|
|
/* REX cannot be combined with VEX */
|
|
|
|
if ((ins->rex & REX_V) && (prefix->rex & REX_P))
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2007-09-18 09:45:44 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
2002-05-01 04:57:59 +08:00
|
|
|
* Check for unused rep or a/o prefixes.
|
2002-05-01 04:51:32 +08:00
|
|
|
*/
|
2007-09-23 12:49:51 +08:00
|
|
|
for (i = 0; i < t->operands; i++) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (ins->oprs[i].segment != SEG_RMREG)
|
|
|
|
a_used = true;
|
2007-09-23 12:49:51 +08:00
|
|
|
}
|
|
|
|
|
2007-10-02 06:41:25 +08:00
|
|
|
if (lock) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (ins->prefixes[PPS_LOCK])
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
ins->prefixes[PPS_LOCK] = P_LOCK;
|
2007-10-02 06:41:25 +08:00
|
|
|
}
|
|
|
|
if (drep) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (ins->prefixes[PPS_REP])
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2012-02-25 12:57:04 +08:00
|
|
|
ins->prefixes[PPS_REP] = drep;
|
2007-10-02 06:41:25 +08:00
|
|
|
}
|
2009-02-22 10:22:56 +08:00
|
|
|
ins->prefixes[PPS_WAIT] = dwait;
|
2007-11-13 14:55:27 +08:00
|
|
|
if (!o_used) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (osize != ((segsize == 16) ? 16 : 32)) {
|
|
|
|
enum prefixes pfx = 0;
|
|
|
|
|
|
|
|
switch (osize) {
|
|
|
|
case 16:
|
|
|
|
pfx = P_O16;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
pfx = P_O32;
|
|
|
|
break;
|
|
|
|
case 64:
|
|
|
|
pfx = P_O64;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ins->prefixes[PPS_OSIZE])
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2013-11-09 09:22:24 +08:00
|
|
|
ins->prefixes[PPS_OSIZE] = pfx;
|
|
|
|
}
|
2007-10-02 06:41:25 +08:00
|
|
|
}
|
|
|
|
if (!a_used && asize != segsize) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (ins->prefixes[PPS_ASIZE])
|
2014-12-14 15:57:53 +08:00
|
|
|
return 0;
|
2007-10-02 06:41:25 +08:00
|
|
|
ins->prefixes[PPS_ASIZE] = asize == 16 ? P_A16 : P_A32;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-04-16 09:18:30 +08:00
|
|
|
/* Fix: check for redundant REX prefixes */
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return data - origdata;
|
|
|
|
}
|
|
|
|
|
2018-08-15 06:12:19 +08:00
|
|
|
int32_t disasm(uint8_t *data, int32_t data_size, char *output, int outbufsize, int segsize,
|
|
|
|
int64_t offset, int autosync, iflag_t *prefer)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2007-09-11 12:16:57 +08:00
|
|
|
const struct itemplate * const *p, * const *best_p;
|
2007-09-19 06:08:20 +08:00
|
|
|
const struct disasm_index *ix;
|
|
|
|
uint8_t *dp;
|
2002-05-01 04:56:43 +08:00
|
|
|
int length, best_length = 0;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *segover;
|
2007-09-19 06:08:20 +08:00
|
|
|
int i, slen, colon, n;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint8_t *origdata;
|
2002-05-01 04:51:32 +08:00
|
|
|
int works;
|
2002-05-01 04:57:59 +08:00
|
|
|
insn tmp_ins, ins;
|
2013-11-10 02:16:11 +08:00
|
|
|
iflag_t goodness, best;
|
2007-09-12 06:44:03 +08:00
|
|
|
int best_pref;
|
|
|
|
struct prefix_info prefix;
|
2007-11-13 13:57:00 +08:00
|
|
|
bool end_prefix;
|
2013-11-16 05:18:32 +08:00
|
|
|
bool is_evex;
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2007-09-11 12:26:44 +08:00
|
|
|
memset(&ins, 0, sizeof ins);
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
/*
|
|
|
|
* Scan for prefixes.
|
|
|
|
*/
|
2007-09-12 06:44:03 +08:00
|
|
|
memset(&prefix, 0, sizeof prefix);
|
|
|
|
prefix.asize = segsize;
|
|
|
|
prefix.osize = (segsize == 64) ? 32 : segsize;
|
2002-05-01 04:51:32 +08:00
|
|
|
segover = NULL;
|
|
|
|
origdata = data;
|
2007-11-13 13:57:00 +08:00
|
|
|
|
2008-05-22 02:05:39 +08:00
|
|
|
ix = itable;
|
|
|
|
|
2008-05-06 09:47:27 +08:00
|
|
|
end_prefix = false;
|
|
|
|
while (!end_prefix) {
|
2013-11-09 09:22:24 +08:00
|
|
|
switch (*data) {
|
|
|
|
case 0xF2:
|
|
|
|
case 0xF3:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2007-09-12 06:44:03 +08:00
|
|
|
prefix.rep = *data++;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2008-05-22 02:05:39 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0x9B:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.wait = *data++;
|
|
|
|
break;
|
2009-02-22 10:22:56 +08:00
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0xF0:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2007-09-12 06:44:03 +08:00
|
|
|
prefix.lock = *data++;
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x2E:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
segover = "cs", prefix.seg = *data++;
|
|
|
|
break;
|
|
|
|
case 0x36:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
segover = "ss", prefix.seg = *data++;
|
|
|
|
break;
|
|
|
|
case 0x3E:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
segover = "ds", prefix.seg = *data++;
|
|
|
|
break;
|
|
|
|
case 0x26:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
segover = "es", prefix.seg = *data++;
|
|
|
|
break;
|
|
|
|
case 0x64:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
segover = "fs", prefix.seg = *data++;
|
|
|
|
break;
|
|
|
|
case 0x65:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
segover = "gs", prefix.seg = *data++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x66:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.osize = (segsize == 16) ? 32 : 16;
|
|
|
|
prefix.osp = *data++;
|
|
|
|
break;
|
|
|
|
case 0x67:
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.asize = (segsize == 32) ? 16 : 32;
|
|
|
|
prefix.asp = *data++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xC4:
|
|
|
|
case 0xC5:
|
|
|
|
if (segsize == 64 || (data[1] & 0xc0) == 0xc0) {
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 2);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.vex[0] = *data++;
|
|
|
|
prefix.vex[1] = *data++;
|
|
|
|
|
|
|
|
prefix.rex = REX_V;
|
|
|
|
prefix.vex_c = RV_VEX;
|
|
|
|
|
|
|
|
if (prefix.vex[0] == 0xc4) {
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.vex[2] = *data++;
|
|
|
|
prefix.rex |= (~prefix.vex[1] >> 5) & 7; /* REX_RXB */
|
|
|
|
prefix.rex |= (prefix.vex[2] >> (7-3)) & REX_W;
|
|
|
|
prefix.vex_m = prefix.vex[1] & 0x1f;
|
|
|
|
prefix.vex_v = (~prefix.vex[2] >> 3) & 15;
|
|
|
|
prefix.vex_lp = prefix.vex[2] & 7;
|
|
|
|
} else {
|
|
|
|
prefix.rex |= (~prefix.vex[1] >> (7-2)) & REX_R;
|
|
|
|
prefix.vex_m = 1;
|
|
|
|
prefix.vex_v = (~prefix.vex[1] >> 3) & 15;
|
|
|
|
prefix.vex_lp = prefix.vex[1] & 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
ix = itable_vex[RV_VEX][prefix.vex_m][prefix.vex_lp & 3];
|
|
|
|
}
|
|
|
|
end_prefix = true;
|
|
|
|
break;
|
|
|
|
|
2013-11-12 03:49:10 +08:00
|
|
|
case 0x62:
|
|
|
|
{
|
2015-11-05 06:14:05 +08:00
|
|
|
if (segsize == 64 || ((data[1] & 0xc0) == 0xc0)) {
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 4);
|
2013-11-12 03:49:10 +08:00
|
|
|
data++; /* 62h EVEX prefix */
|
|
|
|
prefix.evex[0] = *data++;
|
|
|
|
prefix.evex[1] = *data++;
|
|
|
|
prefix.evex[2] = *data++;
|
|
|
|
|
|
|
|
prefix.rex = REX_EV;
|
|
|
|
prefix.vex_c = RV_EVEX;
|
|
|
|
prefix.rex |= (~prefix.evex[0] >> 5) & 7; /* REX_RXB */
|
|
|
|
prefix.rex |= (prefix.evex[1] >> (7-3)) & REX_W;
|
|
|
|
prefix.vex_m = prefix.evex[0] & EVEX_P0MM;
|
|
|
|
prefix.vex_v = (~prefix.evex[1] & EVEX_P1VVVV) >> 3;
|
|
|
|
prefix.vex_lp = ((prefix.evex[2] & EVEX_P2LL) >> (5-2)) |
|
|
|
|
(prefix.evex[1] & EVEX_P1PP);
|
|
|
|
|
|
|
|
ix = itable_vex[prefix.vex_c][prefix.vex_m][prefix.vex_lp & 3];
|
|
|
|
}
|
|
|
|
end_prefix = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
case 0x8F:
|
|
|
|
if ((data[1] & 030) != 0 &&
|
|
|
|
(segsize == 64 || (data[1] & 0xc0) == 0xc0)) {
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 3);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.vex[0] = *data++;
|
|
|
|
prefix.vex[1] = *data++;
|
|
|
|
prefix.vex[2] = *data++;
|
|
|
|
|
|
|
|
prefix.rex = REX_V;
|
|
|
|
prefix.vex_c = RV_XOP;
|
|
|
|
|
|
|
|
prefix.rex |= (~prefix.vex[1] >> 5) & 7; /* REX_RXB */
|
|
|
|
prefix.rex |= (prefix.vex[2] >> (7-3)) & REX_W;
|
|
|
|
prefix.vex_m = prefix.vex[1] & 0x1f;
|
|
|
|
prefix.vex_v = (~prefix.vex[2] >> 3) & 15;
|
|
|
|
prefix.vex_lp = prefix.vex[2] & 7;
|
|
|
|
|
|
|
|
ix = itable_vex[RV_XOP][prefix.vex_m][prefix.vex_lp & 3];
|
|
|
|
}
|
|
|
|
end_prefix = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REX_P + 0x0:
|
|
|
|
case REX_P + 0x1:
|
|
|
|
case REX_P + 0x2:
|
|
|
|
case REX_P + 0x3:
|
|
|
|
case REX_P + 0x4:
|
|
|
|
case REX_P + 0x5:
|
|
|
|
case REX_P + 0x6:
|
|
|
|
case REX_P + 0x7:
|
|
|
|
case REX_P + 0x8:
|
|
|
|
case REX_P + 0x9:
|
|
|
|
case REX_P + 0xA:
|
|
|
|
case REX_P + 0xB:
|
|
|
|
case REX_P + 0xC:
|
|
|
|
case REX_P + 0xD:
|
|
|
|
case REX_P + 0xE:
|
|
|
|
case REX_P + 0xF:
|
|
|
|
if (segsize == 64) {
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, data, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
prefix.rex = *data++;
|
|
|
|
if (prefix.rex & REX_W)
|
|
|
|
prefix.osize = 64;
|
|
|
|
}
|
|
|
|
end_prefix = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
end_prefix = true;
|
|
|
|
break;
|
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
|
2013-11-10 02:16:11 +08:00
|
|
|
iflag_set_all(&best); /* Worst possible */
|
2002-05-01 04:56:43 +08:00
|
|
|
best_p = NULL;
|
2007-09-12 06:13:17 +08:00
|
|
|
best_pref = INT_MAX;
|
|
|
|
|
2008-05-22 02:05:39 +08:00
|
|
|
if (!ix)
|
2013-11-09 09:22:24 +08:00
|
|
|
return 0; /* No instruction table at all... */
|
2008-05-22 02:05:39 +08:00
|
|
|
|
2007-09-19 06:08:20 +08:00
|
|
|
dp = data;
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, dp, data_size, 1);
|
2008-05-22 02:05:39 +08:00
|
|
|
ix += *dp++;
|
2007-10-19 12:17:20 +08:00
|
|
|
while (ix->n == -1) {
|
2018-08-15 06:12:19 +08:00
|
|
|
fetch_or_return(origdata, dp, data_size, 1);
|
2013-11-09 09:22:24 +08:00
|
|
|
ix = (const struct disasm_index *)ix->p + *dp++;
|
2007-09-19 06:08:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p = (const struct itemplate * const *)ix->p;
|
|
|
|
for (n = ix->n; n; n--, p++) {
|
2007-09-12 06:44:03 +08:00
|
|
|
if ((length = matches(*p, data, &prefix, segsize, &tmp_ins))) {
|
2007-10-11 05:58:45 +08:00
|
|
|
works = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* Final check to make sure the types of r/m match up.
|
2013-11-09 09:22:24 +08:00
|
|
|
* XXX: Need to make sure this is actually correct.
|
2005-01-16 06:15:51 +08:00
|
|
|
*/
|
|
|
|
for (i = 0; i < (*p)->operands; i++) {
|
2013-02-28 02:13:14 +08:00
|
|
|
if (
|
2013-11-09 09:22:24 +08:00
|
|
|
/* If it's a mem-only EA but we have a
|
|
|
|
register, die. */
|
|
|
|
((tmp_ins.oprs[i].segment & SEG_RMREG) &&
|
|
|
|
is_class(MEMORY, (*p)->opd[i])) ||
|
|
|
|
/* If it's a reg-only EA but we have a memory
|
|
|
|
ref, die. */
|
|
|
|
(!(tmp_ins.oprs[i].segment & SEG_RMREG) &&
|
|
|
|
!(REG_EA & ~(*p)->opd[i]) &&
|
|
|
|
!((*p)->opd[i] & REG_SMASK)) ||
|
|
|
|
/* Register type mismatch (eg FS vs REG_DESS):
|
|
|
|
die. */
|
|
|
|
((((*p)->opd[i] & (REGISTER | FPUREG)) ||
|
|
|
|
(tmp_ins.oprs[i].segment & SEG_RMREG)) &&
|
|
|
|
!whichreg((*p)->opd[i],
|
|
|
|
tmp_ins.oprs[i].basereg, tmp_ins.rex))
|
|
|
|
) {
|
2007-10-11 05:58:45 +08:00
|
|
|
works = false;
|
2005-01-16 06:15:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
/*
|
|
|
|
* Note: we always prefer instructions which incorporate
|
|
|
|
* prefixes in the instructions themselves. This is to allow
|
|
|
|
* e.g. PAUSE to be preferred to REP NOP, and deal with
|
|
|
|
* MMX/SSE instructions where prefixes are used to select
|
|
|
|
* between MMX and SSE register sets or outright opcode
|
|
|
|
* selection.
|
|
|
|
*/
|
2005-01-16 06:15:51 +08:00
|
|
|
if (works) {
|
2013-11-09 09:22:24 +08:00
|
|
|
int i, nprefix;
|
2013-11-10 02:16:11 +08:00
|
|
|
goodness = iflag_pfmask(*p);
|
|
|
|
goodness = iflag_xor(&goodness, prefer);
|
|
|
|
nprefix = 0;
|
|
|
|
for (i = 0; i < MAXPREFIX; i++)
|
|
|
|
if (tmp_ins.prefixes[i])
|
|
|
|
nprefix++;
|
2007-10-02 06:41:25 +08:00
|
|
|
if (nprefix < best_pref ||
|
2013-11-28 05:43:45 +08:00
|
|
|
(nprefix == best_pref &&
|
|
|
|
iflag_cmp(&goodness, &best) < 0)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
/* This is the best one found so far */
|
|
|
|
best = goodness;
|
|
|
|
best_p = p;
|
2013-11-09 09:22:24 +08:00
|
|
|
best_pref = nprefix;
|
2005-01-16 06:15:51 +08:00
|
|
|
best_length = length;
|
|
|
|
ins = tmp_ins;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-05-01 04:56:43 +08:00
|
|
|
}
|
2002-05-01 04:53:55 +08:00
|
|
|
|
2002-05-01 04:57:59 +08:00
|
|
|
if (!best_p)
|
2005-01-16 06:15:51 +08:00
|
|
|
return 0; /* no instruction was matched */
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2002-05-01 04:56:43 +08:00
|
|
|
/* Pick the best match */
|
2005-01-16 06:15:51 +08:00
|
|
|
p = best_p;
|
2002-05-01 04:56:43 +08:00
|
|
|
length = best_length;
|
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
slen = 0;
|
|
|
|
|
2004-12-16 07:32:57 +08:00
|
|
|
/* TODO: snprintf returns the value that the string would have if
|
2007-10-20 05:42:29 +08:00
|
|
|
* the buffer were long enough, and not the actual length of
|
2005-01-16 06:15:51 +08:00
|
|
|
* the returned string, so each instance of using the return
|
|
|
|
* value of snprintf should actually be checked to assure that
|
|
|
|
* the return value is "sane." Maybe a macro wrapper could
|
|
|
|
* be used for that purpose.
|
|
|
|
*/
|
2009-02-22 10:22:56 +08:00
|
|
|
for (i = 0; i < MAXPREFIX; i++) {
|
2013-11-09 09:22:24 +08:00
|
|
|
const char *prefix = prefix_name(ins.prefixes[i]);
|
|
|
|
if (prefix)
|
|
|
|
slen += snprintf(output+slen, outbufsize-slen, "%s ", prefix);
|
2009-02-22 10:22:56 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
|
2008-05-21 08:07:57 +08:00
|
|
|
i = (*p)->opcode;
|
2022-11-13 04:26:28 +08:00
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "%s",
|
|
|
|
nasm_insn_names[i]);
|
2008-05-22 06:09:31 +08:00
|
|
|
|
2007-10-11 05:58:45 +08:00
|
|
|
colon = false;
|
2013-11-16 05:18:32 +08:00
|
|
|
is_evex = !!(ins.rex & REX_EV);
|
2005-01-16 06:15:51 +08:00
|
|
|
length += data - origdata; /* fix up for prefixes */
|
|
|
|
for (i = 0; i < (*p)->operands; i++) {
|
2013-11-09 09:22:24 +08:00
|
|
|
opflags_t t = (*p)->opd[i];
|
2013-11-20 10:43:04 +08:00
|
|
|
decoflags_t deco = (*p)->deco[i];
|
2013-11-09 09:22:24 +08:00
|
|
|
const operand *o = &ins.oprs[i];
|
|
|
|
int64_t offs;
|
2007-09-18 09:45:44 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
output[slen++] = (colon ? ':' : i == 0 ? ' ' : ',');
|
|
|
|
|
2013-11-09 09:22:24 +08:00
|
|
|
offs = o->offset;
|
2007-09-18 09:45:44 +08:00
|
|
|
if (o->segment & SEG_RELATIVE) {
|
|
|
|
offs += offset + length;
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* sort out wraparound
|
|
|
|
*/
|
2007-09-18 09:45:44 +08:00
|
|
|
if (!(o->segment & (SEG_32BIT|SEG_64BIT)))
|
2013-11-09 09:22:24 +08:00
|
|
|
offs &= 0xffff;
|
|
|
|
else if (segsize != 64)
|
|
|
|
offs &= 0xffffffff;
|
2008-01-03 04:19:41 +08:00
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
/*
|
|
|
|
* add sync marker, if autosync is on
|
|
|
|
*/
|
|
|
|
if (autosync)
|
2007-09-18 09:45:44 +08:00
|
|
|
add_sync(offs, 0L);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
|
|
|
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & COLON)
|
2007-10-11 05:58:45 +08:00
|
|
|
colon = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
else
|
2007-10-11 05:58:45 +08:00
|
|
|
colon = false;
|
2005-01-16 06:15:51 +08:00
|
|
|
|
2007-09-18 09:45:44 +08:00
|
|
|
if ((t & (REGISTER | FPUREG)) ||
|
2013-11-09 09:22:24 +08:00
|
|
|
(o->segment & SEG_RMREG)) {
|
|
|
|
enum reg_enum reg;
|
2007-09-18 09:45:44 +08:00
|
|
|
reg = whichreg(t, o->basereg, ins.rex);
|
|
|
|
if (t & TO)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "to ");
|
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "%s",
|
2013-11-09 09:22:24 +08:00
|
|
|
nasm_reg_names[reg-EXPR_REG_START]);
|
2018-06-26 08:15:08 +08:00
|
|
|
if (t & REGSET_MASK)
|
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "+%d",
|
|
|
|
(int)((t & REGSET_MASK) >> (REGSET_SHIFT-1))-1);
|
2013-11-20 10:43:04 +08:00
|
|
|
if (is_evex && deco)
|
|
|
|
slen += append_evex_reg_deco(output + slen, outbufsize - slen,
|
|
|
|
deco, ins.evex_p);
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (!(UNITY & ~t)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
output[slen++] = '1';
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (t & IMMEDIATE) {
|
|
|
|
if (t & BITS8) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "byte ");
|
2007-09-18 09:45:44 +08:00
|
|
|
if (o->segment & SEG_SIGNED) {
|
|
|
|
if (offs < 0) {
|
|
|
|
offs *= -1;
|
2005-01-16 06:15:51 +08:00
|
|
|
output[slen++] = '-';
|
|
|
|
} else
|
|
|
|
output[slen++] = '+';
|
|
|
|
}
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (t & BITS16) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "word ");
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (t & BITS32) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "dword ");
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (t & BITS64) {
|
2007-04-16 09:18:30 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "qword ");
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (t & NEAR) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "near ");
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (t & SHORT) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "short ");
|
|
|
|
}
|
|
|
|
slen +=
|
2007-04-14 08:10:59 +08:00
|
|
|
snprintf(output + slen, outbufsize - slen, "0x%"PRIx64"",
|
2013-11-09 09:22:24 +08:00
|
|
|
offs);
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (!(MEM_OFFS & ~t)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
2007-11-13 13:57:00 +08:00
|
|
|
snprintf(output + slen, outbufsize - slen,
|
2013-11-09 09:22:24 +08:00
|
|
|
"[%s%s%s0x%"PRIx64"]",
|
|
|
|
(segover ? segover : ""),
|
|
|
|
(segover ? ":" : ""),
|
|
|
|
(o->disp_size == 64 ? "qword " :
|
|
|
|
o->disp_size == 32 ? "dword " :
|
|
|
|
o->disp_size == 16 ? "word " : ""), offs);
|
2005-01-16 06:15:51 +08:00
|
|
|
segover = NULL;
|
2009-10-18 16:41:14 +08:00
|
|
|
} else if (is_class(REGMEM, t)) {
|
2007-10-11 05:58:45 +08:00
|
|
|
int started = false;
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & BITS8)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "byte ");
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & BITS16)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "word ");
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & BITS32)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "dword ");
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & BITS64)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "qword ");
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & BITS80)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "tword ");
|
2013-11-20 10:43:04 +08:00
|
|
|
if ((ins.evex_p[2] & EVEX_P2B) && (deco & BRDCAST_MASK)) {
|
|
|
|
/* when broadcasting, each element size should be used */
|
2022-10-06 18:32:10 +08:00
|
|
|
if (deco & BR_BITS16)
|
2013-11-20 10:43:04 +08:00
|
|
|
slen +=
|
2022-11-08 08:24:39 +08:00
|
|
|
snprintf(output + slen, outbufsize - slen, "word ");
|
2022-10-06 18:32:10 +08:00
|
|
|
else if (deco & BR_BITS32)
|
|
|
|
slen +=
|
2022-11-08 08:24:39 +08:00
|
|
|
snprintf(output + slen, outbufsize - slen, "dword ");
|
2013-11-20 10:43:04 +08:00
|
|
|
else if (deco & BR_BITS64)
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "qword ");
|
|
|
|
} else {
|
|
|
|
if (t & BITS128)
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "oword ");
|
|
|
|
if (t & BITS256)
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "yword ");
|
|
|
|
if (t & BITS512)
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "zword ");
|
|
|
|
}
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & FAR)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "far ");
|
2007-09-18 09:45:44 +08:00
|
|
|
if (t & NEAR)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "near ");
|
|
|
|
output[slen++] = '[';
|
2007-10-02 06:41:25 +08:00
|
|
|
if (o->disp_size)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "%s",
|
2013-11-09 09:22:24 +08:00
|
|
|
(o->disp_size == 64 ? "qword " :
|
|
|
|
o->disp_size == 32 ? "dword " :
|
|
|
|
o->disp_size == 16 ? "word " :
|
|
|
|
""));
|
|
|
|
if (o->eaflags & EAF_REL)
|
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "rel ");
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segover) {
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "%s:",
|
2013-11-09 09:22:24 +08:00
|
|
|
segover);
|
2005-01-16 06:15:51 +08:00
|
|
|
segover = NULL;
|
|
|
|
}
|
2007-09-18 09:45:44 +08:00
|
|
|
if (o->basereg != -1) {
|
2005-01-16 06:15:51 +08:00
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "%s",
|
2013-11-09 09:22:24 +08:00
|
|
|
nasm_reg_names[(o->basereg-EXPR_REG_START)]);
|
2007-10-11 05:58:45 +08:00
|
|
|
started = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2013-11-10 02:16:11 +08:00
|
|
|
if (o->indexreg != -1 && !itemp_has(*best_p, IF_MIB)) {
|
2005-01-16 06:15:51 +08:00
|
|
|
if (started)
|
|
|
|
output[slen++] = '+';
|
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "%s",
|
2013-11-09 09:22:24 +08:00
|
|
|
nasm_reg_names[(o->indexreg-EXPR_REG_START)]);
|
2007-09-18 09:45:44 +08:00
|
|
|
if (o->scale > 1)
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "*%d",
|
2013-11-09 09:22:24 +08:00
|
|
|
o->scale);
|
2007-10-11 05:58:45 +08:00
|
|
|
started = true;
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2007-11-13 13:57:00 +08:00
|
|
|
|
|
|
|
|
2007-09-18 09:45:44 +08:00
|
|
|
if (o->segment & SEG_DISP8) {
|
2013-11-16 05:18:32 +08:00
|
|
|
if (is_evex) {
|
|
|
|
const char *prefix;
|
|
|
|
uint32_t offset = offs;
|
|
|
|
if ((int32_t)offset < 0) {
|
|
|
|
prefix = "-";
|
|
|
|
offset = -offset;
|
|
|
|
} else {
|
|
|
|
prefix = "+";
|
|
|
|
}
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx32"",
|
|
|
|
prefix, offset);
|
2013-11-09 09:22:24 +08:00
|
|
|
} else {
|
2013-11-16 05:18:32 +08:00
|
|
|
const char *prefix;
|
|
|
|
uint8_t offset = offs;
|
|
|
|
if ((int8_t)offset < 0) {
|
|
|
|
prefix = "-";
|
|
|
|
offset = -offset;
|
|
|
|
} else {
|
|
|
|
prefix = "+";
|
|
|
|
}
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "%s0x%"PRIx8"",
|
|
|
|
prefix, offset);
|
2013-11-09 09:22:24 +08:00
|
|
|
}
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (o->segment & SEG_DISP16) {
|
2013-11-09 09:22:24 +08:00
|
|
|
const char *prefix;
|
|
|
|
uint16_t offset = offs;
|
|
|
|
if ((int16_t)offset < 0 && started) {
|
|
|
|
offset = -offset;
|
|
|
|
prefix = "-";
|
|
|
|
} else {
|
|
|
|
prefix = started ? "+" : "";
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
slen +=
|
2007-11-13 13:57:00 +08:00
|
|
|
snprintf(output + slen, outbufsize - slen,
|
2013-11-09 09:22:24 +08:00
|
|
|
"%s0x%"PRIx16"", prefix, offset);
|
2007-09-18 09:45:44 +08:00
|
|
|
} else if (o->segment & SEG_DISP32) {
|
2013-11-09 09:22:24 +08:00
|
|
|
if (prefix.asize == 64) {
|
|
|
|
const char *prefix;
|
2017-02-13 00:31:19 +08:00
|
|
|
uint64_t offset = offs;
|
2013-11-09 09:22:24 +08:00
|
|
|
if ((int32_t)offs < 0 && started) {
|
|
|
|
offset = -offset;
|
|
|
|
prefix = "-";
|
|
|
|
} else {
|
|
|
|
prefix = started ? "+" : "";
|
|
|
|
}
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen,
|
|
|
|
"%s0x%"PRIx64"", prefix, offset);
|
|
|
|
} else {
|
|
|
|
const char *prefix;
|
|
|
|
uint32_t offset = offs;
|
|
|
|
if ((int32_t) offset < 0 && started) {
|
|
|
|
offset = -offset;
|
|
|
|
prefix = "-";
|
|
|
|
} else {
|
|
|
|
prefix = started ? "+" : "";
|
|
|
|
}
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen,
|
|
|
|
"%s0x%"PRIx32"", prefix, offset);
|
|
|
|
}
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2013-09-28 07:53:51 +08:00
|
|
|
|
2013-11-10 02:16:11 +08:00
|
|
|
if (o->indexreg != -1 && itemp_has(*best_p, IF_MIB)) {
|
2013-09-28 07:53:51 +08:00
|
|
|
output[slen++] = ',';
|
|
|
|
slen += snprintf(output + slen, outbufsize - slen, "%s",
|
2013-11-09 09:22:24 +08:00
|
|
|
nasm_reg_names[(o->indexreg-EXPR_REG_START)]);
|
2013-09-28 07:53:51 +08:00
|
|
|
if (o->scale > 1)
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "*%d",
|
2013-11-09 09:22:24 +08:00
|
|
|
o->scale);
|
2013-09-28 07:53:51 +08:00
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
|
2005-01-16 06:15:51 +08:00
|
|
|
output[slen++] = ']';
|
2013-11-20 10:43:04 +08:00
|
|
|
|
|
|
|
if (is_evex && deco)
|
|
|
|
slen += append_evex_mem_deco(output + slen, outbufsize - slen,
|
|
|
|
t, deco, ins.evex_p);
|
2005-01-16 06:15:51 +08:00
|
|
|
} else {
|
|
|
|
slen +=
|
|
|
|
snprintf(output + slen, outbufsize - slen, "<operand%d>",
|
2013-11-09 09:22:24 +08:00
|
|
|
i);
|
2005-01-16 06:15:51 +08:00
|
|
|
}
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
output[slen] = '\0';
|
2005-01-16 06:15:51 +08:00
|
|
|
if (segover) { /* unused segment override */
|
2007-04-14 00:47:53 +08:00
|
|
|
char *p = output;
|
2005-01-16 06:15:51 +08:00
|
|
|
int count = slen + 1;
|
|
|
|
while (count--)
|
|
|
|
p[count + 3] = p[count];
|
|
|
|
strncpy(output, segover, 2);
|
|
|
|
output[2] = ' ';
|
2002-05-01 04:51:32 +08:00
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2009-03-19 14:10:19 +08:00
|
|
|
/*
|
|
|
|
* This is called when we don't have a complete instruction. If it
|
|
|
|
* is a standalone *single-byte* prefix show it as such, otherwise
|
|
|
|
* print it as a literal.
|
|
|
|
*/
|
|
|
|
int32_t eatbyte(uint8_t *data, char *output, int outbufsize, int segsize)
|
2002-05-01 04:53:55 +08:00
|
|
|
{
|
2009-03-19 14:10:19 +08:00
|
|
|
uint8_t byte = *data;
|
|
|
|
const char *str = NULL;
|
2013-11-09 09:22:24 +08:00
|
|
|
|
2009-03-19 14:10:19 +08:00
|
|
|
switch (byte) {
|
|
|
|
case 0xF2:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "repne";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0xF3:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "rep";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x9B:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "wait";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0xF0:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "lock";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x2E:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "cs";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x36:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "ss";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x3E:
|
2015-09-13 22:16:06 +08:00
|
|
|
str = "ds";
|
2013-11-09 09:22:24 +08:00
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x26:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "es";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x64:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "fs";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x65:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = "gs";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x66:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = (segsize == 16) ? "o32" : "o16";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case 0x67:
|
2013-11-09 09:22:24 +08:00
|
|
|
str = (segsize == 32) ? "a16" : "a32";
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
case REX_P + 0x0:
|
|
|
|
case REX_P + 0x1:
|
|
|
|
case REX_P + 0x2:
|
|
|
|
case REX_P + 0x3:
|
|
|
|
case REX_P + 0x4:
|
|
|
|
case REX_P + 0x5:
|
|
|
|
case REX_P + 0x6:
|
|
|
|
case REX_P + 0x7:
|
|
|
|
case REX_P + 0x8:
|
|
|
|
case REX_P + 0x9:
|
|
|
|
case REX_P + 0xA:
|
|
|
|
case REX_P + 0xB:
|
|
|
|
case REX_P + 0xC:
|
|
|
|
case REX_P + 0xD:
|
|
|
|
case REX_P + 0xE:
|
|
|
|
case REX_P + 0xF:
|
2013-11-09 09:22:24 +08:00
|
|
|
if (segsize == 64) {
|
|
|
|
snprintf(output, outbufsize, "rex%s%s%s%s%s",
|
|
|
|
(byte == REX_P) ? "" : ".",
|
|
|
|
(byte & REX_W) ? "w" : "",
|
|
|
|
(byte & REX_R) ? "r" : "",
|
|
|
|
(byte & REX_X) ? "x" : "",
|
|
|
|
(byte & REX_B) ? "b" : "");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* else fall through */
|
2009-03-19 14:10:19 +08:00
|
|
|
default:
|
2013-11-09 09:22:24 +08:00
|
|
|
snprintf(output, outbufsize, "db 0x%02x", byte);
|
|
|
|
break;
|
2009-03-19 14:10:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (str)
|
2013-11-09 09:22:24 +08:00
|
|
|
snprintf(output, outbufsize, "%s", str);
|
2009-03-19 14:10:19 +08:00
|
|
|
|
2002-05-01 04:51:32 +08:00
|
|
|
return 1;
|
|
|
|
}
|