1 /***************************************************************************************************
2 
3   Zyan Disassembler Library (Zydis)
4 
5   Original Author : Florian Bernd, Joel Hoener
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
27 /**
28  * @file
29  * @brief   Implements the `INTEL` style instruction-formatter.
30  */
31 
32 #ifndef ZYDIS_FORMATTER_INTEL_H
33 #define ZYDIS_FORMATTER_INTEL_H
34 
35 #include "zydis/Zydis/Formatter.h"
36 #include "zydis/Zydis/Internal/FormatterBase.h"
37 #include "zydis/Zydis/Internal/String.h"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* ============================================================================================== */
44 /* Formatter functions                                                                            */
45 /* ============================================================================================== */
46 
47 /* ---------------------------------------------------------------------------------------------- */
48 /* Intel                                                                                          */
49 /* ---------------------------------------------------------------------------------------------- */
50 
51 ZyanStatus ZydisFormatterIntelFormatInstruction(const ZydisFormatter* formatter,
52     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
53 
54 ZyanStatus ZydisFormatterIntelFormatOperandMEM(const ZydisFormatter* formatter,
55     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
56 
57 ZyanStatus ZydisFormatterIntelPrintMnemonic(const ZydisFormatter* formatter,
58     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
59 
60 ZyanStatus ZydisFormatterIntelPrintRegister(const ZydisFormatter* formatter,
61     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context, ZydisRegister reg);
62 
63 ZyanStatus ZydisFormatterIntelPrintDISP(const ZydisFormatter* formatter,
64     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
65 
66 ZyanStatus ZydisFormatterIntelPrintTypecast(const ZydisFormatter* formatter,
67     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
68 
69 /* ---------------------------------------------------------------------------------------------- */
70 /* MASM                                                                                           */
71 /* ---------------------------------------------------------------------------------------------- */
72 
73 ZyanStatus ZydisFormatterIntelFormatInstructionMASM(const ZydisFormatter* formatter,
74     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
75 
76 ZyanStatus ZydisFormatterIntelPrintAddressMASM(const ZydisFormatter* formatter,
77     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
78 
79 /* ---------------------------------------------------------------------------------------------- */
80 
81 /* ============================================================================================== */
82 /* Fomatter presets                                                                               */
83 /* ============================================================================================== */
84 
85 /* ---------------------------------------------------------------------------------------------- */
86 /* INTEL                                                                                          */
87 /* ---------------------------------------------------------------------------------------------- */
88 
89 /**
90  * @brief   The default formatter configuration for `INTEL` style disassembly.
91  */
92 static const ZydisFormatter FORMATTER_INTEL =
93 {
94     /* style                   */ ZYDIS_FORMATTER_STYLE_INTEL,
95     /* force_memory_size       */ ZYAN_FALSE,
96     /* force_memory_seg        */ ZYAN_FALSE,
97     /* force_relative_branches */ ZYAN_FALSE,
98     /* force_relative_riprel   */ ZYAN_FALSE,
99     /* print_branch_size       */ ZYAN_FALSE,
100     /* detailed_prefixes       */ ZYAN_FALSE,
101     /* addr_base               */ ZYDIS_NUMERIC_BASE_HEX,
102     /* addr_signedness         */ ZYDIS_SIGNEDNESS_SIGNED,
103     /* addr_padding_absolute   */ ZYDIS_PADDING_AUTO,
104     /* addr_padding_relative   */ 2,
105     /* disp_base               */ ZYDIS_NUMERIC_BASE_HEX,
106     /* disp_signedness         */ ZYDIS_SIGNEDNESS_SIGNED,
107     /* disp_padding            */ 2,
108     /* imm_base                */ ZYDIS_NUMERIC_BASE_HEX,
109     /* imm_signedness          */ ZYDIS_SIGNEDNESS_UNSIGNED,
110     /* imm_padding             */ 2,
111     /* case_prefixes           */ ZYDIS_LETTER_CASE_DEFAULT,
112     /* case_mnemonic           */ ZYDIS_LETTER_CASE_DEFAULT,
113     /* case_registers          */ ZYDIS_LETTER_CASE_DEFAULT,
114     /* case_typecasts          */ ZYDIS_LETTER_CASE_DEFAULT,
115     /* case_decorators         */ ZYDIS_LETTER_CASE_DEFAULT,
116     /* hex_uppercase           */ ZYAN_TRUE,
117     /* number_format           */
118     {
119         // ZYDIS_NUMERIC_BASE_DEC
120         {
121             // Prefix
122             {
123                 /* string      */ ZYAN_NULL,
124                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
125                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
126             },
127             // Suffix
128             {
129                 /* string      */ ZYAN_NULL,
130                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
131                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
132             }
133         },
134         // ZYDIS_NUMERIC_BASE_HEX
135         {
136             // Prefix
137             {
138                 /* string      */ &FORMATTER_INTEL.number_format[
139                                       ZYDIS_NUMERIC_BASE_HEX][0].string_data,
140                 /* string_data */ ZYAN_DEFINE_STRING_VIEW("0x"),
141                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
142             },
143             // Suffix
144             {
145                 /* string      */ ZYAN_NULL,
146                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
147                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
148             }
149         }
150     },
151     /* func_pre_instruction    */ ZYAN_NULL,
152     /* func_post_instruction   */ ZYAN_NULL,
153     /* func_format_instruction */ &ZydisFormatterIntelFormatInstruction,
154     /* func_pre_operand        */ ZYAN_NULL,
155     /* func_post_operand       */ ZYAN_NULL,
156     /* func_format_operand_reg */ &ZydisFormatterBaseFormatOperandREG,
157     /* func_format_operand_mem */ &ZydisFormatterIntelFormatOperandMEM,
158     /* func_format_operand_ptr */ &ZydisFormatterBaseFormatOperandPTR,
159     /* func_format_operand_imm */ &ZydisFormatterBaseFormatOperandIMM,
160     /* func_print_mnemonic     */ &ZydisFormatterIntelPrintMnemonic,
161     /* func_print_register     */ &ZydisFormatterIntelPrintRegister,
162     /* func_print_address_abs  */ &ZydisFormatterBasePrintAddressABS,
163     /* func_print_address_rel  */ &ZydisFormatterBasePrintAddressREL,
164     /* func_print_disp         */ &ZydisFormatterIntelPrintDISP,
165     /* func_print_imm          */ &ZydisFormatterBasePrintIMM,
166     /* func_print_typecast     */ &ZydisFormatterIntelPrintTypecast,
167     /* func_print_segment      */ &ZydisFormatterBasePrintSegment,
168     /* func_print_prefixes     */ &ZydisFormatterBasePrintPrefixes,
169     /* func_print_decorator    */ &ZydisFormatterBasePrintDecorator
170 };
171 
172 /* ---------------------------------------------------------------------------------------------- */
173 /* MASM                                                                                           */
174 /* ---------------------------------------------------------------------------------------------- */
175 
176 /**
177  * @brief   The default formatter configuration for `MASM` style disassembly.
178  */
179 static const ZydisFormatter FORMATTER_INTEL_MASM =
180 {
181     /* style                   */ ZYDIS_FORMATTER_STYLE_INTEL_MASM,
182     /* force_memory_size       */ ZYAN_TRUE,
183     /* force_memory_seg        */ ZYAN_FALSE,
184     /* force_relative_branches */ ZYAN_FALSE,
185     /* force_relative_riprel   */ ZYAN_FALSE,
186     /* print_branch_size       */ ZYAN_FALSE,
187     /* detailed_prefixes       */ ZYAN_FALSE,
188     /* addr_base               */ ZYDIS_NUMERIC_BASE_HEX,
189     /* addr_signedness         */ ZYDIS_SIGNEDNESS_SIGNED,
190     /* addr_padding_absolute   */ ZYDIS_PADDING_DISABLED,
191     /* addr_padding_relative   */ ZYDIS_PADDING_DISABLED,
192     /* disp_base               */ ZYDIS_NUMERIC_BASE_HEX,
193     /* disp_signedness         */ ZYDIS_SIGNEDNESS_SIGNED,
194     /* disp_padding            */ ZYDIS_PADDING_DISABLED,
195     /* imm_base                */ ZYDIS_NUMERIC_BASE_HEX,
196     /* imm_signedness          */ ZYDIS_SIGNEDNESS_AUTO,
197     /* imm_padding             */ ZYDIS_PADDING_DISABLED,
198     /* case_prefixes           */ ZYDIS_LETTER_CASE_DEFAULT,
199     /* case_mnemonic           */ ZYDIS_LETTER_CASE_DEFAULT,
200     /* case_registers          */ ZYDIS_LETTER_CASE_DEFAULT,
201     /* case_typecasts          */ ZYDIS_LETTER_CASE_DEFAULT,
202     /* case_decorators         */ ZYDIS_LETTER_CASE_DEFAULT,
203     /* hex_uppercase           */ ZYAN_TRUE,
204     /* number_format           */
205     {
206         // ZYDIS_NUMERIC_BASE_DEC
207         {
208             // Prefix
209             {
210                 /* string      */ ZYAN_NULL,
211                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
212                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
213             },
214             // Suffix
215             {
216                 /* string      */ ZYAN_NULL,
217                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
218                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
219             }
220         },
221         // ZYDIS_NUMERIC_BASE_HEX
222         {
223             // Prefix
224             {
225                 /* string      */ ZYAN_NULL,
226                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
227                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
228             },
229             // Suffix
230             {
231                 /* string      */ &FORMATTER_INTEL_MASM.number_format[
232                                       ZYDIS_NUMERIC_BASE_HEX][1].string_data,
233                 /* string_data */ ZYAN_DEFINE_STRING_VIEW("h"),
234                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
235             }
236         }
237     },
238     /* func_pre_instruction    */ ZYAN_NULL,
239     /* func_post_instruction   */ ZYAN_NULL,
240     /* func_format_instruction */ &ZydisFormatterIntelFormatInstructionMASM,
241     /* func_pre_operand        */ ZYAN_NULL,
242     /* func_post_operand       */ ZYAN_NULL,
243     /* func_format_operand_reg */ &ZydisFormatterBaseFormatOperandREG,
244     /* func_format_operand_mem */ &ZydisFormatterIntelFormatOperandMEM,
245     /* func_format_operand_ptr */ &ZydisFormatterBaseFormatOperandPTR,
246     /* func_format_operand_imm */ &ZydisFormatterBaseFormatOperandIMM,
247     /* func_print_mnemonic     */ &ZydisFormatterIntelPrintMnemonic,
248     /* func_print_register     */ &ZydisFormatterIntelPrintRegister,
249     /* func_print_address_abs  */ &ZydisFormatterIntelPrintAddressMASM,
250     /* func_print_address_rel  */ &ZydisFormatterIntelPrintAddressMASM,
251     /* func_print_disp         */ &ZydisFormatterIntelPrintDISP,
252     /* func_print_imm          */ &ZydisFormatterBasePrintIMM,
253     /* func_print_typecast     */ &ZydisFormatterIntelPrintTypecast,
254     /* func_print_segment      */ &ZydisFormatterBasePrintSegment,
255     /* func_print_prefixes     */ &ZydisFormatterBasePrintPrefixes,
256     /* func_print_decorator    */ &ZydisFormatterBasePrintDecorator
257 };
258 
259 /* ---------------------------------------------------------------------------------------------- */
260 
261 /* ============================================================================================== */
262 
263 #ifdef __cplusplus
264 }
265 #endif
266 
267 #endif // ZYDIS_FORMATTER_INTEL_H
268