fcml  1.1.3
fcml_disassembler.h
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2015 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 {
55  fcml_bool increment_ip;
61  fcml_uint8_t conditional_group;
65  fcml_bool short_forms;
67  fcml_bool extend_disp_to_asa;
73 
83  fcml_ptr code;
85  fcml_usize code_length;
87 
88 /* Prefixes */
89 
91 typedef enum fcml_en_prefix_types {
92  FCML_PT_GROUP_UNKNOWN = 0,
93  FCML_PT_GROUP_1 = 1,
94  FCML_PT_GROUP_2,
95  FCML_PT_GROUP_3,
96  FCML_PT_GROUP_4,
97  FCML_PT_REX,
98  FCML_PT_VEX,
99  FCML_PT_XOP,
101 
105  fcml_uint8_t prefix;
107  fcml_en_prefix_types prefix_type;
109  fcml_bool mandatory_prefix;
111  fcml_uint8_t vex_xop_bytes[2];
113 
115 typedef struct fcml_st_prefixes_details {
119  fcml_int prefixes_count;
123  fcml_bool is_branch;
125  fcml_bool is_nobranch;
127  fcml_bool is_lock;
129  fcml_bool is_rep;
131  fcml_bool is_repne;
133  fcml_bool is_xrelease;
135  fcml_bool is_xacquire;
137  fcml_bool is_vex;
139  fcml_bool is_xop;
141  fcml_bool is_rex;
143  fcml_uint8_t vex_xop_first_byte;
145  fcml_uint8_t r;
147  fcml_uint8_t x;
149  fcml_uint8_t b;
151  fcml_uint8_t w;
153  fcml_uint8_t l;
155  fcml_uint8_t mmmm;
157  fcml_uint8_t vvvv;
159  fcml_uint8_t pp;
161 
163 typedef struct fcml_st_operand_details {
167 
171  fcml_uint8_t modrm;
175  fcml_bool is_rip;
177  fcml_bool is_modrm;
179 
187  fcml_bool is_shortcut;
189  fcml_bool is_pseudo_op;
191  fcml_uint8_t instruction_code[FCML_INSTRUCTION_SIZE];
193  fcml_usize instruction_size;
213  fcml_uint16_t addr_mode;
215  fcml_uint64_t instruction_group;
217 
227 
240 LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_disassembler_init( const fcml_st_dialect *dialect, fcml_st_disassembler **disassembler );
241 
259 
273 LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_prepare( fcml_st_disassembler_result *result );
274 
284 LIB_EXPORT void LIB_CALL fcml_fn_disassembler_result_free( fcml_st_disassembler_result *result );
285 
292 LIB_EXPORT void LIB_CALL fcml_fn_disassembler_free( fcml_st_disassembler *disassembler );
293 
294 #ifdef __cplusplus
295 }
296 #endif
297 
298 #endif /* FCML_DISASSEMBLER_H_ */
fcml_uint8_t mmmm
m-mmmm field of XOP or VEX prefix.
Definition: fcml_disassembler.h:155
fcml_bool is_xacquire
FCML_TRUE if xacquire explicit prefix exists.
Definition: fcml_disassembler.h:135
fcml_en_access_mode access_mode
Instruction operand access mode READ, WRITE or both.
Definition: fcml_disassembler.h:165
fcml_bool is_rep
FCML_TRUE if rep explicit prefix exists.
Definition: fcml_disassembler.h:129
fcml_bool opcode_field_s_bit
Opcode field 's'.
Definition: fcml_disassembler.h:203
fcml_st_instruction instruction
Decoded instruction in its generic form.
Definition: fcml_disassembler.h:225
fcml_uint8_t vvvv
vvvv field of XOP or VEX prefix.
Definition: fcml_disassembler.h:157
struct fcml_st_disassembler_context fcml_st_disassembler_context
Disassembler context.
fcml_uint8_t vex_xop_first_byte
Various fields encoded inside decoded prefixes.
Definition: fcml_disassembler.h:143
fcml_bool is_vex
FCML_TRUE if VEX prefix exists.
Definition: fcml_disassembler.h:137
fcml_bool is_rip
True if RIP encoding is used by decoded instruction.
Definition: fcml_disassembler.h:175
fcml_en_instruction
Instruction codes.
Definition: fcml_instructions.h:181
fcml_bool is_repne
FCML_TRUE if repne explicit prefix exists.
Definition: fcml_disassembler.h:131
fcml_st_disassembler_conf configuration
Disassembler configuration.
Definition: fcml_disassembler.h:79
fcml_en_pseudo_operations
Pseudo operations.
Definition: fcml_instructions.h:1238
Contains some additional information about all decoded instruction prefixes.
Definition: fcml_disassembler.h:115
fcml_en_pseudo_operations pseudo_op
Pseudo operation code.
Definition: fcml_disassembler.h:211
fcml_st_instruction_details instruction_details
Additional disassembler specific information about decoded instruction.
Definition: fcml_disassembler.h:223
fcml_int prefixes_bytes_count
Number of bytes used by all decoded prefixes.
Definition: fcml_disassembler.h:121
Disassembler context.
Definition: fcml_disassembler.h:75
Definition: fcml_types.h:217
fcml_en_prefix_types
Available types of instruction prefixes.
Definition: fcml_disassembler.h:91
fcml_uint8_t w
W field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:151
fcml_uint64_t instruction_group
Instruction group.
Definition: fcml_disassembler.h:215
fcml_en_access_mode
Operand access mode.
Definition: fcml_common.h:412
Container for all collected errors and warnings.
Definition: fcml_errors.h:162
fcml_bool is_nobranch
FCML_TRUE if nobranch prefix exists.
Definition: fcml_disassembler.h:125
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_en_instruction instruction
Instruction code/number.
Definition: fcml_disassembler.h:209
fcml_bool is_modrm
True if ModR/M exists.
Definition: fcml_disassembler.h:177
struct fcml_st_dialect fcml_st_dialect
Assembler dialect.
Definition: fcml_dialect.h:34
fcml_int prefixes_count
Number of decoded prefixes.
Definition: fcml_disassembler.h:119
fcml_bool is_pseudo_op
True if given instruction is a short form of pseudo-ops instructions.
Definition: fcml_disassembler.h:189
Generic instruction model.
Definition: fcml_common.h:611
fcml_bool is_xop
FCML_TRUE if XOP prefix exists.
Definition: fcml_disassembler.h:139
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_ptr code
Pointer to the encoded instruction.
Definition: fcml_disassembler.h:83
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:55
Some basic information about decoded ModR/M and SIB bytes.
Definition: fcml_disassembler.h:169
fcml_uint8_t l
L field of XOP or VEX prefix.
Definition: fcml_disassembler.h:153
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:57
Some additional disassembler specific information about decoded operands.
Definition: fcml_disassembler.h:163
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:67
fcml_st_decoded_modrm_details modrm_details
Details about decoded ModR/M and SIB bytes.
Definition: fcml_disassembler.h:199
fcml_uint8_t prefix
Prefix itself as raw byte.
Definition: fcml_disassembler.h:105
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:71
fcml_bool short_forms
Set to true in order to use short forms.
Definition: fcml_disassembler.h:65
fcml_en_prefix_types prefix_type
Type of the prefix.
Definition: fcml_disassembler.h:107
Reusable disassembler result holder.
Definition: fcml_disassembler.h:219
fcml_st_entry_point entry_point
Instruction entry point configuration.
Definition: fcml_disassembler.h:81
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:103
fcml_bool mandatory_prefix
FCML_TRUE if prefix is treated as mandatory one.
Definition: fcml_disassembler.h:109
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_bool is_branch
FCML_TRUE if branch prefix exists.
Definition: fcml_disassembler.h:123
fcml_bool is_rex
FCML_TRUE if REX prefix exists.
Definition: fcml_disassembler.h:141
fcml_uint16_t addr_mode
Code of the instruction form/addressing mode of the instruction above.
Definition: fcml_disassembler.h:213
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:61
fcml_st_prefixes_details prefixes_details
Some additional information about decoded instruction prefixes.
Definition: fcml_disassembler.h:195
fcml_bool is_shortcut
True if this is a shortcut.
Definition: fcml_disassembler.h:187
fcml_st_disassembler * disassembler
Disassembler used to decode instructions.
Definition: fcml_disassembler.h:77
fcml_uint8_t r
R field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:145
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:85
#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:207
fcml_uint8_t x
X field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:147
fcml_uint8_t modrm
ModR/M byte if exists.
Definition: fcml_disassembler.h:171
fcml_st_ceh_error_container errors
All errors and warnings messages going here.
Definition: fcml_disassembler.h:221
fcml_nuint8_t sib
SIB byte if exists.
Definition: fcml_disassembler.h:173
Describes address of an instruction code.
Definition: fcml_common.h:649
fcml_bool carry_flag_conditional_suffix
True if suffixes for carry flag has to be used by disassembler.
Definition: fcml_disassembler.h:59
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:133
Global error handling related declarations.
fcml_usize instruction_size
Instruction size in bytes.
Definition: fcml_disassembler.h:193
fcml_bool is_lock
FCML_TRUE if lock explicit prefix exists.
Definition: fcml_disassembler.h:127
struct fcml_st_disassembler_result fcml_st_disassembler_result
Reusable disassembler result holder.
fcml_uint8_t b
B field of REX,XOP or VEX prefix.
Definition: fcml_disassembler.h:149
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:181
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:139
Types declarations.
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.
struct fcml_st_instruction_prefix fcml_st_instruction_prefix
Describes one decoded prefix.
fcml_uint8_t pp
pp field of XOP or VEX prefix.
Definition: fcml_disassembler.h:159