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 `AT&T` style instruction-formatter.
30  */
31 
32 #ifndef ZYDIS_FORMATTER_ATT_H
33 #define ZYDIS_FORMATTER_ATT_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 /* Instruction                                                                                    */
49 /* ---------------------------------------------------------------------------------------------- */
50 
51 ZyanStatus ZydisFormatterATTFormatInstruction(const ZydisFormatter* formatter,
52     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
53 
54 /* ---------------------------------------------------------------------------------------------- */
55 /* Operands                                                                                       */
56 /* ---------------------------------------------------------------------------------------------- */
57 
58 ZyanStatus ZydisFormatterATTFormatOperandMEM(const ZydisFormatter* formatter,
59     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
60 
61 /* ---------------------------------------------------------------------------------------------- */
62 /* Elemental tokens                                                                               */
63 /* ---------------------------------------------------------------------------------------------- */
64 
65 ZyanStatus ZydisFormatterATTPrintMnemonic(const ZydisFormatter* formatter,
66     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
67 
68 ZyanStatus ZydisFormatterATTPrintRegister(const ZydisFormatter* formatter,
69     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context, ZydisRegister reg);
70 
71 ZyanStatus ZydisFormatterATTPrintDISP(const ZydisFormatter* formatter,
72     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
73 
74 ZyanStatus ZydisFormatterATTPrintIMM(const ZydisFormatter* formatter,
75     ZydisFormatterBuffer* buffer, ZydisFormatterContext* context);
76 
77 /* ---------------------------------------------------------------------------------------------- */
78 
79 /* ============================================================================================== */
80 /* Fomatter presets                                                                               */
81 /* ============================================================================================== */
82 
83 /* ---------------------------------------------------------------------------------------------- */
84 /* AT&T                                                                                           */
85 /* ---------------------------------------------------------------------------------------------- */
86 
87 /**
88  * @brief   The default formatter configuration for `AT&T` style disassembly.
89  */
90 static const ZydisFormatter FORMATTER_ATT =
91 {
92     /* style                   */ ZYDIS_FORMATTER_STYLE_ATT,
93     /* force_memory_size       */ ZYAN_FALSE,
94     /* force_memory_seg        */ ZYAN_FALSE,
95     /* force_relative_branches */ ZYAN_FALSE,
96     /* force_relative_riprel   */ ZYAN_FALSE,
97     /* print_branch_size       */ ZYAN_FALSE,
98     /* detailed_prefixes       */ ZYAN_FALSE,
99     /* addr_base               */ ZYDIS_NUMERIC_BASE_HEX,
100     /* addr_signedness         */ ZYDIS_SIGNEDNESS_SIGNED,
101     /* addr_padding_absolute   */ ZYDIS_PADDING_AUTO,
102     /* addr_padding_relative   */ 2,
103     /* disp_base               */ ZYDIS_NUMERIC_BASE_HEX,
104     /* disp_signedness         */ ZYDIS_SIGNEDNESS_SIGNED,
105     /* disp_padding            */ 2,
106     /* imm_base                */ ZYDIS_NUMERIC_BASE_HEX,
107     /* imm_signedness          */ ZYDIS_SIGNEDNESS_AUTO,
108     /* imm_padding             */ 2,
109     /* case_prefixes           */ ZYDIS_LETTER_CASE_DEFAULT,
110     /* case_mnemonic           */ ZYDIS_LETTER_CASE_DEFAULT,
111     /* case_registers          */ ZYDIS_LETTER_CASE_DEFAULT,
112     /* case_typecasts          */ ZYDIS_LETTER_CASE_DEFAULT,
113     /* case_decorators         */ ZYDIS_LETTER_CASE_DEFAULT,
114     /* hex_uppercase           */ ZYAN_TRUE,
115     /* number_format           */
116     {
117         // ZYDIS_NUMERIC_BASE_DEC
118         {
119             // Prefix
120             {
121                 /* string      */ ZYAN_NULL,
122                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
123                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
124             },
125             // Suffix
126             {
127                 /* string      */ ZYAN_NULL,
128                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
129                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
130             }
131         },
132         // ZYDIS_NUMERIC_BASE_HEX
133         {
134             // Prefix
135             {
136                 /* string      */ &FORMATTER_ATT.number_format[
137                                     ZYDIS_NUMERIC_BASE_HEX][0].string_data,
138                 /* string_data */ ZYAN_DEFINE_STRING_VIEW("0x"),
139                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
140             },
141             // Suffix
142             {
143                 /* string      */ ZYAN_NULL,
144                 /* string_data */ ZYAN_DEFINE_STRING_VIEW(""),
145                 /* buffer      */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
146             }
147         }
148     },
149     /* func_pre_instruction    */ ZYAN_NULL,
150     /* func_post_instruction   */ ZYAN_NULL,
151     /* func_format_instruction */ &ZydisFormatterATTFormatInstruction,
152     /* func_pre_operand        */ ZYAN_NULL,
153     /* func_post_operand       */ ZYAN_NULL,
154     /* func_format_operand_reg */ &ZydisFormatterBaseFormatOperandREG,
155     /* func_format_operand_mem */ &ZydisFormatterATTFormatOperandMEM,
156     /* func_format_operand_ptr */ &ZydisFormatterBaseFormatOperandPTR,
157     /* func_format_operand_imm */ &ZydisFormatterBaseFormatOperandIMM,
158     /* func_print_mnemonic     */ &ZydisFormatterATTPrintMnemonic,
159     /* func_print_register     */ &ZydisFormatterATTPrintRegister,
160     /* func_print_address_abs  */ &ZydisFormatterBasePrintAddressABS,
161     /* func_print_address_rel  */ &ZydisFormatterBasePrintAddressREL,
162     /* func_print_disp         */ &ZydisFormatterATTPrintDISP,
163     /* func_print_imm          */ &ZydisFormatterATTPrintIMM,
164     /* func_print_typecast     */ ZYAN_NULL,
165     /* func_print_segment      */ &ZydisFormatterBasePrintSegment,
166     /* func_print_prefixes     */ &ZydisFormatterBasePrintPrefixes,
167     /* func_print_decorator    */ &ZydisFormatterBasePrintDecorator
168 };
169 
170 /* ---------------------------------------------------------------------------------------------- */
171 
172 /* ============================================================================================== */
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 #endif // ZYDIS_FORMATTER_ATT_H
179