Rizin
unix-like reverse engineering framework and cli tools
test_systemz.c
Go to the documentation of this file.
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3 
4 #include <stdio.h>
5 
6 #include <capstone/platform.h>
7 #include <capstone/capstone.h>
8 
9 struct platform {
10  cs_arch arch;
11  cs_mode mode;
12  unsigned char *code;
13  size_t size;
14  const char *comment;
15 };
16 
17 static csh handle;
18 
19 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
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 }
30 
31 static void print_insn_detail(cs_insn *ins)
32 {
33  cs_sysz *sysz;
34  int i;
35 
36  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37  if (ins->detail == NULL)
38  return;
39 
40  sysz = &(ins->detail->sysz);
41  if (sysz->op_count)
42  printf("\top_count: %u\n", sysz->op_count);
43 
44  for (i = 0; i < sysz->op_count; i++) {
45  cs_sysz_op *op = &(sysz->operands[i]);
46  switch((int)op->type) {
47  default:
48  break;
49  case SYSZ_OP_REG:
50  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
51  break;
52  case SYSZ_OP_ACREG:
53  printf("\t\toperands[%u].type: ACREG = %u\n", i, op->reg);
54  break;
55  case SYSZ_OP_IMM:
56  printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
57  break;
58  case SYSZ_OP_MEM:
59  printf("\t\toperands[%u].type: MEM\n", i);
60  if (op->mem.base != SYSZ_REG_INVALID)
61  printf("\t\t\toperands[%u].mem.base: REG = %s\n",
62  i, cs_reg_name(handle, op->mem.base));
63  if (op->mem.index != SYSZ_REG_INVALID)
64  printf("\t\t\toperands[%u].mem.index: REG = %s\n",
65  i, cs_reg_name(handle, op->mem.index));
66  if (op->mem.length != 0)
67  printf("\t\t\toperands[%u].mem.length: 0x%" PRIx64 "\n", i, op->mem.length);
68  if (op->mem.disp != 0)
69  printf("\t\t\toperands[%u].mem.disp: 0x%" PRIx64 "\n", i, op->mem.disp);
70 
71  break;
72  }
73  }
74 
75  if (sysz->cc != 0)
76  printf("\tCode condition: %u\n", sysz->cc);
77 
78  printf("\n");
79 }
80 
81 static void test()
82 {
83 #define SYSZ_CODE "\xed\x00\x00\x00\x00\x1a\x5a\x0f\x1f\xff\xc2\x09\x80\x00\x00\x00\x07\xf7\xeb\x2a\xff\xff\x7f\x57\xe3\x01\xff\xff\x7f\x57\xeb\x00\xf0\x00\x00\x24\xb2\x4f\x00\x78\xec\x18\x00\x00\xc1\x7f"
84 
85  struct platform platforms[] = {
86  {
89  (unsigned char*)SYSZ_CODE,
90  sizeof(SYSZ_CODE) - 1,
91  "SystemZ",
92  },
93  };
94 
95  uint64_t address = 0x1000;
96  cs_insn *insn;
97  int i;
98  size_t count;
99 
100  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
101  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
102  if (err) {
103  printf("Failed on cs_open() with error returned: %u\n", err);
104  abort();
105  }
106 
108 
109  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
110  if (count) {
111  size_t j;
112 
113  printf("****************\n");
114  printf("Platform: %s\n", platforms[i].comment);
116  printf("Disasm:\n");
117 
118  for (j = 0; j < count; j++) {
119  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
120  print_insn_detail(&insn[j]);
121  }
122  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
123 
124  // free memory allocated by cs_disasm()
125  cs_free(insn, count);
126  } else {
127  printf("****************\n");
128  printf("Platform: %s\n", platforms[i].comment);
130  printf("ERROR: Failed to disasm given code!\n");
131  abort();
132  }
133 
134  printf("\n");
135 
136  cs_close(&handle);
137  }
138 }
139 
140 int main()
141 {
142  test();
143 
144  return 0;
145 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
cs_arch
Architecture type.
Definition: capstone.h:74
@ CS_ARCH_SYSZ
SystemZ architecture.
Definition: capstone.h:81
cs_mode
Mode type.
Definition: capstone.h:102
@ 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_ACREG
Access register operand.
Definition: systemz.h:43
@ SYSZ_OP_REG
= CS_OP_REG (Register operand).
Definition: systemz.h:40
@ SYSZ_REG_INVALID
Definition: systemz.h:48
#define NULL
Definition: cris-opc.c:27
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
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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
#define c(i)
Definition: sha256.c:43
Definition: inftree9.h:24
Instruction operand.
Definition: systemz.h:101
sysz_cc cc
Definition: systemz.h:112
cs_sysz_op operands[6]
operands for this instruction.
Definition: systemz.h:116
uint8_t op_count
Definition: systemz.h:115
unsigned char * code
#define PRIx64
Definition: sysdefs.h:94
static csh handle
Definition: test_systemz.c:17
#define SYSZ_CODE
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_systemz.c:19
static void print_insn_detail(cs_insn *ins)
Definition: test_systemz.c:31
static void test()
Definition: test_systemz.c:81
int main()
Definition: test_systemz.c:140
Definition: dis.c:32
mnemonic
Definition: z80asm.h:48