Rizin
unix-like reverse engineering framework and cli tools
analysis_sysz.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2019 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_analysis.h>
5 #include <rz_lib.h>
6 #include <capstone/capstone.h>
7 #include <capstone/systemz.h>
8 // instruction set: http://www.tachyonsoft.com/inst390m.htm
9 
10 #if CS_API_MAJOR < 2
11 #error Old Capstone not supported
12 #endif
13 
14 #define INSOP(n) insn->detail->sysz.operands[n]
15 
16 static void opex(RzStrBuf *buf, csh handle, cs_insn *insn) {
17  int i;
18  PJ *pj = pj_new();
19  if (!pj) {
20  return;
21  }
22  pj_o(pj);
23  pj_ka(pj, "operands");
24  cs_sysz *x = &insn->detail->sysz;
25  for (i = 0; i < x->op_count; i++) {
26  cs_sysz_op *op = x->operands + i;
27  pj_o(pj);
28  switch (op->type) {
29  case SYSZ_OP_REG:
30  pj_ks(pj, "type", "reg");
31  pj_ks(pj, "value", cs_reg_name(handle, op->reg));
32  break;
33  case SYSZ_OP_IMM:
34  pj_ks(pj, "type", "imm");
35  pj_kN(pj, "value", op->imm);
36  break;
37  case SYSZ_OP_MEM:
38  pj_ks(pj, "type", "mem");
39  if (op->mem.base != SYSZ_REG_INVALID) {
40  pj_ks(pj, "base", cs_reg_name(handle, op->mem.base));
41  }
42  pj_kN(pj, "disp", op->mem.disp);
43  break;
44  default:
45  pj_ks(pj, "type", "invalid");
46  break;
47  }
48  pj_end(pj); /* o operand */
49  }
50  pj_end(pj); /* a operands */
51  pj_end(pj);
52 
55  pj_free(pj);
56 }
57 
59  csh handle;
60  cs_insn *insn;
62  int ret = cs_open(CS_ARCH_SYSZ, mode, &handle);
63  if (ret == CS_ERR_OK) {
65  // capstone-next
66  int n = cs_disasm(handle, (const ut8 *)buf, len, addr, 1, &insn);
67  if (n < 1) {
69  } else {
71  opex(&op->opex, handle, insn);
72  }
73  op->size = insn->size;
74  switch (insn->id) {
75  case SYSZ_INS_BRCL:
76  case SYSZ_INS_BRASL:
78  break;
79  case SYSZ_INS_BR:
81  break;
82  case SYSZ_INS_BRC:
83  case SYSZ_INS_BER:
84  case SYSZ_INS_BHR:
85  case SYSZ_INS_BHER:
86  case SYSZ_INS_BLR:
87  case SYSZ_INS_BLER:
88  case SYSZ_INS_BLHR:
89  case SYSZ_INS_BNER:
90  case SYSZ_INS_BNHR:
91  case SYSZ_INS_BNHER:
92  case SYSZ_INS_BNLR:
93  case SYSZ_INS_BNLER:
94  case SYSZ_INS_BNLHR:
95  case SYSZ_INS_BNOR:
96  case SYSZ_INS_BOR:
97  case SYSZ_INS_BASR:
98  case SYSZ_INS_BRAS:
99  case SYSZ_INS_BRCT:
100  case SYSZ_INS_BRCTG:
102  break;
103  case SYSZ_INS_JE:
104  case SYSZ_INS_JGE:
105  case SYSZ_INS_JHE:
106  case SYSZ_INS_JGHE:
107  case SYSZ_INS_JH:
108  case SYSZ_INS_JGH:
109  case SYSZ_INS_JLE:
110  case SYSZ_INS_JGLE:
111  case SYSZ_INS_JLH:
112  case SYSZ_INS_JGLH:
113  case SYSZ_INS_JL:
114  case SYSZ_INS_JGL:
115  case SYSZ_INS_JNE:
116  case SYSZ_INS_JGNE:
117  case SYSZ_INS_JNHE:
118  case SYSZ_INS_JGNHE:
119  case SYSZ_INS_JNH:
120  case SYSZ_INS_JGNH:
121  case SYSZ_INS_JNLE:
122  case SYSZ_INS_JGNLE:
123  case SYSZ_INS_JNLH:
124  case SYSZ_INS_JGNLH:
125  case SYSZ_INS_JNL:
126  case SYSZ_INS_JGNL:
127  case SYSZ_INS_JNO:
128  case SYSZ_INS_JGNO:
129  case SYSZ_INS_JO:
130  case SYSZ_INS_JGO:
131  case SYSZ_INS_JG:
133  op->jump = INSOP(0).imm;
134  op->fail = addr + op->size;
135  break;
136  case SYSZ_INS_J:
137  op->type = RZ_ANALYSIS_OP_TYPE_JMP;
138  op->jump = INSOP(0).imm;
139  op->fail = UT64_MAX;
140  break;
141  }
142  }
143  cs_free(insn, n);
144  cs_close(&handle);
145  }
146  return op->size;
147 }
148 
149 static char *get_reg_profile(RzAnalysis *analysis) {
150  const char *p =
151  "=PC r15\n"
152  "=LR r14\n"
153  "=SP r13\n"
154  "=BP r12\n"
155  "=A0 r0\n"
156  "=A1 r1\n"
157  "=A2 r2\n"
158  "=A3 r3\n"
159  "=SN r0\n"
160  "gpr sb .32 36 0\n" // r9
161  "gpr sl .32 40 0\n" // rl0
162  "gpr fp .32 44 0\n" // r11
163  "gpr ip .32 48 0\n" // r12
164  "gpr sp .32 52 0\n" // r13
165  "gpr lr .32 56 0\n" // r14
166  "gpr pc .32 60 0\n" // r15
167 
168  "gpr r0 .32 0 0\n"
169  "gpr r1 .32 4 0\n"
170  "gpr r2 .32 8 0\n"
171  "gpr r3 .32 12 0\n"
172  "gpr r4 .32 16 0\n"
173  "gpr r5 .32 20 0\n"
174  "gpr r6 .32 24 0\n"
175  "gpr r7 .32 28 0\n"
176  "gpr r8 .32 32 0\n"
177  "gpr r9 .32 36 0\n"
178  "gpr r10 .32 40 0\n"
179  "gpr r11 .32 44 0\n"
180  "gpr r12 .32 48 0\n"
181  "gpr r13 .32 52 0\n"
182  "gpr r14 .32 56 0\n"
183  "gpr r15 .32 60 0\n";
184  return strdup(p);
185 }
186 
187 static int archinfo(RzAnalysis *analysis, int q) {
188  switch (q) {
190  return 2;
192  return 4;
194  return 2;
195  }
196  return 2;
197 }
198 
200  .name = "sysz",
201  .desc = "Capstone SystemZ microanalysis",
202  .esil = false,
203  .license = "BSD",
204  .arch = "sysz",
205  .bits = 32 | 64,
206  .op = &analop,
207  .archinfo = archinfo,
208  .get_reg_profile = &get_reg_profile,
209 };
210 
211 #ifndef RZ_PLUGIN_INCORE
214  .data = &rz_analysis_plugin_sysz,
216 };
217 #endif
size_t len
Definition: 6502dis.c:15
#define mask()
static char * get_reg_profile(RzAnalysis *analysis)
#define INSOP(n)
Definition: analysis_sysz.c:14
static void opex(RzStrBuf *buf, csh handle, cs_insn *insn)
Definition: analysis_sysz.c:16
static int analop(RzAnalysis *a, RzAnalysisOp *op, ut64 addr, const ut8 *buf, int len, RzAnalysisOpMask mask)
Definition: analysis_sysz.c:58
RZ_API RzLibStruct rizin_plugin
static int archinfo(RzAnalysis *analysis, int q)
RzAnalysisPlugin rz_analysis_plugin_sysz
lzma_index ** i
Definition: index.h:629
static mcore_handle handle
Definition: asm_mcore.c:8
@ CS_ARCH_SYSZ
SystemZ architecture.
Definition: capstone.h:81
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
@ SYSZ_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: systemz.h:42
@ SYSZ_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: systemz.h:41
@ SYSZ_OP_REG
= CS_OP_REG (Register operand).
Definition: systemz.h:40
@ SYSZ_REG_INVALID
Definition: systemz.h:48
@ SYSZ_INS_JGNL
Definition: systemz.h:377
@ SYSZ_INS_JGLH
Definition: systemz.h:311
@ SYSZ_INS_BNLR
Definition: systemz.h:357
@ SYSZ_INS_BRASL
Definition: systemz.h:407
@ SYSZ_INS_JGNO
Definition: systemz.h:386
@ SYSZ_INS_BR
Definition: systemz.h:405
@ SYSZ_INS_JGL
Definition: systemz.h:319
@ SYSZ_INS_JNH
Definition: systemz.h:349
@ SYSZ_INS_JH
Definition: systemz.h:195
@ SYSZ_INS_BLR
Definition: systemz.h:299
@ SYSZ_INS_BHR
Definition: systemz.h:185
@ SYSZ_INS_JGNLE
Definition: systemz.h:360
@ SYSZ_INS_JGNE
Definition: systemz.h:332
@ SYSZ_INS_JGNHE
Definition: systemz.h:342
@ SYSZ_INS_J
Definition: systemz.h:408
@ SYSZ_INS_JNO
Definition: systemz.h:385
@ SYSZ_INS_JNL
Definition: systemz.h:376
@ SYSZ_INS_JGH
Definition: systemz.h:196
@ SYSZ_INS_BNLHR
Definition: systemz.h:367
@ SYSZ_INS_BER
Definition: systemz.h:176
@ SYSZ_INS_JNLE
Definition: systemz.h:359
@ SYSZ_INS_BNER
Definition: systemz.h:330
@ SYSZ_INS_BRC
Definition: systemz.h:166
@ SYSZ_INS_BRCTG
Definition: systemz.h:411
@ SYSZ_INS_JGE
Definition: systemz.h:178
@ SYSZ_INS_BRAS
Definition: systemz.h:406
@ SYSZ_INS_JHE
Definition: systemz.h:187
@ SYSZ_INS_BASR
Definition: systemz.h:404
@ SYSZ_INS_JNLH
Definition: systemz.h:368
@ SYSZ_INS_BLHR
Definition: systemz.h:309
@ SYSZ_INS_BLER
Definition: systemz.h:300
@ SYSZ_INS_BNOR
Definition: systemz.h:384
@ SYSZ_INS_BOR
Definition: systemz.h:393
@ SYSZ_INS_JGO
Definition: systemz.h:395
@ SYSZ_INS_BNHR
Definition: systemz.h:339
@ SYSZ_INS_JGNH
Definition: systemz.h:350
@ SYSZ_INS_BRCT
Definition: systemz.h:410
@ SYSZ_INS_BRCL
Definition: systemz.h:167
@ SYSZ_INS_JO
Definition: systemz.h:394
@ SYSZ_INS_BNLER
Definition: systemz.h:358
@ SYSZ_INS_BHER
Definition: systemz.h:186
@ SYSZ_INS_JLE
Definition: systemz.h:301
@ SYSZ_INS_JGHE
Definition: systemz.h:188
@ SYSZ_INS_JGNLH
Definition: systemz.h:369
@ SYSZ_INS_BNHER
Definition: systemz.h:340
@ SYSZ_INS_JL
Definition: systemz.h:318
@ SYSZ_INS_JGLE
Definition: systemz.h:302
@ SYSZ_INS_JNE
Definition: systemz.h:331
@ SYSZ_INS_JLH
Definition: systemz.h:310
@ SYSZ_INS_JNHE
Definition: systemz.h:341
@ SYSZ_INS_JG
Definition: systemz.h:409
@ SYSZ_INS_JE
Definition: systemz.h:177
#define RZ_API
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
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")
int x
Definition: mipsasm.c:20
int n
Definition: mipsasm.c:19
int CS_ERR_OK
Definition: __init__.py:235
#define RZ_ANALYSIS_ARCHINFO_ALIGN
Definition: rz_analysis.h:100
#define RZ_ANALYSIS_ARCHINFO_MAX_OP_SIZE
Definition: rz_analysis.h:99
RzAnalysisOpMask
Definition: rz_analysis.h:439
@ RZ_ANALYSIS_OP_MASK_OPEX
Definition: rz_analysis.h:444
#define RZ_ANALYSIS_ARCHINFO_MIN_OP_SIZE
Definition: rz_analysis.h:98
@ RZ_ANALYSIS_OP_TYPE_JMP
Definition: rz_analysis.h:368
@ RZ_ANALYSIS_OP_TYPE_CALL
Definition: rz_analysis.h:378
@ RZ_ANALYSIS_OP_TYPE_CJMP
Definition: rz_analysis.h:373
@ RZ_ANALYSIS_OP_TYPE_ILL
Definition: rz_analysis.h:387
@ RZ_LIB_TYPE_ANALYSIS
Definition: rz_lib.h:73
RZ_API PJ * pj_ka(PJ *j, const char *k)
Definition: pj.c:163
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API const char * pj_string(PJ *pj)
Definition: pj.c:57
RZ_API void pj_free(PJ *j)
Definition: pj.c:34
RZ_API PJ * pj_o(PJ *j)
Definition: pj.c:75
RZ_API PJ * pj_ks(PJ *j, const char *k, const char *v)
Definition: pj.c:170
RZ_API PJ * pj_kN(PJ *j, const char *k, st64 n)
Definition: pj.c:128
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
#define UT64_MAX
Definition: rz_types_base.h:86
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
Instruction operand.
Definition: systemz.h:101
Definition: rz_pj.h:12
const char * version
Definition: rz_analysis.h:1239
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58