Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | /* Capstone Disassembler Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <capstone.h> |
| 8 | |
| 9 | #include "cs_priv.h" |
| 10 | |
| 11 | #include "MCRegisterInfo.h" |
| 12 | |
| 13 | #include "arch/X86/X86Disassembler.h" |
| 14 | #include "arch/X86/X86InstPrinter.h" |
| 15 | #include "arch/X86/mapping.h" |
| 16 | |
| 17 | #include "arch/ARM/ARMDisassembler.h" |
| 18 | #include "arch/ARM/ARMInstPrinter.h" |
| 19 | #include "arch/ARM/mapping.h" |
| 20 | |
| 21 | #include "arch/Mips/MipsDisassembler.h" |
| 22 | #include "arch/Mips/MipsInstPrinter.h" |
| 23 | #include "arch/Mips/mapping.h" |
| 24 | |
| 25 | #include "arch/AArch64/AArch64Disassembler.h" |
| 26 | #include "arch/AArch64/AArch64InstPrinter.h" |
| 27 | #include "arch/AArch64/mapping.h" |
| 28 | |
| 29 | #include "utils.h" |
| 30 | |
Nguyen Anh Quynh | 36df4bb | 2013-12-10 13:31:20 +0800 | [diff] [blame] | 31 | |
| 32 | void cs_version(int *major, int *minor) |
| 33 | { |
| 34 | *major = CS_API_MAJOR; |
| 35 | *minor = CS_API_MINOR; |
| 36 | } |
| 37 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 38 | cs_err cs_errno(csh handle) |
| 39 | { |
| 40 | if (!handle) |
| 41 | return CS_ERR_CSH; |
| 42 | |
| 43 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 44 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 45 | return ud->errnum; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 46 | } |
| 47 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 48 | cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle) |
| 49 | { |
| 50 | cs_struct *ud; |
| 51 | |
| 52 | ud = calloc(1, sizeof(*ud)); |
| 53 | if (!ud) { |
| 54 | // memory insufficient |
| 55 | return CS_ERR_MEM; |
| 56 | } |
| 57 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 58 | ud->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 59 | ud->arch = arch; |
| 60 | ud->mode = mode; |
| 61 | ud->big_endian = mode & CS_MODE_BIG_ENDIAN; |
| 62 | ud->reg_name = NULL; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 63 | ud->detail = CS_OPT_ON; // by default break instruction into details |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 64 | |
| 65 | switch (ud->arch) { |
| 66 | case CS_ARCH_X86: |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 67 | // by default, we use Intel syntax |
| 68 | ud->printer = X86_Intel_printInst; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 69 | ud->printer_info = NULL; |
| 70 | ud->disasm = X86_getInstruction; |
| 71 | ud->reg_name = X86_reg_name; |
| 72 | ud->insn_id = X86_get_insn_id; |
| 73 | ud->insn_name = X86_insn_name; |
Nguyen Anh Quynh | a01d154 | 2013-12-12 15:54:30 +0800 | [diff] [blame] | 74 | ud->post_printer = X86_post_printer; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 75 | break; |
| 76 | case CS_ARCH_ARM: { |
| 77 | MCRegisterInfo *mri = malloc(sizeof(*mri)); |
| 78 | |
| 79 | ARM_init(mri); |
| 80 | |
| 81 | ud->printer = ARM_printInst; |
| 82 | ud->printer_info = mri; |
| 83 | ud->reg_name = ARM_reg_name; |
| 84 | ud->insn_id = ARM_get_insn_id; |
| 85 | ud->insn_name = ARM_insn_name; |
| 86 | ud->post_printer = ARM_post_printer; |
| 87 | |
| 88 | if (ud->mode & CS_MODE_THUMB) |
| 89 | ud->disasm = Thumb_getInstruction; |
| 90 | else |
| 91 | ud->disasm = ARM_getInstruction; |
| 92 | break; |
| 93 | } |
| 94 | case CS_ARCH_MIPS: { |
| 95 | MCRegisterInfo *mri = malloc(sizeof(*mri)); |
| 96 | |
| 97 | Mips_init(mri); |
| 98 | ud->printer = Mips_printInst; |
| 99 | ud->printer_info = mri; |
| 100 | ud->getinsn_info = mri; |
| 101 | ud->reg_name = Mips_reg_name; |
| 102 | ud->insn_id = Mips_get_insn_id; |
| 103 | ud->insn_name = Mips_insn_name; |
| 104 | |
| 105 | if (ud->mode & CS_MODE_32) |
| 106 | ud->disasm = Mips_getInstruction; |
| 107 | else |
| 108 | ud->disasm = Mips64_getInstruction; |
| 109 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | case CS_ARCH_ARM64: { |
| 113 | MCRegisterInfo *mri = malloc(sizeof(*mri)); |
| 114 | |
| 115 | AArch64_init(mri); |
| 116 | ud->printer = AArch64_printInst; |
| 117 | ud->printer_info = mri; |
| 118 | ud->getinsn_info = mri; |
| 119 | ud->disasm = AArch64_getInstruction; |
| 120 | ud->reg_name = AArch64_reg_name; |
| 121 | ud->insn_id = AArch64_get_insn_id; |
| 122 | ud->insn_name = AArch64_insn_name; |
| 123 | ud->post_printer = AArch64_post_printer; |
| 124 | break; |
| 125 | } |
| 126 | default: // unsupported architecture |
| 127 | free(ud); |
| 128 | return CS_ERR_ARCH; |
| 129 | } |
| 130 | |
| 131 | *handle = (uintptr_t)ud; |
| 132 | |
| 133 | return CS_ERR_OK; |
| 134 | } |
| 135 | |
| 136 | cs_err cs_close(csh handle) |
| 137 | { |
| 138 | if (!handle) |
| 139 | return CS_ERR_CSH; |
| 140 | |
| 141 | cs_struct *ud = (cs_struct *)(uintptr_t)handle; |
| 142 | |
| 143 | switch (ud->arch) { |
| 144 | case CS_ARCH_X86: |
| 145 | break; |
| 146 | case CS_ARCH_ARM: |
| 147 | case CS_ARCH_MIPS: |
| 148 | case CS_ARCH_ARM64: |
| 149 | free(ud->printer_info); |
| 150 | break; |
| 151 | default: // unsupported architecture |
| 152 | return CS_ERR_HANDLE; |
| 153 | } |
| 154 | |
| 155 | memset(ud, 0, sizeof(*ud)); |
| 156 | free(ud); |
| 157 | |
| 158 | return CS_ERR_OK; |
| 159 | } |
| 160 | |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 161 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 162 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 163 | // fill insn with mnemonic & operands info |
| 164 | static void fill_insn(cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci, |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 165 | PostPrinter_t printer, const uint8_t *code) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 166 | { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 167 | if (handle->detail) { |
| 168 | memcpy(insn, &mci->pub_insn, sizeof(*insn)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 169 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 170 | // fill the instruction bytes |
| 171 | memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size)); |
Nguyen Anh Quynh | ad61c49 | 2013-11-30 16:23:31 +0800 | [diff] [blame] | 172 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 173 | } else { |
| 174 | insn->address = mci->address; |
| 175 | insn->size = mci->insn_size; |
| 176 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 177 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 178 | // map internal instruction opcode to public insn ID |
| 179 | if (handle->insn_id) |
| 180 | handle->insn_id(insn, MCInst_getOpcode(mci), handle->detail); |
| 181 | |
| 182 | // alias instruction might have ID saved in OpcodePub |
| 183 | if (MCInst_getOpcodePub(mci)) |
| 184 | insn->id = MCInst_getOpcodePub(mci); |
| 185 | |
| 186 | // post printer handles some corner cases (hacky) |
| 187 | if (printer) |
| 188 | printer((csh)handle, insn, buffer); |
| 189 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 190 | // fill in mnemonic & operands |
Nguyen Anh Quynh | defb9bc | 2013-12-12 14:00:12 +0800 | [diff] [blame] | 191 | // find first space or tab |
| 192 | char *sp = buffer; |
| 193 | for (sp = buffer; *sp; sp++) |
| 194 | if (*sp == ' '||*sp == '\t') |
| 195 | break; |
| 196 | if (*sp) { |
| 197 | *sp = '\0'; |
Nguyen Anh Quynh | 86dc393 | 2013-12-12 14:43:39 +0800 | [diff] [blame] | 198 | // find the next non-space char |
| 199 | sp++; |
| 200 | for (; ((*sp == ' ') || (*sp == '\t')); sp++); |
| 201 | strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 202 | insn->op_str[sizeof(insn->op_str) - 1] = '\0'; |
| 203 | } else |
| 204 | insn->op_str[0] = '\0'; |
| 205 | |
| 206 | strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1); |
| 207 | insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0'; |
| 208 | } |
| 209 | |
Nguyen Anh Quynh | da8adad | 2013-12-04 09:44:07 +0800 | [diff] [blame] | 210 | cs_err cs_option(csh ud, cs_opt_type type, size_t value) |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 211 | { |
| 212 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 213 | if (!handle) |
| 214 | return CS_ERR_CSH; |
| 215 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 216 | switch(type) { |
| 217 | default: |
| 218 | break; |
| 219 | case CS_OPT_DETAIL: |
| 220 | handle->detail = value; |
| 221 | return CS_ERR_OK; |
| 222 | } |
| 223 | |
| 224 | // only selected archs care about CS_OPT_SYNTAX |
Nguyen Anh Quynh | 4a60a56 | 2013-12-03 21:56:54 +0800 | [diff] [blame] | 225 | switch (handle->arch) { |
| 226 | default: |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 227 | handle->errnum = CS_ERR_OPTION; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 228 | return CS_ERR_OPTION; |
| 229 | |
Nguyen Anh Quynh | 4a60a56 | 2013-12-03 21:56:54 +0800 | [diff] [blame] | 230 | case CS_ARCH_X86: |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 231 | if (type & CS_OPT_SYNTAX) { |
| 232 | switch(value) { |
| 233 | default: |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 234 | handle->errnum = CS_ERR_OPTION; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 235 | return CS_ERR_OPTION; |
| 236 | |
Nguyen Anh Quynh | c618db4 | 2013-12-04 00:05:04 +0800 | [diff] [blame] | 237 | case CS_OPT_SYNTAX_INTEL: |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 238 | handle->printer = X86_Intel_printInst; |
| 239 | break; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 240 | |
Nguyen Anh Quynh | c618db4 | 2013-12-04 00:05:04 +0800 | [diff] [blame] | 241 | case CS_OPT_SYNTAX_ATT: |
Nguyen Anh Quynh | b8ce68e | 2013-12-03 23:45:08 +0800 | [diff] [blame] | 242 | handle->printer = X86_ATT_printInst; |
| 243 | break; |
| 244 | } |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 245 | } else { |
| 246 | handle->errnum = CS_ERR_OPTION; |
Nguyen Anh Quynh | 041e25d | 2013-12-06 00:37:32 +0800 | [diff] [blame] | 247 | return CS_ERR_OPTION; |
Nguyen Anh Quynh | fe8030b | 2013-12-06 10:09:43 +0800 | [diff] [blame] | 248 | } |
Nguyen Anh Quynh | 4a60a56 | 2013-12-03 21:56:54 +0800 | [diff] [blame] | 249 | break; |
| 250 | } |
Nguyen Anh Quynh | 01aba00 | 2013-12-03 21:00:09 +0800 | [diff] [blame] | 251 | |
| 252 | return CS_ERR_OK; |
| 253 | } |
| 254 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 255 | size_t cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn *insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 256 | { |
| 257 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 258 | MCInst mci; |
| 259 | uint16_t insn_size; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 260 | size_t c = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 261 | |
| 262 | if (!handle) { |
| 263 | // FIXME: handle this case? |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 264 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 268 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 269 | memset(insn, 0, count * sizeof(*insn)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 270 | |
| 271 | while (size > 0) { |
| 272 | MCInst_Init(&mci); |
| 273 | |
| 274 | bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
| 275 | if (r) { |
| 276 | SStream ss; |
| 277 | SStream_Init(&ss); |
| 278 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 279 | mci.detail = handle->detail; |
| 280 | // relative branches need to know the address & size of current insn |
| 281 | mci.insn_size = insn_size; |
| 282 | mci.address = offset; |
| 283 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 284 | if (handle->detail) { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 285 | mci.mode = handle->mode; |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 286 | // save all the information for non-detailed mode |
| 287 | mci.pub_insn.address = offset; |
| 288 | mci.pub_insn.size = insn_size; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 289 | } |
| 290 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 291 | handle->printer(&mci, &ss, handle->printer_info); |
| 292 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 293 | fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 294 | |
| 295 | c++; |
| 296 | insn++; |
| 297 | buffer += insn_size; |
| 298 | size -= insn_size; |
| 299 | offset += insn_size; |
| 300 | |
| 301 | if (count > 0) { |
| 302 | if (c == count) |
| 303 | return c; |
| 304 | } |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 305 | } else |
| 306 | // face a broken instruction? |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 307 | return c; |
| 308 | } |
| 309 | |
| 310 | return c; |
| 311 | } |
| 312 | |
| 313 | // dynamicly allocate memory to contain disasm insn |
| 314 | // NOTE: caller must free() the allocated memory itself to avoid memory leaking |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 315 | size_t cs_disasm_dyn(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 316 | { |
| 317 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 318 | MCInst mci; |
| 319 | uint16_t insn_size; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 320 | size_t c = 0, f = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 321 | cs_insn insn_cache[64]; |
| 322 | void *total = NULL; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 323 | size_t total_size = 0; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 324 | |
| 325 | if (!handle) { |
| 326 | // FIXME: how to handle this case: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 327 | // handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 331 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 332 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 333 | memset(insn_cache, 0, sizeof(insn_cache)); |
| 334 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 335 | while (size > 0) { |
| 336 | MCInst_Init(&mci); |
| 337 | |
| 338 | bool r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info); |
| 339 | if (r) { |
| 340 | SStream ss; |
| 341 | SStream_Init(&ss); |
| 342 | |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 343 | mci.detail = handle->detail; |
| 344 | // relative branches need to know the address & size of current insn |
| 345 | mci.insn_size = insn_size; |
| 346 | mci.address = offset; |
| 347 | |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 348 | if (handle->detail) { |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 349 | mci.mode = handle->mode; |
Nguyen Anh Quynh | 4d3e852 | 2013-12-14 10:45:09 +0800 | [diff] [blame^] | 350 | // save all the information for non-detailed mode |
| 351 | mci.pub_insn.address = offset; |
| 352 | mci.pub_insn.size = insn_size; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 353 | } |
| 354 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 355 | handle->printer(&mci, &ss, handle->printer_info); |
| 356 | |
Joxean | 114df0e | 2013-12-04 07:11:32 +0100 | [diff] [blame] | 357 | fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer); |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 358 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 359 | f++; |
| 360 | |
| 361 | if (f == ARR_SIZE(insn_cache)) { |
| 362 | // resize total to contain newly disasm insns |
| 363 | total_size += sizeof(insn_cache); |
| 364 | void *tmp = realloc(total, total_size); |
| 365 | if (tmp == NULL) { // insufficient memory |
| 366 | free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 367 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | total = tmp; |
| 372 | memcpy(total + total_size - sizeof(insn_cache), insn_cache, sizeof(insn_cache)); |
| 373 | // reset f back to 0 |
| 374 | f = 0; |
| 375 | } |
| 376 | |
| 377 | c++; |
| 378 | buffer += insn_size; |
| 379 | size -= insn_size; |
| 380 | offset += insn_size; |
| 381 | |
| 382 | if (count > 0 && c == count) |
| 383 | break; |
Nguyen Anh Quynh | 8f13f3c | 2013-12-04 22:57:04 +0800 | [diff] [blame] | 384 | } else { |
| 385 | // encounter a broken instruction |
| 386 | // XXX: TODO: JOXEAN continue here |
| 387 | break; |
| 388 | } |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | if (f) { |
| 392 | // resize total to contain newly disasm insns |
| 393 | void *tmp = realloc(total, total_size + f * sizeof(insn_cache[0])); |
| 394 | if (tmp == NULL) { // insufficient memory |
| 395 | free(total); |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 396 | handle->errnum = CS_ERR_MEM; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | total = tmp; |
| 401 | memcpy(total + total_size, insn_cache, f * sizeof(insn_cache[0])); |
| 402 | } |
| 403 | |
| 404 | *insn = total; |
| 405 | |
| 406 | return c; |
| 407 | } |
| 408 | |
| 409 | void cs_free(void *m) |
| 410 | { |
| 411 | free(m); |
| 412 | } |
| 413 | |
| 414 | // return friendly name of regiser in a string |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 415 | const char *cs_reg_name(csh ud, unsigned int reg) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 416 | { |
| 417 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 418 | |
| 419 | if (!handle || handle->reg_name == NULL) { |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 423 | return handle->reg_name(ud, reg); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 424 | } |
| 425 | |
pancake | f0e4eed | 2013-12-11 22:14:42 +0100 | [diff] [blame] | 426 | const char *cs_insn_name(csh ud, unsigned int insn) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 427 | { |
| 428 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 429 | |
| 430 | if (!handle || handle->insn_name == NULL) { |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Nguyen Anh Quynh | a253c7a | 2013-12-09 10:26:18 +0800 | [diff] [blame] | 434 | return handle->insn_name(ud, insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static bool arr_exist(unsigned int *arr, int max, unsigned int id) |
| 438 | { |
| 439 | int i; |
| 440 | |
| 441 | for (i = 0; i < max; i++) { |
| 442 | if (arr[i] == id) |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | bool cs_insn_group(csh handle, cs_insn *insn, unsigned int group_id) |
| 450 | { |
| 451 | if (!handle) |
| 452 | return false; |
| 453 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 454 | return arr_exist(insn->groups, insn->groups_count, group_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | bool cs_reg_read(csh handle, cs_insn *insn, unsigned int reg_id) |
| 458 | { |
| 459 | if (!handle) |
| 460 | return false; |
| 461 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 462 | return arr_exist(insn->regs_read, insn->regs_read_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | bool cs_reg_write(csh handle, cs_insn *insn, unsigned int reg_id) |
| 466 | { |
| 467 | if (!handle) |
| 468 | return false; |
| 469 | |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 470 | return arr_exist(insn->regs_write, insn->regs_write_count, reg_id); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type) |
| 474 | { |
| 475 | if (!ud) |
| 476 | return -1; |
| 477 | |
| 478 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 479 | unsigned int count = 0, i; |
| 480 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 481 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 482 | |
| 483 | switch (handle->arch) { |
| 484 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 485 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 486 | return -1; |
| 487 | case CS_ARCH_ARM: |
| 488 | for (i = 0; i < insn->arm.op_count; i++) |
| 489 | if (insn->arm.operands[i].type == op_type) |
| 490 | count++; |
| 491 | break; |
| 492 | case CS_ARCH_ARM64: |
| 493 | for (i = 0; i < insn->arm64.op_count; i++) |
| 494 | if (insn->arm64.operands[i].type == op_type) |
| 495 | count++; |
| 496 | break; |
| 497 | case CS_ARCH_X86: |
| 498 | for (i = 0; i < insn->x86.op_count; i++) |
| 499 | if (insn->x86.operands[i].type == op_type) |
| 500 | count++; |
| 501 | break; |
| 502 | case CS_ARCH_MIPS: |
| 503 | for (i = 0; i < insn->mips.op_count; i++) |
| 504 | if (insn->mips.operands[i].type == op_type) |
| 505 | count++; |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | return count; |
| 510 | } |
| 511 | |
| 512 | int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type, |
| 513 | unsigned int post) |
| 514 | { |
| 515 | if (!ud) |
| 516 | return -1; |
| 517 | |
| 518 | cs_struct *handle = (cs_struct *)(uintptr_t)ud; |
| 519 | unsigned int count = 0, i; |
| 520 | |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 521 | handle->errnum = CS_ERR_OK; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 522 | |
| 523 | switch (handle->arch) { |
| 524 | default: |
Nguyen Anh Quynh | 3eb9ac9 | 2013-11-27 15:24:47 +0800 | [diff] [blame] | 525 | handle->errnum = CS_ERR_HANDLE; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 526 | return -1; |
| 527 | case CS_ARCH_ARM: |
| 528 | for (i = 0; i < insn->arm.op_count; i++) { |
| 529 | if (insn->arm.operands[i].type == op_type) |
| 530 | count++; |
| 531 | if (count == post) |
| 532 | return i; |
| 533 | } |
| 534 | break; |
| 535 | case CS_ARCH_ARM64: |
| 536 | for (i = 0; i < insn->arm64.op_count; i++) { |
| 537 | if (insn->arm64.operands[i].type == op_type) |
| 538 | count++; |
| 539 | if (count == post) |
| 540 | return i; |
| 541 | } |
| 542 | break; |
| 543 | case CS_ARCH_X86: |
| 544 | for (i = 0; i < insn->x86.op_count; i++) { |
| 545 | if (insn->x86.operands[i].type == op_type) |
| 546 | count++; |
| 547 | if (count == post) |
| 548 | return i; |
| 549 | } |
| 550 | break; |
| 551 | case CS_ARCH_MIPS: |
| 552 | for (i = 0; i < insn->mips.op_count; i++) { |
| 553 | if (insn->mips.operands[i].type == op_type) |
| 554 | count++; |
| 555 | if (count == post) |
| 556 | return i; |
| 557 | } |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | return -1; |
| 562 | } |