Rizin
unix-like reverse engineering framework and cli tools
asm_ppc_cs.c File Reference
#include <rz_asm.h>
#include <rz_lib.h>
#include <capstone/capstone.h>
#include "../arch/ppc/libvle/vle.h"
#include "../arch/ppc/libps/libps.h"

Go to the source code of this file.

Functions

static bool the_end (void *p)
 
static int decompile_vle (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 
static int decompile_ps (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 
static int disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 

Variables

static csh handle = 0
 
RzAsmPlugin rz_asm_plugin_ppc_cs
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ decompile_ps()

static int decompile_ps ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 40 of file asm_ppc_cs.c.

40  {
41  ppcps_t instr = { 0 };
42  if (len < 4) {
43  return -1;
44  }
45  op->size = 4;
46  const ut32 data = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
47  if (libps_decode(data, &instr) < 1) {
48  rz_asm_op_set_asm(op, "invalid");
49  return -1;
50  }
51  char buf_asm[64];
52  libps_snprint(buf_asm, sizeof(buf_asm), a->pc, &instr);
53  rz_asm_op_set_asm(op, buf_asm);
54  return op->size;
55 }
size_t len
Definition: 6502dis.c:15
RZ_API void rz_asm_op_set_asm(RzAsmOp *op, const char *str)
Definition: aop.c:53
uint32_t ut32
voidpf void * buf
Definition: ioapi.h:138
void libps_snprint(char *str, int size, ut64 addr, ppcps_t *instr)
Definition: libps.c:177
bool libps_decode(ut32 data, ppcps_t *ps)
Definition: libps.c:97
#define a(i)
Definition: sha256.c:41
Definition: libps.h:13
Definition: dis.c:32

References a, len, libps_decode(), libps_snprint(), and rz_asm_op_set_asm().

Referenced by disassemble().

◆ decompile_vle()

static int decompile_vle ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 20 of file asm_ppc_cs.c.

20  {
21  vle_t *instr = 0;
22  vle_handle handle = { 0 };
23  if (len < 2) {
24  return -1;
25  }
26  if (!vle_init(&handle, buf, len) && (instr = vle_next(&handle))) {
27  op->size = instr->size;
28  char buf_asm[64];
29  vle_snprint(buf_asm, sizeof(buf_asm), a->pc, instr);
30  rz_asm_op_set_asm(op, buf_asm);
31  vle_free(instr);
32  } else {
33  rz_asm_op_set_asm(op, "invalid");
34  op->size = 2;
35  return -1;
36  }
37  return op->size;
38 }
static csh handle
Definition: asm_ppc_cs.c:10
Definition: vle.h:18
Definition: vle.h:30
ut16 size
Definition: vle.h:34
vle_t * vle_next(vle_handle *handle)
Definition: vle.c:889
int vle_init(vle_handle *handle, const ut8 *buffer, const ut32 size)
Definition: vle.c:870
void vle_snprint(char *str, int size, ut32 addr, vle_t *instr)
Definition: vle.c:915
void vle_free(vle_t *instr)
Definition: vle.c:911

References a, handle, len, rz_asm_op_set_asm(), vle_t::size, vle_free(), vle_init(), vle_next(), and vle_snprint().

Referenced by disassemble().

◆ disassemble()

static int disassemble ( RzAsm a,
RzAsmOp op,
const ut8 buf,
int  len 
)
static

Definition at line 57 of file asm_ppc_cs.c.

57  {
58  static int omode = -1, obits = -1;
59  int n, ret, mode;
60  ut64 off = a->pc;
61  cs_insn *insn;
62  if (a->cpu && strncmp(a->cpu, "vle", 3) == 0) {
63  // vle is big-endian only
64  if (!a->big_endian) {
65  return -1;
66  }
67  ret = decompile_vle(a, op, buf, len);
68  if (ret >= 0) {
69  return op->size;
70  }
71  } else if (a->cpu && strncmp(a->cpu, "ps", 2) == 0) {
72  // libps is big-endian only
73  if (!a->big_endian) {
74  return -1;
75  }
76  ret = decompile_ps(a, op, buf, len);
77  if (ret >= 0) {
78  return op->size;
79  }
80  }
81  switch (a->bits) {
82  case 32:
83  mode = CS_MODE_32;
84  break;
85  case 64:
86  mode = CS_MODE_64;
87  break;
88  default:
89  mode = 0;
90  break;
91  }
93  if (mode != omode || a->bits != obits) {
94  cs_close(&handle);
95  handle = 0;
96  omode = mode;
97  obits = a->bits;
98  }
99  if (handle == 0) {
100  ret = cs_open(CS_ARCH_PPC, mode, &handle);
101  if (ret != CS_ERR_OK) {
102  return -1;
103  }
104  }
105  op->size = 4;
107  n = cs_disasm(handle, (const ut8 *)buf, len, off, 1, &insn);
108  op->size = 4;
109  if (n > 0 && insn->size > 0) {
110  const char *opstr = sdb_fmt("%s%s%s", insn->mnemonic,
111  insn->op_str[0] ? " " : "", insn->op_str);
113  cs_free(insn, n);
114  return op->size;
115  }
116  rz_asm_op_set_asm(op, "invalid");
117  op->size = 4;
118  cs_free(insn, n);
119  return op->size;
120 }
static int decompile_vle(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_ppc_cs.c:20
static int decompile_ps(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_ppc_cs.c:40
static int opstr(RzAsm *a, ut8 *data, const Opcode *op)
Definition: asm_x86_nz.c:4054
@ CS_ARCH_PPC
PowerPC architecture.
Definition: capstone.h:79
@ CS_MODE_64
64-bit mode (X86, PPC)
Definition: capstone.h:107
@ CS_MODE_32
32-bit mode (X86)
Definition: capstone.h:106
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_MODE_LITTLE_ENDIAN
little-endian mode (default mode)
Definition: capstone.h:103
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
@ CS_OPT_OFF
Turn OFF an option - default for CS_OPT_DETAIL, CS_OPT_SKIPDATA, CS_OPT_UNSIGNED.
Definition: capstone.h:182
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 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
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
const char int mode
Definition: ioapi.h:137
uint8_t ut8
Definition: lh5801.h:11
int n
Definition: mipsasm.c:19
int CS_ERR_OK
Definition: __init__.py:235
int off
Definition: pal.c:13
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References a, CS_ARCH_PPC, cs_close(), cs_disasm(), capstone::CS_ERR_OK, cs_free(), CS_MODE_32, CS_MODE_64, CS_MODE_BIG_ENDIAN, CS_MODE_LITTLE_ENDIAN, cs_open(), CS_OPT_DETAIL, CS_OPT_OFF, cs_option(), decompile_ps(), decompile_vle(), handle, len, n, off, opstr(), rz_asm_op_set_asm(), sdb_fmt(), and ut64().

◆ the_end()

static bool the_end ( void *  p)
static

Definition at line 12 of file asm_ppc_cs.c.

12  {
13  if (handle) {
14  cs_close(&handle);
15  handle = 0;
16  }
17  return true;
18 }

References cs_close(), and handle.

Referenced by rz_core_rtr_http_run().

Variable Documentation

◆ handle

csh handle = 0
static

Definition at line 10 of file asm_ppc_cs.c.

Referenced by decompile_vle(), disassemble(), and the_end().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_ASM,
}
RzAsmPlugin rz_asm_plugin_ppc_cs
Definition: asm_ppc_cs.c:122
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_asm.h:133

Definition at line 136 of file asm_ppc_cs.c.

◆ rz_asm_plugin_ppc_cs

RzAsmPlugin rz_asm_plugin_ppc_cs
Initial value:
= {
.name = "ppc",
.desc = "Capstone PowerPC disassembler",
.license = "BSD",
.author = "pancake",
.arch = "ppc",
.cpus = "ppc,vle,ps",
.bits = 32 | 64,
.fini = the_end,
.disassemble = &disassemble,
}
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_ppc_cs.c:57
static bool the_end(void *p)
Definition: asm_ppc_cs.c:12
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526

Definition at line 122 of file asm_ppc_cs.c.