Rizin
unix-like reverse engineering framework and cli tools
test_ppc.c File Reference
#include <stdio.h>
#include <capstone/platform.h>
#include <capstone/capstone.h>

Go to the source code of this file.

Classes

struct  platform
 

Macros

#define PPC_CODE   "\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"
 
#define PPC_CODE2   "\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f"
 

Functions

static void print_string_hex (const char *comment, unsigned char *str, size_t len)
 
static const char * get_bc_name (int bc)
 
static void print_insn_detail (cs_insn *ins)
 
static void test ()
 
int main ()
 

Variables

static csh handle
 

Macro Definition Documentation

◆ PPC_CODE

#define PPC_CODE   "\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"

◆ PPC_CODE2

#define PPC_CODE2   "\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f"

Function Documentation

◆ get_bc_name()

static const char* get_bc_name ( int  bc)
static

Definition at line 31 of file test_ppc.c.

32 {
33  switch(bc) {
34  default:
35  case PPC_BC_INVALID:
36  return ("invalid");
37  case PPC_BC_LT:
38  return ("lt");
39  case PPC_BC_LE:
40  return ("le");
41  case PPC_BC_EQ:
42  return ("eq");
43  case PPC_BC_GE:
44  return ("ge");
45  case PPC_BC_GT:
46  return ("gt");
47  case PPC_BC_NE:
48  return ("ne");
49  case PPC_BC_UN:
50  return ("un");
51  case PPC_BC_NU:
52  return ("nu");
53  case PPC_BC_SO:
54  return ("so");
55  case PPC_BC_NS:
56  return ("ns");
57  }
58 }
@ PPC_BC_LE
Definition: ppc.h:21
@ PPC_BC_EQ
Definition: ppc.h:22
@ PPC_BC_SO
summary overflow
Definition: ppc.h:30
@ PPC_BC_UN
Definition: ppc.h:26
@ PPC_BC_NE
Definition: ppc.h:25
@ PPC_BC_GT
Definition: ppc.h:24
@ PPC_BC_NU
Definition: ppc.h:27
@ PPC_BC_NS
not summary overflow
Definition: ppc.h:31
@ PPC_BC_LT
Definition: ppc.h:20
@ PPC_BC_INVALID
Definition: ppc.h:19
@ PPC_BC_GE
Definition: ppc.h:23

References PPC_BC_EQ, PPC_BC_GE, PPC_BC_GT, PPC_BC_INVALID, PPC_BC_LE, PPC_BC_LT, PPC_BC_NE, PPC_BC_NS, PPC_BC_NU, PPC_BC_SO, and PPC_BC_UN.

Referenced by print_insn_detail().

◆ main()

int main ( void  )

Definition at line 181 of file test_ppc.c.

182 {
183  test();
184 
185  return 0;
186 }
static void test()
Definition: test_ppc.c:114

References test().

◆ print_insn_detail()

static void print_insn_detail ( cs_insn *  ins)
static

Definition at line 60 of file test_ppc.c.

61 {
62  cs_ppc *ppc;
63  int i;
64 
65  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
66  if (ins->detail == NULL)
67  return;
68 
69  ppc = &(ins->detail->ppc);
70  if (ppc->op_count)
71  printf("\top_count: %u\n", ppc->op_count);
72 
73  for (i = 0; i < ppc->op_count; i++) {
74  cs_ppc_op *op = &(ppc->operands[i]);
75  switch((int)op->type) {
76  default:
77  break;
78  case PPC_OP_REG:
79  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
80  break;
81  case PPC_OP_IMM:
82  printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
83  break;
84  case PPC_OP_MEM:
85  printf("\t\toperands[%u].type: MEM\n", i);
86  if (op->mem.base != PPC_REG_INVALID)
87  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
88  i, cs_reg_name(handle, op->mem.base));
89  if (op->mem.disp != 0)
90  printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
91 
92  break;
93  case PPC_OP_CRX:
94  printf("\t\toperands[%u].type: CRX\n", i);
95  printf("\t\t\toperands[%u].crx.scale: %d\n", i, op->crx.scale);
96  printf("\t\t\toperands[%u].crx.reg: %s\n", i, cs_reg_name(handle, op->crx.reg));
97  printf("\t\t\toperands[%u].crx.cond: %s\n", i, get_bc_name(op->crx.cond));
98  break;
99  }
100  }
101 
102  if (ppc->bc != 0)
103  printf("\tBranch code: %u\n", ppc->bc);
104 
105  if (ppc->bh != 0)
106  printf("\tBranch hint: %u\n", ppc->bh);
107 
108  if (ppc->update_cr0)
109  printf("\tUpdate-CR0: True\n");
110 
111  printf("\n");
112 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
Instruction operand.
Definition: ppc.h:283
Instruction structure.
Definition: ppc.h:294
@ PPC_OP_REG
= CS_OP_REG (Register operand).
Definition: ppc.h:44
@ PPC_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: ppc.h:45
@ PPC_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: ppc.h:46
@ PPC_OP_CRX
Condition Register field.
Definition: ppc.h:47
@ PPC_REG_INVALID
Definition: ppc.h:52
#define PRIx64
Definition: sysdefs.h:94
static csh handle
Definition: test_ppc.c:17
static const char * get_bc_name(int bc)
Definition: test_ppc.c:31
Definition: dis.c:32

