Rizin
unix-like reverse engineering framework and cli tools
asm_cris_gnu.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #if 0
5 
6 Documentation
7 -------------
8 http://developer.axis.com/old/documentation/hw/etraxfs/des_ref/des_ref.pdf
9 http://developer.axis.com/old/documentation/hw/etraxfs/iop_howto/iop_howto.pdf
10 
11 #endif
12 
13 #include <stdio.h>
14 #include <stdarg.h>
15 #include <string.h>
16 
17 #include <rz_types.h>
18 #include <rz_lib.h>
19 #include <rz_util.h>
20 #include <rz_asm.h>
21 
22 #include "disas-asm.h"
23 
24 static unsigned long Offset = 0;
26 static unsigned char bytes[8];
27 
28 static int cris_buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, ut32 length, struct disassemble_info *info) {
29  int delta = (memaddr - Offset);
30  if (delta < 0) {
31  return -1; // disable backward reads
32  }
33  if ((delta + length) > 8) {
34  return -1;
35  }
36  memcpy(myaddr, bytes + delta, length);
37  return 0;
38 }
39 
41  return 0;
42 }
43 
44 static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info) {
45  //--
46 }
47 
50 
52 
53 // TODO: refactor the gnu code to have a getter instead of exposing so many disasm entrypoints
60 
61 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
62  struct disassemble_info disasm_obj;
63  int mode = 2;
64  if (len < 4) {
65  return -1;
66  }
67  buf_global = &op->buf_asm;
68  Offset = a->pc;
69  memcpy(bytes, buf, RZ_MIN(len, 8)); // TODO handle thumb
70 
71  /* prepare disassembler */
72  memset(&disasm_obj, '\0', sizeof(struct disassemble_info));
73  disasm_obj.disassembler_options = (a->bits == 64) ? "64" : "";
74  disasm_obj.buffer = bytes;
78  disasm_obj.print_address_func = &generic_print_address_func;
79  disasm_obj.endian = !a->big_endian;
80  disasm_obj.fprintf_func = &generic_fprintf_func;
81  disasm_obj.stream = stdout;
82 
83  if (a->cpu && *a->cpu) {
84  // enum cris_disass_family { cris_dis_v0_v10, cris_dis_common_v10_v32, cris_dis_v32 };
85  // 0: v0-v10
86  // 1: v10-v32
87  // 2: v32
88  mode = 0;
89  if (strstr(a->cpu, "v10")) {
90  mode = 1;
91  }
92  if (strstr(a->cpu, "v32")) {
93  mode = 2;
94  }
95  } else {
96  mode = 2;
97  }
98  (void)cris_parse_disassembler_options(&disasm_obj, mode);
99  if (a->syntax == RZ_ASM_SYNTAX_ATT) {
100  switch (mode) {
101  case 0:
102  op->size = print_insn_cris_with_register_prefix((bfd_vma)Offset, &disasm_obj);
103  break;
104  case 1:
106  break;
107  default:
109  break;
110  }
111  } else {
112  switch (mode) {
113  case 0:
115  break;
116  case 1:
118  break;
119  default:
121  break;
122  }
123  }
124  if (op->size == -1) {
125  rz_strbuf_set(&op->buf_asm, "(data)");
126  }
127  return op->size;
128 }
129 
131  .name = "cris",
132  .arch = "cris",
133  .cpus = "v0,v10,v32",
134  .license = "GPL3",
135  .author = "pancake",
136  .bits = 32,
137  .endian = RZ_SYS_ENDIAN_LITTLE,
138  .desc = "Axis Communications 32-bit embedded processor",
139  .disassemble = &disassemble
140 };
141 
142 #ifndef RZ_PLUGIN_INCORE
145  .data = &rz_asm_plugin_cris_gnu,
147 };
148 #endif
size_t len
Definition: 6502dis.c:15
int print_insn_cris_without_register_prefix(bfd_vma vma, disassemble_info *info)
Definition: cris-dis.c:1594
static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info)
Definition: asm_cris_gnu.c:44
int print_insn_crisv32_without_register_prefix(bfd_vma vma, disassemble_info *info)
Definition: cris-dis.c:1606
int print_insn_cris_with_register_prefix(bfd_vma vma, disassemble_info *info)
Definition: cris-dis.c:1557
int print_insn_crisv32_with_register_prefix(bfd_vma vma, disassemble_info *info)
Definition: cris-dis.c:1569
int print_insn_crisv10_v32_with_register_prefix(bfd_vma vma, disassemble_info *info)
Definition: cris-dis.c:1582
RzAsmPlugin rz_asm_plugin_cris_gnu
Definition: asm_cris_gnu.c:130
static unsigned char bytes[8]
Definition: asm_cris_gnu.c:26
static unsigned long Offset
Definition: asm_cris_gnu.c:24
RZ_API RzLibStruct rizin_plugin
Definition: asm_cris_gnu.c:143
static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
Definition: asm_cris_gnu.c:40
static RzStrBuf * buf_global
Definition: asm_cris_gnu.c:25
static int cris_buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, ut32 length, struct disassemble_info *info)
Definition: asm_cris_gnu.c:28
bfd_boolean cris_parse_disassembler_options(disassemble_info *info, int distype)
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_cris_gnu.c:61
int print_insn_crisv10_v32_without_register_prefix(bfd_vma vma, disassemble_info *info)
Definition: cris-dis.c:1619
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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 static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
uint32_t ut32
#define DECLARE_GENERIC_FPRINTF_FUNC()
Definition: disas-asm.h:422
#define DECLARE_GENERIC_PRINT_ADDRESS_FUNC()
Definition: disas-asm.h:435
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define const
Definition: ansidecl.h:240
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
unsigned char bfd_byte
Definition: mybfd.h:176
BFD_HOST_U_64_BIT bfd_vma
Definition: mybfd.h:111
int bfd_boolean
Definition: mybfd.h:98
@ RZ_ASM_SYNTAX_ATT
Definition: rz_asm.h:51
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
#define RZ_MIN(x, y)
#define RZ_VERSION
Definition: rz_version.h:8
#define a(i)
Definition: sha256.c:41
fprintf_ftype fprintf_func
Definition: disas-asm.h:69
char * disassembler_options
Definition: disas-asm.h:214
enum bfd_endian endian
Definition: disas-asm.h:83
void(* memory_error_func)(int status, bfd_vma memaddr, struct disassemble_info *dinfo)
Definition: disas-asm.h:140
void(* print_address_func)(bfd_vma addr, struct disassemble_info *dinfo)
Definition: disas-asm.h:143
int(* symbol_at_address_func)(bfd_vma addr, struct disassemble_info *dinfo)
Definition: disas-asm.h:152
int(* read_memory_func)(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *dinfo)
Definition: disas-asm.h:133
bfd_byte * buffer
Definition: disas-asm.h:160
const char * name
Definition: rz_asm.h:130
const char * version
Definition: rz_asm.h:133
Definition: dis.c:32
static st64 delta
Definition: vmenus.c:2425
static int addr
Definition: z80asm.c:58