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_CODE2 "\x56\x34\x21\x34\xc2\x17\x01\x00" |
| 41 | //#define ARM64_CODE "\x00\x40\x21\x4b" // sub w0, w0, w1, uxtw |
| 42 | //#define ARM64_CODE "\x21\x7c\x02\x9b" // mul x1, x1, x2 |
| 43 | //#define ARM64_CODE "\x20\x74\x0b\xd5" // dc zva, x0 |
| 44 | //#define ARM64_CODE "\xe1\x0b\x40\xb9" // ldr w1, [sp, #0x8] |
| 45 | #define ARM64_CODE "\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9" |
| 46 | |
| 47 | struct platform platforms[] = { |
| 48 | { |
| 49 | .arch = CS_ARCH_X86, |
| 50 | .mode = CS_MODE_16, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 51 | .code = (unsigned char*)X86_CODE16, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 52 | .size = sizeof(X86_CODE16) - 1, |
| 53 | .comment = "X86 16bit (Intel syntax)" |
| 54 | }, |
| 55 | { |
| 56 | .arch = CS_ARCH_X86, |
| 57 | .mode = CS_MODE_32 + CS_MODE_SYNTAX_ATT, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 58 | .code = (unsigned char*)X86_CODE32, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 59 | .size = sizeof(X86_CODE32) - 1, |
| 60 | .comment = "X86 32bit (ATT syntax)" |
| 61 | }, |
| 62 | { |
| 63 | .arch = CS_ARCH_X86, |
| 64 | .mode = CS_MODE_32, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 65 | .code = (unsigned char*)X86_CODE32, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 66 | .size = sizeof(X86_CODE32) - 1, |
| 67 | .comment = "X86 32 (Intel syntax)" |
| 68 | }, |
| 69 | { |
| 70 | .arch = CS_ARCH_X86, |
| 71 | .mode = CS_MODE_64, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 72 | .code = (unsigned char*)X86_CODE64, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 73 | .size = sizeof(X86_CODE64) - 1, |
| 74 | .comment = "X86 64 (Intel syntax)" |
| 75 | }, |
| 76 | { |
| 77 | .arch = CS_ARCH_ARM, |
| 78 | .mode = CS_MODE_ARM, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 79 | .code = (unsigned char*)ARM_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 80 | .size = sizeof(ARM_CODE) - 1, |
| 81 | .comment = "ARM" |
| 82 | }, |
| 83 | { |
| 84 | .arch = CS_ARCH_ARM, |
| 85 | .mode = CS_MODE_THUMB, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 86 | .code = (unsigned char*)THUMB_CODE2, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 87 | .size = sizeof(THUMB_CODE2) - 1, |
| 88 | .comment = "THUMB-2" |
| 89 | }, |
| 90 | { |
| 91 | .arch = CS_ARCH_ARM, |
| 92 | .mode = CS_MODE_ARM, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 93 | .code = (unsigned char*)ARM_CODE2, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 94 | .size = sizeof(ARM_CODE2) - 1, |
| 95 | .comment = "ARM: Cortex-A15 + NEON" |
| 96 | }, |
| 97 | { |
| 98 | .arch = CS_ARCH_ARM, |
| 99 | .mode = CS_MODE_THUMB, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 100 | .code = (unsigned char*)THUMB_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 101 | .size = sizeof(THUMB_CODE) - 1, |
| 102 | .comment = "THUMB" |
| 103 | }, |
| 104 | { |
| 105 | .arch = CS_ARCH_MIPS, |
| 106 | .mode = CS_MODE_32 + CS_MODE_BIG_ENDIAN, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 107 | .code = (unsigned char*)MIPS_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 108 | .size = sizeof(MIPS_CODE) - 1, |
| 109 | .comment = "MIPS-32 (Big-endian)" |
| 110 | }, |
| 111 | { |
| 112 | .arch = CS_ARCH_MIPS, |
| 113 | .mode = CS_MODE_64+ CS_MODE_LITTLE_ENDIAN, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 114 | .code = (unsigned char*)MIPS_CODE2, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 115 | .size = sizeof(MIPS_CODE2) - 1, |
| 116 | .comment = "MIPS-64-EL (Little-endian)" |
| 117 | }, |
| 118 | { |
| 119 | .arch = CS_ARCH_ARM64, |
| 120 | .mode = CS_MODE_ARM, |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 121 | .code = (unsigned char*)ARM64_CODE, |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 122 | .size = sizeof(ARM64_CODE) - 1, |
| 123 | .comment = "ARM-64" |
| 124 | }, |
| 125 | }; |
| 126 | |
| 127 | csh handle; |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 128 | size_t address = 0x1000; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 129 | //cs_insn insn[16]; |
| 130 | cs_insn *insn; |
| 131 | int i; |
| 132 | |
| 133 | for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) { |
| 134 | cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle); |
| 135 | if (err) { |
| 136 | printf("Failed on cs_open() with error returned: %u\n", err); |
| 137 | return; |
| 138 | } |
| 139 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 140 | //size_t count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, insn); |
| 141 | size_t count = cs_disasm_dyn(handle, platforms[i].code, platforms[i].size, address, 0, &insn); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 142 | if (count) { |
| 143 | printf("****************\n"); |
| 144 | printf("Platform: %s\n", platforms[i].comment); |
| 145 | print_string_hex(platforms[i].code, platforms[i].size); |
| 146 | printf("Disasm:\n"); |
| 147 | |
Nguyen Anh Quynh | b42a657 | 2013-11-29 17:40:07 +0800 | [diff] [blame] | 148 | size_t j; |
Nguyen Anh Quynh | 723687e | 2013-11-29 22:36:45 +0800 | [diff] [blame^] | 149 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 150 | for (j = 0; j < count; j++) { |
Nguyen Anh Quynh | 723687e | 2013-11-29 22:36:45 +0800 | [diff] [blame^] | 151 | printf("0x%"PRIx64":\t%s\t\t%s\n", |
| 152 | (uint64_t)insn[j].address, insn[j].mnemonic, insn[j].op_str); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // print out the next offset, after the last insn |
Nguyen Anh Quynh | 723687e | 2013-11-29 22:36:45 +0800 | [diff] [blame^] | 156 | printf("0x%"PRIx64":\n", (uint64_t)insn[j-1].address + insn[j-1].size); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 157 | |
| 158 | // free memory allocated by cs_disasm_dyn() |
| 159 | cs_free(insn); |
| 160 | } else { |
| 161 | printf("****************\n"); |
| 162 | printf("Platform: %s\n", platforms[i].comment); |
| 163 | print_string_hex(platforms[i].code, platforms[i].size); |
| 164 | printf("ERROR: Failed to disasm given code!\n"); |
| 165 | } |
| 166 | |
| 167 | printf("\n"); |
| 168 | |
| 169 | cs_close(handle); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | int main() |
| 174 | { |
| 175 | test(); |
| 176 | |
| 177 | #if 0 |
| 178 | #define offsetof(type, member) (int)(&((type *)0)->member) |
Nguyen Anh Quynh | 723687e | 2013-11-29 22:36:45 +0800 | [diff] [blame^] | 179 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 180 | cs_insn insn; |
| 181 | printf("size: %lu\n", sizeof(insn)); |
| 182 | printf("@id: %u\n", offsetof(cs_insn, id)); |
| 183 | printf("@address: %u\n", offsetof(cs_insn, address)); |
| 184 | printf("@size: %u\n", offsetof(cs_insn, size)); |
| 185 | printf("@mnemonic: %u\n", offsetof(cs_insn, mnemonic)); |
| 186 | printf("@op_str: %u\n", offsetof(cs_insn, op_str)); |
| 187 | printf("@regs_read: %u\n", offsetof(cs_insn, regs_read)); |
| 188 | printf("@regs_write: %u\n", offsetof(cs_insn, regs_write)); |
| 189 | printf("@groups: %u\n", offsetof(cs_insn, groups)); |
| 190 | printf("@arch: %u\n", offsetof(cs_insn, x86)); |
| 191 | #endif |
| 192 | |
| 193 | return 0; |
| 194 | } |