Rizin
unix-like reverse engineering framework and cli tools
asm_hexagon.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Rot127 <unisono@quyllur.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 // LLVM commit: 96e220e6886868d6663d966ecc396befffc355e7
5 // LLVM commit date: 2022-01-05 11:01:52 +0000 (ISO 8601 format)
6 // Date of code generation: 2022-04-17 16:44:52+02:00
7 //========================================
8 // The following code is generated.
9 // Do not edit. Repository of code generator:
10 // https://github.com/rizinorg/rz-hexagon
11 
12 #include <rz_types.h>
13 #include <rz_util.h>
14 #include <rz_asm.h>
15 #include <rz_lib.h>
16 #include <rz_util/rz_print.h>
17 #include <rz_vector.h>
18 #include "hexagon.h"
19 #include "hexagon_insn.h"
20 #include "hexagon_arch.h"
21 
22 static RZ_OWN RzPVector /* RzAsmTokenPattern */ *get_token_patterns() {
23  static RzPVector *pvec = NULL;
24  if (pvec) {
25  return pvec;
26  }
27 
29 
31  pat->type = RZ_ASM_TOKEN_META;
32  pat->pattern = strdup(
33  "(^[\\[\\?\\/\\|\\\\\\{])|(┌)|(│)|(└)|" // Packet prefix
34  "((∎)|[<\\}])([ :])(endloop[01]{1,2})" // Endloop markers
35  );
36  rz_pvector_push(pvec, pat);
37 
39  pat->type = RZ_ASM_TOKEN_META;
40  pat->pattern = strdup(
41  "(#{1,2})|(\\}$)|" // Immediate prefix, Closing packet bracket
42  "\\.new|:n?t|:raw|<err>" // .new and jump hints
43  );
44  rz_pvector_push(pvec, pat);
45 
48  pat->pattern = strdup(
49  "([CNPRMQVO][[:digit:]]{1,2}(:[[:digit:]]{1,2})?(in)?)" // Registers and double registers
50  );
51  rz_pvector_push(pvec, pat);
52 
55  pat->pattern = strdup(
56  "GP|HTID|UGP|LR|FP|SP" // Other regs
57  );
58  rz_pvector_push(pvec, pat);
59 
62  pat->pattern = strdup(
63  "(0x[[:digit:]abcdef]+)" // Hexadecimal numbers
64  );
65  rz_pvector_push(pvec, pat);
66 
69  pat->pattern = strdup(
70  "([[:alpha:]]+[[:digit:]]+[[:alpha:]]*)" // Mnemonics with a decimal number in the name.
71  );
72  rz_pvector_push(pvec, pat);
73 
76  pat->pattern = strdup(
77  "([[:digit:]]+)" // Decimal numbers
78  );
79  rz_pvector_push(pvec, pat);
80 
83  pat->pattern = strdup(
84  "([[:blank:]]+)|" // Spaces and tabs
85  "([,;\\.\\(\\)\\{\\}:])" // Brackets and others
86  );
87  rz_pvector_push(pvec, pat);
88 
91  pat->pattern = strdup(
92  "(\\+)|(=)|(!)|(-)" // +,-,=,],[, ! (not the packet prefix)
93  );
94  rz_pvector_push(pvec, pat);
95 
98  pat->pattern = strdup(
99  "(\\])|(\\[|<{1,2}|>{1,2})" // +,-,=,],[, ! (not the packet prefix)
100  );
101  rz_pvector_push(pvec, pat);
102 
103  pat = RZ_NEW0(RzAsmTokenPattern);
105  pat->pattern = strdup(
106  "([[:alnum:]]+)|" // Alphanumeric mnemonics
107  "([[:alnum:]]+_[[:alnum:]]+)" // Menmonics with "_" e.g dealloc_return
108  );
109  rz_pvector_push(pvec, pat);
110 
111  return pvec;
112 }
113 
114 static void compile_token_patterns(RZ_INOUT RzPVector /* RzAsmTokenPattern* */ *patterns) {
116 
117  void **it;
119  RzAsmTokenPattern *pat = *it;
120  if (!pat->regex) {
121  pat->regex = rz_regex_new(pat->pattern, "e");
122  if (!pat->regex) {
123  RZ_LOG_WARN("Did not compile regex pattern %s.\n", pat->pattern);
125  }
126  }
127  }
128 }
129 
139 static bool hex_cfg_set(void *user, void *data) {
140  rz_return_val_if_fail(user && data, false);
142  if (!state) {
143  return false;
144  }
145  RzConfig *pcfg = state->cfg;
146 
147  RzConfigNode *cnode = (RzConfigNode *)data; // Config node from core.
148  RzConfigNode *pnode = rz_config_node_get(pcfg, cnode->name); // Config node of plugin.
149  if (pnode == cnode) {
150  return true;
151  }
152  if (cnode) {
153  pnode->i_value = cnode->i_value;
154  pnode->value = cnode->value;
155  return true;
156  }
157  return false;
158 }
159 
160 static bool hexagon_init(void **user) {
163 
164  *user = state; // user = RzAsm.plugin_data
165  state->cfg = rz_config_new(state);
166  rz_return_val_if_fail(state->cfg, false);
167 
168  RzConfig *cfg = state->cfg; // Rename for SETCB macros.
169  // Add nodes
170  SETCB("plugins.hexagon.imm.hash", "true", &hex_cfg_set, "Display ## before 32bit immediates and # before immidiates with other width.");
171  SETCB("plugins.hexagon.imm.sign", "true", &hex_cfg_set, "True: Print them with sign. False: Print signed immediates in unsigned representation.");
172  SETCB("plugins.hexagon.sdk", "false", &hex_cfg_set, "Print packet syntax in objdump style.");
173  SETCB("plugins.hexagon.reg.alias", "true", &hex_cfg_set, "Print the alias of registers (Alias from C0 = SA0).");
174 
175  state->token_patterns = get_token_patterns();
176  compile_token_patterns(state->token_patterns);
177 
178  return true;
179 }
180 
184  return state->cfg;
185 }
186 
196 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int l) {
197  rz_return_val_if_fail(a && op && buf, -1);
198  if (l < 4) {
199  return -1;
200  }
201  ut32 addr = (ut32)a->pc;
202  HexReversedOpcode rev = { .action = HEXAGON_DISAS, .ana_op = NULL, .asm_op = op };
203 
205  return op->size;
206 }
207 
208 RzAsmPlugin rz_asm_plugin_hexagon = {
209  .name = "hexagon",
210  .arch = "hexagon",
211  .author = "Rot127",
212  .license = "LGPL3",
213  .bits = 32,
214  .desc = "Qualcomm Hexagon (QDSP6) V6",
215  .init = &hexagon_init,
216  .disassemble = &disassemble,
217  .get_config = &hexagon_get_config,
218 };
219 
220 #ifndef RZ_PLUGIN_INCORE
223  .data = &rz_asm_plugin_hexagon
224 };
225 #endif
RZ_API RzLibStruct rizin_plugin
static RzILOpEffect * rev(cs_insn *insn, bool is_thumb)
Definition: arm_il32.c:1590
RZ_API void rz_asm_token_pattern_free(void *p)
Definition: asm.c:1333
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_6502.c:12
static RZ_OWN RzPVector * get_token_patterns()
Definition: asm_hexagon.c:22
RZ_API RZ_BORROW RzConfigNode * rz_config_node_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:48
RZ_API RzConfig * rz_config_new(void *user)
Definition: config.c:490
#define RZ_API
#define NULL
Definition: cris-opc.c:27
uint32_t ut32
RZ_API RZ_BORROW RzConfig * hexagon_get_config()
RZ_API void hexagon_reverse_opcode(const RzAsm *rz_asm, HexReversedOpcode *rz_reverse, const ut8 *buf, const ut64 addr)
Reverses a given opcode and copies the result into one of the rizin structs in rz_reverse.
Definition: hexagon_arch.c:772
RZ_API HexState * hexagon_get_state()
Initializes each packet of the state once.
Definition: hexagon_arch.c:217
@ HEXAGON_DISAS
Definition: hexagon_arch.h:30
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define SETCB(w, x, y, z)
Definition: rz_config.h:31
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
@ RZ_ASM_TOKEN_MNEMONIC
Definition: rz_print.h:47
@ RZ_ASM_TOKEN_REGISTER
Definition: rz_print.h:50
@ RZ_ASM_TOKEN_OPERATOR
Definition: rz_print.h:48
@ RZ_ASM_TOKEN_META
Definition: rz_print.h:52
@ RZ_ASM_TOKEN_NUMBER
Definition: rz_print.h:49
@ RZ_ASM_TOKEN_SEPARATOR
Definition: rz_print.h:51
RZ_API RzRegex * rz_regex_new(const char *pattern, const char *cflags)
Definition: regcomp.c:183
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_INOUT
Definition: rz_types.h:52
#define RZ_BORROW
Definition: rz_types.h:63
RZ_API RzPVector * rz_pvector_new(RzPVectorFree free)
Definition: vector.c:302
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
#define a(i)
Definition: sha256.c:41
Pointer to the rizin structs for disassembled and analysed instructions.
Definition: hexagon_arch.h:37
Buffer packets for reversed instructions.
Definition: hexagon.h:134
Pattern for a asm string token.
Definition: rz_print.h:86
RzAsmTokenType type
Definition: rz_print.h:87
char * pattern
Definition: rz_print.h:88
RzRegex * regex
Definition: rz_print.h:89
const char * name
Definition: rz_asm.h:130
Definition: dis.h:43
Definition: dis.c:32
static int addr
Definition: z80asm.c:58