Rizin
unix-like reverse engineering framework and cli tools
asm_mips_gnu.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2018 pancake <pancake@nopcode.org>
2 // SPDX-FileCopyrightText: 2009-2018 nibble <nibble.ds@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <string.h>
8 
9 #include <rz_types.h>
10 #include <rz_lib.h>
11 #include <rz_util.h>
12 #include <rz_asm.h>
13 
14 #include "disas-asm.h"
15 #include "opcode/mips.h"
16 int mips_assemble(const char *str, ut64 pc, ut8 *out);
17 
18 static int mips_mode = 0;
19 static unsigned long Offset = 0;
21 static unsigned char bytes[4];
22 static char *pre_cpu = NULL;
23 static char *pre_features = NULL;
24 
25 static int mips_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) > 4) {
31  return -1;
32  }
33  memcpy(myaddr, bytes + 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(struct rz_asm_t *a, struct rz_asm_op_t *op, const ut8 *buf, int len) {
49  static struct disassemble_info disasm_obj;
50  if (len < 4) {
51  return -1;
52  }
53  buf_global = &op->buf_asm;
54  Offset = a->pc;
55  memcpy(bytes, buf, 4); // TODO handle thumb
56 
57  if ((a->cpu != pre_cpu) && (a->features != pre_features)) {
58  free(disasm_obj.disassembler_options);
59  memset(&disasm_obj, '\0', sizeof(struct disassemble_info));
60  }
61 
62  /* prepare disassembler */
63  if (a->cpu && (!pre_cpu || !strcmp(a->cpu, pre_cpu))) {
64  if (!rz_str_casecmp(a->cpu, "mips64r2")) {
65  disasm_obj.mach = bfd_mach_mipsisa64r2;
66  } else if (!rz_str_casecmp(a->cpu, "mips32r2")) {
67  disasm_obj.mach = bfd_mach_mipsisa32r2;
68  } else if (!rz_str_casecmp(a->cpu, "mips64")) {
69  disasm_obj.mach = bfd_mach_mipsisa64;
70  } else if (!rz_str_casecmp(a->cpu, "mips32")) {
71  disasm_obj.mach = bfd_mach_mipsisa32;
72  }
73  pre_cpu = rz_str_dup(pre_cpu, a->cpu);
74  }
75 
76  if (a->features && (!pre_features || !strcmp(a->features, pre_features))) {
77  free(disasm_obj.disassembler_options);
78  if (strstr(a->features, "n64")) {
79  disasm_obj.disassembler_options = rz_str_new("abi=n64");
80  } else if (strstr(a->features, "n32")) {
81  disasm_obj.disassembler_options = rz_str_new("abi=n32");
82  } else if (strstr(a->features, "o32")) {
83  disasm_obj.disassembler_options = rz_str_new("abi=o32");
84  }
85  pre_features = rz_str_dup(pre_features, a->features);
86  }
87 
88  mips_mode = a->bits;
89  disasm_obj.arch = CPU_LOONGSON_2F;
90  disasm_obj.buffer = bytes;
94  disasm_obj.print_address_func = &generic_print_address_func;
95  disasm_obj.buffer_vma = Offset;
96  disasm_obj.buffer_length = 4;
97  disasm_obj.endian = !a->big_endian;
98  disasm_obj.fprintf_func = &generic_fprintf_func;
99  disasm_obj.stream = stdout;
100  op->size = (disasm_obj.endian == BFD_ENDIAN_LITTLE)
101  ? print_insn_little_mips((bfd_vma)Offset, &disasm_obj)
102  : print_insn_big_mips((bfd_vma)Offset, &disasm_obj);
103  if (op->size == -1) {
104  rz_strbuf_set(&op->buf_asm, "(data)");
105  }
106  return op->size;
107 }
108 
109 static int assemble(RzAsm *a, RzAsmOp *op, const char *str) {
110  ut8 *opbuf = (ut8 *)rz_strbuf_get(&op->buf);
111  int ret = mips_assemble(str, a->pc, opbuf);
112  if (a->big_endian) {
113  ut8 tmp = opbuf[0];
114  opbuf[0] = opbuf[3];
115  opbuf[3] = tmp;
116  tmp = opbuf[1];
117  opbuf[1] = opbuf[2];
118  opbuf[2] = tmp;
119  }
120  return ret;
121 }
122 
124  .name = "mips.gnu",
125  .arch = "mips",
126  .license = "GPL3",
127  .bits = 32 | 64,
129  .desc = "MIPS CPU",
130  .disassemble = &disassemble,
131  .assemble = &assemble
132 };
133 
134 #ifndef RZ_PLUGIN_INCORE
137  .data = &rz_asm_plugin_mips_gnu,
139 };
140 #endif
size_t len
Definition: 6502dis.c:15
static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info)
Definition: asm_mips_gnu.c:41
static int mips_mode
Definition: asm_mips_gnu.c:18
static char * pre_features
Definition: asm_mips_gnu.c:23
static int disassemble(struct rz_asm_t *a, struct rz_asm_op_t *op, const ut8 *buf, int len)
Definition: asm_mips_gnu.c:48
int mips_assemble(const char *str, ut64 pc, ut8 *out)
Definition: mipsasm.c:148
static unsigned long Offset
Definition: asm_mips_gnu.c:19
RZ_API RzLibStruct rizin_plugin
Definition: asm_mips_gnu.c:135
static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
Definition: asm_mips_gnu.c:37
static RzStrBuf * buf_global
Definition: asm_mips_gnu.c:20
static unsigned char bytes[4]
Definition: asm_mips_gnu.c:21
static int assemble(RzAsm *a, RzAsmOp *op, const char *str)
Definition: asm_mips_gnu.c:109
static int mips_buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, struct disassemble_info *info)
Definition: asm_mips_gnu.c:25
RzAsmPlugin rz_asm_plugin_mips_gnu
Definition: asm_mips_gnu.c:123
static char * pre_cpu
Definition: asm_mips_gnu.c:22
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#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
#define DECLARE_GENERIC_FPRINTF_FUNC()
Definition: disas-asm.h:422
#define DECLARE_GENERIC_PRINT_ADDRESS_FUNC()
Definition: disas-asm.h:435
int print_insn_little_mips(bfd_vma, disassemble_info *)
Definition: mips-dis.c:2104
int print_insn_big_mips(bfd_vma, disassemble_info *)
Definition: mips-dis.c:2098
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
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
#define CPU_LOONGSON_2F
Definition: mips.h:610
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
#define bfd_mach_mipsisa32r2
Definition: mybfd.h:1657
BFD_HOST_U_64_BIT bfd_vma
Definition: mybfd.h:111
#define bfd_mach_mipsisa64
Definition: mybfd.h:1658
#define bfd_mach_mipsisa32
Definition: mybfd.h:1656
@ BFD_ENDIAN_LITTLE
Definition: mybfd.h:4618
#define bfd_mach_mipsisa64r2
Definition: mybfd.h:1659
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
RZ_API int rz_str_casecmp(const char *dst, const char *orig)
Definition: str.c:121
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
RZ_API char * rz_str_dup(char *ptr, const char *string)
Definition: str.c:1021
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
RZ_API char * rz_strbuf_get(RzStrBuf *sb)
Definition: strbuf.c:321
#define RZ_SYS_ENDIAN_BIG
Definition: rz_types.h:527
#define RZ_SYS_ENDIAN_LITTLE
Definition: rz_types.h:526
#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
char * disassembler_options
Definition: disas-asm.h:214
enum bfd_endian endian
Definition: disas-asm.h:83
unsigned int buffer_length
Definition: disas-asm.h:162
bfd_vma buffer_vma
Definition: disas-asm.h:161
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
enum bfd_architecture arch
Definition: disas-asm.h:79
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
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58