-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.c
144 lines (139 loc) · 6.31 KB
/
globals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "include/globals.h"
#include "keystone/keystone.h"
#include <stdio.h>
#include <string.h>
char emit_8_default[] = "emit_8";
char emit_16_default[] = "emit_16";
char emit_32_default[] = "emit_32";
char emit_64_default[] = "emit_64";
const char* list_arch =
"x16: X86 16bit, Intel syntax\n"
"x32: X86 32bit, Intel syntax\n"
"x64: X86 64bit, Intel syntax\n"
"x16att: X86 16bit, AT&T syntax\n"
"x32att: X86 32bit, AT&T syntax\n"
"x64att: X86 64bit, AT&T syntax\n"
"x16nasm: X86 16bit, NASM syntax\n"
"x32nasm: X86 32bit, NASM syntax\n"
"x64nasm: X86 64bit, NASM syntax\n"
"arm: ARM - little endian\n"
"armbe: ARM - big endian\n"
"thumb: Thumb - little endian\n"
"thumbbe: Thumb - big endian\n"
"armv8: ARM V8 - little endian\n"
"armv8be: ARM V8 - big endian\n"
"thumbv8: Thumb V8 - little endian\n"
"thumbv8be: Thumb V8 - big endian\n"
"arm64: AArch64\n"
"hexagon: Hexagon\n"
"mips: Mips - little endian\n"
"mipsbe: Mips - big endian\n"
"mips64: Mips64 - little endian\n"
"mips64be: Mips64 - big endian\n"
"ppc32be: PowerPC32 - big endian\n"
"ppc64: PowerPC64 - little endian\n"
"ppc64be: PowerPC64 - big endian\n"
"sparc: Sparc - little endian\n"
"sparcbe: Sparc - big endian\n"
"sparc64be: Sparc64 - big endian\n"
"systemz: SystemZ (S390x)\n"
"evm: Ethereum Virtual Machine";
char* must_inverse_bytes(const char *arch) {
if (!strcmp(arch, "arm")) return "armbe";
if (!strcmp(arch, "thumb")) return "thumbbe";
if (!strcmp(arch, "armv8")) return "armv8be";
if (!strcmp(arch, "thumbv8")) return "thumbv8be";
if (!strcmp(arch, "mips")) return "mipsbe";
if (!strcmp(arch, "mips64")) return "mips64be";
if (!strcmp(arch, "ppc64")) return "ppc64be";
if (!strcmp(arch, "sparc")) return "sparcbe";
return NULL;
}
bool is_big_endian_architecture(const char *arch) {
if (!strcmp(arch, "armbe")) return 1;
if (!strcmp(arch, "thumbbe")) return 1;
if (!strcmp(arch, "armv8be")) return 1;
if (!strcmp(arch, "thumbv8be")) return 1;
if (!strcmp(arch, "mipsbe")) return 1;
if (!strcmp(arch, "mips64be")) return 1;
if (!strcmp(arch, "ppc32be")) return 1;
if (!strcmp(arch, "ppc64be")) return 1;
if (!strcmp(arch, "sparcbe")) return 1;
if (!strcmp(arch, "sparc64be")) return 1;
if (!strcmp(arch, "systemz")) return 1;
return 0;
}
ks_err ks_open_arch(ks_engine** ks, const char* arch) {
ks_err err;
if (!strcmp(arch, "x16")) {
err = ks_open(KS_ARCH_X86, KS_MODE_16 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_INTEL);
return err;
}
if (!strcmp(arch, "x32")) {
err = ks_open(KS_ARCH_X86, KS_MODE_32 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_INTEL);
return err;
}
if (!strcmp(arch, "x64")) {
err = ks_open(KS_ARCH_X86, KS_MODE_64 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_INTEL);
return err;
}
if (!strcmp(arch, "x16att")) {
err = ks_open(KS_ARCH_X86, KS_MODE_16 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_ATT);
return err;
}
if (!strcmp(arch, "x32att")) {
err = ks_open(KS_ARCH_X86, KS_MODE_32 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_ATT);
return err;
}
if (!strcmp(arch, "x64att")) {
err = ks_open(KS_ARCH_X86, KS_MODE_64 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_ATT);
return err;
}
if (!strcmp(arch, "x16nasm")) {
err = ks_open(KS_ARCH_X86, KS_MODE_16 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_NASM);
return err;
}
if (!strcmp(arch, "x32nasm")) {
err = ks_open(KS_ARCH_X86, KS_MODE_32 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_NASM);
return err;
}
if (!strcmp(arch, "x64nasm")) {
err = ks_open(KS_ARCH_X86, KS_MODE_64 | KS_MODE_LITTLE_ENDIAN, ks);
ks_option(*ks, KS_OPT_SYNTAX, KS_OPT_SYNTAX_NASM);
return err;
}
if (!strcmp(arch, "arm")) return ks_open(KS_ARCH_ARM, KS_MODE_ARM | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "armbe")) return ks_open(KS_ARCH_ARM, KS_MODE_ARM | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "thumb")) return ks_open(KS_ARCH_ARM, KS_MODE_THUMB | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "thumbbe")) return ks_open(KS_ARCH_ARM, KS_MODE_THUMB | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "armv8")) return ks_open(KS_ARCH_ARM, KS_MODE_V8 | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "armv8be")) return ks_open(KS_ARCH_ARM, KS_MODE_V8 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "thumbv8")) return ks_open(KS_ARCH_ARM, KS_MODE_THUMB | KS_MODE_V8 | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "thumbv8be")) return ks_open(KS_ARCH_ARM, KS_MODE_THUMB | KS_MODE_V8 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "arm64")) return ks_open(KS_ARCH_ARM64, KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "hexagon")) return ks_open(KS_ARCH_HEXAGON, KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "mips")) return ks_open(KS_ARCH_MIPS, KS_MODE_MIPS32 | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "mipsbe")) return ks_open(KS_ARCH_MIPS, KS_MODE_MIPS32 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "mips64")) return ks_open(KS_ARCH_MIPS, KS_MODE_MIPS64 | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "mips64be")) return ks_open(KS_ARCH_MIPS, KS_MODE_MIPS64 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "ppc32be")) return ks_open(KS_ARCH_PPC, KS_MODE_PPC32 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "ppc64")) return ks_open(KS_ARCH_PPC, KS_MODE_PPC64 | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "ppc64be")) return ks_open(KS_ARCH_PPC, KS_MODE_PPC64 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "sparc")) return ks_open(KS_ARCH_SPARC, KS_MODE_SPARC32 | KS_MODE_LITTLE_ENDIAN, ks);
if (!strcmp(arch, "sparcbe")) return ks_open(KS_ARCH_SPARC, KS_MODE_SPARC32 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "sparc64be")) return ks_open(KS_ARCH_SPARC, KS_MODE_SPARC64 | KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "systemz")) return ks_open(KS_ARCH_SYSTEMZ, KS_MODE_BIG_ENDIAN, ks);
if (!strcmp(arch, "evm")) return ks_open(KS_ARCH_EVM, KS_MODE_LITTLE_ENDIAN, ks);
printf("Unsupported architecture: %s\n", arch);
printf("List of architectures:\n%s\n", list_arch);
exit(-1);
return (ks_err)-1;
}