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 <inttypes.h> |
| 7 | |
| 8 | #include <capstone.h> |
| 9 | |
| 10 | struct platform { |
| 11 | cs_arch arch; |
| 12 | cs_mode mode; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 13 | unsigned char *code; |
| 14 | size_t size; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 15 | char *comment; |
| 16 | }; |
| 17 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 18 | static void print_string_hex(unsigned char *str, int len) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 19 | { |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 20 | unsigned char *c; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 21 | |
| 22 | printf("Code: "); |
| 23 | for (c = str; c < str + len; c++) { |
| 24 | printf("0x%02x ", *c & 0xff); |
| 25 | } |
| 26 | printf("\n"); |
| 27 | } |
| 28 | |
| 29 | static void test() |
| 30 | { |
| 31 | #define X86_CODE16 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00" |
| 32 | #define X86_CODE32 "\x8d\x4c\x32\x08\x01\xd8\x81\xc6\x34\x12\x00\x00" |
| 33 | #define X86_CODE64 "\x55\x48\x8b\x05\xb8\x13\x00\x00" |
| 34 | //#define ARM_CODE "\x04\xe0\x2d\xe5" |
| 35 | #define ARM_CODE "\xED\xFF\xFF\xEB\x04\xe0\x2d\xe5\x00\x00\x00\x00\xe0\x83\x22\xe5\xf1\x02\x03\x0e\x00\x00\xa0\xe3\x02\x30\xc1\xe7\x00\x00\x53\xe3" |
| 36 | #define ARM_CODE2 "\x10\xf1\x10\xe7\x11\xf2\x31\xe7\xdc\xa1\x2e\xf3\xe8\x4e\x62\xf3" |
| 37 | #define THUMB_CODE "\x70\x47\xeb\x46\x83\xb0\xc9\x68" |
| 38 | #define THUMB_CODE2 "\x4f\xf0\x00\x01\xbd\xe8\x00\x88\xd1\xe8\x00\xf0" |
| 39 | #define MIPS_CODE "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56" |
| 40 | //#define MIPS_CODE "\x21\x38\x00\x01" |
| 41 | //#define MIPS_CODE "\x21\x30\xe6\x70" |
| 42 | #define MIPS_CODE2 "\x56\x34\x21\x34\xc2\x17\x01\x00" |
| 43 | //#define ARM64_CODE "\xe1\x0b\x40\xb9" // ldr w1, [sp, #0x8] |
| 44 | //#define ARM64_CODE "\x00\x40\x21\x4b" // sub w0, w0, w1, uxtw |
| 45 | //#define ARM64_CODE "\x21\x7c\x02\x9b" // mul x1, x1, x2 |
| 46 | //#define ARM64_CODE "\x20\x74\x0b\xd5" // dc zva, x0 |
Nguyen Anh Quynh | ad61c49 | 2013-11-30 16:23:31 +0800 | [diff] [blame] | 47 | //#define ARM64_CODE "\x20\xfc\x02\x9b" // mneg x0, x1, x2 |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 48 | #define ARM64_CODE "\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x10\x20\x21\x1e" |
Nguyen Anh Quynh | f1c2eee | 2013-12-02 12:29:07 +0800 | [diff] [blame] | 49 | //#define THUMB_CODE "\x0a\xbf" // itet eq |
Nguyen Anh Quynh | ec0ed8e | 2013-12-02 13:55:38 +0800 | [diff] [blame] | 50 | //#define X86_CODE32 "\x77\x04" // ja +6 |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 51 | |
| 52 | struct platform platforms[] = { |
| 53 | { |
| 54 | .arch = CS_ARCH_X86, |
| 55 | .mode = CS_MODE_16, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 56 | .code = (unsigned char *)X86_CODE16, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 57 | .size = sizeof(X86_CODE32) - 1, |
| 58 | .comment = "X86 16bit (Intel syntax)" |
| 59 | }, |
| 60 | { |
| 61 | .arch = CS_ARCH_X86, |
| 62 | .mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 63 | .code = (unsigned char *)X86_CODE32, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 64 | .size = sizeof(X86_CODE32) - 1, |
| 65 | .comment = "X86 32bit (ATT syntax)" |
| 66 | }, |
| 67 | { |
| 68 | .arch = CS_ARCH_X86, |
| 69 | .mode = CS_MODE_32, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 70 | .code = (unsigned char *)X86_CODE32, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 71 | .size = sizeof(X86_CODE32) - 1, |
| 72 | .comment = "X86 32 (Intel syntax)" |
| 73 | }, |
| 74 | { |
| 75 | .arch = CS_ARCH_X86, |
| 76 | .mode = CS_MODE_64, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 77 | .code = (unsigned char *)X86_CODE64, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 78 | .size = sizeof(X86_CODE64) - 1, |
| 79 | .comment = "X86 64 (Intel syntax)" |
| 80 | }, |
| 81 | { |
| 82 | .arch = CS_ARCH_ARM, |
| 83 | .mode = CS_MODE_ARM, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 84 | .code = (unsigned char *)ARM_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 85 | .size = sizeof(ARM_CODE) - 1, |
| 86 | .comment = "ARM" |
| 87 | }, |
| 88 | { |
| 89 | .arch = CS_ARCH_ARM, |
| 90 | .mode = CS_MODE_THUMB, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 91 | .code = (unsigned char *)THUMB_CODE2, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 92 | .size = sizeof(THUMB_CODE2) - 1, |
| 93 | .comment = "THUMB-2" |
| 94 | }, |
| 95 | { |
| 96 | .arch = CS_ARCH_ARM, |
| 97 | .mode = CS_MODE_ARM, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 98 | .code = (unsigned char *)ARM_CODE2, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 99 | .size = sizeof(ARM_CODE2) - 1, |
| 100 | .comment = "ARM: Cortex-A15 + NEON" |
| 101 | }, |
| 102 | { |
| 103 | .arch = CS_ARCH_ARM, |
| 104 | .mode = CS_MODE_THUMB, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 105 | .code = (unsigned char *)THUMB_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 106 | .size = sizeof(THUMB_CODE) - 1, |
| 107 | .comment = "THUMB" |
| 108 | }, |
| 109 | { |
| 110 | .arch = CS_ARCH_MIPS, |
| 111 | .mode = CS_MODE_32 + CS_MODE_BIG_ENDIAN, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 112 | .code = (unsigned char *)MIPS_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 113 | .size = sizeof(MIPS_CODE) - 1, |
| 114 | .comment = "MIPS-32 (Big-endian)" |
| 115 | }, |
| 116 | { |
| 117 | .arch = CS_ARCH_MIPS, |
| 118 | .mode = CS_MODE_64+ CS_MODE_LITTLE_ENDIAN, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 119 | .code = (unsigned char *)MIPS_CODE2, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 120 | .size = sizeof(MIPS_CODE2) - 1, |
| 121 | .comment = "MIPS-64-EL (Little-endian)" |
| 122 | }, |
| 123 | { |
| 124 | .arch = CS_ARCH_ARM64, |
| 125 | .mode = CS_MODE_ARM, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 126 | .code = (unsigned char *)ARM64_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 127 | .size = sizeof(ARM64_CODE) - 1, |
| 128 | .comment = "ARM-64" |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | csh handle; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 133 | size_t address = 0x1000; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 134 | //cs_insn all_insn[16]; |
| 135 | cs_insn *all_insn; |
| 136 | int i; |
| 137 | |
| 138 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 139 | if (cs_open(platforms[i].arch, platforms[i].mode, &handle)) |
| 140 | return; |
| 141 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 142 | //size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, all_insn); |
| 143 | size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &all_insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 144 | if (count) { |
| 145 | printf("****************\n"); |
| 146 | printf("Platform: %s\n", platforms[i].comment); |
| 147 | print_string_hex(platforms[i].code, platforms[i].size); |
| 148 | printf("Disasm:\n"); |
| 149 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 150 | size_t j; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 151 | int n; |
| 152 | for (j = 0; j < count; j++) { |
| 153 | cs_insn *i = &(all_insn[j]); |
Nguyen Anh Quynh | 723687e | 2013-11-29 22:36:45 +0800 | [diff] [blame] | 154 | printf("0x%"PRIx64":\t%s\t\t%s // insn-ID: %u, insn-mnem: %s\n", |
| 155 | (uint64_t)i->address, i->mnemonic, i->op_str, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 156 | i->id, cs_insn_name(handle, i->id)); |
| 157 | |
| 158 | // print implicit registers used by this instruction |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 159 | if (i->regs_read_count > 0) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 160 | printf("\tImplicit registers read: "); |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 161 | for (n = 0; n < i->regs_read_count; n++) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 162 | printf("%s ", cs_reg_name(handle, i->regs_read[n])); |
| 163 | } |
| 164 | printf("\n"); |
| 165 | } |
| 166 | |
| 167 | // print implicit registers modified by this instruction |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 168 | if (i->regs_write_count > 0) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 169 | printf("\tImplicit registers modified: "); |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 170 | for (n = 0; n < i->regs_write_count; n++) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 171 | printf("%s ", cs_reg_name(handle, i->regs_write[n])); |
| 172 | } |
| 173 | printf("\n"); |
| 174 | } |
| 175 | |
| 176 | // print the groups this instruction belong to |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 177 | if (i->groups_count > 0) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 178 | printf("\tThis instruction belongs to groups: "); |
Nguyen Anh Quynh | f35e2ad | 2013-12-03 11:10:26 +0800 | [diff] [blame] | 179 | for (n = 0; n < i->groups_count; n++) { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 180 | printf("%u ", i->groups[n]); |
| 181 | } |
| 182 | printf("\n"); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // print out the next offset, after the last insn |
Nguyen Anh Quynh | 723687e | 2013-11-29 22:36:45 +0800 | [diff] [blame] | 187 | printf("0x%"PRIx64":\n", (uint64_t)all_insn[j-1].address + all_insn[j-1].size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 188 | |
| 189 | // free memory allocated by cs_disasm_dyn() |
| 190 | cs_free(all_insn); |
| 191 | } else { |
| 192 | printf("****************\n"); |
| 193 | printf("Platform: %s\n", platforms[i].comment); |
| 194 | print_string_hex(platforms[i].code, platforms[i].size); |
| 195 | printf("ERROR: Failed to disasm given code!\n"); |
| 196 | } |
| 197 | |
| 198 | printf("\n"); |
| 199 | |
| 200 | cs_close(handle); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | int main() |
| 205 | { |
| 206 | test(); |
| 207 | |
| 208 | return 0; |
| 209 | } |