Rizin
unix-like reverse engineering framework and cli tools
hexagon_arch.h File Reference
#include "hexagon.h"

Go to the source code of this file.

Classes

struct  HexReversedOpcode
 Pointer to the rizin structs for disassembled and analysed instructions. More...
 

Macros

#define HEX_PKT_UNK   "? "
 
#define HEX_PKT_SINGLE   "[ "
 
#define HEX_PKT_SINGLE_UTF8   "[ "
 
#define HEX_PKT_FIRST_UTF8   "┌ "
 
#define HEX_PKT_MID_UTF8   "│ "
 
#define HEX_PKT_LAST_UTF8   "└ "
 
#define HEX_PKT_FIRST_SDK   "{ "
 
#define HEX_PKT_SDK_PADDING   " "
 
#define HEX_PKT_LAST_SDK   " }"
 
#define HEX_PKT_FIRST   "/ "
 
#define HEX_PKT_MID   "| "
 
#define HEX_PKT_LAST   "\\ "
 
#define HEX_PKT_ELOOP_01_UTF8   " ∎ endloop01"
 
#define HEX_PKT_ELOOP_1_UTF8   " ∎ endloop1"
 
#define HEX_PKT_ELOOP_0_UTF8   " ∎ endloop0"
 
#define HEX_PKT_ELOOP_01   " < endloop01"
 
#define HEX_PKT_ELOOP_1   " < endloop1"
 
#define HEX_PKT_ELOOP_0   " < endloop0"
 
#define HEX_PKT_ELOOP_01_SDK   ":endloop01"
 
#define HEX_PKT_ELOOP_1_SDK   ":endloop1"
 
#define HEX_PKT_ELOOP_0_SDK   ":endloop0"
 

Enumerations

enum  HexPktSyntaxIndicator {
  SINGLE_IN_PKT , FIRST_IN_PKT , MID_IN_PKT , LAST_IN_PKT ,
  ELOOP_0_PKT , ELOOP_1_PKT , ELOOP_01_PKT
}
 
enum  HexReverseAction { HEXAGON_ANALYSIS , HEXAGON_DISAS }
 

Functions

RZ_API void hex_insn_free (HexInsn *i)
 
RZ_API void hex_const_ext_free (HexConstExt *ce)
 
RZ_API HexStatehexagon_get_state ()
 Initializes each packet of the state once. More...
 
RZ_API void hexagon_reverse_opcode (const RzAsm *rz_asm, HexReversedOpcode *rz_reverse, const ut8 *buf, const ut64 addr)
 Reverses a given opcode and copies the result into one of the rizin structs in rz_reverse. More...
 
RZ_API ut8 hexagon_get_pkt_index_of_addr (const ut32 addr, const HexPkt *p)
 Returns the index of an addr in a given packet. More...
 
RZ_API HexLoopAttr hex_get_loop_flag (const HexPkt *p)
 Returns the loop type of a packet. Though only if this packet is the last packet in last packet in a hardware loop. Otherwise it returns HEX_NO_LOOP. More...
 

Macro Definition Documentation

◆ HEX_PKT_ELOOP_0

#define HEX_PKT_ELOOP_0   " < endloop0"

Definition at line 60 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_01

#define HEX_PKT_ELOOP_01   " < endloop01"

Definition at line 58 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_01_SDK

#define HEX_PKT_ELOOP_01_SDK   ":endloop01"

Definition at line 61 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_01_UTF8

#define HEX_PKT_ELOOP_01_UTF8   " ∎ endloop01"

Definition at line 55 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_0_SDK

#define HEX_PKT_ELOOP_0_SDK   ":endloop0"

Definition at line 63 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_0_UTF8

#define HEX_PKT_ELOOP_0_UTF8   " ∎ endloop0"

Definition at line 57 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_1

#define HEX_PKT_ELOOP_1   " < endloop1"

Definition at line 59 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_1_SDK

#define HEX_PKT_ELOOP_1_SDK   ":endloop1"

Definition at line 62 of file hexagon_arch.h.

◆ HEX_PKT_ELOOP_1_UTF8

