• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Capstone Disassembly Engine */
2 /* By Dang Hoang Vu <danghvu@gmail.com> 2013 */
3 
4 #ifdef CAPSTONE_HAS_X86
5 
6 #include "../../cs_priv.h"
7 #include "../../MCRegisterInfo.h"
8 #include "X86Disassembler.h"
9 #include "X86InstPrinter.h"
10 #include "X86Mapping.h"
11 
init(cs_struct * ud)12 static cs_err init(cs_struct *ud)
13 {
14 	MCRegisterInfo *mri;
15 
16 	// verify if requested mode is valid
17 	if (ud->mode & ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_16))
18 		return CS_ERR_MODE;
19 
20 	mri = cs_mem_malloc(sizeof(*mri));
21 
22 	X86_init(mri);
23 
24 	// by default, we use Intel syntax
25 	ud->printer = X86_Intel_printInst;
26 	ud->syntax = CS_OPT_SYNTAX_INTEL;
27 	ud->printer_info = mri;
28 	ud->disasm = X86_getInstruction;
29 	ud->reg_name = X86_reg_name;
30 	ud->insn_id = X86_get_insn_id;
31 	ud->insn_name = X86_insn_name;
32 	ud->group_name = X86_group_name;
33 	ud->post_printer = NULL;;
34 
35 	if (ud->mode == CS_MODE_64)
36 		ud->regsize_map = regsize_map_64;
37 	else
38 		ud->regsize_map = regsize_map_32;
39 
40 	return CS_ERR_OK;
41 }
42 
option(cs_struct * handle,cs_opt_type type,size_t value)43 static cs_err option(cs_struct *handle, cs_opt_type type, size_t value)
44 {
45 	switch(type) {
46 		default:
47 			break;
48 		case CS_OPT_MODE:
49 			if (value == CS_MODE_64)
50 				handle->regsize_map = regsize_map_64;
51 			else
52 				handle->regsize_map = regsize_map_32;
53 
54 			handle->mode = (cs_mode)value;
55 			break;
56 		case CS_OPT_SYNTAX:
57 			switch(value) {
58 				default:
59 					// wrong syntax value
60 					handle->errnum = CS_ERR_OPTION;
61 					return CS_ERR_OPTION;
62 
63 				case CS_OPT_SYNTAX_DEFAULT:
64 				case CS_OPT_SYNTAX_INTEL:
65 					handle->printer = X86_Intel_printInst;
66 					handle->syntax = CS_OPT_SYNTAX_INTEL;
67 					break;
68 
69 				case CS_OPT_SYNTAX_ATT:
70 #if !defined(CAPSTONE_DIET) && !defined(CAPSTONE_X86_ATT_DISABLE)
71 					handle->printer = X86_ATT_printInst;
72 					handle->syntax = CS_OPT_SYNTAX_ATT;
73 					break;
74 #elif !defined(CAPSTONE_DIET) && defined(CAPSTONE_X86_ATT_DISABLE)
75 					// ATT syntax is unsupported
76 					handle->errnum = CS_ERR_X86_ATT;
77 					return CS_ERR_X86_ATT;
78 #else	// CAPSTONE_DIET
79 					// this is irrelevant in CAPSTONE_DIET mode
80 					handle->errnum = CS_ERR_DIET;
81 					return CS_ERR_DIET;
82 #endif
83 			}
84 			break;
85 	}
86 
87 	return CS_ERR_OK;
88 }
89 
destroy(cs_struct * handle)90 static void destroy(cs_struct *handle)
91 {
92 }
93 
X86_enable(void)94 void X86_enable(void)
95 {
96 	arch_init[CS_ARCH_X86] = init;
97 	arch_option[CS_ARCH_X86] = option;
98 	arch_destroy[CS_ARCH_X86] = destroy;
99 
100 	// support this arch
101 	all_arch |= (1 << CS_ARCH_X86);
102 }
103 
104 #endif
105