References cs_reg_name(), get_bc_name(), handle, i, NULL, PPC_OP_CRX, PPC_OP_IMM, PPC_OP_MEM, PPC_OP_REG, PPC_REG_INVALID, printf(), and PRIx64.

Referenced by test().

◆ print_string_hex()

static void print_string_hex ( const char *  comment,
unsigned char *  str,
size_t  len 
)
static

Definition at line 19 of file test_ppc.c.

20 {
21  unsigned char *c;
22 
23  printf("%s", comment);
24  for (c = str; c < str + len; c++) {
25  printf("0x%02x ", *c & 0xff);
26  }
27 
28  printf("\n");
29 }
size_t len
Definition: 6502dis.c:15
#define c(i)
Definition: sha256.c:43

References c, len, printf(), and cmd_descs_generate::str.

Referenced by test().

◆ test()

static void test ( )
static

Definition at line 114 of file test_ppc.c.

115 {
116 #define PPC_CODE "\x43\x20\x0c\x07\x41\x56\xff\x17\x80\x20\x00\x00\x80\x3f\x00\x00\x10\x43\x23\x0e\xd0\x44\x00\x80\x4c\x43\x22\x02\x2d\x03\x00\x80\x7c\x43\x20\x14\x7c\x43\x20\x93\x4f\x20\x00\x21\x4c\xc8\x00\x21\x40\x82\x00\x14"
117 #define PPC_CODE2 "\x10\x60\x2a\x10\x10\x64\x28\x88\x7c\x4a\x5d\x0f"
118 
119  struct platform platforms[] = {
120  {
121  CS_ARCH_PPC,
123  (unsigned char*)PPC_CODE,
124  sizeof(PPC_CODE) - 1,
125  "PPC-64",
126  },
127  {
128  CS_ARCH_PPC,
130  (unsigned char*)PPC_CODE2,
131  sizeof(PPC_CODE2) - 1,
132  "PPC-64 + QPX",
133  },
134  };
135 
136  uint64_t address = 0x1000;
137  cs_insn *insn;
138  int i;
139  size_t count;
140 
141  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
142  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
143  if (err) {
144  printf("Failed on cs_open() with error returned: %u\n", err);
145  abort();
146  }
147 
149 
150  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
151  if (count) {
152  size_t j;
153 
154  printf("****************\n");
155  printf("Platform: %s\n", platforms[i].comment);
157  printf("Disasm:\n");
158 
159  for (j = 0; j < count; j++) {
160  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
161  print_insn_detail(&insn[j]);
162  }
163  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
164 
165  // free memory allocated by cs_disasm()
166  cs_free(insn, count);
167  } else {
168  printf("****************\n");
169  printf("Platform: %s\n", platforms[i].comment);
171  printf("ERROR: Failed to disasm given code!\n");
172  abort();
173  }
174 
175  printf("\n");
176 
177  cs_close(&handle);
178  }
179 }
static bool err
Definition: armass.c:435
@ CS_ARCH_PPC
PowerPC architecture.
Definition: capstone.h:79
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_BIG_ENDIAN
big-endian mode
Definition: capstone.h:123
@ CS_MODE_QPX
Quad Processing eXtensions mode (PPC)
Definition: capstone.h:116
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
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
cs_arch arch
Definition: cstool.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
struct platform platforms[]
Definition: fuzz_diff.c:18
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
unsigned long uint64_t
Definition: sftypes.h:28
Definition: inftree9.h:24
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_ppc.c:19
static void print_insn_detail(cs_insn *ins)
Definition: test_ppc.c:60
#define PPC_CODE2
#define PPC_CODE
mnemonic
Definition: z80asm.h:48

References arch, platform::comment, count, CS_ARCH_PPC, cs_close(), cs_disasm(), cs_free(), CS_MODE_BIG_ENDIAN, CS_MODE_QPX, cs_open(), CS_OPT_DETAIL, CS_OPT_ON, cs_option(), err, handle, i, platforms, PPC_CODE, PPC_CODE2, print_insn_detail(), print_string_hex(), printf(), and PRIx64.

Referenced by main().

Variable Documentation

◆ handle

csh handle
static

Definition at line 17 of file test_ppc.c.

Referenced by print_insn_detail(), and test().