#define HEX_PKT_ELOOP_1_UTF8   " ∎ endloop1"

Definition at line 56 of file hexagon_arch.h.

◆ HEX_PKT_FIRST

#define HEX_PKT_FIRST   "/ "

Definition at line 52 of file hexagon_arch.h.

◆ HEX_PKT_FIRST_SDK

#define HEX_PKT_FIRST_SDK   "{ "

Definition at line 49 of file hexagon_arch.h.

◆ HEX_PKT_FIRST_UTF8

#define HEX_PKT_FIRST_UTF8   "┌ "

Definition at line 46 of file hexagon_arch.h.

◆ HEX_PKT_LAST

#define HEX_PKT_LAST   "\\ "

Definition at line 54 of file hexagon_arch.h.

◆ HEX_PKT_LAST_SDK

#define HEX_PKT_LAST_SDK   " }"

Definition at line 51 of file hexagon_arch.h.

◆ HEX_PKT_LAST_UTF8

#define HEX_PKT_LAST_UTF8   "└ "

Definition at line 48 of file hexagon_arch.h.

◆ HEX_PKT_MID

#define HEX_PKT_MID   "| "

Definition at line 53 of file hexagon_arch.h.

◆ HEX_PKT_MID_UTF8

#define HEX_PKT_MID_UTF8   "│ "

Definition at line 47 of file hexagon_arch.h.

◆ HEX_PKT_SDK_PADDING

#define HEX_PKT_SDK_PADDING   " "

Definition at line 50 of file hexagon_arch.h.

◆ HEX_PKT_SINGLE

#define HEX_PKT_SINGLE   "[ "

Definition at line 44 of file hexagon_arch.h.

◆ HEX_PKT_SINGLE_UTF8

#define HEX_PKT_SINGLE_UTF8   "[ "

Definition at line 45 of file hexagon_arch.h.

◆ HEX_PKT_UNK

#define HEX_PKT_UNK   "? "

Definition at line 43 of file hexagon_arch.h.

Enumeration Type Documentation

◆ HexPktSyntaxIndicator

Enumerator
SINGLE_IN_PKT 
FIRST_IN_PKT 
MID_IN_PKT 
LAST_IN_PKT 
ELOOP_0_PKT 
ELOOP_1_PKT 
ELOOP_01_PKT 

Definition at line 17 of file hexagon_arch.h.

