Skip to content

Commit

Permalink
Import encoding implementation from @AngelDev06
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelDev06 authored and Rot127 committed Jan 3, 2024
1 parent b9c260e commit 6b946b1
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 193 deletions.
24 changes: 24 additions & 0 deletions Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ void map_groups(MCInst *MI, const insn_map *imap)
#endif // CAPSTONE_DIET
}

/// Copies the opcode encoding info from @imap to @MI->flat_insn.
void map_opcode_encoding(MCInst *MI, const insn_map *imap)
{
#ifndef CAPSTONE_DIET
if (!MI->flat_insn->detail)
return;

unsigned Opcode = MCInst_getOpcode(MI);
MI->flat_insn->detail->opcode_encoding = imap[Opcode].opcode_encoding;
#endif
}

// Search for the CS instruction id for the given @MC_Opcode in @imap.
// return -1 if none is found.
unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
Expand Down Expand Up @@ -298,6 +310,18 @@ const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
return access;
}

const cs_operand_encoding
mapping_get_op_encoding(MCInst *MI, unsigned OpNum,
const map_insn_ops *insn_ops_map, size_t map_size)
{
assert(MI);
assert(MI->Opcode < map_size);
assert(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) /
sizeof(insn_ops_map[MI->Opcode].ops[0]));

return insn_ops_map[MI->Opcode].ops[OpNum].encoding;
}

/// Returns the operand at detail->arch.operands[op_count + offset]
/// Or NULL if detail is not set.
#define DEFINE_get_detail_op(arch, ARCH) \
Expand Down
14 changes: 13 additions & 1 deletion Mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct insn_map {
union {
ppc_suppl_info ppc;
} suppl_info; // Supplementary information for each instruction.
cs_opcode_encoding opcode_encoding; // the opcode encoding info of the instruction
#endif
} insn_map;

Expand All @@ -51,6 +52,7 @@ typedef struct {
uint8_t /* cs_data_type */
dtypes[MAX_NO_DATA_TYPES]; ///< List of op types. Terminated by
///< CS_DATA_TYPE_LAST
cs_operand_encoding encoding; ///< The encoding of the operand
} mapping_op;

#define MAX_NO_INSN_MAP_OPS 16
Expand All @@ -71,6 +73,10 @@ const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
const map_insn_ops *insn_ops_map,
size_t map_size);

const cs_operand_encoding
mapping_get_op_encoding(MCInst *MI, unsigned OpNum,
const map_insn_ops *insn_ops_map, size_t map_size);

/// Macro for easier access of operand types from the map.
/// Assumes the istruction operands map is called "insn_operands"
/// Only usable by `auto-sync` archs!
Expand All @@ -86,6 +92,10 @@ const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
sizeof(insn_operands) / \
sizeof(insn_operands[0]))

#define map_get_op_encoding(MI, OpNum) \
mapping_get_op_encoding(MI, OpNum, insn_operands, \
sizeof(insn_operands) / sizeof(insn_operands[0]))

///< Map for ids to their string
typedef struct name_map {
unsigned int id;
Expand All @@ -112,6 +122,8 @@ void add_group(MCInst *MI, unsigned /* arch_group */ group);

void map_groups(MCInst *MI, const insn_map *imap);

void map_opcode_encoding(MCInst *MI, const insn_map *imap);

void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size);

#define DECL_get_detail_op(arch, ARCH) \
Expand Down Expand Up @@ -206,4 +218,4 @@ bool map_use_alias_details(const MCInst *MI);

void map_set_alias_id(MCInst *MI, const SStream *O, const name_map *alias_mnem_id_map, int map_size);

#endif // CS_MAPPING_H
#endif // CS_MAPPING_H
Loading

0 comments on commit 6b946b1

Please sign in to comment.