2023-05-30 03:08:18 +00:00
|
|
|
/* Capstone Disassembly Engine */
|
|
|
|
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
|
|
|
|
/* Rot127 <unisono@quyllur.org>, 2022-2023 */
|
|
|
|
|
|
|
|
#ifndef CS_MAPPING_H
|
|
|
|
#define CS_MAPPING_H
|
|
|
|
|
|
|
|
#if defined(CAPSTONE_HAS_OSXKERNEL)
|
|
|
|
#include <libkern/libkern.h>
|
|
|
|
#else
|
|
|
|
#include "include/capstone/capstone.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
#endif
|
|
|
|
#include "cs_priv.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// map instruction to its characteristics
|
|
|
|
typedef struct insn_map {
|
2023-05-30 03:09:37 +00:00
|
|
|
unsigned short id; // The LLVM instruction id
|
|
|
|
unsigned short mapid; // The Capstone instruction id
|
2023-05-30 03:08:18 +00:00
|
|
|
#ifndef CAPSTONE_DIET
|
|
|
|
uint16_t regs_use[MAX_IMPL_R_REGS]; ///< list of implicit registers used by
|
2023-07-22 18:56:45 +00:00
|
|
|
///< this instruction
|
2023-05-30 03:08:18 +00:00
|
|
|
uint16_t regs_mod[MAX_IMPL_W_REGS]; ///< list of implicit registers modified
|
2023-07-22 18:56:45 +00:00
|
|
|
///< by this instruction
|
2023-05-30 03:09:37 +00:00
|
|
|
unsigned char groups
|
|
|
|
[MAX_NUM_GROUPS]; ///< list of group this instruction belong to
|
|
|
|
bool branch; // branch instruction?
|
|
|
|
bool indirect_branch; // indirect branch instruction?
|
2023-09-05 04:24:59 +00:00
|
|
|
union {
|
|
|
|
ppc_suppl_info ppc;
|
2024-06-26 06:47:44 +00:00
|
|
|
loongarch_suppl_info loongarch;
|
2024-07-08 02:28:54 +00:00
|
|
|
aarch64_suppl_info aarch64;
|
2024-09-14 08:57:54 +00:00
|
|
|
systemz_suppl_info systemz;
|
2023-09-05 04:24:59 +00:00
|
|
|
} suppl_info; // Supplementary information for each instruction.
|
2023-05-30 03:08:18 +00:00
|
|
|
#endif
|
|
|
|
} insn_map;
|
|
|
|
|
|
|
|
// look for @id in @m, given its size in @max. first time call will update
|
|
|
|
// @cache. return 0 if not found
|
|
|
|
unsigned short insn_find(const insn_map *m, unsigned int max, unsigned int id,
|
2023-05-30 03:09:37 +00:00
|
|
|
unsigned short **cache);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
|
2023-05-30 03:09:37 +00:00
|
|
|
unsigned imap_size);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
2023-11-15 04:12:14 +00:00
|
|
|
#define MAX_NO_DATA_TYPES 16
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
///< A LLVM<->CS Mapping entry of an MCOperand.
|
|
|
|
typedef struct {
|
|
|
|
uint8_t /* cs_op_type */ type; ///< Operand type (e.g.: reg, imm, mem)
|
|
|
|
uint8_t /* cs_ac_type */ access; ///< The access type (read, write)
|
2023-05-30 03:09:37 +00:00
|
|
|
uint8_t /* cs_data_type */
|
|
|
|
dtypes[MAX_NO_DATA_TYPES]; ///< List of op types. Terminated by
|
2023-07-22 18:56:45 +00:00
|
|
|
///< CS_DATA_TYPE_LAST
|
2023-05-30 03:08:18 +00:00
|
|
|
} mapping_op;
|
|
|
|
|
|
|
|
#define MAX_NO_INSN_MAP_OPS 16
|
|
|
|
|
|
|
|
///< MCOperands of an instruction.
|
|
|
|
typedef struct {
|
2023-05-30 03:09:37 +00:00
|
|
|
mapping_op
|
|
|
|
ops[MAX_NO_INSN_MAP_OPS]; ///< NULL terminated array of insn_op.
|
2023-05-30 03:08:18 +00:00
|
|
|
} map_insn_ops;
|
|
|
|
|
|
|
|
/// Only usable by `auto-sync` archs!
|
|
|
|
const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum,
|
2023-05-30 03:09:37 +00:00
|
|
|
const map_insn_ops *insn_ops_map,
|
|
|
|
size_t map_size);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
/// Only usable by `auto-sync` archs!
|
|
|
|
const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
|
2023-05-30 03:09:37 +00:00
|
|
|
const map_insn_ops *insn_ops_map,
|
|
|
|
size_t map_size);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
/// 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!
|
2024-06-10 02:01:00 +00:00
|
|
|
#ifndef CAPSTONE_DIET
|
2023-05-30 03:09:37 +00:00
|
|
|
#define map_get_op_type(MI, OpNum) \
|
2023-05-30 03:13:03 +00:00
|
|
|
mapping_get_op_type(MI, OpNum, (const map_insn_ops *)insn_operands, \
|
2023-05-30 03:09:37 +00:00
|
|
|
sizeof(insn_operands) / sizeof(insn_operands[0]))
|
2024-06-10 02:01:00 +00:00
|
|
|
#else
|
|
|
|
#define map_get_op_type(MI, OpNum) \
|
|
|
|
CS_OP_INVALID
|
|
|
|
#endif
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
/// Macro for easier access of operand access flags from the map.
|
|
|
|
/// Assumes the istruction operands map is called "insn_operands"
|
|
|
|
/// Only usable by `auto-sync` archs!
|
2024-06-10 02:01:00 +00:00
|
|
|
#ifndef CAPSTONE_DIET
|
2023-05-30 03:09:37 +00:00
|
|
|
#define map_get_op_access(MI, OpNum) \
|
2023-05-30 03:13:03 +00:00
|
|
|
mapping_get_op_access(MI, OpNum, (const map_insn_ops *)insn_operands, \
|
2023-05-30 03:09:37 +00:00
|
|
|
sizeof(insn_operands) / \
|
|
|
|
sizeof(insn_operands[0]))
|
2024-06-10 02:01:00 +00:00
|
|
|
#else
|
|
|
|
#define map_get_op_access(MI, OpNum) \
|
|
|
|
CS_AC_INVALID
|
|
|
|
#endif
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
///< Map for ids to their string
|
|
|
|
typedef struct name_map {
|
|
|
|
unsigned int id;
|
|
|
|
const char *name;
|
|
|
|
} name_map;
|
|
|
|
|
|
|
|
// map a name to its ID
|
|
|
|
// return 0 if not found
|
|
|
|
int name2id(const name_map *map, int max, const char *name);
|
|
|
|
|
|
|
|
// map ID to a name
|
|
|
|
// return NULL if not found
|
|
|
|
const char *id2name(const name_map *map, int max, const unsigned int id);
|
|
|
|
|
|
|
|
void map_add_implicit_write(MCInst *MI, uint32_t Reg);
|
2023-07-22 18:56:32 +00:00
|
|
|
void map_add_implicit_read(MCInst *MI, uint32_t Reg);
|
2023-07-19 09:56:27 +00:00
|
|
|
void map_remove_implicit_write(MCInst *MI, uint32_t Reg);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
void map_implicit_reads(MCInst *MI, const insn_map *imap);
|
|
|
|
|
|
|
|
void map_implicit_writes(MCInst *MI, const insn_map *imap);
|
|
|
|
|
2023-07-19 09:56:27 +00:00
|
|
|
void add_group(MCInst *MI, unsigned /* arch_group */ group);
|
|
|
|
|
2023-05-30 03:08:18 +00:00
|
|
|
void map_groups(MCInst *MI, const insn_map *imap);
|
|
|
|
|
|
|
|
void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size);
|
|
|
|
|
2024-02-23 05:20:46 +00:00
|
|
|
const void *map_get_suppl_info(MCInst *MI, const insn_map *imap);
|
|
|
|
|
2023-05-30 03:09:37 +00:00
|
|
|
#define DECL_get_detail_op(arch, ARCH) \
|
2023-05-30 03:08:18 +00:00
|
|
|
cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset);
|
|
|
|
|
|
|
|
DECL_get_detail_op(arm, ARM);
|
|
|
|
DECL_get_detail_op(ppc, PPC);
|
2023-05-30 03:13:03 +00:00
|
|
|
DECL_get_detail_op(tricore, TriCore);
|
2023-11-15 04:12:14 +00:00
|
|
|
DECL_get_detail_op(aarch64, AArch64);
|
2023-12-28 02:10:38 +00:00
|
|
|
DECL_get_detail_op(alpha, Alpha);
|
2024-03-26 05:58:56 +00:00
|
|
|
DECL_get_detail_op(hppa, HPPA);
|
2024-06-26 06:47:44 +00:00
|
|
|
DECL_get_detail_op(loongarch, LoongArch);
|
2024-09-07 14:30:47 +00:00
|
|
|
DECL_get_detail_op(mips, Mips);
|
2024-07-10 03:36:39 +00:00
|
|
|
DECL_get_detail_op(riscv, RISCV);
|
2024-09-14 08:57:54 +00:00
|
|
|
DECL_get_detail_op(systemz, SystemZ);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
/// Increments the detail->arch.op_count by one.
|
2023-05-30 03:09:37 +00:00
|
|
|
#define DEFINE_inc_detail_op_count(arch, ARCH) \
|
|
|
|
static inline void ARCH##_inc_op_count(MCInst *MI) \
|
|
|
|
{ \
|
|
|
|
MI->flat_insn->detail->arch.op_count++; \
|
2023-05-30 03:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decrements the detail->arch.op_count by one.
|
2023-05-30 03:09:37 +00:00
|
|
|
#define DEFINE_dec_detail_op_count(arch, ARCH) \
|
|
|
|
static inline void ARCH##_dec_op_count(MCInst *MI) \
|
|
|
|
{ \
|
|
|
|
MI->flat_insn->detail->arch.op_count--; \
|
2023-05-30 03:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_inc_detail_op_count(arm, ARM);
|
|
|
|
DEFINE_dec_detail_op_count(arm, ARM);
|
|
|
|
DEFINE_inc_detail_op_count(ppc, PPC);
|
|
|
|
DEFINE_dec_detail_op_count(ppc, PPC);
|
2023-05-30 03:13:03 +00:00
|
|
|
DEFINE_inc_detail_op_count(tricore, TriCore);
|
|
|
|
DEFINE_dec_detail_op_count(tricore, TriCore);
|
2023-11-15 04:12:14 +00:00
|
|
|
DEFINE_inc_detail_op_count(aarch64, AArch64);
|
|
|
|
DEFINE_dec_detail_op_count(aarch64, AArch64);
|
2023-12-28 02:10:38 +00:00
|
|
|
DEFINE_inc_detail_op_count(alpha, Alpha);
|
|
|
|
DEFINE_dec_detail_op_count(alpha, Alpha);
|
2024-03-26 05:58:56 +00:00
|
|
|
DEFINE_inc_detail_op_count(hppa, HPPA);
|
|
|
|
DEFINE_dec_detail_op_count(hppa, HPPA);
|
2024-06-26 06:47:44 +00:00
|
|
|
DEFINE_inc_detail_op_count(loongarch, LoongArch);
|
|
|
|
DEFINE_dec_detail_op_count(loongarch, LoongArch);
|
2024-09-07 14:30:47 +00:00
|
|
|
DEFINE_inc_detail_op_count(mips, Mips);
|
|
|
|
DEFINE_dec_detail_op_count(mips, Mips);
|
2024-07-10 03:36:39 +00:00
|
|
|
DEFINE_inc_detail_op_count(riscv, RISCV);
|
|
|
|
DEFINE_dec_detail_op_count(riscv, RISCV);
|
2024-09-14 08:57:54 +00:00
|
|
|
DEFINE_inc_detail_op_count(systemz, SystemZ);
|
|
|
|
DEFINE_dec_detail_op_count(systemz, SystemZ);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
/// Returns true if a memory operand is currently edited.
|
2023-05-30 03:09:37 +00:00
|
|
|
static inline bool doing_mem(const MCInst *MI)
|
|
|
|
{
|
|
|
|
return MI->csh->doing_mem;
|
|
|
|
}
|
2023-05-30 03:08:18 +00:00
|
|
|
|
|
|
|
/// Sets the doing_mem flag to @status.
|
|
|
|
static inline void set_doing_mem(const MCInst *MI, bool status)
|
|
|
|
{
|
|
|
|
MI->csh->doing_mem = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns detail->arch
|
2023-05-30 03:09:37 +00:00
|
|
|
#define DEFINE_get_arch_detail(arch, ARCH) \
|
|
|
|
static inline cs_##arch *ARCH##_get_detail(const MCInst *MI) \
|
|
|
|
{ \
|
|
|
|
assert(MI && MI->flat_insn && MI->flat_insn->detail); \
|
|
|
|
return &MI->flat_insn->detail->arch; \
|
2023-05-30 03:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_get_arch_detail(arm, ARM);
|
|
|
|
DEFINE_get_arch_detail(ppc, PPC);
|
2023-05-30 03:13:03 +00:00
|
|
|
DEFINE_get_arch_detail(tricore, TriCore);
|
2023-11-15 04:12:14 +00:00
|
|
|
DEFINE_get_arch_detail(aarch64, AArch64);
|
2023-12-28 02:10:38 +00:00
|
|
|
DEFINE_get_arch_detail(alpha, Alpha);
|
2024-03-26 05:58:56 +00:00
|
|
|
DEFINE_get_arch_detail(hppa, HPPA);
|
2024-06-26 06:47:44 +00:00
|
|
|
DEFINE_get_arch_detail(loongarch, LoongArch);
|
2024-09-07 14:30:47 +00:00
|
|
|
DEFINE_get_arch_detail(mips, Mips);
|
2024-07-10 03:36:39 +00:00
|
|
|
DEFINE_get_arch_detail(riscv, RISCV);
|
2024-09-14 08:57:54 +00:00
|
|
|
DEFINE_get_arch_detail(systemz, SystemZ);
|
2023-05-30 03:08:18 +00:00
|
|
|
|
2024-09-18 13:19:42 +00:00
|
|
|
#define DEFINE_check_safe_inc(Arch, ARCH) \
|
|
|
|
static inline void Arch##_check_safe_inc() { \
|
|
|
|
CS_ASSERT(Arch##_get_detail(MI)->op_count + 1 < NUM_##ARCH##_OPS); \
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_check_safe_inc(ARM, ARM);
|
|
|
|
DEFINE_check_safe_inc(PPC, PPC);
|
|
|
|
DEFINE_check_safe_inc(TriCore, TRICORE);
|
|
|
|
DEFINE_check_safe_inc(AArch64, AARCH64);
|
|
|
|
DEFINE_check_safe_inc(Alpha, ALPHA);
|
|
|
|
DEFINE_check_safe_inc(HPPA, HPPA);
|
|
|
|
DEFINE_check_safe_inc(LoongArch, LOONGARCH);
|
|
|
|
DEFINE_check_safe_inc(RISCV, RISCV);
|
|
|
|
DEFINE_check_safe_inc(SystemZ, SYSTEMZ);
|
|
|
|
DEFINE_check_safe_inc(Mips, MIPS);
|
|
|
|
|
2023-05-30 03:08:18 +00:00
|
|
|
static inline bool detail_is_set(const MCInst *MI)
|
|
|
|
{
|
|
|
|
assert(MI && MI->flat_insn);
|
2023-09-05 04:24:59 +00:00
|
|
|
return MI->flat_insn->detail != NULL && MI->csh->detail_opt & CS_OPT_ON;
|
2023-05-30 03:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline cs_detail *get_detail(const MCInst *MI)
|
|
|
|
{
|
|
|
|
assert(MI && MI->flat_insn);
|
|
|
|
return MI->flat_insn->detail;
|
|
|
|
}
|
|
|
|
|
2023-09-05 04:24:59 +00:00
|
|
|
/// Returns if the given instruction is an alias instruction.
|
|
|
|
#define RETURN_IF_INSN_IS_ALIAS(MI) \
|
|
|
|
do { \
|
|
|
|
if (MI->isAliasInstr) \
|
|
|
|
return; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
void map_set_fill_detail_ops(MCInst *MI, bool Val);
|
|
|
|
|
|
|
|
static inline bool map_fill_detail_ops(MCInst *MI) {
|
|
|
|
assert(MI);
|
|
|
|
return MI->fillDetailOps;
|
|
|
|
}
|
|
|
|
|
|
|
|
void map_set_is_alias_insn(MCInst *MI, bool Val, uint64_t Alias);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2024-08-31 13:33:38 +00:00
|
|
|
/// Mapping from Capstone enumeration identifiers and their values.
|
|
|
|
///
|
|
|
|
/// This map MUST BE sorted to allow binary searches.
|
|
|
|
/// Please always ensure the map is sorted after you added a value.
|
|
|
|
///
|
|
|
|
/// You can sort the map with Python.
|
|
|
|
/// Copy the map into a file and run:
|
|
|
|
///
|
|
|
|
/// ```python
|
|
|
|
/// with open("/tmp/file_with_map_entries") as f:
|
|
|
|
/// text = f.readlines()
|
|
|
|
///
|
|
|
|
/// text.sort()
|
|
|
|
/// print(''.join(text))
|
|
|
|
/// ```
|
|
|
|
typedef struct {
|
|
|
|
const char *str; ///< The name of the enumeration identifier
|
|
|
|
uint64_t val; ///< The value of the identifier
|
|
|
|
} cs_enum_id_map;
|
|
|
|
|
|
|
|
uint64_t enum_map_bin_search(const cs_enum_id_map *map, size_t map_len,
|
|
|
|
const char *id, bool *found);
|
|
|
|
|
2024-06-10 02:01:00 +00:00
|
|
|
#endif // CS_MAPPING_H
|