| 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_ENCODER_H_ |
| 21 |
#define FCML_MODRM_ENCODER_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 encoder.*/ |
| 31 |
|
| 32 |
#define FCML_MODRM_ENC_MOD(x) ((x) << 6) |
| 33 |
#define FCML_MODRM_ENC_REG_OPCODE(x) ((x) << 3) |
| 34 |
#define FCML_MODRM_ENC_RM(x) (x) |
| 35 |
#define FCML_MODRM_ENC(mod,reg,rm) (FCML_MODRM_ENC_MOD(mod) | \ |
| 36 |
FCML_MODRM_ENC_REG_OPCODE(reg) | FCML_MODRM_ENC_RM(rm)) |
| 37 |
|
| 38 |
#define FCML_MODRM_ENC_SIB_SS(x) ((x) << 6) |
| 39 |
#define FCML_MODRM_ENC_SIB_INDEX(x) ((x) << 3) |
| 40 |
#define FCML_MODRM_ENC_SIB_BASE(x) (x) |
| 41 |
#define FCML_MODRM_SIB_ENC(ss,index,base) (FCML_MODRM_ENC_SIB_SS(ss) | \ |
| 42 |
FCML_MODRM_ENC_SIB_INDEX(index) | FCML_MODRM_ENC_SIB_BASE(base)) |
| 43 |
|
| 44 |
typedef struct fcml_st_encoded_modrm { |
| 45 |
fcml_uint8_t modrm; |
| 46 |
fcml_nuint8_t sib; |
| 47 |
fcml_uint8_t ext_R; |
| 48 |
fcml_uint8_t ext_R_prim; |
| 49 |
fcml_uint8_t ext_X; |
| 50 |
fcml_uint8_t ext_B; |
| 51 |
fcml_uint8_t ext_V_prim; |
| 52 |
fcml_uint8_t displacement[4]; |
| 53 |
fcml_uint8_t displacement_size; |
| 54 |
fcml_bool is_rip; |
| 55 |
fcml_bool is_rip_encoded; |
| 56 |
fcml_int64_t rip_address; |
| 57 |
} fcml_st_encoded_modrm; |
| 58 |
|
| 59 |
typedef struct fcml_st_modrm_encoder_context { |
| 60 |
/* Sets 32 or 64 bit addressing mode.*/ |
| 61 |
fcml_en_operating_mode op_mode; |
| 62 |
/* Preferred effective address size that should be |
| 63 |
* used to encode ModR/M. |
| 64 |
*/ |
| 65 |
fcml_usize effective_address_size; |
| 66 |
/* Effective address size ModR/M was encoded for. For example |
| 67 |
* "effective_address_size" might be set to 32 bits, but [BX+SI] |
| 68 |
* encoding forces 16 bit encoding. Set be encoder. |
| 69 |
*/ |
| 70 |
fcml_usize chosen_effective_address_size; |
| 71 |
/* Used to calculate N in compressed disp8. */ |
| 72 |
fcml_usize input_size; |
| 73 |
/* SIMD vector size. */ |
| 74 |
fcml_usize vector_length; |
| 75 |
/* This flag is set by encoder if there is alternative SIB |
| 76 |
* encoding available, but user chooses ModR/M only encoding. |
| 77 |
*/ |
| 78 |
fcml_bool is_sib_alternative; |
| 79 |
/* If set to true, encoder chooses SIB encoding instead on ModR/M |
| 80 |
* only alternative. |
| 81 |
*/ |
| 82 |
fcml_bool choose_sib_encoding; |
| 83 |
/* If there is alternative, use RIP encoding instead of SIB alternative. */ |
| 84 |
fcml_bool choose_rip_encoding; |
| 85 |
/* SIMD instruction type. */ |
| 86 |
fcml_uint32_t tuple_type; |
| 87 |
/* True if EVEX is available. */ |
| 88 |
fcml_bool is_evex; |
| 89 |
/* EVEX.b field. */ |
| 90 |
fcml_bool b; |
| 91 |
} fcml_st_modrm_encoder_context; |
| 92 |
|
| 93 |
fcml_ceh_error fcml_fn_modrm_encode(fcml_st_modrm_encoder_context *context, |
| 94 |
const fcml_st_modrm *decoded_modrm, |
| 95 |
fcml_st_encoded_modrm *encoded_modrm); |
| 96 |
|
| 97 |
fcml_ceh_error fcml_fn_modrm_encode_rip_offset(fcml_st_memory_stream *stream, |
| 98 |
fcml_int64_t rip, fcml_uint8_t instruction_size, |
| 99 |
const fcml_st_encoded_modrm *encoded_modrm); |
| 100 |
|
| 101 |
fcml_ceh_error fcml_fn_modrm_calculate_effective_address_size( |
| 102 |
const fcml_st_modrm *decoded_modrm, fcml_flags *effective_address_size); |
| 103 |
|
| 104 |
#endif /* FCML_MODRM_ENCODER_H_ */ |