fcml  1.2.2
fcml_disassembler.h
Go to the documentation of this file.
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 
26 #ifndef FCML_DISASSEMBLER_H_
27 #define FCML_DISASSEMBLER_H_
28 
29 #include "fcml_lib_export.h"
30 
31 #include "fcml_instructions.h"
32 #include "fcml_types.h"
33 #include "fcml_errors.h"
34 #include "fcml_common.h"
35 #include "fcml_dialect.h"
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
42 #define FCML_DASM_PREFIXES_COUNT 12
43 
45 #define FCML_DASM_CONDITIONAL_GROUP_1 0x00
46 
47 #define FCML_DASM_CONDITIONAL_GROUP_2 0x01
48 
51 
53 typedef struct fcml_st_disassembler_conf {
56  fcml_bool increment_ip;
64  fcml_uint8_t conditional_group;
69  fcml_bool short_forms;
72  fcml_bool extend_disp_to_asa;
78 
88  fcml_ptr code;
90  fcml_usize code_length;
92 
93 /* Prefixes */
94 
97 typedef enum fcml_en_prefix_types {
98  FCML_PT_GROUP_UNKNOWN = 0,
99  FCML_PT_GROUP_1 = 1,
100  FCML_PT_GROUP_2,
101  FCML_PT_GROUP_3,
102  FCML_PT_GROUP_4,
103  FCML_PT_REX,
104  FCML_PT_VEX,
105  FCML_PT_XOP,
106  FCML_PT_EVEX
108 
112  fcml_uint8_t prefix;
114  fcml_en_prefix_types prefix_type;
116  fcml_bool mandatory_prefix;
120  fcml_uint8_t avx_bytes[3];
122 
127 typedef struct fcml_st_prefixes_details {
131  fcml_int prefixes_count;
135  fcml_bool is_branch;
137  fcml_bool is_nobranch;
139  fcml_bool is_lock;
141  fcml_bool is_rep;
143  fcml_bool is_repne;
145  fcml_bool is_xrelease;
147  fcml_bool is_xacquire;
149  fcml_bool is_vex;
151  fcml_bool is_evex;
153  fcml_bool is_xop;
155  fcml_bool is_avx;
157  fcml_bool is_rex;
159  fcml_uint8_t avx_first_byte;
161  fcml_uint8_t R;
163  fcml_uint8_t R_prim;
165  fcml_uint8_t X;
167  fcml_uint8_t B;
169  fcml_uint8_t b;
171  fcml_uint8_t W;
173  fcml_uint8_t L;
175  fcml_uint8_t L_prim;
177  fcml_uint8_t mmmm;
179  fcml_uint8_t vvvv;
181  fcml_uint8_t pp;
183  fcml_uint8_t z;
185  fcml_uint8_t V_prim;
187  fcml_uint8_t aaa;
189 
191 typedef struct fcml_st_operand_details {
195 
199 typedef struct fcml_st_raw_displacement {
205 
209  fcml_uint8_t modrm;
214  fcml_bool is_rip;
216  fcml_bool is_modrm;
220 
229  fcml_bool is_shortcut;
232  fcml_bool is_pseudo_op;
234  fcml_uint8_t instruction_code[FCML_INSTRUCTION_SIZE];
236  fcml_usize instruction_size;
259  fcml_uint16_t addr_mode;
261  fcml_uint64_t instruction_group;
263  fcml_uint8_t tuple_type;
265 
276 
290 LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_disassembler_init(
291  const fcml_st_dialect *dialect, fcml_st_disassembler **disassembler);
292 
309 LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_disassemble(
312 
327 LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_prepare(
329 
340 LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_free(
342 
349 LIB_EXPORT void LIB_CALL fcml_fn_disassembler_free(
350  fcml_st_disassembler *disassembler);
351 
352 #ifdef __cplusplus
353 }
354 #endif
355 
356 #endif /* FCML_DISASSEMBLER_H_ */
fcml_uint8_t mmmm
m-mmmm field of XOP or VEX prefix.
Definition: fcml_disassembler.h:177
fcml_bool is_xacquire
FCML_TRUE if xacquire explicit prefix exists.
Definition: fcml_disassembler.h:147
fcml_en_access_mode access_mode
Instruction operand access mode READ, WRITE or both.
Definition: fcml_disassembler.h:193
fcml_bool is_rep
FCML_TRUE if rep explicit prefix exists.
Definition: fcml_disassembler.h:141
fcml_bool opcode_field_s_bit
Opcode field 's'.
Definition: fcml_disassembler.h:247
fcml_st_instruction instruction
Decoded instruction in its generic form.
Definition: fcml_disassembler.h:274
fcml_uint8_t vvvv
vvvv field of XOP or VEX prefix.
Definition: fcml_disassembler.h:179
struct fcml_st_disassembler_context fcml_st_disassembler_context
Disassembler context.
fcml_uint8_t V_prim
V’ field of EVEX prefix.
Definition: fcml_disassembler.h:185
fcml_bool is_vex
FCML_TRUE if VEX prefix exists.
Definition: fcml_disassembler.h:149
fcml_bool is_rip
True if RIP encoding is used by decoded instruction.
Definition: fcml_disassembler.h:214
fcml_en_instruction
Instruction codes.
Definition: fcml_instructions.h:184
fcml_bool is_repne
FCML_TRUE if repne explicit prefix exists.
Definition: fcml_disassembler.h:143
Displacement in raw form.
Definition: fcml_disassembler.h:199
fcml_st_disassembler_conf configuration
Disassembler configuration.
Definition: fcml_disassembler.h:84
fcml_uint8_t R_prim
EVEX R’ High-16 register specifier modifier.
Definition: fcml_disassembler.h:163
fcml_en_pseudo_operations
Pseudo operations.
Definition: fcml_instructions.h:1539
struct fcml_st_raw_displacement fcml_st_raw_displacement
Displacement in raw form.
Contains some additional information about all decoded instruction prefixes.
Definition: fcml_disassembler.h:127
fcml_en_pseudo_operations pseudo_op
Pseudo operation code.
Definition: fcml_disassembler.h:256
fcml_st_instruction_details instruction_details
Additional disassembler specific information about decoded instruction.
Definition: fcml_disassembler.h:272
fcml_int prefixes_bytes_count
Number of bytes used by all decoded prefixes.
Definition: fcml_disassembler.h:133
Disassembler context.
Definition: fcml_disassembler.h:80
Definition: fcml_types.h:217
fcml_en_prefix_types
Available types of instruction prefixes.
Definition: fcml_disassembler.h:97
fcml_uint64_t instruction_group
Instruction group.
Definition: fcml_disassembler.h:261
fcml_en_access_mode
Operand access mode.
Definition: fcml_common.h:542
Container for all collected errors and warnings.
Definition: fcml_errors.h:180
fcml_bool is_nobranch
FCML_TRUE if nobranch prefix exists.
Definition: fcml_disassembler.h:137
Definitions of common structures used by FCML components.
LIB_EXPORT void LIB_CALL fcml_fn_disassembler_free(fcml_st_disassembler *disassembler)
Frees disassembler instance.
fcml_bool is_avx
True if it is an AVX instruction (VEX/XOP/EVEX).
Definition: fcml_disassembler.h:155
fcml_en_instruction instruction
Instruction code/number.
Definition: fcml_disassembler.h:254
fcml_bool is_modrm
True if ModR/M exists.
Definition: fcml_disassembler.h:216
struct fcml_st_dialect fcml_st_dialect
Assembler dialect.
Definition: fcml_dialect.h:36
fcml_int prefixes_count
Number of decoded prefixes.
Definition: fcml_disassembler.h:131
fcml_bool is_pseudo_op
True if given instruction is a short form of pseudo-ops instructions.
Definition: fcml_disassembler.h:232
Generic instruction model.
Definition: fcml_common.h:783
fcml_bool is_xop
FCML_TRUE if XOP prefix exists.
Definition: fcml_disassembler.h:153
Disassembler configuration.
Definition: fcml_disassembler.h:53
struct fcml_st_decoded_modrm_details fcml_st_decoded_modrm_details
Some basic information about decoded ModR/M and SIB bytes.
fcml_bool is_evex
FCML TRUE if EVEX prefix exists.
Definition: fcml_disassembler.h:151
fcml_ptr code
Pointer to the encoded instruction.
Definition: fcml_disassembler.h:88
fcml_bool increment_ip
Set to true in order to make disassembler to increment IP address by length of the disassembled instr...
Definition: fcml_disassembler.h:56
Some basic information about decoded ModR/M and SIB bytes.
Definition: fcml_disassembler.h:207
Handles Win32 DLL symbols importing/exporting.
#define FCML_OPERANDS_COUNT
Maximal number of the instruction operands.
Definition: fcml_common.h:35
fcml_bool enable_error_messages
True if optional error and warning messages should be collected during processing.
Definition: fcml_disassembler.h:59
Some additional disassembler specific information about decoded operands.
Definition: fcml_disassembler.h:191
fcml_uint8_t L
L field of XOP or VEX prefix.
Definition: fcml_disassembler.h:173
fcml_uint8_t tuple_type
avx-512 tuple type
Definition: fcml_disassembler.h:263
struct fcml_st_prefixes_details fcml_st_prefixes_details
Contains some additional information about all decoded instruction prefixes.
fcml_bool extend_disp_to_asa
True if displacement should be sign extended to effective address size; otherwise false...
Definition: fcml_disassembler.h:72
fcml_uint8_t B
B field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:167
fcml_st_decoded_modrm_details modrm_details
Details about decoded ModR/M and SIB bytes.
Definition: fcml_disassembler.h:242
Definition: fcml_types.h:227
fcml_uint8_t prefix
Prefix itself as raw byte.
Definition: fcml_disassembler.h:112
struct fcml_st_operand_details fcml_st_operand_details
Some additional disassembler specific information about decoded operands.
fcml_bool fail_if_unknown_instruction
If set to true assembler will return FCML_CEH_GEC_UNKNOWN_INSTRUCTION error code if instruction is no...
Definition: fcml_disassembler.h:76
fcml_bool short_forms
Set to true in order to use short forms.
Definition: fcml_disassembler.h:69
fcml_uint8_t W
W field of REX,XOP or VEX/EVEX prefix.
Definition: fcml_disassembler.h:171
fcml_en_prefix_types prefix_type
Type of the prefix.
Definition: fcml_disassembler.h:114
Reusable disassembler result holder.
Definition: fcml_disassembler.h:267
fcml_uint8_t X
X field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:165
fcml_st_entry_point entry_point
Instruction entry point configuration.
Definition: fcml_disassembler.h:86
fcml_st_integer displacement
Displacement as encoded in disp8/disp16/disp32/disp8*N.
Definition: fcml_disassembler.h:201
LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_free(fcml_st_disassembler_result *result)
Cleans result holder.
Describes one decoded prefix.
Definition: fcml_disassembler.h:110
fcml_bool mandatory_prefix
FCML_TRUE if prefix is treated as mandatory one.
Definition: fcml_disassembler.h:116
LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_prepare(fcml_st_disassembler_result *result)
Prepares reusable result holder for disassembler.
struct fcml_st_disassembler_conf fcml_st_disassembler_conf
Disassembler configuration.
fcml_uint8_t R
R field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:161
fcml_bool is_branch
FCML_TRUE if branch prefix exists.
Definition: fcml_disassembler.h:135
fcml_bool is_rex
FCML_TRUE if REX prefix exists.
Definition: fcml_disassembler.h:157
fcml_uint8_t L_prim
L’ field of EVEX prefix.
Definition: fcml_disassembler.h:175
fcml_st_raw_displacement displacement
Raw displacement.
Definition: fcml_disassembler.h:218
fcml_uint16_t addr_mode
Code of the instruction form/addressing mode of the instruction above.
Definition: fcml_disassembler.h:259
fcml_uint8_t conditional_group
There are two groups of suffixes for conditional instructions, you can choose which one should be use...
Definition: fcml_disassembler.h:64
fcml_st_prefixes_details prefixes_details
Some additional information about decoded instruction prefixes.
Definition: fcml_disassembler.h:238
Definition: fcml_types.h:257
fcml_bool is_shortcut
True if this is a shortcut.
Definition: fcml_disassembler.h:229
fcml_st_disassembler * disassembler
Disassembler used to decode instructions.
Definition: fcml_disassembler.h:82
Instruction codes and addressing modes/instruction forms.
#define FCML_INSTRUCTION_SIZE
Maximal number of bytes instruction can use.
Definition: fcml_common.h:37
fcml_usize code_length
Size of the code in the buffer above.
Definition: fcml_disassembler.h:90
#define FCML_DASM_PREFIXES_COUNT
Maximal number of instruction prefixes.
Definition: fcml_disassembler.h:42
fcml_bool opcode_field_w_bit
Opcode field 'w'.
Definition: fcml_disassembler.h:252
fcml_uint8_t modrm
ModR/M byte if exists.
Definition: fcml_disassembler.h:209
fcml_st_ceh_error_container errors
All errors and warnings messages going here.
Definition: fcml_disassembler.h:269
fcml_nuint8_t sib
SIB byte if exists.
Definition: fcml_disassembler.h:211
Describes address of an instruction code.
Definition: fcml_common.h:824
fcml_bool carry_flag_conditional_suffix
True if suffixes for carry flag has to be used by disassembler.
Definition: fcml_disassembler.h:61
struct fcml_st_instruction_details fcml_st_instruction_details
Additional instruction details provided by disassembler.
Structures and functions related to dialects.
fcml_bool is_xrelease
FCML_TRUE if xrelease explicit prefix exists.
Definition: fcml_disassembler.h:145
Global error handling related declarations.
fcml_usize instruction_size
Instruction size in bytes.
Definition: fcml_disassembler.h:236
fcml_uint8_t aaa
Embedded opmask register specifier.
Definition: fcml_disassembler.h:187
fcml_uint8_t avx_first_byte
First byte of AVX prefix.
Definition: fcml_disassembler.h:159
fcml_bool is_lock
FCML_TRUE if lock explicit prefix exists.
Definition: fcml_disassembler.h:139
struct fcml_st_disassembler_result fcml_st_disassembler_result
Reusable disassembler result holder.
fcml_uint8_t b
b field of EVEX prefix.
Definition: fcml_disassembler.h:169
struct fcml_st_disassembler fcml_st_disassembler
This structure and type declaration represents an abstract disassembler.
Definition: fcml_disassembler.h:50
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_disassembler_init(const fcml_st_dialect *dialect, fcml_st_disassembler **disassembler)
Initializes disassembler instance.
Additional instruction details provided by disassembler.
Definition: fcml_disassembler.h:222
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:156
Types declarations.
fcml_uint8_t z
z field of EVEX prefix
Definition: fcml_disassembler.h:183
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_disassemble(fcml_st_disassembler_context *context, fcml_st_disassembler_result *result)
Disassembles one instruction from provided code buffer.
fcml_nuint32_t N
Scaling factor N in EVEX specific compressed disp8*N.
Definition: fcml_disassembler.h:203
struct fcml_st_instruction_prefix fcml_st_instruction_prefix
Describes one decoded prefix.
fcml_uint8_t pp
pp field of XOP or VEX/EVEX prefix.
Definition: fcml_disassembler.h:181