Rizin
unix-like reverse engineering framework and cli tools
asm_arc.c File Reference
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <rz_types.h>
#include <rz_lib.h>
#include <rz_util.h>
#include <rz_asm.h>
#include "disas-asm.h"
#include <mybfd.h>

Go to the source code of this file.

Functions

int decodeInstr (bfd_vma address, disassemble_info *info)
 
int ARCTangent_decodeInstr (bfd_vma address, disassemble_info *info)
 
int ARCompact_decodeInstr (bfd_vma address, disassemble_info *info)
 
static int arc_buffer_read_memory (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
 
static int symbol_at_address (bfd_vma addr, struct disassemble_info *info)
 
static void memory_error_func (int status, bfd_vma memaddr, struct disassemble_info *info)
 
static int disassemble (RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
 

Variables

static ut32 Offset = 0
 
static RzStrBufbuf_global = NULL
 
static int buf_len = 0
 
static ut8 bytes [32] = { 0 }
 
RzAsmPlugin rz_asm_plugin_arc
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ arc_buffer_read_memory()

static int arc_buffer_read_memory ( bfd_vma  memaddr,
bfd_byte myaddr,
unsigned int  length,
struct disassemble_info info 
)
static

Definition at line 25 of file asm_arc.c.

25  {
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 }
static ut8 bytes[32]
Definition: asm_arc.c:23
static int buf_len
Definition: asm_arc.c:22
static ut32 Offset
Definition: asm_arc.c:20
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
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define RZ_MIN(x, y)
static st64 delta
Definition: vmenus.c:2425

References buf_len, bytes, delta, length, memcpy(), Offset, and RZ_MIN.

Referenced by disassemble().

◆ ARCompact_decodeInstr()

int ARCompact_decodeInstr ( bfd_vma  address,
disassemble_info info 
)

Definition at line 3722 of file arcompact-dis.c.

3723  {
3724  int status;
3725  bfd_byte buffer[4];
3726  struct arcDisState s; /* ARC Disassembler state */
3727  void *stream = info->stream; /* output stream */
3728  fprintf_ftype func = info->fprintf_func;
3729  int bytes;
3730  int lowbyte, highbyte;
3731  char buf[256];
3732 
3733  if (info->disassembler_options) {
3734  parse_disassembler_options(info->disassembler_options);
3735 
3736  /* To avoid repeated parsing of these options, we remove them here. */
3737  info->disassembler_options = NULL;
3738  }
3739 
3740  lowbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 1 : 0);
3741  highbyte = ((info->endian == BFD_ENDIAN_LITTLE) ? 0 : 1);
3742 
3743  memset(&s, 0, sizeof(struct arcDisState));
3744 
3745  /* read first instruction */
3746  status = (*info->read_memory_func)(address, buffer, 2, info);
3747 
3748  if (status != 0) {
3749  (*info->memory_error_func)(status, address, info);
3750  return -1;
3751  }
3752 
3753  if (((buffer[lowbyte] & 0xf8) > 0x38) && ((buffer[lowbyte] & 0xf8) != 0x48)) {
3754  s.instructionLen = 2;
3755  s.words[0] = (buffer[lowbyte] << 8) | buffer[highbyte];
3756  (*info->read_memory_func)(address + 2, buffer, 4, info);
3757  if (info->endian == BFD_ENDIAN_LITTLE) {
3758  s.words[1] = bfd_getl32(buffer);
3759  } else {
3760  s.words[1] = bfd_getb32(buffer);
3761  }
3762  } else {
3763  s.instructionLen = 4;
3764  status = (*info->read_memory_func)(address + 2, &buffer[2], 2, info);
3765  if (status != 0) {
3766  (*info->memory_error_func)(status, address + 2, info);
3767  return -1;
3768  }
3769  if (info->endian == BFD_ENDIAN_LITTLE) {
3770  s.words[0] = bfd_getl32(buffer);
3771  } else {
3772  s.words[0] = bfd_getb32(buffer);
3773  }
3774 
3775  /* always read second word in case of limm */
3776  /* we ignore the result since last insn may not have a limm */
3777  (*info->read_memory_func)(address + 4, buffer, 4, info);
3778  if (info->endian == BFD_ENDIAN_LITTLE) {
3779  s.words[1] = bfd_getl32(buffer);
3780  } else {
3781  s.words[1] = bfd_getb32(buffer);
3782  }
3783  }
3784 
3785  s._this = &s;
3786  s.coreRegName = _coreRegName;
3787  s.auxRegName = _auxRegName;
3788  s.condCodeName = _condCodeName;
3789  s.instName = _instName;
3790 
3791  /* disassemble */
3792  bytes = dsmOneArcInst(address, (void *)&s, info);
3793 
3794  /* display the disassembled instruction */
3795  {
3796  char *instr = s.instrBuffer;
3797  char *operand = s.operandBuffer;
3798  char *space = strchr(instr, ' ');
3799 
3800  if (enable_insn_stream) {
3801  /* Show instruction stream from MSB to LSB*/
3802 
3803  if (s.instructionLen == 2) {
3804  (*func)(stream, " %04x ", (unsigned int)s.words[0]);
3805  } else {
3806  (*func)(stream, "%08x ", (unsigned int)s.words[0]);
3807  }
3808 
3809  (*func)(stream, " ");
3810  }
3811 
3812  /* if the operand is actually in the instruction buffer */
3813  if ((space != NULL) && (operand[0] == '\0')) {
3814  *space = '\0';
3815  operand = space + 1;
3816  }
3817 
3818  (*func)(stream, "%s ", instr);
3819 
3820  if (__TRANSLATION_REQUIRED(s)) {
3821  bfd_vma addr;
3822  char *tmpBuffer;
3823  int i = 1;
3824 
3825  if (operand[0] != '@') {
3826  /* Branch instruction with 3 operands, Translation is required
3827  only for the third operand. Print the first 2 operands */
3828  strncpy(buf, operand, sizeof(buf));
3829  buf[sizeof(buf) - 1] = '\0';
3830  tmpBuffer = strtok(buf, "@");
3831  (*func)(stream, "%s", tmpBuffer);
3832  i = strlen(tmpBuffer) + 1;
3833  }
3834 
3835  addr = s.addresses[operand[i] - '0'];
3836  (*info->print_address_func)((bfd_vma)addr, info);
3837  //(*func) (stream, "\n");
3838  } else {
3839  (*func)(stream, "%s", operand);
3840  }
3841  }
3842 
3843  /* We print max bytes for instruction */
3844  info->bytes_per_line = 8;
3845 
3846  return bytes; // s.instructionLen;
3847 }
lzma_index ** i
Definition: index.h:629
#define __TRANSLATION_REQUIRED(state)
Definition: arc-dis.h:110
operand
Definition: arc-opc.c:39
static int dsmOneArcInst(bfd_vma addr, struct arcDisState *state, disassemble_info *info)
static const char * _coreRegName(void *_this ATTRIBUTE_UNUSED, int v)
static const char * _instName(void *_this ATTRIBUTE_UNUSED, int op1, int op2, int *flags)
static const char * _auxRegName(void *_this ATTRIBUTE_UNUSED, int v)
static void parse_disassembler_options(char *options)
static const char * _condCodeName(void *_this ATTRIBUTE_UNUSED, int v)
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define NULL
Definition: cris-opc.c:27
int(* fprintf_ftype)(void *, const char *,...) ATTRIBUTE_FPTR_PRINTF_2
Definition: disas-asm.h:45
voidpf stream
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
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
static bfd_vma bfd_getb32(const void *p)
Definition: mybfd.h:4979
@ BFD_ENDIAN_LITTLE
Definition: mybfd.h:4618
static bfd_vma bfd_getl32(const void *p)
Definition: mybfd.h:4990
static RzSocket * s
Definition: rtr.c:28
static int
Definition: sfsocketcall.h:114
Definition: buffer.h:15
static int addr
Definition: z80asm.c:58

Referenced by disassemble().

◆ ARCTangent_decodeInstr()

int ARCTangent_decodeInstr ( bfd_vma  address,
disassemble_info info 
)

Definition at line 1025 of file arc-dis.c.

1025  {
1026  int status;
1027  bfd_byte buffer[4];
1028  struct arcDisState s; /* ARC Disassembler state. */
1029  void *stream = info->stream; /* Output stream. */
1030  fprintf_ftype func = info->fprintf_func;
1031 
1032  memset(&s, 0, sizeof(struct arcDisState));
1033 
1034  /* Read first instruction. */
1035  status = (*info->read_memory_func)(address, buffer, 4, info);
1036  if (status != 0) {
1037  (*info->memory_error_func)(status, address, info);
1038  return -1;
1039  }
1040  if (info->endian == BFD_ENDIAN_LITTLE) {
1041  s.words[0] = bfd_getl32(buffer);
1042  } else {
1043  s.words[0] = bfd_getb32(buffer);
1044  }
1045  /* Always read second word in case of limm. */
1046  /* We ignore the result since last insn may not have a limm. */
1047  (*info->read_memory_func)(address + 4, buffer, 4, info);
1048  if (info->endian == BFD_ENDIAN_LITTLE) {
1049  s.words[1] = bfd_getl32(buffer);
1050  } else {
1051  s.words[1] = bfd_getb32(buffer);
1052  }
1053 
1054  s._this = &s;
1055  s.coreRegName = _coreRegName;
1056  s.auxRegName = _auxRegName;
1057  s.condCodeName = _condCodeName;
1058  s.instName = _instName;
1059 
1060  /* Disassemble. */
1061  dsmOneArcInst(address, (void *)&s);
1062 
1063  /* Display the disassembly instruction. */
1064  /*
1065  (*func) (stream, "%08lx ", s.words[0]);
1066  (*func) (stream, " ");
1067  */
1068  (*func)(stream, "%s ", s.instrBuffer);
1069 
1070  if (__TRANSLATION_REQUIRED(s)) {
1071  bfd_vma addr = s.addresses[s.operandBuffer[1] - '0'];
1072 
1073  (*info->print_address_func)((bfd_vma)addr, info);
1074  //(*func) (stream, "\n");
1075  } else {
1076  (*func)(stream, "%s", s.operandBuffer);
1077  }
1078 
1079  return s.instructionLen;
1080 }
static const char * _condCodeName(void *cpp_this ATTRIBUTE_UNUSED, int num)
Definition: arc-dis.c:1007
static const char * _auxRegName(void *cpp_this ATTRIBUTE_UNUSED, int regnum)
Definition: arc-dis.c:999
static const char * _instName(void *_this ATTRIBUTE_UNUSED, int majop, int minop, int *flags)
Definition: arc-dis.c:1017
static int dsmOneArcInst(bfd_vma addr, struct arcDisState *state)
Definition: arc-dis.c:459
static const char * _coreRegName(void *cpp_this ATTRIBUTE_UNUSED, int regnum)
Definition: arc-dis.c:991

References __TRANSLATION_REQUIRED, _auxRegName(), _condCodeName(), _coreRegName(), _instName(), addr, BFD_ENDIAN_LITTLE, bfd_getb32(), bfd_getl32(), dsmOneArcInst(), info(), memset(), s, and status.

Referenced by disassemble().

◆ decodeInstr()

int decodeInstr ( bfd_vma  address,
disassemble_info info 
)

◆ disassemble()

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

Definition at line 48 of file asm_arc.c.

48  {
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;
64  disasm_obj.read_memory_func = &arc_buffer_read_memory;
65  disasm_obj.symbol_at_address_func = &symbol_at_address;
66  disasm_obj.memory_error_func = &memory_error_func;
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 }
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
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 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
int ARCompact_decodeInstr(bfd_vma address, disassemble_info *info)
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
#define a(i)
Definition: sha256.c:41
Definition: dis.c:32

References a, arc_buffer_read_memory(), ARCompact_decodeInstr(), ARCTangent_decodeInstr(), buf_global, buf_len, disassemble_info::buffer, disassemble_info::buffer_length, bytes, disassemble_info::endian, disassemble_info::fprintf_func, len, disassemble_info::mach, memcpy(), memory_error_func(), disassemble_info::memory_error_func, memset(), Offset, disassemble_info::print_address_func, disassemble_info::read_memory_func, rz_strbuf_set(), disassemble_info::stream, symbol_at_address(), and disassemble_info::symbol_at_address_func.

◆ memory_error_func()

static void memory_error_func ( int  status,
bfd_vma  memaddr,
struct disassemble_info info 
)
static

Definition at line 41 of file asm_arc.c.

41  {
42  //--
43 }

Referenced by disassemble().

◆ symbol_at_address()

static int symbol_at_address ( bfd_vma  addr,
struct disassemble_info info 
)
static

Definition at line 37 of file asm_arc.c.

37  {
38  return 0;
39 }

Referenced by disassemble().

Variable Documentation

◆ buf_global

RzStrBuf* buf_global = NULL
static

Definition at line 21 of file asm_arc.c.

Referenced by disassemble().

◆ buf_len

◆ bytes

ut8 bytes[32] = { 0 }
static

Definition at line 23 of file asm_arc.c.

Referenced by __reg_read(), _cb_hit(), _cs_disasm(), annotated_hexdump(), arc_buffer_read_memory(), arcAnalyzeInstr(), bfd_get_bits(), cabd_checksum(), cabd_extract(), cabd_sys_read(), cabd_sys_write(), cabx_alloc(), cabx_copy(), cabx_read(), cabx_write(), carve_kexts(), check_buffer(), chmd_extract(), chmd_sys_write(), core_cmd_pipe(), decode_abs162r16(), decode_abs2r_short(), decode_add(), decode_adds(), decode_andc(), decode_arith(), decode_break(), decode_bsr(), decode_call(), decode_cmp(), decode_cmpeq(), decode_cmpgte(), decode_cmpi(), decode_cmplte(), decode_cmpugte(), decode_cmpulte(), decode_daa(), decode_disp162r16(), decode_div(), decode_divu(), decode_eepmov(), decode_imm162r16(), decode_imm2abs8(), decode_imm2ind16(), decode_imm2r8(), decode_imm2r_short(), decode_ind162r16(), decode_indinc162r16(), decode_jmp(), decode_jmp8(), decode_jmp_abs16(), decode_jmp_abs8(), decode_jmp_ind(), decode_ldc(), decode_loadsp(), decode_mov(), decode_mov_args(), decode_movi(), decode_movin(), decode_movn(), decode_movrel(), decode_movsn(), decode_movsn_args(), decode_mul(), decode_mulu(), decode_neg(), decode_nop(), decode_not(), decode_opcode(), decode_opcode_4bit(), decode_pop(), decode_push_pop(), decode_r162r16(), decode_r2r8(), decode_r82abs16(), decode_r82dispr16(), decode_r82ind16(), decode_r82r16(), decode_r82rdec16(), decode_rzabs8(), decode_rzimm_short(), decode_rzind16(), decode_storesp(), decode_sub(), decode_subs(), deserialize_checkpoints_cb(), disassemble(), disassembly_as_table(), esil_peek_n(), esil_poke_n(), extract_arg(), extract_binobj(), flirt_pat_parse_pattern_mask(), fs__create_junction(), fs__read(), fs__readlink_handle(), fs__write(), FUZ_test(), test-lz4-list::generate_files(), go_is_sign_match(), go_offset(), go_uintptr(), interact_fix_asm(), iterate_rebase_list(), iterator_compare(), load_buffer(), lzxd_set_reference_data(), m_alloc(), m_copy(), m_read(), m_write(), main(), md5_finish_ctx(), mem_alloc(), mem_copy(), mem_read(), mem_write(), msp_alloc(), msp_copy(), msp_free(), msp_read(), msp_write(), noned_decompress(), print_hex_from_base2(), TestM680x::print_ins_detail(), process_kmod_init_term(), rasm_asm(), replace_asm_test(), rz_bin_file_set_bytes(), rz_bin_file_xtr_load_buffer(), rz_bp_add_sw(), rz_buf_new_with_bytes(), rz_buf_new_with_pointers(), rz_bv_set_to_bytes_be(), rz_bv_set_to_bytes_le(), rz_cmd_debug(), rz_cmd_disassemble_ropchain_handler(), rz_cmd_disassembly_function_handler(), rz_core_analysis_resolve_golang_strings(), rz_debug_qnx_reg_write(), rz_file_slurp_hexpairs(), rz_main_rz_gg(), rz_mem_copybits(), RZ_PACKED(), rz_print_hexdump_str(), rz_rebase_info_new_from_mach0(), rz_reg_set_value(), rz_search_pattern(), rz_str_is_utf8(), rz_test_load_asm_test_file(), rz_test_run_asm_test(), rz_type_format_data_internal(), rz_type_format_num(), rz_type_format_num_specifier(), rz_write_extend_hexbytes_handler(), showanalysis(), stack__subtree_is_equivalent(), tar(), test(), ts_parser__can_reuse_first_leaf(), ts_parser__lex(), ts_subtree_get_changed_ranges(), tuklib_mbstr_width(), uv__count_bufs(), uv__pipe_write_data(), uv__poll_wine(), uv__send(), uv__signal_event(), uv__tcp_try_write(), uv__udp_try_send(), uv_get_extension_function(), uv_pipe_writefile_thread_proc(), uv_pipe_zero_readfile_thread_proc(), uv_poll_init_socket(), uv_process_tcp_read_req(), uv_process_tty_read_line_req(), uv_process_udp_recv_req(), uv_tcp_queue_accept(), uv_tcp_queue_read(), uv_tcp_try_cancel_io(), uv_tcp_try_connect(), uv_tcp_write(), uv_tty_line_read_thread(), uv_udp_queue_recv(), uv_wsarecv_workaround(), uv_wsarecvfrom_workaround(), wasm_dis(), and xap_op().

◆ Offset

ut32 Offset = 0
static

Definition at line 20 of file asm_arc.c.

Referenced by arc_buffer_read_memory(), ARM_AM_getAM5Opc(), disassemble(), and getAM3Opc().

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_ASM,
}
RzAsmPlugin rz_asm_plugin_arc
Definition: asm_arc.c:84
@ 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 95 of file asm_arc.c.

◆ rz_asm_plugin_arc

RzAsmPlugin rz_asm_plugin_arc
Initial value:
= {
.name = "arc",
.arch = "arc",
.bits = 16 | 32,
.desc = "Argonaut RISC Core",
.disassemble = &disassemble,
.license = "GPL3"
}
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_arc.c:48
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526

Definition at line 84 of file asm_arc.c.