17  {
20  MID_IN_PKT,
HexPktSyntaxIndicator
Definition: hexagon_arch.h:17
@ FIRST_IN_PKT
Definition: hexagon_arch.h:19
@ ELOOP_01_PKT
Definition: hexagon_arch.h:24
@ LAST_IN_PKT
Definition: hexagon_arch.h:21
@ SINGLE_IN_PKT
Definition: hexagon_arch.h:18
@ MID_IN_PKT
Definition: hexagon_arch.h:20
@ ELOOP_0_PKT
Definition: hexagon_arch.h:22
@ ELOOP_1_PKT
Definition: hexagon_arch.h:23

◆ HexReverseAction

Enumerator
HEXAGON_ANALYSIS 
HEXAGON_DISAS 

Definition at line 28 of file hexagon_arch.h.

28  {
HexReverseAction
Definition: hexagon_arch.h:28
@ HEXAGON_ANALYSIS
Definition: hexagon_arch.h:29
@ HEXAGON_DISAS
Definition: hexagon_arch.h:30

Function Documentation

◆ hex_const_ext_free()

RZ_API void hex_const_ext_free ( HexConstExt ce)

◆ hex_get_loop_flag()

RZ_API HexLoopAttr hex_get_loop_flag ( const HexPkt p)

Returns the loop type of a packet. Though only if this packet is the last packet in last packet in a hardware loop. Otherwise it returns HEX_NO_LOOP.

Parameters
pThe instruction packet.
Returns
HexLoopAttr The loop type this packet belongs to.

Definition at line 446 of file hexagon_arch.c.

446  {
447  if (!p || rz_list_length(p->insn) < 2) {
448  return HEX_NO_LOOP;
449  }
450 
451  ut8 pb_0 = ((HexInsn *)rz_list_get_n(p->insn, 0))->parse_bits;
452  ut8 pb_1 = ((HexInsn *)rz_list_get_n(p->insn, 1))->parse_bits;
453 
454  if (is_endloop0_pkt(pb_0, pb_1)) {
455  return HEX_LOOP_0;
456  } else if (is_endloop1_pkt(pb_0, pb_1)) {
457  return HEX_LOOP_1;
458  } else if (is_endloop01_pkt(pb_0, pb_1)) {
459  return HEX_LOOP_01;
460  } else if (is_undoc_endloop0_pkt(pb_0, pb_1)) {
461  RZ_LOG_VERBOSE("Undocumented hardware loop 0 endloop packet.");
462  return HEX_LOOP_0;
463  } else {
464  return HEX_NO_LOOP;
465  }
466 }
@ HEX_NO_LOOP
Definition: hexagon.h:72
@ HEX_LOOP_0
Definition: hexagon.h:73
@ HEX_LOOP_1
Definition: hexagon.h:74
@ HEX_LOOP_01
Definition: hexagon.h:75
static bool is_endloop0_pkt(const ut8 pb_hi_0, const ut8 pb_hi_1)
Checks if packet ends hardware loop 0.
Definition: hexagon_arch.c:32
static bool is_endloop1_pkt(const ut8 pb_hi_0, const ut8 pb_hi_1)
Checks if packet ends hardware loop 1.
Definition: hexagon_arch.c:57
static bool is_endloop01_pkt(const ut8 pb_hi_0, const ut8 pb_hi_1)
Checks if packet ends hardware loop 0 and hw-loop 1.
Definition: hexagon_arch.c:69
static bool is_undoc_endloop0_pkt(const ut8 pb_hi_0, const ut8 pb_hi_1)
Checks if packet ends hardware loop 0. But for an undocumented variant where the packet has only two ...
Definition: hexagon_arch.c:45
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
RZ_API RZ_BORROW void * rz_list_get_n(RZ_NONNULL const RzList *list, ut32 n)
Returns the N-th element of the list.
Definition: list.c:574
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
#define RZ_LOG_VERBOSE(fmtstr,...)
Definition: rz_log.h:52

References HEX_LOOP_0, HEX_LOOP_01, HEX_LOOP_1, HEX_NO_LOOP, is_endloop01_pkt(), is_endloop0_pkt(), is_endloop1_pkt(), is_undoc_endloop0_pkt(), p, rz_list_get_n(), rz_list_length(), and RZ_LOG_VERBOSE.

Referenced by hex_set_pkt_info(), and hexagon_disasm_instruction().

◆ hex_insn_free()

RZ_API void hex_insn_free ( HexInsn i)

◆ hexagon_get_pkt_index_of_addr()

RZ_API ut8 hexagon_get_pkt_index_of_addr ( const ut32  addr,
const HexPkt p 
)

Returns the index of an addr in a given packet.

Parameters
addrAddress of an instruction.
pThe packet. to search in.
Returns
ut8 The index of the addr if it is in the packet. UT8_MAX otherwise.

Definition at line 103 of file hexagon_arch.c.

103  {
105 
106  HexInsn *hi = NULL;
107  RzListIter *it = NULL;
108  ut8 i = 0;
109  rz_list_foreach (p->insn, it, hi) {
110  if (hi->addr == addr) {
111  return i;
112  }
113  ++i;
114  }
115  return UT8_MAX;
116 }
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
hi(addr) 0x03
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define UT8_MAX
static int addr
Definition: z80asm.c:58

References addr, hi(), i, NULL, p, rz_return_val_if_fail, and UT8_MAX.

Referenced by resolve_n_register().

◆ hexagon_get_state()

RZ_API HexState* hexagon_get_state ( )

Initializes each packet of the state once.

Returns
The initialized state of the plugins.

Definition at line 217 of file hexagon_arch.c.

217  {
218  static HexState *state = NULL;
219  if (state) {
220  return state;
221  }
222 
223  state = calloc(1, sizeof(HexState));
224  if (!state) {
225  RZ_LOG_FATAL("Could not allocate memory for HexState!");
226  }
227  for (int i = 0; i < HEXAGON_STATE_PKTS; ++i) {
228  state->pkts[i].insn = rz_list_newf((RzListFree)hex_insn_free);
229  if (!state->pkts[i].insn) {
230  RZ_LOG_FATAL("Could not initialize instruction list!");
231  }
232  hex_clear_pkt(&(state->pkts[i]));
233  }
235  return state;
236 }
#define HEXAGON_STATE_PKTS
Definition: hexagon.h:25
RZ_API void hex_insn_free(RZ_NULLABLE HexInsn *i)
Frees an instruction.
Definition: hexagon_arch.c:175
RZ_API void hex_const_ext_free(RZ_NULLABLE HexConstExt *ce)
Frees an constant extender.
Definition: hexagon_arch.c:187
static void hex_clear_pkt(RZ_NONNULL HexPkt *p)
Clears a packet and sets its attributes to invalid values.
Definition: hexagon_arch.c:123
RZ_API RZ_OWN RzList * rz_list_newf(RzListFree f)
Returns a new initialized RzList pointer and sets the free method.
Definition: list.c:248
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
void(* RzListFree)(void *ptr)
Definition: rz_list.h:11
#define RZ_LOG_FATAL(fmtstr,...)
Definition: rz_log.h:60
Buffer packets for reversed instructions.
Definition: hexagon.h:134
Definition: dis.h:43

References calloc(), hex_clear_pkt(), hex_const_ext_free(), hex_insn_free(), HEXAGON_STATE_PKTS, i, NULL, rz_list_newf(), and RZ_LOG_FATAL.

Referenced by hex_set_pkt_info(), and hexagon_reverse_opcode().

◆ hexagon_reverse_opcode()

RZ_API void hexagon_reverse_opcode ( const RzAsm rz_asm,
HexReversedOpcode rz_reverse,
const ut8 buf,
const ut64  addr 
)

Reverses a given opcode and copies the result into one of the rizin structs in rz_reverse.

Parameters
rz_reverseRizin core structs which store asm and analysis information.
bufThe buffer which stores the current opcode.
addrThe address of the current opcode.

Definition at line 772 of file hexagon_arch.c.

772  {
774  if (!state) {
775  RZ_LOG_FATAL("HexState was NULL.");
776  }
777  if (rz_asm) {
778  memcpy(&state->rz_asm, rz_asm, sizeof(RzAsm));
779  }
781  if (hi) {
782  // Opcode was already reversed and is still in the state. Copy the result and return.
783  switch (rz_reverse->action) {
784  default:
785  memcpy(rz_reverse->asm_op, &(hi->asm_op), sizeof(RzAsmOp));
786  memcpy(rz_reverse->ana_op, &(hi->ana_op), sizeof(RzAnalysisOp));
787  rz_strbuf_set(&rz_reverse->asm_op->buf_asm, hi->mnem);
788  rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
789  rz_reverse->asm_op->asm_toks->op_type = hi->ana_op.type;
790  return;
791  case HEXAGON_DISAS:
792  memcpy(rz_reverse->asm_op, &(hi->asm_op), sizeof(RzAsmOp));
793  rz_strbuf_set(&rz_reverse->asm_op->buf_asm, hi->mnem);
794  rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
795  rz_reverse->asm_op->asm_toks->op_type = hi->ana_op.type;
796  return;
797  case HEXAGON_ANALYSIS:
798  memcpy(rz_reverse->ana_op, &(hi->ana_op), sizeof(RzAnalysisOp));
799  return;
800  }
801  }
802 
803  ut32 data = rz_read_le32(buf);
804  ut8 parse_bits = (data & 0x0000c000) >> 14;
805  HexInsn instr = { 0 };
806  setup_new_instr(&instr, rz_reverse, addr, parse_bits);
807  // Add to state
808  hi = hex_add_instr_to_state(state, &instr);
809  if (!hi) {
810  return;
811  }
812  HexPkt *p = hex_get_pkt(state, hi->addr);
813 
814  // Do disasassembly and analysis
816 
817  switch (rz_reverse->action) {
818  default:
819  memcpy(rz_reverse->asm_op, &hi->asm_op, sizeof(RzAsmOp));
820  memcpy(rz_reverse->ana_op, &hi->ana_op, sizeof(RzAnalysisOp));
821  rz_strbuf_set(&rz_reverse->asm_op->buf_asm, hi->mnem);
822  rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
823  rz_reverse->asm_op->asm_toks->op_type = hi->ana_op.type;
824  break;
825  case HEXAGON_DISAS:
826  memcpy(rz_reverse->asm_op, &hi->asm_op, sizeof(RzAsmOp));
827  rz_strbuf_set(&rz_reverse->asm_op->buf_asm, hi->mnem);
828  rz_reverse->asm_op->asm_toks = rz_asm_tokenize_asm_regex(&rz_reverse->asm_op->buf_asm, state->token_patterns);
829  rz_reverse->asm_op->asm_toks->op_type = hi->ana_op.type;
830  break;
831  case HEXAGON_ANALYSIS:
832  memcpy(rz_reverse->ana_op, &hi->ana_op, sizeof(RzAnalysisOp));
833  break;
834  }
835 }
RZ_API RZ_OWN RzAsmTokenString * rz_asm_tokenize_asm_regex(RZ_BORROW RzStrBuf *asm_str, RzPVector *patterns)
Splits an asm string into tokens by using the given regex patterns.
Definition: asm.c:1472
uint32_t ut32
int hexagon_disasm_instruction(HexState *state, const ut32 hi_u32, RZ_INOUT HexInsn *hi, HexPkt *pkt)
static HexPkt * hex_get_pkt(HexState *state, const ut32 addr)
Returns the packet which covers the given address.
Definition: hexagon_arch.c:155
static HexInsn * hex_get_instr_at_addr(HexState *state, const ut32 addr)
Gives the instruction at a given address from the state.
Definition: hexagon_arch.c:80
RZ_API HexState * hexagon_get_state()
Initializes each packet of the state once.
Definition: hexagon_arch.c:217
static HexInsn * hex_add_instr_to_state(HexState *state, const HexInsn *new_ins)
Copies the given instruction to a state packet it belongs to. If the instruction does not fit to any ...
Definition: hexagon_arch.c:615
static void setup_new_instr(HexInsn *hi, const HexReversedOpcode *rz_reverse, const ut32 addr, const ut8 parse_bits)
Set the up new instr.
Definition: hexagon_arch.c:685
voidpf void * buf
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static ut32 rz_read_le32(const void *src)
Definition: rz_endian.h:239
RZ_API const char * rz_strbuf_set(RzStrBuf *sb, const char *s)
Definition: strbuf.c:153
RzAnalysisOp * ana_op
Definition: hexagon_arch.h:39
RzAsmOp * asm_op
Definition: hexagon_arch.h:40
HexReverseAction action
Definition: hexagon_arch.h:38
ut32 op_type
RzAnalysisOpType. Mnemonic color depends on this.
Definition: rz_print.h:73
RzStrBuf buf_asm
Definition: rz_asm.h:72
RzAsmTokenString * asm_toks
Tokenized asm string.
Definition: rz_asm.h:74

References HexReversedOpcode::action, addr, HexReversedOpcode::ana_op, HexReversedOpcode::asm_op, rz_asm_op_t::asm_toks, rz_asm_op_t::buf_asm, hex_add_instr_to_state(), hex_get_instr_at_addr(), hex_get_pkt(), HEXAGON_ANALYSIS, HEXAGON_DISAS, hexagon_disasm_instruction(), hexagon_get_state(), hi(), memcpy(), RzAsmTokenString::op_type, p, rz_asm_tokenize_asm_regex(), RZ_LOG_FATAL, rz_read_le32(), rz_strbuf_set(), and setup_new_instr().

Referenced by hexagon_v6_op().