Repository navigation
Expand file tree
/
Copy pathx64id.h
More file actions
535 lines (485 loc) · 17.9 KB
/
Copy pathx64id.h
File metadata and controls
535 lines (485 loc) · 17.9 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef x64id_H
#define x64id_H
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#define _ENABLE_RAW_BYTES
#define _ENABLE_VEX_INFO
enum supported_architecture { X86 = 1, X64 = 2 };
enum decode_status {
x64id_ERROR = 0,
};
#define ALL (X86 | X64)
//
// instruction prefix look-up table
// clang-format off
static size_t x86_64_prefix[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ALL,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, ALL, 0, 0, 0, 0, 0, 0, 0, ALL, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, ALL, 0, 0, 0, 0, 0, 0, 0, ALL, 0,
/* 40 */ X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, X64, // REX prefixes
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, ALL, ALL, ALL, ALL, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B0 */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, 0, 0, 0, 0, 0, 0, 0,
/* C0 */ 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, // 2 = Coprocessor Escape
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1,
};
// clang-format on
#define b 1 // byte
#define v 2 // word, dword or qword (64bit mode), depending on OS attribute
#define z 3 // word for 16bit OS or dword for 32/64-bit OS
#define p \
4 // 32-bit, 48-bit, or 80-bit pointer, depending on operand-size attribute
#define z1 6 // word for 16bit OS or dword for 32/64-bit OS
#define w 7 // word
#define wb 8 // word, byte
#define gr3b 9 // byte (imm exists only if mod.reg == 0)
#define gr3z 10 // word, dword depending on OS (imm exists only if mod.reg == 0)
// clang-format off
static size_t imm_byte_1b[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ 0, 0, 0, 0, b, z, 0, 0, 0, 0, 0, 0, b, z, 0, 0,
/* 10 */ 0, 0, 0, 0, b, z, 0, 0, 0, 0, 0, 0, b, z, 0, 0,
/* 20 */ 0, 0, 0, 0, b, z, 0, 0, 0, 0, 0, 0, b, z, 0, 0,
/* 30 */ 0, 0, 0, 0, b, z, 0, 0, 0, 0, 0, 0, b, z, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, z, z, b, b, 0, 0, 0, 0,
/* 70 */ b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b,
/* 80 */ b, z, b, b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, p, 0, 0, 0, 0, 0,
/* A0 */ z1, z1, z1, z1, 0, 0, 0, 0, b, z, 0, 0, 0, 0, 0, 0,
/* B0 */ b, b, b, b, b, b, b, b, v, v, v, v, v, v, v, v,
/* C0 */ b, b, w, 0, 0, 0, b, z, wb, 0, w, 0, 0, b, 0, 0,
/* D0 */ 0, 0, 0, 0, b, b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E0 */ b, b, b, b, b, b, b, b, z, z, p, b, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, gr3b, gr3z, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
/*
* first byte:
* - 1: 1-byte
* - 2: 4-byte
*
* second byte (LSB):
* - 1: Jcc
* - 2: JMP
*
*/
#define j1 0x12
#define j2 0x22
#define call4 0x32
#define jc1 0x11
#define jc2 0x21
// check if the OP is Jcc or JMP
// clang-format off
static size_t op1b_labels[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1, jc1,
/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E0 */ jc1, jc1, jc1, jc1, 0, 0, 0, 0, call4, j2, 0, j1, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
//
// 2-byte OP look-up table
// 0x0f
#define OE 0x01
// 0x66 0x0f
#define O66 0x02
// 0xf2 0x0f
#define OF2 0x04
// 0xf3 0x0f
#define OF3 0x08
#define P1 (OE)
#define P2 (O66 | OE)
#define P4 (OF3 | OE)
#define P5 (O66 | OF2)
#define P6 (OE | O66 | OF3)
#define P7 (OE | O66 | OF2 | OF3)
#define P8 (O66 | OF2 | OF3)
// clang-format off
static size_t modrm_2b[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ P1, P1, P1, P1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ P7, P7, P7, P2, P2, P2, P6, P2, P1, 0, 0, 0, 0, 0, OF3, P1,
/* 20 */ P1, P1, P1, P1, 0, 0, 0, 0, P2, P2, P7, P2, P7, P7, P2, P2,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 40 */ P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1,
/* 50 */ P2, P7, P4, P4, P2, P2, P2, P2, P7, P7, P7, P6, P7, P7, P7, P7,
/* 60 */ P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, O66, O66, P2, P2,
/* 70 */ P7, P1, P1, P1, P2, P2, P2, P1, P1, P1, 0, 0, P5, P5, P6, P6,
/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1, P1,
/* A0 */ 0, 0, 0, P1, P1, P1, 0, 0, 0, 0, 0, P1, P1, P1, P1, P1,
/* B0 */ P1, P1, P1, P1, P1, P1, P1, P1, OF3, P1, P1, P1, P4, P4, P1, P1,
/* C0 */ P1, P1, P7, P1, P2, P2, P2, P1, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ P5, P2, P2, P2, P2, P2, P8, P2, P2, P2, P2, P2, P2, P2, P2, P2,
/* E0 */ P2, P2, P2, P2, P2, P2, P8, P2, P2, P2, P2, P2, P2, P2, P2, P2,
/* F0 */ OF2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, 0,
};
// clang-format on
// clang-format off
static size_t imm_byte_2b[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ b, b, b, b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ z, z, z, z, z, z, z, z, z, z, z, z, z, z, z, z,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, b, 0, 0, 0, 0, 0, 0, 0, b, 0, 0, 0,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, b, 0, 0, 0, 0, 0,
/* C0 */ 0, 0, b, 0, b, b, b, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
// check if the OP is Jcc or JMP
// clang-format off
static size_t op2b_labels[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2, jc2,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
//
// 3-byte OP look-up table
#define OP3 (OE | OF2 | O66)
#define OP2 (OE | OF2 | OF3)
#define OP4 (O66 | OF2 | OF3)
//
// 3-byte OP look-up table (0x38)
// clang-format off
static size_t modreg_3b_38[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, P2, O66, O66, O66, O66,
/* 10 */ O66, 0, 0, O66, O66, O66, O66, O66, 0, 0, 0, 0, O66, O66, O66, 0,
/* 20 */ O66, O66, O66, O66, O66, O66, 0, 0, O66, O66, O66, O66, O66, O66, O66, O66,
/* 30 */ O66, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66,
/* 40 */ O66, O66, 0, 0, 0, O66, O66, O66, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ O66, O66, O66, 0, 0, 0, 0, 0, 0, 0, 0, 0, O66, 0, O66, 0,
/* 90 */ O66, O66, O66, O66, 0, 0, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66,
/* A0 */ 0, 0, 0, 0, 0, 0, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66,
/* B0 */ 0, 0, 0, 0, 0, 0, O66, O66, O66, O66, O66, O66, O66, O66, O66, O66,
/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, O66, O66, O66, O66, O66, O66, O66, O66,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, O66, O66, O66, O66, O66,
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, O66, O66, O66, O66, O66,
/* F0 */ OP3, OP3, OE, 0, 0, OP2, OP4, P7, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
// clang-format off
static size_t imm_byte_3b_38[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
#define OP5 (OE | O66)
//
// 3-byte OP look-up table (0x3A)
// clang-format off
static size_t modreg_3b_3A[256] = {
// 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
/* 00 */ O66, O66, O66, 0, O66, O66, O66, 0, O66, O66, O66, O66, O66, O66, O66, OP5,
/* 10 */ 0, 0, 0, 0, O66, O66, O66, O66, O66, O66, 0, 0, 0, O66, 0, 0,
/* 20 */ O66, O66, O66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 30 */ 0, 0, 0, 0, 0, 0, 0, 0, O66, O66, 0, 0, 0, 0, 0, 0,
/* 40 */ O66, O66, O66, 0, O66, 0, O66, 0, 0, 0, O66, O66, O66, 0, 0, 0,
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ O66, O66, O66, O66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 70 */ 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* A0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* B0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* C0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
/* D0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
/* E0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* F0 */ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
// clang-format on
enum jmp_type {
JCC_SHORT = 1, // 1-byte JCC
JCC_FAR = 2, // 2-byte JCC, 4bytes imm
JMP_SHORT = 4, // 1-byte JMP
JMP_FAR = 8, // 4-byte JMP
CALL_RELATIVE = 16,
};
enum prefixes {
ES = 1, // 0x26
CS = 2, // 0x2E
SS = 4, // 0x36
DS = 8, // 0x3E
FS = 16, // 0x64
GS = 32, // 0x65
OS = 64, // 0x66
AS = 128, // 0x67
REPNE = 256,
REPE = 512,
OP64 = 1024,
VEX = 2048
};
enum instruction_feature {
PREFIX = 1,
ESCAPE = 2, // 0x0F
OP = 4,
OP3B = 8,
MODRM = 16,
SIB = 32,
REX = 64,
DISP = 128,
IMM = 512,
FPU = 1024,
};
/*
* VEX FORMAT
*
* 3-byte VEX bit 7 0 765 4 0 7 6 3 2 0
* 11000100 RXB m-mmmm W vvvv L pp
*
* 2-byte VEX bit 7 0 7 6 3 2 0
* 11000101 R vvvv L pp
*
* pp: equivalent to a SIMD prefix
* 00 : None
* 01 : 0x66
* 02 : 0xF3
* 03 : 0xF2
*
* m-mmmm:
* 00000: Reserved for future use (will #UD)
* 00001: implied 0F leading opcode byte
* 00010: implied 0F 38 leading opcode bytes
* 00011: implied 0F 3A leading opcode bytes
* 00100-11111: Reserved for future use (will #UD)
*
*/
#ifdef _ENABLE_VEX_INFO
struct vex_info {
struct {
uint8_t type;
union {
struct byte2 {
uint8_t vex_pp : 2;
uint8_t vex_l : 1;
uint8_t vex_v : 4;
uint8_t vex_r : 1;
} vexc5b;
uint8_t val5;
};
union {
struct byte3 {
uint8_t vex_m : 5;
uint8_t vex_b : 1;
uint8_t vex_x : 1;
uint8_t vex_r : 1;
uint8_t vex_pp : 2;
uint8_t vex_l : 1;
uint8_t vex_v : 4;
uint8_t vex_w : 1;
} vexc4b;
uint16_t val4;
};
};
};
#endif
#define MAX_INSTR_LEN_STR 256
enum decoded_operand_kind {
DECODED_OPERAND_NONE = 0,
DECODED_OPERAND_REGISTER,
DECODED_OPERAND_MEMORY,
DECODED_OPERAND_IMMEDIATE,
DECODED_OPERAND_SEGMENT_REGISTER,
DECODED_OPERAND_CONTROL_REGISTER,
DECODED_OPERAND_DEBUG_REGISTER
};
/* Structural operands decoded from the instruction bytes. Operand width is
* assigned by the disassembler handler because it depends on the opcode. */
struct decoded_operand {
enum decoded_operand_kind kind;
uint8_t reg;
uint8_t address_size;
uint8_t scale;
uint8_t base;
uint8_t index;
bool has_base;
bool has_index;
bool rip_relative;
int64_t displacement;
uint64_t immediate;
};
struct instruction {
uint64_t disp;
uint64_t imm;
uint64_t label;
#ifdef _ENABLE_VEX_INFO
struct vex_info _vex;
#endif
#ifdef _ENABLE_RAW_BYTES
uint8_t instr[15];
#endif
uint8_t prefixes[4];
uint8_t op[3];
union {
struct {
uint8_t rm : 3;
uint8_t reg : 3;
uint8_t mod : 2;
} bits;
uint8_t value;
} modrm;
union {
struct {
uint8_t rex_b : 1;
uint8_t rex_x : 1;
uint8_t rex_r : 1;
uint8_t rex_w : 1;
} bits;
uint8_t value;
} rex;
union {
struct {
uint8_t base : 3;
uint8_t index : 3;
uint8_t scaled : 2;
} bits;
uint8_t value;
} sib;
uint8_t vex[3];
int length;
int disp_len;
int imm_len;
uint16_t set_prefix; // bit mask
uint16_t set_field;
uint8_t jcc_type;
int8_t vex_cnt;
int8_t prefix_cnt;
int8_t op_cnt; // we count as opcode 0x0f, 0x66 / 0xf3 / 0xf2, op
char disasm[MAX_INSTR_LEN_STR];
int disasm_str_len;
struct decoded_operand operands[3];
uint8_t operand_count;
};
//
// Functions
//
/* Configuration snapshot: set with x64id_set_arch(), read during decoding. */
extern bool x64id_is_64bit;
void x64id_set_arch(int arch);
int x64id_decode(struct instruction *instr, char *data_src, int offset);
static void x64id_decode_modrm(struct instruction *instr, const char *data_src,
const size_t *modrm_table,
const size_t *imm_table,
const size_t *jcc_table);
static inline bool x64id_check_sib(uint8_t mod, uint8_t rm);
static inline int x64id_displacement_size(uint8_t mod, uint8_t rm);
static inline int x64id_imm_size(struct instruction *instr, size_t val);
static int x64id_decode_2b(struct instruction *instr, const char *data_src);
static inline int x64id_vex_size(struct instruction *instr, const char *data);
static inline void x64id_vex_decode(struct instruction *instr, const char *data,
uint8_t vex_size);
#endif // x64id_H