Rizin
unix-like reverse engineering framework and cli tools
asm_tricore.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2016 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 
8 #include <rz_types.h>
9 #include <rz_lib.h>
10 #include <rz_util.h>
11 #include <rz_asm.h>
12 
13 #include "disas-asm.h"
14 
15 static unsigned long Offset = 0;
17 static ut8 bytes[128];
18 enum {
19  TRICORE_GENERIC = 0x00000000,
20  TRICORE_RIDER_A = 0x00000001,
21  TRICORE_RIDER_B = 0x00000002,
23  TRICORE_V2 = 0x00000004,
24  TRICORE_PCP = 0x00000010,
25  TRICORE_PCP2 = 0x00000020
26 };
27 
28 static int cpu_to_mach(char *cpu_type) {
29  if (cpu_type && *cpu_type) {
30  if (!strcmp(cpu_type, "generic")) {
31  return TRICORE_GENERIC;
32  }
33  if (!strcmp(cpu_type, "rider-a")) {
34  return TRICORE_RIDER_A;
35  }
36  if ((!strcmp(cpu_type, "rider-b")) || (!strcmp(cpu_type, "rider-d"))) {
37  return TRICORE_RIDER_B;
38  }
39  if (!strcmp(cpu_type, "v2")) {
40  return TRICORE_V2;
41  }
42  if (!strcmp(cpu_type, "pcp")) {
43  return TRICORE_PCP;
44  }
45  if (!strcmp(cpu_type, "pcp2")) {
46  return TRICORE_PCP2;
47  }
48  }
49  return TRICORE_RIDER_B;
50 }
51 
53  int delta = memaddr - Offset;
54  if (delta >= 0 && length + delta < sizeof(bytes)) {
55  memcpy(myaddr, bytes + delta, length);
56  }
57  return 0;
58 }
59 
61  return 0;
62 }
63 
64 static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info) {
65  //--
66 }
67 
70 
71 static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
72  struct disassemble_info disasm_obj;
73  buf_global = &op->buf_asm;
74  Offset = a->pc;
75  memcpy(bytes, buf, RZ_MIN(len, 8)); // TODO handle thumb
76 
77  /* prepare disassembler */
78  memset(&disasm_obj, '\0', sizeof(struct disassemble_info));
79  disasm_obj.disassembler_options = (a->bits == 64) ? "64" : "";
80  disasm_obj.buffer = bytes;
84  disasm_obj.print_address_func = &generic_print_address_func;
85  disasm_obj.endian = BFD_ENDIAN_LITTLE;
86  disasm_obj.fprintf_func = &generic_fprintf_func;
87  disasm_obj.stream = stdout;
88 
89  // cpu type
90  disasm_obj.mach = cpu_to_mach(a->cpu);
91 
92  op->size = print_insn_tricore((bfd_vma)Offset, &disasm_obj);
93  if (op->size == -1) {
94  rz_asm_op_set_asm(op, " (data)");
95  }
96  return op->size;
97 }
98 
100  .name = "tricore",
101  .arch = "tricore",
102  .license = "GPL3",
103  .bits = 32,
104  .endian = RZ_SYS_ENDIAN_LITTLE,
105  .desc = "Siemens TriCore CPU",
106  .disassemble = &disassemble,
107 };
108 
109 #ifndef RZ_PLUGIN_INCORE
112  .data = &rz_asm_plugin_tricore,
114 };
115 #endif
size_t len
Definition: 6502dis.c:15
RZ_API void rz_asm_op_set_asm(RzAsmOp *op, const char *str)
Definition: aop.c:53
static int cpu_type
Definition: arc-opc.c:143
static ut8 bytes[128]
Definition: asm_tricore.c:17
static void memory_error_func(int status, bfd_vma memaddr, struct disassemble_info *info)
Definition: asm_tricore.c:64
RzAsmPlugin rz_asm_plugin_tricore
Definition: asm_tricore.c:99
@ TRICORE_RIDER_A
Definition: asm_tricore.c:20
@ TRICORE_PCP2
Definition: asm_tricore.c:25
@ TRICORE_RIDER_B
Definition: asm_tricore.c:21
@ TRICORE_V2
Definition: asm_tricore.c:23
@ TRICORE_PCP
Definition: asm_tricore.c:24
@ TRICORE_RIDER_D
Definition: asm_tricore.c:22
@ TRICORE_GENERIC
Definition: asm_tricore.c:19
static unsigned long Offset
Definition: asm_tricore.c:15
RZ_API RzLibStruct rizin_plugin
Definition: asm_tricore.c:110
static int symbol_at_address(bfd_vma addr, struct disassemble_info *info)
Definition: asm_tricore.c:60
static RzStrBuf * buf_global
Definition: asm_tricore.c:16
static int cpu_to_mach(char *cpu_type)
Definition: asm_tricore.c:28
static int tricore_buffer_read_memory(bfd_vma memaddr, bfd_byte *myaddr, ut32 length, struct disassemble_info *info)
Definition: asm_tricore.c:52
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len)
Definition: asm_tricore.c:71
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
int print_insn_tricore(bfd_vma memaddr, struct disassemble_info *info)
Definition: tricore-dis.c:1884
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
@ BFD_ENDIAN_LITTLE
Definition: mybfd.h:4618
@ RZ_LIB_TYPE_ASM
Definition: rz_lib.h:72
#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
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