Rizin
unix-like reverse engineering framework and cli tools
mcore.c File Reference
#include "mcore.h"
#include <rz_analysis.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

Go to the source code of this file.

Classes

struct  mcore_mask
 
struct  mcore_ops
 

Macros

#define MCORE_INSTR_ALIGN   (2)
 
#define INVALID_FIELD   (0)
 
#define MCORE_INSTRS   265
 

Typedefs

typedef struct mcore_mask mcore_mask_t
 
typedef struct mcore_ops mcore_ops_t
 

Functions

static mcore_tfind_instruction (const ut8 *buffer)
 
int mcore_init (mcore_handle *handle, const ut8 *buffer, const ut32 size)
 
mcore_tmcore_next (mcore_handle *handle)
 
void mcore_free (mcore_t *instr)
 
void print_loop (char *str, int size, ut64 addr, mcore_t *instr)
 
void mcore_snprint (char *str, int size, ut64 addr, mcore_t *instr)
 

Variables

static const char * mcore_ctrl_registers []
 
ut16 load_shift [4] = { 2, 0, 1, 0 }
 
mcore_ops_t mcore_instructions [MCORE_INSTRS]
 

Macro Definition Documentation

◆ INVALID_FIELD

#define INVALID_FIELD   (0)

Definition at line 76 of file mcore.c.

◆ MCORE_INSTR_ALIGN

#define MCORE_INSTR_ALIGN   (2)

Definition at line 14 of file mcore.c.

◆ MCORE_INSTRS

#define MCORE_INSTRS   265

Definition at line 95 of file mcore.c.

Typedef Documentation

◆ mcore_mask_t

typedef struct mcore_mask mcore_mask_t

◆ mcore_ops_t

typedef struct mcore_ops mcore_ops_t

Function Documentation

◆ find_instruction()

static mcore_t* find_instruction ( const ut8 buffer)
static

Definition at line 363 of file mcore.c.

363  {
364  ut32 i = 0;
365  mcore_ops_t *op_ptr = NULL;
366  mcore_t *op = NULL;
367  if (!buffer || !(op = malloc(sizeof(mcore_t)))) {
368  return NULL;
369  }
370  memset(op, 0, sizeof(mcore_t));
371  ut32 count = sizeof(mcore_instructions) / sizeof(mcore_ops_t);
372  ut16 data = buffer[1] << 8;
373  data |= buffer[0];
374  op->bytes = data;
375  op->size = MCORE_INSTR_ALIGN;
376  if (data == 0) {
377  op_ptr = &mcore_instructions[0];
378  } else {
379  for (i = 1; i < count; i++) {
380  op_ptr = &mcore_instructions[i];
381  ut16 masked = data & op_ptr->mask;
382  // always masking with zero returns 0
383  if (masked == data) {
384  break;
385  }
386  }
387  if (i >= count) {
388  op->name = "illegal";
389  return op;
390  }
391  }
392 
393  if (!strncmp(op_ptr->name, "lrw", 3) && (data & 0xf00) == 0xf00) {
394  // is jump
395  if (i > 0 && i < MCORE_INSTRS) {
396  op_ptr = &mcore_instructions[i + 1];
397  }
398  }
399  op->type = op_ptr->type;
400  op->name = op_ptr->name;
401  op->n_args = op_ptr->n_args;
402  for (i = 0; i < op_ptr->n_args; i++) {
403  op->args[i].value = (data & op_ptr->args[i].mask) >> op_ptr->args[i].shift;
404  op->args[i].type = op_ptr->args[i].type;
405  }
406  return op;
407 }
ut8 op
Definition: 6502dis.c:13
lzma_index ** i
Definition: index.h:629
#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 count
Definition: sflib.h:98
uint16_t ut16
uint32_t ut32
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123
#define MCORE_INSTR_ALIGN
Definition: mcore.c:14
#define MCORE_INSTRS
Definition: mcore.c:95
mcore_ops_t mcore_instructions[MCORE_INSTRS]
Definition: mcore.c:96
Definition: buffer.h:15
ut16 type
Definition: mcore.c:81
ut16 shift
Definition: mcore.c:80
ut16 mask
Definition: mcore.c:79
const char * name
Definition: mcore.c:85
mcore_mask_t args[ARGS_SIZE]
Definition: mcore.c:90
ut16 mask
Definition: mcore.c:87
ut64 type
Definition: mcore.c:88
ut16 n_args
Definition: mcore.c:89
Definition: mcore.h:40
Definition: dis.c:32

