| 1 | /* Capstone Disassembly Engine */cxx . . $ `<file>/Mapping.c`/ |
| 2 | /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */ |
| 3 | /* Rot127 <[email protected]>, 2022-2023 */ |
| 4 | |
| 5 | #include "Mapping.h"cxx . . $ `<file>/Mapping.h`/ |
| 6 | |
| 7 | // create a cache for fast id lookup |
| 8 | static unsigned short *make_id2insncxx . . $ make_id2insn(4527e4810430f9b).(const insn_mapcxx . . $ insn_map# *insns, unsigned int size) |
| 9 | { |
| 10 | // NOTE: assume that the max id is always put at the end of insns array |
| 11 | unsigned short max_id = insns[size - 1].idcxx . . $ insn_map#id.; |
| 12 | unsigned int i; |
| 13 | |
| 14 | unsigned short *cache = |
| 15 | (unsigned short *)cs_mem_calloccxx . . $ cs_mem_calloc.(max_id + 1, sizeof(*cache)); |
| 16 | |
| 17 | for (i = 1; i < size; i++) |
| 18 | cache[insns[i].idcxx . . $ insn_map#id.] = i; |
| 19 | |
| 20 | return cache; |
| 21 | } |
| 22 | |
| 23 | // look for @id in @insns, given its size in @max. first time call will update |
| 24 | // @cache. return 0 if not found |
| 25 | unsigned short insn_findcxx . . $ insn_find(1ce6b9640f5aa651).(const insn_mapcxx . . $ insn_map# *insns, unsigned int max, |
| 26 | unsigned int id, unsigned short **cache) |
| 27 | { |
| 28 | if (id > insns[max - 1].idcxx . . $ insn_map#id.) |
| 29 | return 0; |
| 30 | |
| 31 | if (*cache == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) |
| 32 | *cache = make_id2insncxx . . $ make_id2insn(4527e4810430f9b).(insns, max); |
| 33 | |
| 34 | return (*cache)[id]; |
| 35 | } |
| 36 | |
| 37 | // Gives the id for the given @name if it is saved in @map. |
| 38 | // Returns the id or -1 if not found. |
| 39 | int name2idcxx . . $ name2id(b6128f0fa858e731).(const name_mapcxx . . $ name_map# *map, int max, const char *name) |
| 40 | { |
| 41 | int i; |
| 42 | |
| 43 | for (i = 0; i < max; i++) { |
| 44 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(map[i].namecxx . . $ name_map#name., name)) { |
| 45 | return map[i].idcxx . . $ name_map#id.; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // nothing match |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | // Gives the name for the given @id if it is saved in @map. |
| 54 | // Returns the name or NULL if not found. |
| 55 | const char *id2namecxx . . $ id2name(92d4aec78e87b576).(const name_mapcxx . . $ name_map# *map, int max, const unsigned int id) |
| 56 | { |
| 57 | int i; |
| 58 | |
| 59 | for (i = 0; i < max; i++) { |
| 60 | if (map[i].idcxx . . $ name_map#id. == id) { |
| 61 | return map[i].namecxx . . $ name_map#name.; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // nothing match |
| 66 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 67 | } |
| 68 | |
| 69 | /// Adds a register to the implicit write register list. |
| 70 | /// It will not add the same register twice. |
| 71 | void map_add_implicit_writecxx . . $ map_add_implicit_write(1d3fdd902ae43da2).(MCInstcxx . . $ MCInst# *MI, uint32_tcxx . . $ uint32_t# Reg) |
| 72 | { |
| 73 | if (!MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.) |
| 74 | return; |
| 75 | |
| 76 | uint16_tcxx . . $ uint16_t# *regs_write = MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.->regs_writecxx . . $ cs_detail#regs_write.; |
| 77 | for (int i = 0; i < MAX_IMPL_W_REGScxx . . $ `include/capstone/capstone.h:402:9`!; ++i) { |
| 78 | if (i == MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.->regs_write_countcxx . . $ cs_detail#regs_write_count.) { |
| 79 | regs_write[i] = Reg; |
| 80 | MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.->regs_write_countcxx . . $ cs_detail#regs_write_count.++; |
| 81 | return; |
| 82 | } |
| 83 | if (regs_write[i] == Reg) |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Copies the implicit read registers of @imap to @MI->flat_insn. |
| 89 | /// Already present registers will be preserved. |
| 90 | void map_implicit_readscxx . . $ map_implicit_reads(86236b952129e9c2).(MCInstcxx . . $ MCInst# *MI, const insn_mapcxx . . $ insn_map# *imap) |
| 91 | { |
| 92 | #ifndef CAPSTONE_DIET |
| 93 | if (!MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.) |
| 94 | return; |
| 95 | |
| 96 | cs_detailcxx . . $ cs_detail# *detail = MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.; |
| 97 | unsigned Opcode = MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI); |
| 98 | unsigned i = 0; |
| 99 | uint16_tcxx . . $ uint16_t# reg = imap[Opcode].regs_usecxx . . $ insn_map#regs_use.[i]; |
| 100 | while (reg != 0) { |
| 101 | if (i >= MAX_IMPL_R_REGScxx . . $ `include/capstone/capstone.h:403:9`! || |
| 102 | detail->regs_read_countcxx . . $ cs_detail#regs_read_count. >= MAX_IMPL_R_REGScxx . . $ `include/capstone/capstone.h:403:9`!) { |
| 103 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Too many implicit read register defined in " |
| 104 | "instruction mapping.\n"); |
| 105 | return; |
| 106 | } |
| 107 | detail->regs_readcxx . . $ cs_detail#regs_read.[detail->regs_read_countcxx . . $ cs_detail#regs_read_count.++] = reg; |
| 108 | reg = imap[Opcode].regs_usecxx . . $ insn_map#regs_use.[++i]; |
| 109 | } |
| 110 | #endif // CAPSTONE_DIET |
| 111 | } |
| 112 | |
| 113 | /// Copies the implicit write registers of @imap to @MI->flat_insn. |
| 114 | /// Already present registers will be preserved. |
| 115 | void map_implicit_writescxx . . $ map_implicit_writes(86236b952129e9c2).(MCInstcxx . . $ MCInst# *MI, const insn_mapcxx . . $ insn_map# *imap) |
| 116 | { |
| 117 | #ifndef CAPSTONE_DIET |
| 118 | if (!MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.) |
| 119 | return; |
| 120 | |
| 121 | cs_detailcxx . . $ cs_detail# *detail = MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.; |
| 122 | unsigned Opcode = MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI); |
| 123 | unsigned i = 0; |
| 124 | uint16_tcxx . . $ uint16_t# reg = imap[Opcode].regs_modcxx . . $ insn_map#regs_mod.[i]; |
| 125 | while (reg != 0) { |
| 126 | if (i >= MAX_IMPL_W_REGScxx . . $ `include/capstone/capstone.h:402:9`! || |
| 127 | detail->regs_write_countcxx . . $ cs_detail#regs_write_count. >= MAX_IMPL_W_REGScxx . . $ `include/capstone/capstone.h:402:9`!) { |
| 128 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Too many implicit write register defined in " |
| 129 | "instruction mapping.\n"); |
| 130 | return; |
| 131 | } |
| 132 | detail->regs_writecxx . . $ cs_detail#regs_write.[detail->regs_write_countcxx . . $ cs_detail#regs_write_count.++] = reg; |
| 133 | reg = imap[Opcode].regs_modcxx . . $ insn_map#regs_mod.[++i]; |
| 134 | } |
| 135 | #endif // CAPSTONE_DIET |
| 136 | } |
| 137 | |
| 138 | /// Copies the groups from @imap to @MI->flat_insn. |
| 139 | /// Already present groups will be preserved. |
| 140 | void map_groupscxx . . $ map_groups(86236b952129e9c2).(MCInstcxx . . $ MCInst# *MI, const insn_mapcxx . . $ insn_map# *imap) |
| 141 | { |
| 142 | #ifndef CAPSTONE_DIET |
| 143 | if (!MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.) |
| 144 | return; |
| 145 | |
| 146 | cs_detailcxx . . $ cs_detail# *detail = MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.; |
| 147 | unsigned Opcode = MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI); |
| 148 | unsigned i = 0; |
| 149 | uint16_tcxx . . $ uint16_t# group = imap[Opcode].groupscxx . . $ insn_map#groups.[i]; |
| 150 | while (group != 0) { |
| 151 | if (detail->groups_countcxx . . $ cs_detail#groups_count. >= MAX_NUM_GROUPScxx . . $ `include/capstone/capstone.h:404:9`!) { |
| 152 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Too many groups defined in instruction mapping.\n"); |
| 153 | return; |
| 154 | } |
| 155 | detail->groupscxx . . $ cs_detail#groups.[detail->groups_countcxx . . $ cs_detail#groups_count.++] = group; |
| 156 | group = imap[Opcode].groupscxx . . $ insn_map#groups.[++i]; |
| 157 | } |
| 158 | #endif // CAPSTONE_DIET |
| 159 | } |
| 160 | |
| 161 | // Search for the CS instruction id for the given @MC_Opcode in @imap. |
| 162 | // return -1 if none is found. |
| 163 | unsigned int find_cs_idcxx . . $ find_cs_id(cca7d6eccb98dd8e).(unsigned MC_Opcode, const insn_mapcxx . . $ insn_map# *imap, |
| 164 | unsigned imap_size) |
| 165 | { |
| 166 | // binary searching since the IDs are sorted in order |
| 167 | unsigned int left, right, m; |
| 168 | unsigned int max = imap_size; |
| 169 | |
| 170 | right = max - 1; |
| 171 | |
| 172 | if (MC_Opcode < imap[0].idcxx . . $ insn_map#id. || MC_Opcode > imap[right].idcxx . . $ insn_map#id.) |
| 173 | // not found |
| 174 | return -1; |
| 175 | |
| 176 | left = 0; |
| 177 | |
| 178 | while (left <= right) { |
| 179 | m = (left + right) / 2; |
| 180 | if (MC_Opcode == imap[m].idcxx . . $ insn_map#id.) { |
| 181 | return m; |
| 182 | } |
| 183 | |
| 184 | if (MC_Opcode < imap[m].idcxx . . $ insn_map#id.) |
| 185 | right = m - 1; |
| 186 | else |
| 187 | left = m + 1; |
| 188 | } |
| 189 | |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | /// Sets the Capstone instruction id which maps to the @MI opcode. |
| 194 | /// If no mapping is found the function returns and prints an error. |
| 195 | void map_cs_idcxx . . $ map_cs_id(4a809e07614e3f82).(MCInstcxx . . $ MCInst# *MI, const insn_mapcxx . . $ insn_map# *imap, unsigned int imap_size) |
| 196 | { |
| 197 | unsigned int i = find_cs_idcxx . . $ find_cs_id(cca7d6eccb98dd8e).(MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI), imap, imap_size); |
| 198 | if (i != -1) { |
| 199 | MI->flat_insncxx . . $ MCInst#flat_insn.->idcxx . . $ cs_insn#id. = imap[i].mapidcxx . . $ insn_map#mapid.; |
| 200 | return; |
| 201 | } |
| 202 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Could not find CS id for MCInst opcode: %d\n", |
| 203 | MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI)); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | /// Returns the operand type information from the |
| 208 | /// mapping table for instruction operands. |
| 209 | /// Only usable by `auto-sync` archs! |
| 210 | const cs_op_typecxx . . $ cs_op_type# mapping_get_op_typecxx . . $ mapping_get_op_type(8296293786d992b4).(MCInstcxx . . $ MCInst# *MI, unsigned OpNum, |
| 211 | const map_insn_opscxx . . $ map_insn_ops# *insn_ops_map, |
| 212 | size_t map_size) |
| 213 | { |
| 214 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(MI); |
| 215 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(MI->Opcode < map_size); |
| 216 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) / |
| 217 | sizeof(insn_ops_map[MI->Opcode].ops[0])); |
| 218 | |
| 219 | return insn_ops_map[MI->Opcodecxx . . $ MCInst#Opcode.].opscxx . . $ $anonymous_type_338b1da8dd16858f_1#ops.[OpNum].typecxx . . $ $anonymous_type_338b1da8dd16858f_0#type.; |
| 220 | } |
| 221 | |
| 222 | /// Returns the operand access flags from the |
| 223 | /// mapping table for instruction operands. |
| 224 | /// Only usable by `auto-sync` archs! |
| 225 | const cs_ac_typecxx . . $ cs_ac_type# mapping_get_op_accesscxx . . $ mapping_get_op_access(caa755bed75115f0).(MCInstcxx . . $ MCInst# *MI, unsigned OpNum, |
| 226 | const map_insn_opscxx . . $ map_insn_ops# *insn_ops_map, |
| 227 | size_t map_size) |
| 228 | { |
| 229 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(MI); |
| 230 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(MI->Opcode < map_size); |
| 231 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(OpNum < sizeof(insn_ops_map[MI->Opcode].ops) / |
| 232 | sizeof(insn_ops_map[MI->Opcode].ops[0])); |
| 233 | |
| 234 | cs_ac_typecxx . . $ cs_ac_type# access = insn_ops_map[MI->Opcodecxx . . $ MCInst#Opcode.].opscxx . . $ $anonymous_type_338b1da8dd16858f_1#ops.[OpNum].accesscxx . . $ $anonymous_type_338b1da8dd16858f_0#access.; |
| 235 | if (MCInst_opIsTiedcxx . . $ MCInst_opIsTied(3f89b876b015f5e2).(MI, OpNum) || MCInst_opIsTyingcxx . . $ MCInst_opIsTying(3f89b876b015f5e2).(MI, OpNum)) |
| 236 | access |= (access == CS_AC_READcxx . . $ CS_AC_READ.) ? CS_AC_WRITEcxx . . $ CS_AC_WRITE. : CS_AC_READcxx . . $ CS_AC_READ.; |
| 237 | return access; |
| 238 | } |
| 239 | |
| 240 | /// Returns the operand at detail->arch.operands[op_count + offset] |
| 241 | /// Or NULL if detail is not set. |
| 242 | #define DEFINE_get_detail_opcxx . . $ `Mapping.c:242:9`!(arch, ARCH) \ |
| 243 | cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset) \ |
| 244 | { \ |
| 245 | if (!MI->flat_insn->detail) \ |
| 246 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; \ |
| 247 | int OpIdx = MI->flat_insn->detail->arch.op_count + offset; \ |
| 248 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(OpIdx >= 0 && OpIdx < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!); \ |
| 249 | return &MI->flat_insn->detail->arch.operands[OpIdx]; \ |
| 250 | } |
| 251 | |
| 252 | DEFINE_get_detail_opcxx . . $ `Mapping.c:242:9`!(arm, ARM); |
| 253 | DEFINE_get_detail_opcxx . . $ `Mapping.c:242:9`!(ppc, PPC); |
| 254 | DEFINE_get_detail_opcxx . . $ `Mapping.c:242:9`!(tricore, TriCore); |