| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2020 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef FCML_MODRM_DECODER_H_ |
| 21 |
#define FCML_MODRM_DECODER_H_ |
| 22 |
|
| 23 |
#include <fcml_common.h> |
| 24 |
#include <fcml_types.h> |
| 25 |
|
| 26 |
#include "fcml_ceh.h" |
| 27 |
#include "fcml_modrm.h" |
| 28 |
#include "fcml_stream.h" |
| 29 |
|
| 30 |
/* Some macros that might be also useful outside decoder.*/ |
| 31 |
|
| 32 |
#define FCML_MODRM_DEC_MOD(x) (x >> 6) |
| 33 |
#define FCML_MODRM_DEC_REG_OPCODE(x) ((x & 0x38) >> 3) |
| 34 |
#define FCML_MODRM_DEC_RM(x) (x & 0x07) |
| 35 |
|
| 36 |
#define FCML_MODRM_SIB_SS(x) (x >> 6) |
| 37 |
#define FCML_MODRM_SIB_INDEX(x) ((x & 0x38) >> 3) |
| 38 |
#define FCML_MODRM_SIB_BASE(x) (x& 0x07) |
| 39 |
|
| 40 |
#define FCML_MODRM_DEC_FLAG_EXTEND_DISPLACEMENT_TO_ASA 0x01 |
| 41 |
|
| 42 |
typedef struct fcml_st_modrm_source { |
| 43 |
fcml_uint8_t ext_R; |
| 44 |
fcml_uint8_t ext_R_prim; |
| 45 |
fcml_uint8_t ext_X; |
| 46 |
fcml_uint8_t ext_B; |
| 47 |
fcml_uint8_t ext_V_prim; |
| 48 |
fcml_bool is_vsib; |
| 49 |
fcml_usize vsib_index_size; |
| 50 |
fcml_st_memory_stream *stream; |
| 51 |
} fcml_st_modrm_source; |
| 52 |
|
| 53 |
/** |
| 54 |
* Displacement in raw form. |
| 55 |
*/ |
| 56 |
typedef struct fcml_st_modrm_displacement { |
| 57 |
/** Displacement as encoded in disp8/disp16/disp32/disp8*N. */ |
| 58 |
fcml_st_integer displacement; |
| 59 |
/** Scaling factor N in EVEX specific compressed disp8*N. */ |
| 60 |
fcml_nuint32_t N; |
| 61 |
} fcml_st_modrm_displacement; |
| 62 |
|
| 63 |
typedef struct fcml_st_modrm_details { |
| 64 |
/** ModR/M byte. */ |
| 65 |
fcml_uint8_t modrm; |
| 66 |
/** Optional SIB byte. */ |
| 67 |
fcml_nuint8_t sib; |
| 68 |
/** Displacement as it is encoded in the instruction. */ |
| 69 |
fcml_st_modrm_displacement displacement; |
| 70 |
} fcml_st_modrm_details; |
| 71 |
|
| 72 |
typedef struct fcml_st_modrm_decoder_context { |
| 73 |
/* Sets 32 or 64 bit addressing mode. */ |
| 74 |
fcml_en_operating_mode op_mode; |
| 75 |
/* Effective address size using to decode/encode ModR/M. |
| 76 |
* It's very important to set this value properly, |
| 77 |
* because 16 and 32/64 addressing forms and encoded in |
| 78 |
* different way. |
| 79 |
*/ |
| 80 |
fcml_usize effective_address_size; |
| 81 |
/** |
| 82 |
* Used as an input size when N factor for compressed disp8 is calculated. |
| 83 |
*/ |
| 84 |
fcml_usize input_size; |
| 85 |
/** Length of the SIMD vector. */ |
| 86 |
fcml_usize vector_length; |
| 87 |
/** Tuple type needed to calculate N factor for compressed disp8. */ |
| 88 |
fcml_uint32_t tuple_type; |
| 89 |
/** True if EVEX prefix is available. Used to handle compressed |
| 90 |
* disp8 encoding. */ |
| 91 |
fcml_bool is_evex; |
| 92 |
/** True if EVEX.b is set. */ |
| 93 |
fcml_bool b; |
| 94 |
} fcml_st_modrm_decoder_context; |
| 95 |
|
| 96 |
fcml_ceh_error fcml_fn_modrm_decode(fcml_st_modrm_decoder_context *context, |
| 97 |
fcml_st_modrm_source *modrm_source, fcml_st_modrm *decoded_modrm, |
| 98 |
fcml_st_modrm_details *modrm_details, fcml_uint8_t flags); |
| 99 |
|
| 100 |
fcml_ceh_error fcml_fn_modrm_decode_rip(fcml_uint64_t rip, |
| 101 |
fcml_usize effective_address_size, fcml_st_offset *offset, |
| 102 |
fcml_st_offset *address); |
| 103 |
|
| 104 |
#endif /* FCML_MODRM_DECODER_H_ */ |