References mcore_ops::args, count, i, malloc(), mcore_mask::mask, mcore_ops::mask, MCORE_INSTR_ALIGN, MCORE_INSTRS, mcore_instructions, memset(), mcore_ops::n_args, mcore_ops::name, NULL, op, mcore_mask::shift, mcore_mask::type, and mcore_ops::type.

Referenced by mcore_next().

◆ mcore_free()

void mcore_free ( mcore_t instr)

Definition at line 432 of file mcore.c.

432  {
433  free(instr);
434 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

Referenced by disassemble(), and mcore_analysis().

◆ mcore_init()

int mcore_init ( mcore_handle handle,
const ut8 buffer,
const ut32  size 
)

Definition at line 409 of file mcore.c.

409  {
410  if (!handle || !buffer || size < 2) {
411  return 1;
412  }
413  handle->pos = buffer;
414  handle->end = buffer + size;
415  return 0;
416 }
static mcore_handle handle
Definition: asm_mcore.c:8
struct buffer buffer
voidpf void uLong size
Definition: ioapi.h:138
const ut8 * pos
Definition: mcore.h:31
const ut8 * end
Definition: mcore.h:30

References mcore_handle::end, handle, and mcore_handle::pos.

Referenced by disassemble(), and mcore_analysis().

◆ mcore_next()

mcore_t* mcore_next ( mcore_handle handle)

Definition at line 418 of file mcore.c.

418  {
419  mcore_t *op = NULL;
420  if (!handle || handle->pos + MCORE_INSTR_ALIGN > handle->end) {
421  return NULL;
422  }
423 
424  if (!op && handle->pos + 2 <= handle->end) {
426  }
428 
429  return op;
430 }
static mcore_t * find_instruction(const ut8 *buffer)
Definition: mcore.c:363

References mcore_handle::end, find_instruction(), handle, MCORE_INSTR_ALIGN, NULL, op, and mcore_handle::pos.

Referenced by disassemble(), and mcore_analysis().

◆ mcore_snprint()

void mcore_snprint ( char *  str,
int  size,
ut64  addr,
mcore_t instr 
)

Definition at line 469 of file mcore.c.

469  {
470  ut32 imm;
471  if (!instr || !str) {
472  return;
473  }
474  switch (instr->type) {
477  imm = instr->args[1].value << load_shift[instr->args[3].value];
478  snprintf(str, size, "%s r%u, (r%u, 0x%x)",
479  instr->name, instr->args[2].value, instr->args[0].value, imm);
480  break;
481  default:
482  print_loop(str, size, addr, instr);
483  break;
484  }
485 }
#define imm
snprintf
Definition: kernel.h:364
void print_loop(char *str, int size, ut64 addr, mcore_t *instr)
Definition: mcore.c:436
ut16 load_shift[4]
Definition: mcore.c:93
@ RZ_ANALYSIS_OP_TYPE_LOAD
Definition: rz_analysis.h:416
@ RZ_ANALYSIS_OP_TYPE_STORE
Definition: rz_analysis.h:415
ut32 value
Definition: mcore.h:36
ut64 type
Definition: mcore.h:43
const char * name
Definition: mcore.h:41
mcore_field_t args[ARGS_SIZE]
Definition: mcore.h:42
static int addr
Definition: z80asm.c:58

References addr, mcore_t::args, imm, load_shift, mcore_t::name, print_loop(), RZ_ANALYSIS_OP_TYPE_LOAD, RZ_ANALYSIS_OP_TYPE_STORE, snprintf, cmd_descs_generate::str, mcore_t::type, and mcore_field_t::value.

Referenced by disassemble().

◆ print_loop()

void print_loop ( char *  str,
int  size,
ut64  addr,
mcore_t instr 
)

Definition at line 436 of file mcore.c.

436  {
437  ut32 i;
438  int bufsize = size;
439  int add = snprintf(str, bufsize, "%s", instr->name);
440  for (i = 0; add > 0 && i < instr->n_args && add < bufsize; i++) {
441  if (instr->args[i].type == TYPE_REG) {
442  add += snprintf(str + add, bufsize - add, " r%u,", instr->args[i].value);
443  } else if (instr->args[i].type == TYPE_IMM) {
444  add += snprintf(str + add, bufsize - add, " 0x%x,", instr->args[i].value);
445  } else if (instr->args[i].type == TYPE_MEM) {
446  add += snprintf(str + add, bufsize - add, " 0x%x(r%d),",
447  instr->args[i + 1].value, instr->args[i].value);
448  i++;
449  } else if (instr->args[i].type == TYPE_JMPI) {
450  ut64 jump = addr + ((instr->args[i].value << 2) & 0xfffffffc);
451  add += snprintf(str + add, bufsize - add, " [0x%" PFMT64x "],", jump);
452  } else if (instr->args[i].type == TYPE_JMP) {
453  ut64 jump = addr + instr->args[i].value + 1;
454  add += snprintf(str + add, bufsize - add, " 0x%" PFMT64x ",", jump);
455  } else if (instr->args[i].type == TYPE_CTRL) {
456  ut32 pos = instr->args[i].value;
457  if (pos >= 32) {
458  pos = 32;
459  }
460  add += snprintf(str + add, bufsize - add, " %s,", mcore_ctrl_registers[pos]);
461  }
462  }
463  if (instr->n_args) {
464  // removing a comma
465  *(str + add - 1) = 0;
466  }
467 }
@ TYPE_IMM
Definition: armass.c:35
@ TYPE_MEM
Definition: armass.c:36
int jump(int a, int b)
Definition: bcj_test.c:35
static const char * mcore_ctrl_registers[]
Definition: mcore.c:17
#define TYPE_JMPI
Definition: mcore.h:26
#define TYPE_CTRL
Definition: mcore.h:27
#define TYPE_REG
Definition: mcore.h:22
#define TYPE_JMP
Definition: mcore.h:25
#define PFMT64x
Definition: rz_types.h:393
ut16 type
Definition: mcore.h:37
ut16 n_args
Definition: mcore.h:44
int pos
Definition: main.c:11
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int add(char *argv[])
Definition: ziptool.c:84

References add(), addr, mcore_t::args, i, jump(), mcore_ctrl_registers, mcore_t::n_args, mcore_t::name, PFMT64x, pos, snprintf, cmd_descs_generate::str, mcore_field_t::type, TYPE_CTRL, TYPE_IMM, TYPE_JMP, TYPE_JMPI, TYPE_MEM, TYPE_REG, ut64(), and mcore_field_t::value.

Referenced by mcore_snprint().

Variable Documentation

◆ load_shift

ut16 load_shift[4] = { 2, 0, 1, 0 }

Definition at line 93 of file mcore.c.

Referenced by mcore_snprint().

◆ mcore_ctrl_registers

const char* mcore_ctrl_registers[]
static

Definition at line 17 of file mcore.c.

Referenced by print_loop().

◆ mcore_instructions

mcore_ops_t mcore_instructions[MCORE_INSTRS]

Definition at line 96 of file mcore.c.

Referenced by find_instruction().