1 /* radare - LGPL3 - Copyright 2016-2020 - c0riolis, x0urc3 */
2 
3 #ifndef OPCODE_H
4 #define OPCODE_H
5 
6 #include <r_types.h>
7 #include <r_list.h>
8 #include <r_util.h>
9 #include <r_anal.h>
10 
11 #define OBJECT_SIZE_ON_STACK 1
12 
13 typedef enum {
14 	HASCOMPARE = 0x1,
15 	HASCONDITION = 0x2, // conditional operator; has jump offset
16 	HASCONST = 0x4,
17 	HASFREE = 0x8,
18 	HASJABS = 0x10, // Will appear with HASCONDITION sometimes
19 	HASJREL = 0x20, // Will appear with HASCONDITION sometimes
20 	HASLOCAL = 0x40,
21 	HASNAME = 0x80,
22 	HASNARGS = 0x100, // For function-like calls
23 	HASSTORE = 0x200, // Some sort of store operation
24 	HASVARGS = 0x400, // Similar but for operators BUILD_xxx
25 	NOFOLLOW = 0x800, // Instruction doesn't fall to the next opcode
26 } pyc_opcode_type;
27 
28 typedef enum {
29 	NAME_OP = 0x1,
30 	LOCAL_OP = 0x2,
31 	FREE_OP = 0x4,
32 	DEF_OP = 0x8,
33 } pyc_store_op_func;
34 
35 typedef struct {
36 	char *op_name;
37 	ut16 type;
38 	ut8 op_code;
39 	st8 op_push;
40 	st8 op_pop;
41 } pyc_opcode_object;
42 
43 typedef struct {
44 	ut8 extended_arg;
45 	ut8 have_argument;
46 	ut8 bits;
47 	void *(*version_sig)();
48 	RList *opcode_arg_fmt;
49 	pyc_opcode_object *opcodes;
50 } pyc_opcodes;
51 
52 typedef struct {
53 	char *op_name;
54 	const char *(*formatter)(ut32 oparg);
55 } pyc_arg_fmt;
56 
57 typedef struct {
58 	char *version;
59 	pyc_opcodes *(*opcode_func)();
60 } version_opcode;
61 
62 typedef struct {
63 	char *op_name;
64 	void (*func)(RAnalOp *op, pyc_opcode_object *op_obj, ut32 oparg);
65 } op_anal_func;
66 
67 void anal_pyc_op(RAnalOp *op, pyc_opcode_object *op_obj, ut32 oparg);
68 
69 pyc_opcodes *opcode_2x(void);
70 pyc_opcodes *opcode_3x(void);
71 pyc_opcodes *opcode_10(void);
72 pyc_opcodes *opcode_11(void);
73 pyc_opcodes *opcode_12(void);
74 pyc_opcodes *opcode_13(void);
75 pyc_opcodes *opcode_14(void);
76 pyc_opcodes *opcode_15(void);
77 pyc_opcodes *opcode_16(void);
78 pyc_opcodes *opcode_20(void);
79 pyc_opcodes *opcode_21(void);
80 pyc_opcodes *opcode_22(void);
81 pyc_opcodes *opcode_23(void);
82 pyc_opcodes *opcode_24(void);
83 pyc_opcodes *opcode_25(void);
84 pyc_opcodes *opcode_26(void);
85 pyc_opcodes *opcode_27(void);
86 pyc_opcodes *opcode_30(void);
87 pyc_opcodes *opcode_31(void);
88 pyc_opcodes *opcode_32(void);
89 pyc_opcodes *opcode_33(void);
90 pyc_opcodes *opcode_34(void);
91 pyc_opcodes *opcode_35(void);
92 pyc_opcodes *opcode_36(void);
93 pyc_opcodes *opcode_37(void);
94 pyc_opcodes *opcode_38(void);
95 pyc_opcodes *opcode_39(void);
96 
97 pyc_opcodes *get_opcode_by_version(char *version);
98 
99 pyc_opcodes *new_pyc_opcodes();
100 void free_opcode(pyc_opcodes *opcodes);
101 bool pyc_opcodes_equal(pyc_opcodes *op, const char *version);
102 
103 void add_arg_fmt(pyc_opcodes *ret, char *op_name, const char *(*formatter) (ut32 oparg));
104 
105 const char *format_MAKE_FUNCTION_arg_3x(ut32 oparg);
106 const char *format_extended_arg(ut32 oparg);
107 const char *format_CALL_FUNCTION_pos_name_encoded(ut32 oparg);
108 const char *format_CALL_FUNCTION_KW_36(ut32 oparg);
109 const char *format_CALL_FUNCTION_EX_36(ut32 oparg);
110 const char *format_MAKE_FUNCTION_arg_36(ut32 oparg);
111 const char *format_value_flags_36(ut32 oparg);
112 const char *format_extended_arg_36(ut32 oparg);
113 
114 struct op_parameter {
115 	pyc_opcode_object *op_obj;
116 	const char *op_name;
117 	ut8 op_code;
118 	st8 pop;
119 	st8 push;
120 	pyc_store_op_func func;
121 	bool conditional;
122 	bool fallthrough;
123 };
124 
125 #define def_op(...) def_opN((struct op_parameter){ .fallthrough = true, __VA_ARGS__ })
126 #define def_op0(...) def_opN((struct op_parameter){ .pop = -2, .push = -2, .fallthrough = true, __VA_ARGS__ })
127 #define def_op00(...) def_opN((struct op_parameter){ __VA_ARGS__ })
128 void (def_opN)(struct op_parameter par);
129 
130 #define name_op0(...) name_opN((struct op_parameter){ .pop = -2, .push = -2, __VA_ARGS__ })
131 #define name_op(...) name_opN((struct op_parameter){ __VA_ARGS__ })
132 void (name_opN)(struct op_parameter par);
133 
134 #define local_op0(...) local_opN((struct op_parameter){ .pop = 0, .push = 1, __VA_ARGS__ })
135 #define local_op(...) local_opN((struct op_parameter){  __VA_ARGS__ })
136 void (local_opN)(struct op_parameter par);
137 
138 #define free_op0(...) free_opN((struct op_parameter){ .pop = 0, .push = 1, __VA_ARGS__ })
139 #define free_op(...) free_opN((struct op_parameter){ __VA_ARGS__ })
140 void (free_opN)(struct op_parameter par);
141 
142 #define store_op00(...) store_opN((struct op_parameter){ __VA_ARGS__ })
143 #define store_op(...) store_opN((struct op_parameter){ .func = DEF_OP, __VA_ARGS__ })
144 #define store_op0(...) store_opN((struct op_parameter){ .pop = 0, .push = 1, .func = DEF_OP, __VA_ARGS__ })
145 void (store_opN)(struct op_parameter par);
146 
147 #define varargs_op(...) varargs_op((struct op_parameter){ __VA_ARGS__ })
148 #define varargs_op0(...) varargs_op((struct op_parameter){ .pop = -1, .push = 1, __VA_ARGS__ })
149 void (varargs_op)(struct op_parameter par);
150 
151 #define const_op(...) const_opN((struct op_parameter){ .pop = 0, .push = 1, __VA_ARGS__ })
152 #define const_op00(...) const_opN((struct op_parameter){ __VA_ARGS__ })
153 void (const_opN)(struct op_parameter par);
154 
155 #define compare_op0(...) compare_op((struct op_parameter){ .pop = 2, .push = 1, __VA_ARGS__ })
156 #define compare_op(...) compare_op((struct op_parameter){ __VA_ARGS__ })
157 void (compare_op)(struct op_parameter par);
158 
159 #define jabs_op00(...) jabs_opN((struct op_parameter){ __VA_ARGS__ })
160 #define jabs_op0(...) jabs_opN((struct op_parameter){ .pop = 0, .push = 0, .conditional = false, .fallthrough = true, __VA_ARGS__ })
161 #define jabs_op(...) jabs_opN((struct op_parameter){ .fallthrough = true, __VA_ARGS__ })
162 void (jabs_opN)(struct op_parameter par);
163 
164 #define jrel_op00(...) jrel_opN((struct op_parameter){ __VA_ARGS__ })
165 #define jrel_op0(...) jrel_opN((struct op_parameter){ .pop = 0, .push = 0, .conditional = false, .fallthrough = true, __VA_ARGS__ })
166 #define jrel_op(...) jrel_opN((struct op_parameter){ .fallthrough = true, __VA_ARGS__ })
167 void (jrel_opN)(struct op_parameter par);
168 
169 #define nargs_op(...) nargs_op((struct op_parameter){ __VA_ARGS__ })
170 #define nargs_op0(...) nargs_op((struct op_parameter){ .pop = -2, .push = -2, __VA_ARGS__ })
171 void (nargs_op)(struct op_parameter par);
172 
173 #define rm_op(...) rm_op((struct op_parameter){ __VA_ARGS__ })
174 void (rm_op)(struct op_parameter par);
175 
176 #endif
177