Rizin
unix-like reverse engineering framework and cli tools
asm_arc.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2012-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <rz_types.h>
8 #include <rz_lib.h>
9 #include <rz_util.h>
10 #include <rz_asm.h>
11 #include "disas-asm.h"
12 #include <mybfd.h>
13 
14 /* extern */
18 
19 /* ugly globals */
20 static ut32 Offset = 0;
22 static int buf_len = 0;
23 static ut8 bytes[32] = { 0 };
24 
25 static int arc_buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info) {
26  int delta = (memaddr - Offset);
27  if (delta < 0) {
28  return -1; // disable backward reads
29  }
30  if ((delta + length) > sizeof(bytes)) {
31  return -1;
32  }
33  memcpy(myaddr, bytes + delta, RZ_MIN(buf_len - delta, length));
34  return 0;
35 }
36 
38  return 0;
39 }
40 
41 static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info) {
42  //--
43 }
44 
47 
48 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
49  static struct disassemble_info disasm_obj;
50  if (len < 2) {
51  return -1;
52  }
53  buf_global = &op->buf_asm;
54  Offset = a->pc;
55  if (len > sizeof(bytes)) {
56  len = sizeof(bytes);
57  }
58  memcpy(bytes, buf, len); // TODO handle compact
59  buf_len = len;
60  /* prepare disassembler */
61  memset(&disasm_obj, '\0', sizeof(struct disassemble_info));
62  disasm_obj.buffer = bytes;
63  disasm_obj.buffer_length = len;
67  disasm_obj.print_address_func = &generic_print_address_func;
68  disasm_obj.endian = !a->big_endian;
69  disasm_obj.fprintf_func = &generic_fprintf_func;
70  disasm_obj.stream = stdout;
71  disasm_obj.mach = 0;
72  rz_strbuf_set(&op->buf_asm, "");
73  if (a->bits == 16) {
74  op->size = ARCompact_decodeInstr((bfd_vma)Offset, &disasm_obj);
75  } else {
76  op->size = ARCTangent_decodeInstr((bfd_vma)Offset, &disasm_obj);
77  }
78  if (op->size == -1) {
79  rz_strbuf_set(&op->buf_asm, "(data)");
80  }
81  return op->size;
82 }
83 
85  .name = "arc",
86  .arch = "arc",
87  .bits = 16 | 32,
89  .desc = "Argonaut RISC Core",
90  .disassemble = &disassemble,
91  .license = "GPL3"
92 };
93 
94 #ifndef RZ_PLUGIN_INCORE
97  .data = &rz_asm_plugin_arc,
99 };
100 #endif
size_t len
Definition: 6502dis.c:15
static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info)
Definition: asm_arc.c:41
RzAsmPlugin rz_asm_plugin_arc
Definition: asm_arc.c:84
static int arc_buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
Definition: asm_arc.c:25
static ut8 bytes[32]
Definition: asm_arc.c:23
static int buf_len
Definition: asm_arc.c:22
RZ_API RzLibStruct rizin_plugin
Definition: asm_arc.c:95
static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
Definition: asm_arc.c:37
static RzStrBuf * buf_global
Definition: asm_arc.c:21
int ARCTangent_decodeInstr(bfd_vma address, disassemble_info *info)
Definition: arc-dis.c:1025
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_arc.c:48
int ARCompact_decodeInstr(bfd_vma address, disassemble_info *info)
static ut32 Offset
Definition: asm_arc.c:20
int decodeInstr(bfd_vma address, disassemble_info *info)
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
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
@ 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_BIG
Definition: rz_types.h:527
#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
unsigned long mach
Definition: disas-asm.h:81
fprintf_ftype fprintf_func
Definition: disas-asm.h:69
enum bfd_endian endian
Definition: disas-asm.h:83
unsigned int buffer_length
Definition: disas-asm.h:162
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