| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | #include <capstone/capstone.h> |
| 5 | #include "cstool.h" |
| 6 | |
| 7 | void print_insn_detail_arm(csh handle, cs_insn *ins) |
| 8 | { |
| 9 | cs_arm *arm; |
| 10 | int i; |
| 11 | cs_regs regs_read, regs_write; |
| 12 | uint8_t regs_read_count, regs_write_count; |
| 13 | |
| 14 | // detail can be NULL on "data" instruction if SKIPDATA option is turned ON |
| 15 | if (ins->detail == NULL) |
| 16 | return; |
| 17 | |
| 18 | arm = &(ins->detail->arm); |
| 19 | |
| 20 | if (arm->op_count) |
| 21 | printf("\top_count: %u\n", arm->op_count); |
| 22 | |
| 23 | for (i = 0; i < arm->op_count; i++) { |
| 24 | cs_arm_op *op = &(arm->operands[i]); |
| 25 | switch((int)op->type) { |
| 26 | default: |
| 27 | break; |
| 28 | case ARM_OP_REG: |
| 29 | printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg)); |
| 30 | break; |
| 31 | case ARM_OP_IMM: { |
| 32 | bool neg_imm = op->imm < 0; |
| 33 | if (neg_imm) |
| 34 | printf("\t\toperands[%u].type: IMM = -0x%" PRIx32 "\n", i, -(op->imm)); |
| 35 | else |
| 36 | printf("\t\toperands[%u].type: IMM = 0x%" PRIx32 "\n", i, op->imm); |
| 37 | break; |
| 38 | } |
| 39 | case ARM_OP_FP: |
| 40 | #if defined(_KERNEL_MODE) |
| 41 | // Issue #681: Windows kernel does not support formatting float point |
| 42 | printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i); |
| 43 | #else |
| 44 | printf("\t\toperands[%u].type: FP = %f\n", i, op->fp); |
| 45 | #endif |
| 46 | break; |
| 47 | case ARM_OP_MEM: |
| 48 | printf("\t\toperands[%u].type: MEM\n", i); |
| 49 | if (op->mem.base != ARM_REG_INVALID) |
| 50 | printf("\t\t\toperands[%u].mem.base: REG = %s\n", |
| 51 | i, cs_reg_name(handle, op->mem.base)); |
| 52 | if (op->mem.index != ARM_REG_INVALID) |
| 53 | printf("\t\t\toperands[%u].mem.index: REG = %s\n", |
| 54 | i, cs_reg_name(handle, op->mem.index)); |
| 55 | if (op->mem.scale != 1) |
| 56 | printf("\t\t\toperands[%u].mem.scale: %d\n", i, op->mem.scale); |
| 57 | if (op->mem.disp != 0) |
| 58 | printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp); |
| 59 | if (op->mem.lshift != 0) |
| 60 | printf("\t\t\toperands[%u].mem.lshift: 0x%x\n", i, op->mem.lshift); |
| 61 | |
| 62 | break; |
| 63 | case ARM_OP_PIMM: |
| 64 | printf("\t\toperands[%u].type: P-IMM = %" PRIu32 "\n", i, op->imm); |
| 65 | break; |
| 66 | case ARM_OP_CIMM: |
| 67 | printf("\t\toperands[%u].type: C-IMM = %" PRIu32 "\n", i, op->imm); |
| 68 | break; |
| 69 | case ARM_OP_SETEND: |
| 70 | printf("\t\toperands[%u].type: SETEND = %s\n", i, op->setend == ARM_SETEND_BE? "be" : "le"); |
| 71 | break; |
| 72 | case ARM_OP_SYSREG: |
| 73 | printf("\t\toperands[%u].type: SYSREG = %u\n", i, op->reg); |
| 74 | break; |
| 75 | } |
| 76 | |
| 77 | if (op->neon_lane != -1) { |
| 78 | printf("\t\toperands[%u].neon_lane = %u\n", i, op->neon_lane); |
| 79 | } |
| 80 | |
| 81 | switch(op->access) { |
| 82 | default: |
| 83 | break; |
| 84 | case CS_AC_READ: |
| 85 | printf("\t\toperands[%u].access: READ\n", i); |
| 86 | break; |
| 87 | case CS_AC_WRITE: |
| 88 | printf("\t\toperands[%u].access: WRITE\n", i); |
| 89 | break; |
| 90 | case CS_AC_READ | CS_AC_WRITE: |
| 91 | printf("\t\toperands[%u].access: READ | WRITE\n", i); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | if (op->shift.type != ARM_SFT_INVALID && op->shift.value) { |
| 96 | if (op->shift.type < ARM_SFT_ASR_REG) |
| 97 | // shift with constant value |
| 98 | printf("\t\t\tShift: %u = %u\n", op->shift.type, op->shift.value); |
| 99 | else |
| 100 | // shift with register |
| 101 | printf("\t\t\tShift: %u = %s\n", op->shift.type, |
| 102 | cs_reg_name(handle, op->shift.value)); |
| 103 | } |
| 104 | |
| 105 | if (op->vector_index != -1) { |
| 106 | printf("\t\toperands[%u].vector_index = %u\n", i, op->vector_index); |
| 107 | } |
| 108 | |
| 109 | if (op->subtracted) |
| 110 | printf("\t\tSubtracted: True\n"); |
| 111 | } |
| 112 | |
| 113 | if (arm->cc != ARM_CC_AL && arm->cc != ARM_CC_INVALID) |
| 114 | printf("\tCode condition: %u\n", arm->cc); |
| 115 | |
| 116 | if (arm->update_flags) |
| 117 | printf("\tUpdate-flags: True\n"); |
| 118 | |
| 119 | if (arm->writeback) |
| 120 | printf("\tWrite-back: True\n"); |
| 121 | |
| 122 | if (arm->cps_mode) |
| 123 | printf("\tCPSI-mode: %u\n", arm->cps_mode); |
| 124 | |
| 125 | if (arm->cps_flag) |
| 126 | printf("\tCPSI-flag: %u\n", arm->cps_flag); |
| 127 | |
| 128 | if (arm->vector_data) |
| 129 | printf("\tVector-data: %u\n", arm->vector_data); |
| 130 | |
| 131 | if (arm->vector_size) |
| 132 | printf("\tVector-size: %u\n", arm->vector_size); |
| 133 | |
| 134 | if (arm->usermode) |
| 135 | printf("\tUser-mode: True\n"); |
| 136 | |
| 137 | if (arm->mem_barrier) |
| 138 | printf("\tMemory-barrier: %u\n", arm->mem_barrier); |
| 139 | |
| 140 | // Print out all registers accessed by this instruction (either implicit or explicit) |
| 141 | if (!cs_regs_access(handle, ins, |
| 142 | regs_read, ®s_read_count, |
| 143 | regs_write, ®s_write_count)) { |
| 144 | if (regs_read_count) { |
| 145 | printf("\tRegisters read:"); |
| 146 | for(i = 0; i < regs_read_count; i++) { |
| 147 | printf(" %s", cs_reg_name(handle, regs_read[i])); |
| 148 | } |
| 149 | printf("\n"); |
| 150 | } |
| 151 | |
| 152 | if (regs_write_count) { |
| 153 | printf("\tRegisters modified:"); |
| 154 | for(i = 0; i < regs_write_count; i++) { |
| 155 | printf(" %s", cs_reg_name(handle, regs_write[i])); |
| 156 | } |
| 157 | printf("\n"); |
| 158 | } |
| 159 | } |
| 160 | } |