HyperDbg Debugger
Loading...
Searching...
No Matches
keystone.h
Go to the documentation of this file.
1/* Keystone Assembler Engine (www.keystone-engine.org) */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2016 */
3
4#ifndef KEYSTONE_ENGINE_H
5#define KEYSTONE_ENGINE_H
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11#include <stdint.h>
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdbool.h>
15
16#ifdef _MSC_VER // MSVC compiler
17# pragma warning(disable : 4201)
18# pragma warning(disable : 4100)
19# ifndef KEYSTONE_STATIC
20# define KEYSTONE_EXPORT __declspec(dllexport)
21# else
22# define KEYSTONE_EXPORT
23# endif
24#else
25# ifdef __GNUC__
26# include <stdbool.h>
27# ifndef KEYSTONE_STATIC
28# define KEYSTONE_EXPORT __attribute__((visibility("default")))
29# else
30# define KEYSTONE_EXPORT
31# endif
32# else
33# define KEYSTONE_EXPORT
34# endif
35#endif
36
37struct ks_struct;
38typedef struct ks_struct ks_engine;
39
40// Keystone API version
41#define KS_API_MAJOR 0
42#define KS_API_MINOR 9
43
44// Package version
45#define KS_VERSION_MAJOR KS_API_MAJOR
46#define KS_VERSION_MINOR KS_API_MINOR
47#define KS_VERSION_EXTRA 2
48
49/*
50 Macro to create combined version which can be compared to
51 result of ks_version() API.
52*/
53#define KS_MAKE_VERSION(major, minor) ((major << 8) + minor)
54
55// Architecture type
56typedef enum ks_arch
57{
58 KS_ARCH_ARM = 1, // ARM architecture (including Thumb, Thumb-2)
59 KS_ARCH_ARM64, // ARM-64, also called AArch64
60 KS_ARCH_MIPS, // Mips architecture
61 KS_ARCH_X86, // X86 architecture (including x86 & x86-64)
62 KS_ARCH_PPC, // PowerPC architecture (currently unsupported)
63 KS_ARCH_SPARC, // Sparc architecture
64 KS_ARCH_SYSTEMZ, // SystemZ architecture (S390X)
65 KS_ARCH_HEXAGON, // Hexagon architecture
66 KS_ARCH_EVM, // Ethereum Virtual Machine architecture
67 KS_ARCH_RISCV, // RISC-V architecture
70
71// Mode type
72typedef enum ks_mode
73{
74 KS_MODE_LITTLE_ENDIAN = 0, // little-endian mode (default mode)
75 KS_MODE_BIG_ENDIAN = 1 << 30, // big-endian mode
76 // arm / arm64
77 KS_MODE_ARM = 1 << 0, // ARM mode
78 KS_MODE_THUMB = 1 << 4, // THUMB mode (including Thumb-2)
79 KS_MODE_V8 = 1 << 6, // ARMv8 A32 encodings for ARM
80 // mips
81 KS_MODE_MICRO = 1 << 4, // MicroMips mode
82 KS_MODE_MIPS3 = 1 << 5, // Mips III ISA
83 KS_MODE_MIPS32R6 = 1 << 6, // Mips32r6 ISA
84 KS_MODE_MIPS32 = 1 << 2, // Mips32 ISA
85 KS_MODE_MIPS64 = 1 << 3, // Mips64 ISA
86 // x86 / x64
87 KS_MODE_16 = 1 << 1, // 16-bit mode
88 KS_MODE_32 = 1 << 2, // 32-bit mode
89 KS_MODE_64 = 1 << 3, // 64-bit mode
90 // ppc
91 KS_MODE_PPC32 = 1 << 2, // 32-bit mode
92 KS_MODE_PPC64 = 1 << 3, // 64-bit mode
93 KS_MODE_QPX = 1 << 4, // Quad Processing eXtensions mode
94 // riscv
95 KS_MODE_RISCV32 = 1 << 2, // 32-bit mode
96 KS_MODE_RISCV64 = 1 << 3, // 64-bit mode
97 // sparc
98 KS_MODE_SPARC32 = 1 << 2, // 32-bit mode
99 KS_MODE_SPARC64 = 1 << 3, // 64-bit mode
100 KS_MODE_V9 = 1 << 4, // SparcV9 mode
102
103// All generic errors related to input assembly >= KS_ERR_ASM
104#define KS_ERR_ASM 128
105
106// All architecture-specific errors related to input assembly >= KS_ERR_ASM_ARCH
107#define KS_ERR_ASM_ARCH 512
108
109// All type of errors encountered by Keystone API.
110typedef enum ks_err
111{
112 KS_ERR_OK = 0, // No error: everything was fine
113 KS_ERR_NOMEM, // Out-Of-Memory error: ks_open(), ks_emulate()
114 KS_ERR_ARCH, // Unsupported architecture: ks_open()
115 KS_ERR_HANDLE, // Invalid handle
116 KS_ERR_MODE, // Invalid/unsupported mode: ks_open()
117 KS_ERR_VERSION, // Unsupported version (bindings)
118 KS_ERR_OPT_INVALID, // Unsupported option
119
120 // generic input assembly errors - parser specific
121 KS_ERR_ASM_EXPR_TOKEN = KS_ERR_ASM, // unknown token in expression
122 KS_ERR_ASM_DIRECTIVE_VALUE_RANGE, // literal value out of range for directive
123 KS_ERR_ASM_DIRECTIVE_ID, // expected identifier in directive
124 KS_ERR_ASM_DIRECTIVE_TOKEN, // unexpected token in directive
125 KS_ERR_ASM_DIRECTIVE_STR, // expected string in directive
126 KS_ERR_ASM_DIRECTIVE_COMMA, // expected comma in directive
127 KS_ERR_ASM_DIRECTIVE_RELOC_NAME, // expected relocation name in directive
128 KS_ERR_ASM_DIRECTIVE_RELOC_TOKEN, // unexpected token in .reloc directive
129 KS_ERR_ASM_DIRECTIVE_FPOINT, // invalid floating point in directive
130 KS_ERR_ASM_DIRECTIVE_UNKNOWN, // unknown directive
131 KS_ERR_ASM_DIRECTIVE_EQU, // invalid equal directive
132 KS_ERR_ASM_DIRECTIVE_INVALID, // (generic) invalid directive
133 KS_ERR_ASM_VARIANT_INVALID, // invalid variant
134 KS_ERR_ASM_EXPR_BRACKET, // brackets expression not supported on this target
135 KS_ERR_ASM_SYMBOL_MODIFIER, // unexpected symbol modifier following '@'
136 KS_ERR_ASM_SYMBOL_REDEFINED, // invalid symbol redefinition
137 KS_ERR_ASM_SYMBOL_MISSING, // cannot find a symbol
138 KS_ERR_ASM_RPAREN, // expected ')' in parentheses expression
139 KS_ERR_ASM_STAT_TOKEN, // unexpected token at start of statement
140 KS_ERR_ASM_UNSUPPORTED, // unsupported token yet
141 KS_ERR_ASM_MACRO_TOKEN, // unexpected token in macro instantiation
142 KS_ERR_ASM_MACRO_PAREN, // unbalanced parentheses in macro argument
143 KS_ERR_ASM_MACRO_EQU, // expected '=' after formal parameter identifier
144 KS_ERR_ASM_MACRO_ARGS, // too many positional arguments
145 KS_ERR_ASM_MACRO_LEVELS_EXCEED, // macros cannot be nested more than 20 levels deep
146 KS_ERR_ASM_MACRO_STR, // invalid macro string
147 KS_ERR_ASM_MACRO_INVALID, // invalid macro (generic error)
148 KS_ERR_ASM_ESC_BACKSLASH, // unexpected backslash at end of escaped string
149 KS_ERR_ASM_ESC_OCTAL, // invalid octal escape sequence (out of range)
150 KS_ERR_ASM_ESC_SEQUENCE, // invalid escape sequence (unrecognized character)
151 KS_ERR_ASM_ESC_STR, // broken escape string
152 KS_ERR_ASM_TOKEN_INVALID, // invalid token
153 KS_ERR_ASM_INSN_UNSUPPORTED, // this instruction is unsupported in this mode
154 KS_ERR_ASM_FIXUP_INVALID, // invalid fixup
155 KS_ERR_ASM_LABEL_INVALID, // invalid label
156 KS_ERR_ASM_FRAGMENT_INVALID, // invalid fragment
157
158 // generic input assembly errors - architecture specific
163
164// Resolver callback to provide value for a missing symbol in @symbol.
165// To handle a symbol, the resolver must put value of the symbol in @value,
166// then returns True.
167// If we do not resolve a missing symbol, this function must return False.
168// In that case, ks_asm() would eventually return with error KS_ERR_ASM_SYMBOL_MISSING.
169
170// To register the resolver, pass its function address to ks_option(), using
171// option KS_OPT_SYM_RESOLVER. For example, see samples/sample.c.
172typedef bool (*ks_sym_resolver)(const char * symbol, uint64_t * value);
173
174// Runtime option for the Keystone engine
175typedef enum ks_opt_type
176{
177 KS_OPT_SYNTAX = 1, // Choose syntax for input assembly
178 KS_OPT_SYM_RESOLVER, // Set symbol resolver callback
180
181// Runtime option value (associated with ks_opt_type above)
182typedef enum ks_opt_value
183{
184 KS_OPT_SYNTAX_INTEL = 1 << 0, // X86 Intel syntax - default on X86 (KS_OPT_SYNTAX).
185 KS_OPT_SYNTAX_ATT = 1 << 1, // X86 ATT asm syntax (KS_OPT_SYNTAX).
186 KS_OPT_SYNTAX_NASM = 1 << 2, // X86 Nasm syntax (KS_OPT_SYNTAX).
187 KS_OPT_SYNTAX_MASM = 1 << 3, // X86 Masm syntax (KS_OPT_SYNTAX) - unsupported yet.
188 KS_OPT_SYNTAX_GAS = 1 << 4, // X86 GNU GAS syntax (KS_OPT_SYNTAX).
189 KS_OPT_SYNTAX_RADIX16 = 1 << 5, // All immediates are in hex format (i.e 12 is 0x12)
191
192#include "arm64.h"
193#include "arm.h"
194#include "evm.h"
195#include "hexagon.h"
196#include "mips.h"
197#include "ppc.h"
198#include "riscv.h"
199#include "sparc.h"
200#include "systemz.h"
201#include "x86.h"
202
203/*
204 Return combined API version & major and minor version numbers.
205
206 @major: major number of API version
207 @minor: minor number of API version
208
209 @return hexical number as (major << 8 | minor), which encodes both
210 major & minor versions.
211 NOTE: This returned value can be compared with version number made
212 with macro KS_MAKE_VERSION
213
214 For example, second API version would return 1 in @major, and 1 in @minor
215 The return value would be 0x0101
216
217 NOTE: if you only care about returned value, but not major and minor values,
218 set both @major & @minor arguments to NULL.
219*/
221unsigned int
222ks_version(unsigned int * major, unsigned int * minor);
223
224/*
225 Determine if the given architecture is supported by this library.
226
227 @arch: architecture type (KS_ARCH_*)
228
229 @return True if this library supports the given arch.
230*/
232bool
234
235/*
236 Create new instance of Keystone engine.
237
238 @arch: architecture type (KS_ARCH_*)
239 @mode: hardware mode. This is combined of KS_MODE_*
240 @ks: pointer to ks_engine, which will be updated at return time
241
242 @return KS_ERR_OK on success, or other value on failure (refer to ks_err enum
243 for detailed error).
244*/
246ks_err
247ks_open(ks_arch arch, int mode, ks_engine ** ks);
248
249/*
250 Close KS instance: MUST do to release the handle when it is not used anymore.
251 NOTE: this must be called only when there is no longer usage of Keystone.
252 The reason is the this API releases some cached memory, thus access to any
253 Keystone API after ks_close() might crash your application.
254 After this, @ks is invalid, and nolonger usable.
255
256 @ks: pointer to a handle returned by ks_open()
257
258 @return KS_ERR_OK on success, or other value on failure (refer to ks_err enum
259 for detailed error).
260*/
262ks_err
264
265/*
266 Report the last error number when some API function fail.
267 Like glibc's errno, ks_errno might not retain its old error once accessed.
268
269 @ks: handle returned by ks_open()
270
271 @return: error code of ks_err enum type (KS_ERR_*, see above)
272*/
274ks_err
276
277/*
278 Return a string describing given error code.
279
280 @code: error code (see KS_ERR_* above)
281
282 @return: returns a pointer to a string that describes the error code
283 passed in the argument @code
284 */
286const char *
288
289/*
290 Set option for Keystone engine at runtime
291
292 @ks: handle returned by ks_open()
293 @type: type of option to be set. See ks_opt_type
294 @value: option value corresponding with @type
295
296 @return: KS_ERR_OK on success, or other value on failure.
297 Refer to ks_err enum for detailed error.
298*/
300ks_err
301ks_option(ks_engine * ks, ks_opt_type type, size_t value);
302
303/*
304 Assemble a string given its the buffer, size, start address and number
305 of instructions to be decoded.
306 This API dynamically allocate memory to contain assembled instruction.
307 Resulted array of bytes containing the machine code is put into @*encoding
308
309 NOTE 1: this API will automatically determine memory needed to contain
310 output bytes in *encoding.
311
312 NOTE 2: caller must free the allocated memory itself to avoid memory leaking.
313
314 @ks: handle returned by ks_open()
315 @str: NULL-terminated assembly string. Use ; or \n to separate statements.
316 @address: address of the first assembly instruction, or 0 to ignore.
317 @encoding: array of bytes containing encoding of input assembly string.
318 NOTE: *encoding will be allocated by this function, and should be freed
319 with ks_free() function.
320 @encoding_size: size of *encoding
321 @stat_count: number of statements successfully processed
322
323 @return: 0 on success, or -1 on failure.
324
325 On failure, call ks_errno() for error code.
326*/
328int
330 const char * string,
331 uint64_t address,
332 unsigned char ** encoding,
333 size_t * encoding_size,
334 size_t * stat_count);
335
336/*
337 Free memory allocated by ks_asm()
338
339 @p: memory allocated in @encoding argument of ks_asm()
340*/
342void
343ks_free(unsigned char * p);
344
345#ifdef __cplusplus
346}
347#endif
348
349#endif
ks_opt_value
Definition keystone.h:183
@ KS_OPT_SYNTAX_ATT
Definition keystone.h:185
@ KS_OPT_SYNTAX_NASM
Definition keystone.h:186
@ KS_OPT_SYNTAX_MASM
Definition keystone.h:187
@ KS_OPT_SYNTAX_INTEL
Definition keystone.h:184
@ KS_OPT_SYNTAX_RADIX16
Definition keystone.h:189
@ KS_OPT_SYNTAX_GAS
Definition keystone.h:188
KEYSTONE_EXPORT ks_err ks_errno(ks_engine *ks)
KEYSTONE_EXPORT unsigned int ks_version(unsigned int *major, unsigned int *minor)
#define KEYSTONE_EXPORT
Definition keystone.h:33
ks_err
Definition keystone.h:111
@ KS_ERR_ASM_VARIANT_INVALID
Definition keystone.h:133
@ KS_ERR_ASM_FIXUP_INVALID
Definition keystone.h:154
@ KS_ERR_VERSION
Definition keystone.h:117
@ KS_ERR_ASM_MACRO_LEVELS_EXCEED
Definition keystone.h:145
@ KS_ERR_ASM_DIRECTIVE_UNKNOWN
Definition keystone.h:130
@ KS_ERR_ASM_MACRO_STR
Definition keystone.h:146
@ KS_ERR_ASM_ESC_OCTAL
Definition keystone.h:149
@ KS_ERR_ASM_MACRO_TOKEN
Definition keystone.h:141
@ KS_ERR_ASM_DIRECTIVE_RELOC_TOKEN
Definition keystone.h:128
@ KS_ERR_ASM_UNSUPPORTED
Definition keystone.h:140
@ KS_ERR_ASM_DIRECTIVE_FPOINT
Definition keystone.h:129
@ KS_ERR_ASM_MACRO_EQU
Definition keystone.h:143
@ KS_ERR_ASM_DIRECTIVE_STR
Definition keystone.h:125
@ KS_ERR_ASM_MACRO_PAREN
Definition keystone.h:142
@ KS_ERR_ASM_INVALIDOPERAND
Definition keystone.h:159
@ KS_ERR_ASM_ESC_SEQUENCE
Definition keystone.h:150
@ KS_ERR_ASM_MISSINGFEATURE
Definition keystone.h:160
@ KS_ERR_ASM_DIRECTIVE_TOKEN
Definition keystone.h:124
@ KS_ERR_ASM_MNEMONICFAIL
Definition keystone.h:161
@ KS_ERR_ASM_MACRO_INVALID
Definition keystone.h:147
@ KS_ERR_ASM_ESC_STR
Definition keystone.h:151
@ KS_ERR_ASM_DIRECTIVE_ID
Definition keystone.h:123
@ KS_ERR_ASM_TOKEN_INVALID
Definition keystone.h:152
@ KS_ERR_ASM_SYMBOL_REDEFINED
Definition keystone.h:136
@ KS_ERR_NOMEM
Definition keystone.h:113
@ KS_ERR_ASM_SYMBOL_MODIFIER
Definition keystone.h:135
@ KS_ERR_ARCH
Definition keystone.h:114
@ KS_ERR_MODE
Definition keystone.h:116
@ KS_ERR_ASM_EXPR_BRACKET
Definition keystone.h:134
@ KS_ERR_ASM_INSN_UNSUPPORTED
Definition keystone.h:153
@ KS_ERR_OK
Definition keystone.h:112
@ KS_ERR_ASM_DIRECTIVE_COMMA
Definition keystone.h:126
@ KS_ERR_ASM_MACRO_ARGS
Definition keystone.h:144
@ KS_ERR_ASM_LABEL_INVALID
Definition keystone.h:155
@ KS_ERR_HANDLE
Definition keystone.h:115
@ KS_ERR_ASM_FRAGMENT_INVALID
Definition keystone.h:156
@ KS_ERR_ASM_DIRECTIVE_INVALID
Definition keystone.h:132
@ KS_ERR_ASM_EXPR_TOKEN
Definition keystone.h:121
@ KS_ERR_ASM_DIRECTIVE_VALUE_RANGE
Definition keystone.h:122
@ KS_ERR_ASM_RPAREN
Definition keystone.h:138
@ KS_ERR_ASM_DIRECTIVE_EQU
Definition keystone.h:131
@ KS_ERR_OPT_INVALID
Definition keystone.h:118
@ KS_ERR_ASM_SYMBOL_MISSING
Definition keystone.h:137
@ KS_ERR_ASM_DIRECTIVE_RELOC_NAME
Definition keystone.h:127
@ KS_ERR_ASM_STAT_TOKEN
Definition keystone.h:139
@ KS_ERR_ASM_ESC_BACKSLASH
Definition keystone.h:148
KEYSTONE_EXPORT void ks_free(unsigned char *p)
KEYSTONE_EXPORT ks_err ks_close(ks_engine *ks)
#define KS_ERR_ASM_ARCH
Definition keystone.h:107
KEYSTONE_EXPORT ks_err ks_option(ks_engine *ks, ks_opt_type type, size_t value)
#define KS_ERR_ASM
Definition keystone.h:104
KEYSTONE_EXPORT const char * ks_strerror(ks_err code)
ks_mode
Definition keystone.h:73
@ KS_MODE_LITTLE_ENDIAN
Definition keystone.h:74
@ KS_MODE_RISCV64
Definition keystone.h:96
@ KS_MODE_THUMB
Definition keystone.h:78
@ KS_MODE_PPC32
Definition keystone.h:91
@ KS_MODE_MIPS3
Definition keystone.h:82
@ KS_MODE_16
Definition keystone.h:87
@ KS_MODE_QPX
Definition keystone.h:93
@ KS_MODE_MIPS64
Definition keystone.h:85
@ KS_MODE_RISCV32
Definition keystone.h:95
@ KS_MODE_MIPS32R6
Definition keystone.h:83
@ KS_MODE_PPC64
Definition keystone.h:92
@ KS_MODE_SPARC64
Definition keystone.h:99
@ KS_MODE_ARM
Definition keystone.h:77
@ KS_MODE_MICRO
Definition keystone.h:81
@ KS_MODE_V9
Definition keystone.h:100
@ KS_MODE_64
Definition keystone.h:89
@ KS_MODE_V8
Definition keystone.h:79
@ KS_MODE_SPARC32
Definition keystone.h:98
@ KS_MODE_BIG_ENDIAN
Definition keystone.h:75
@ KS_MODE_MIPS32
Definition keystone.h:84
@ KS_MODE_32
Definition keystone.h:88
KEYSTONE_EXPORT int ks_asm(ks_engine *ks, const char *string, uint64_t address, unsigned char **encoding, size_t *encoding_size, size_t *stat_count)
ks_arch
Definition keystone.h:57
@ KS_ARCH_RISCV
Definition keystone.h:67
@ KS_ARCH_MIPS
Definition keystone.h:60
@ KS_ARCH_ARM
Definition keystone.h:58
@ KS_ARCH_ARM64
Definition keystone.h:59
@ KS_ARCH_SYSTEMZ
Definition keystone.h:64
@ KS_ARCH_MAX
Definition keystone.h:68
@ KS_ARCH_HEXAGON
Definition keystone.h:65
@ KS_ARCH_SPARC
Definition keystone.h:63
@ KS_ARCH_PPC
Definition keystone.h:62
@ KS_ARCH_X86
Definition keystone.h:61
@ KS_ARCH_EVM
Definition keystone.h:66
KEYSTONE_EXPORT ks_err ks_open(ks_arch arch, int mode, ks_engine **ks)
struct ks_struct ks_engine
Definition keystone.h:38
bool(* ks_sym_resolver)(const char *symbol, uint64_t *value)
Definition keystone.h:172
ks_opt_type
Definition keystone.h:176
@ KS_OPT_SYM_RESOLVER
Definition keystone.h:178
@ KS_OPT_SYNTAX
Definition keystone.h:177
KEYSTONE_EXPORT bool ks_arch_supported(ks_arch arch)