/
X86DisassemblerDecoder.c
2390 lines (2142 loc) · 66.7 KB
/
X86DisassemblerDecoder.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
*
* The LLVM Compiler Infrastructure
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*===----------------------------------------------------------------------===*
*
* This file is part of the X86 Disassembler.
* It contains the implementation of the instruction decoder.
* Documentation for the disassembler can be found in X86Disassembler.h.
*
*===----------------------------------------------------------------------===*/
/* Capstone Disassembly Engine */
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
#ifdef CAPSTONE_HAS_X86
#include <stdarg.h> /* for va_*() */
#if defined(CAPSTONE_HAS_OSXKERNEL)
#include <libkern/libkern.h>
#else
#include <stdlib.h> /* for exit() */
#endif
#include "../../cs_priv.h"
#include "../../utils.h"
#include "X86DisassemblerDecoder.h"
/// Specifies whether a ModR/M byte is needed and (if so) which
/// instruction each possible value of the ModR/M byte corresponds to. Once
/// this information is known, we have narrowed down to a single instruction.
struct ModRMDecision {
uint8_t modrm_type;
uint16_t instructionIDs;
};
/// Specifies which set of ModR/M->instruction tables to look at
/// given a particular opcode.
struct OpcodeDecision {
struct ModRMDecision modRMDecisions[256];
};
/// Specifies which opcode->instruction tables to look at given
/// a particular context (set of attributes). Since there are many possible
/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
/// applies given a specific set of attributes. Hence there are only IC_max
/// entries in this table, rather than 2^(ATTR_max).
struct ContextDecision {
struct OpcodeDecision opcodeDecisions[IC_max];
};
#ifdef CAPSTONE_X86_REDUCE
#include "X86GenDisassemblerTables_reduce.inc"
#else
#include "X86GenDisassemblerTables.inc"
#endif
//#define GET_INSTRINFO_ENUM
#define GET_INSTRINFO_MC_DESC
#ifdef CAPSTONE_X86_REDUCE
#include "X86GenInstrInfo_reduce.inc"
#else
#include "X86GenInstrInfo.inc"
#endif
/*
* contextForAttrs - Client for the instruction context table. Takes a set of
* attributes and returns the appropriate decode context.
*
* @param attrMask - Attributes, from the enumeration attributeBits.
* @return - The InstructionContext to use when looking up an
* an instruction with these attributes.
*/
static InstructionContext contextForAttrs(uint16_t attrMask)
{
return CONTEXTS_SYM[attrMask];
}
/*
* modRMRequired - Reads the appropriate instruction table to determine whether
* the ModR/M byte is required to decode a particular instruction.
*
* @param type - The opcode type (i.e., how many bytes it has).
* @param insnContext - The context for the instruction, as returned by
* contextForAttrs.
* @param opcode - The last byte of the instruction's opcode, not counting
* ModR/M extensions and escapes.
* @return - true if the ModR/M byte is required, false otherwise.
*/
static int modRMRequired(OpcodeType type,
InstructionContext insnContext,
uint16_t opcode)
{
const struct OpcodeDecision *decision = NULL;
const uint8_t *indextable = NULL;
uint8_t index;
switch (type) {
default:
case ONEBYTE:
decision = ONEBYTE_SYM;
indextable = index_x86DisassemblerOneByteOpcodes;
break;
case TWOBYTE:
decision = TWOBYTE_SYM;
indextable = index_x86DisassemblerTwoByteOpcodes;
break;
case THREEBYTE_38:
decision = THREEBYTE38_SYM;
indextable = index_x86DisassemblerThreeByte38Opcodes;
break;
case THREEBYTE_3A:
decision = THREEBYTE3A_SYM;
indextable = index_x86DisassemblerThreeByte3AOpcodes;
break;
#ifndef CAPSTONE_X86_REDUCE
case XOP8_MAP:
decision = XOP8_MAP_SYM;
indextable = index_x86DisassemblerXOP8Opcodes;
break;
case XOP9_MAP:
decision = XOP9_MAP_SYM;
indextable = index_x86DisassemblerXOP9Opcodes;
break;
case XOPA_MAP:
decision = XOPA_MAP_SYM;
indextable = index_x86DisassemblerXOPAOpcodes;
break;
case T3DNOW_MAP:
// 3DNow instructions always have ModRM byte
return true;
#endif
}
index = indextable[insnContext];
if (index)
return decision[index - 1].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
else
return false;
}
/*
* decode - Reads the appropriate instruction table to obtain the unique ID of
* an instruction.
*
* @param type - See modRMRequired().
* @param insnContext - See modRMRequired().
* @param opcode - See modRMRequired().
* @param modRM - The ModR/M byte if required, or any value if not.
* @return - The UID of the instruction, or 0 on failure.
*/
static InstrUID decode(OpcodeType type,
InstructionContext insnContext,
uint8_t opcode,
uint8_t modRM)
{
const struct ModRMDecision *dec = NULL;
const uint8_t *indextable = NULL;
uint8_t index;
switch (type) {
default:
case ONEBYTE:
indextable = index_x86DisassemblerOneByteOpcodes;
index = indextable[insnContext];
if (index)
dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
case TWOBYTE:
indextable = index_x86DisassemblerTwoByteOpcodes;
index = indextable[insnContext];
if (index)
dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
case THREEBYTE_38:
indextable = index_x86DisassemblerThreeByte38Opcodes;
index = indextable[insnContext];
if (index)
dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
case THREEBYTE_3A:
indextable = index_x86DisassemblerThreeByte3AOpcodes;
index = indextable[insnContext];
if (index)
dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
#ifndef CAPSTONE_X86_REDUCE
case XOP8_MAP:
indextable = index_x86DisassemblerXOP8Opcodes;
index = indextable[insnContext];
if (index)
dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
case XOP9_MAP:
indextable = index_x86DisassemblerXOP9Opcodes;
index = indextable[insnContext];
if (index)
dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
case XOPA_MAP:
indextable = index_x86DisassemblerXOPAOpcodes;
index = indextable[insnContext];
if (index)
dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
case T3DNOW_MAP:
indextable = index_x86DisassemblerT3DNOWOpcodes;
index = indextable[insnContext];
if (index)
dec = &T3DNOW_MAP_SYM[index - 1].modRMDecisions[opcode];
else
dec = &emptyTable.modRMDecisions[opcode];
break;
#endif
}
switch (dec->modrm_type) {
default:
//debug("Corrupt table! Unknown modrm_type");
return 0;
case MODRM_ONEENTRY:
return modRMTable[dec->instructionIDs];
case MODRM_SPLITRM:
if (modFromModRM(modRM) == 0x3)
return modRMTable[dec->instructionIDs+1];
return modRMTable[dec->instructionIDs];
case MODRM_SPLITREG:
if (modFromModRM(modRM) == 0x3)
return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)+8];
return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
case MODRM_SPLITMISC:
if (modFromModRM(modRM) == 0x3)
return modRMTable[dec->instructionIDs+(modRM & 0x3f)+8];
return modRMTable[dec->instructionIDs+((modRM & 0x38) >> 3)];
case MODRM_FULL:
return modRMTable[dec->instructionIDs+modRM];
}
}
/*
* specifierForUID - Given a UID, returns the name and operand specification for
* that instruction.
*
* @param uid - The unique ID for the instruction. This should be returned by
* decode(); specifierForUID will not check bounds.
* @return - A pointer to the specification for that instruction.
*/
static const struct InstructionSpecifier *specifierForUID(InstrUID uid)
{
return &INSTRUCTIONS_SYM[uid];
}
/*
* consumeByte - Uses the reader function provided by the user to consume one
* byte from the instruction's memory and advance the cursor.
*
* @param insn - The instruction with the reader function to use. The cursor
* for this instruction is advanced.
* @param byte - A pointer to a pre-allocated memory buffer to be populated
* with the data read.
* @return - 0 if the read was successful; nonzero otherwise.
*/
static int consumeByte(struct InternalInstruction *insn, uint8_t *byte)
{
int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
if (!ret)
++(insn->readerCursor);
return ret;
}
/*
* lookAtByte - Like consumeByte, but does not advance the cursor.
*
* @param insn - See consumeByte().
* @param byte - See consumeByte().
* @return - See consumeByte().
*/
static int lookAtByte(struct InternalInstruction *insn, uint8_t *byte)
{
return insn->reader(insn->readerArg, byte, insn->readerCursor);
}
static void unconsumeByte(struct InternalInstruction *insn)
{
insn->readerCursor--;
}
#define CONSUME_FUNC(name, type) \
static int name(struct InternalInstruction *insn, type *ptr) { \
type combined = 0; \
unsigned offset; \
for (offset = 0; offset < sizeof(type); ++offset) { \
uint8_t byte; \
int ret = insn->reader(insn->readerArg, \
&byte, \
insn->readerCursor + offset); \
if (ret) \
return ret; \
combined = combined | (type)((uint64_t)byte << (offset * 8)); \
} \
*ptr = combined; \
insn->readerCursor += sizeof(type); \
return 0; \
}
/*
* consume* - Use the reader function provided by the user to consume data
* values of various sizes from the instruction's memory and advance the
* cursor appropriately. These readers perform endian conversion.
*
* @param insn - See consumeByte().
* @param ptr - A pointer to a pre-allocated memory of appropriate size to
* be populated with the data read.
* @return - See consumeByte().
*/
CONSUME_FUNC(consumeInt8, int8_t)
CONSUME_FUNC(consumeInt16, int16_t)
CONSUME_FUNC(consumeInt32, int32_t)
CONSUME_FUNC(consumeUInt16, uint16_t)
CONSUME_FUNC(consumeUInt32, uint32_t)
CONSUME_FUNC(consumeUInt64, uint64_t)
/*
* setPrefixPresent - Marks that a particular prefix is present at a particular
* location.
*
* @param insn - The instruction to be marked as having the prefix.
* @param prefix - The prefix that is present.
* @param location - The location where the prefix is located (in the address
* space of the instruction's reader).
*/
static void setPrefixPresent(struct InternalInstruction *insn, uint8_t prefix, uint64_t location)
{
switch (prefix) {
case 0x26:
insn->isPrefix26 = true;
insn->prefix26 = location;
break;
case 0x2e:
insn->isPrefix2e = true;
insn->prefix2e = location;
break;
case 0x36:
insn->isPrefix36 = true;
insn->prefix36 = location;
break;
case 0x3e:
insn->isPrefix3e = true;
insn->prefix3e = location;
break;
case 0x64:
insn->isPrefix64 = true;
insn->prefix64 = location;
break;
case 0x65:
insn->isPrefix65 = true;
insn->prefix65 = location;
break;
case 0x66:
insn->isPrefix66 = true;
insn->prefix66 = location;
break;
case 0x67:
insn->isPrefix67 = true;
insn->prefix67 = location;
break;
case 0xf0:
insn->isPrefixf0 = true;
insn->prefixf0 = location;
break;
case 0xf2:
insn->isPrefixf2 = true;
insn->prefixf2 = location;
break;
case 0xf3:
insn->isPrefixf3 = true;
insn->prefixf3 = location;
break;
default:
break;
}
}
/*
* isPrefixAtLocation - Queries an instruction to determine whether a prefix is
* present at a given location.
*
* @param insn - The instruction to be queried.
* @param prefix - The prefix.
* @param location - The location to query.
* @return - Whether the prefix is at that location.
*/
static bool isPrefixAtLocation(struct InternalInstruction *insn, uint8_t prefix,
uint64_t location)
{
switch (prefix) {
case 0x26:
if (insn->isPrefix26 && insn->prefix26 == location)
return true;
break;
case 0x2e:
if (insn->isPrefix2e && insn->prefix2e == location)
return true;
break;
case 0x36:
if (insn->isPrefix36 && insn->prefix36 == location)
return true;
break;
case 0x3e:
if (insn->isPrefix3e && insn->prefix3e == location)
return true;
break;
case 0x64:
if (insn->isPrefix64 && insn->prefix64 == location)
return true;
break;
case 0x65:
if (insn->isPrefix65 && insn->prefix65 == location)
return true;
break;
case 0x66:
if (insn->isPrefix66 && insn->prefix66 == location)
return true;
break;
case 0x67:
if (insn->isPrefix67 && insn->prefix67 == location)
return true;
break;
case 0xf0:
if (insn->isPrefixf0 && insn->prefixf0 == location)
return true;
break;
case 0xf2:
if (insn->isPrefixf2 && insn->prefixf2 == location)
return true;
break;
case 0xf3:
if (insn->isPrefixf3 && insn->prefixf3 == location)
return true;
break;
default:
break;
}
return false;
}
/*
* readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
* instruction as having them. Also sets the instruction's default operand,
* address, and other relevant data sizes to report operands correctly.
*
* @param insn - The instruction whose prefixes are to be read.
* @return - 0 if the instruction could be read until the end of the prefix
* bytes, and no prefixes conflicted; nonzero otherwise.
*/
static int readPrefixes(struct InternalInstruction *insn)
{
bool isPrefix = true;
uint64_t prefixLocation;
uint8_t byte = 0, nextByte;
bool hasAdSize = false;
bool hasOpSize = false;
while (isPrefix) {
if (insn->mode == MODE_64BIT) {
// eliminate consecutive redundant REX bytes in front
if (consumeByte(insn, &byte))
return -1;
if ((byte & 0xf0) == 0x40) {
while(true) {
if (lookAtByte(insn, &byte)) // out of input code
return -1;
if ((byte & 0xf0) == 0x40) {
// another REX prefix, but we only remember the last one
if (consumeByte(insn, &byte))
return -1;
} else
break;
}
// recover the last REX byte if next byte is not a legacy prefix
switch (byte) {
case 0xf2: /* REPNE/REPNZ */
case 0xf3: /* REP or REPE/REPZ */
case 0xf0: /* LOCK */
case 0x2e: /* CS segment override -OR- Branch not taken */
case 0x36: /* SS segment override -OR- Branch taken */
case 0x3e: /* DS segment override */
case 0x26: /* ES segment override */
case 0x64: /* FS segment override */
case 0x65: /* GS segment override */
case 0x66: /* Operand-size override */
case 0x67: /* Address-size override */
break;
default: /* Not a prefix byte */
unconsumeByte(insn);
break;
}
} else {
unconsumeByte(insn);
}
}
prefixLocation = insn->readerCursor;
/* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
if (consumeByte(insn, &byte))
return -1;
if (insn->readerCursor - 1 == insn->startLocation
&& (byte == 0xf2 || byte == 0xf3)) {
if (lookAtByte(insn, &nextByte))
return -1;
/*
* If the byte is 0xf2 or 0xf3, and any of the following conditions are
* met:
* - it is followed by a LOCK (0xf0) prefix
* - it is followed by an xchg instruction
* then it should be disassembled as a xacquire/xrelease not repne/rep.
*/
if ((byte == 0xf2 || byte == 0xf3) &&
((nextByte == 0xf0) |
((nextByte & 0xfe) == 0x86 || (nextByte & 0xf8) == 0x90)))
insn->xAcquireRelease = true;
/*
* Also if the byte is 0xf3, and the following condition is met:
* - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
* "mov mem, imm" (opcode 0xc6/0xc7) instructions.
* then it should be disassembled as an xrelease not rep.
*/
if (byte == 0xf3 &&
(nextByte == 0x88 || nextByte == 0x89 ||
nextByte == 0xc6 || nextByte == 0xc7))
insn->xAcquireRelease = true;
if (insn->mode == MODE_64BIT && (nextByte & 0xf0) == 0x40) {
if (consumeByte(insn, &nextByte))
return -1;
if (lookAtByte(insn, &nextByte))
return -1;
unconsumeByte(insn);
}
}
switch (byte) {
case 0xf2: /* REPNE/REPNZ */
case 0xf3: /* REP or REPE/REPZ */
case 0xf0: /* LOCK */
// only accept the last prefix
insn->isPrefixf2 = false;
insn->isPrefixf3 = false;
insn->isPrefixf0 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix0 = byte;
break;
case 0x2e: /* CS segment override -OR- Branch not taken */
insn->segmentOverride = SEG_OVERRIDE_CS;
// only accept the last prefix
insn->isPrefix2e = false;
insn->isPrefix36 = false;
insn->isPrefix3e = false;
insn->isPrefix26 = false;
insn->isPrefix64 = false;
insn->isPrefix65 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix1 = byte;
break;
case 0x36: /* SS segment override -OR- Branch taken */
insn->segmentOverride = SEG_OVERRIDE_SS;
// only accept the last prefix
insn->isPrefix2e = false;
insn->isPrefix36 = false;
insn->isPrefix3e = false;
insn->isPrefix26 = false;
insn->isPrefix64 = false;
insn->isPrefix65 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix1 = byte;
break;
case 0x3e: /* DS segment override */
insn->segmentOverride = SEG_OVERRIDE_DS;
// only accept the last prefix
insn->isPrefix2e = false;
insn->isPrefix36 = false;
insn->isPrefix3e = false;
insn->isPrefix26 = false;
insn->isPrefix64 = false;
insn->isPrefix65 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix1 = byte;
break;
case 0x26: /* ES segment override */
insn->segmentOverride = SEG_OVERRIDE_ES;
// only accept the last prefix
insn->isPrefix2e = false;
insn->isPrefix36 = false;
insn->isPrefix3e = false;
insn->isPrefix26 = false;
insn->isPrefix64 = false;
insn->isPrefix65 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix1 = byte;
break;
case 0x64: /* FS segment override */
insn->segmentOverride = SEG_OVERRIDE_FS;
// only accept the last prefix
insn->isPrefix2e = false;
insn->isPrefix36 = false;
insn->isPrefix3e = false;
insn->isPrefix26 = false;
insn->isPrefix64 = false;
insn->isPrefix65 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix1 = byte;
break;
case 0x65: /* GS segment override */
insn->segmentOverride = SEG_OVERRIDE_GS;
// only accept the last prefix
insn->isPrefix2e = false;
insn->isPrefix36 = false;
insn->isPrefix3e = false;
insn->isPrefix26 = false;
insn->isPrefix64 = false;
insn->isPrefix65 = false;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix1 = byte;
break;
case 0x66: /* Operand-size override */
hasOpSize = true;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix2 = byte;
break;
case 0x67: /* Address-size override */
hasAdSize = true;
setPrefixPresent(insn, byte, prefixLocation);
insn->prefix3 = byte;
break;
default: /* Not a prefix byte */
isPrefix = false;
break;
}
//if (isPrefix)
// dbgprintf(insn, "Found prefix 0x%hhx", byte);
}
insn->vectorExtensionType = TYPE_NO_VEX_XOP;
if (byte == 0x62) {
uint8_t byte1, byte2;
if (consumeByte(insn, &byte1)) {
//dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
return -1;
}
if ((insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) &&
((~byte1 & 0xc) == 0xc)) {
if (lookAtByte(insn, &byte2)) {
//dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
return -1;
}
if ((byte2 & 0x4) == 0x4) {
insn->vectorExtensionType = TYPE_EVEX;
} else {
unconsumeByte(insn); /* unconsume byte1 */
unconsumeByte(insn); /* unconsume byte */
insn->necessaryPrefixLocation = insn->readerCursor - 2;
}
if (insn->vectorExtensionType == TYPE_EVEX) {
insn->vectorExtensionPrefix[0] = byte;
insn->vectorExtensionPrefix[1] = byte1;
if (consumeByte(insn, &insn->vectorExtensionPrefix[2])) {
//dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
return -1;
}
if (consumeByte(insn, &insn->vectorExtensionPrefix[3])) {
//dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
return -1;
}
/* We simulate the REX prefix for simplicity's sake */
if (insn->mode == MODE_64BIT) {
insn->rexPrefix = 0x40
| (wFromEVEX3of4(insn->vectorExtensionPrefix[2]) << 3)
| (rFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 2)
| (xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 1)
| (bFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 0);
}
//dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
// insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
// insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
}
} else {
// BOUND instruction
unconsumeByte(insn); /* unconsume byte1 */
unconsumeByte(insn); /* unconsume byte */
}
} else if (byte == 0xc4) {
uint8_t byte1;
if (lookAtByte(insn, &byte1)) {
//dbgprintf(insn, "Couldn't read second byte of VEX");
return -1;
}
if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
insn->vectorExtensionType = TYPE_VEX_3B;
insn->necessaryPrefixLocation = insn->readerCursor - 1;
} else {
unconsumeByte(insn);
insn->necessaryPrefixLocation = insn->readerCursor - 1;
}
if (insn->vectorExtensionType == TYPE_VEX_3B) {
insn->vectorExtensionPrefix[0] = byte;
if (consumeByte(insn, &insn->vectorExtensionPrefix[1]))
return -1;
if (consumeByte(insn, &insn->vectorExtensionPrefix[2]))
return -1;
/* We simulate the REX prefix for simplicity's sake */
if (insn->mode == MODE_64BIT) {
insn->rexPrefix = 0x40
| (wFromVEX3of3(insn->vectorExtensionPrefix[2]) << 3)
| (rFromVEX2of3(insn->vectorExtensionPrefix[1]) << 2)
| (xFromVEX2of3(insn->vectorExtensionPrefix[1]) << 1)
| (bFromVEX2of3(insn->vectorExtensionPrefix[1]) << 0);
}
}
} else if (byte == 0xc5) {
uint8_t byte1;
if (lookAtByte(insn, &byte1)) {
//dbgprintf(insn, "Couldn't read second byte of VEX");
return -1;
}
if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0) {
insn->vectorExtensionType = TYPE_VEX_2B;
} else {
unconsumeByte(insn);
}
if (insn->vectorExtensionType == TYPE_VEX_2B) {
insn->vectorExtensionPrefix[0] = byte;
if (consumeByte(insn, &insn->vectorExtensionPrefix[1]))
return -1;
if (insn->mode == MODE_64BIT) {
insn->rexPrefix = 0x40
| (rFromVEX2of2(insn->vectorExtensionPrefix[1]) << 2);
}
switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
default:
break;
case VEX_PREFIX_66:
hasOpSize = true;
break;
}
}
} else if (byte == 0x8f) {
uint8_t byte1;
if (lookAtByte(insn, &byte1)) {
// dbgprintf(insn, "Couldn't read second byte of XOP");
return -1;
}
if ((byte1 & 0x38) != 0x0) { /* 0 in these 3 bits is a POP instruction. */
insn->vectorExtensionType = TYPE_XOP;
insn->necessaryPrefixLocation = insn->readerCursor - 1;
} else {
unconsumeByte(insn);
insn->necessaryPrefixLocation = insn->readerCursor - 1;
}
if (insn->vectorExtensionType == TYPE_XOP) {
insn->vectorExtensionPrefix[0] = byte;
if (consumeByte(insn, &insn->vectorExtensionPrefix[1]))
return -1;
if (consumeByte(insn, &insn->vectorExtensionPrefix[2]))
return -1;
/* We simulate the REX prefix for simplicity's sake */
if (insn->mode == MODE_64BIT) {
insn->rexPrefix = 0x40
| (wFromXOP3of3(insn->vectorExtensionPrefix[2]) << 3)
| (rFromXOP2of3(insn->vectorExtensionPrefix[1]) << 2)
| (xFromXOP2of3(insn->vectorExtensionPrefix[1]) << 1)
| (bFromXOP2of3(insn->vectorExtensionPrefix[1]) << 0);
}
switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
default:
break;
case VEX_PREFIX_66:
hasOpSize = true;
break;
}
}
} else {
if (insn->mode == MODE_64BIT) {
if ((byte & 0xf0) == 0x40) {
uint8_t opcodeByte;
while(true) {
if (lookAtByte(insn, &opcodeByte)) // out of input code
return -1;
if ((opcodeByte & 0xf0) == 0x40) {
// another REX prefix, but we only remember the last one
if (consumeByte(insn, &byte))
return -1;
} else
break;
}
insn->rexPrefix = byte;
insn->necessaryPrefixLocation = insn->readerCursor - 2;
// dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
} else {
unconsumeByte(insn);
insn->necessaryPrefixLocation = insn->readerCursor - 1;
}
} else {
unconsumeByte(insn);
insn->necessaryPrefixLocation = insn->readerCursor - 1;
}
}
if (insn->mode == MODE_16BIT) {
insn->registerSize = (hasOpSize ? 4 : 2);
insn->addressSize = (hasAdSize ? 4 : 2);
insn->displacementSize = (hasAdSize ? 4 : 2);
insn->immediateSize = (hasOpSize ? 4 : 2);
insn->immSize = (hasOpSize ? 4 : 2);
} else if (insn->mode == MODE_32BIT) {
insn->registerSize = (hasOpSize ? 2 : 4);
insn->addressSize = (hasAdSize ? 2 : 4);
insn->displacementSize = (hasAdSize ? 2 : 4);
insn->immediateSize = (hasOpSize ? 2 : 4);
insn->immSize = (hasOpSize ? 2 : 4);
} else if (insn->mode == MODE_64BIT) {
if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
insn->registerSize = 8;
insn->addressSize = (hasAdSize ? 4 : 8);
insn->displacementSize = 4;
insn->immediateSize = 4;
insn->immSize = 4;
} else if (insn->rexPrefix) {
insn->registerSize = (hasOpSize ? 2 : 4);
insn->addressSize = (hasAdSize ? 4 : 8);
insn->displacementSize = (hasOpSize ? 2 : 4);
insn->immediateSize = (hasOpSize ? 2 : 4);
insn->immSize = (hasOpSize ? 2 : 4);
} else {
insn->registerSize = (hasOpSize ? 2 : 4);
insn->addressSize = (hasAdSize ? 4 : 8);
insn->displacementSize = (hasOpSize ? 2 : 4);
insn->immediateSize = (hasOpSize ? 2 : 4);
insn->immSize = (hasOpSize ? 4 : 8);
}
}
return 0;
}
static int readModRM(struct InternalInstruction *insn);
/*
* readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
* extended or escape opcodes).
*
* @param insn - The instruction whose opcode is to be read.
* @return - 0 if the opcode could be read successfully; nonzero otherwise.
*/
static int readOpcode(struct InternalInstruction *insn)
{
/* Determine the length of the primary opcode */
uint8_t current;
// printf(">>> readOpcode() = %x\n", insn->readerCursor);
insn->opcodeType = ONEBYTE;
insn->firstByte = 0x00;
if (insn->vectorExtensionType == TYPE_EVEX) {
switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
default:
// dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
// mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
return -1;
case VEX_LOB_0F:
insn->opcodeType = TWOBYTE;
return consumeByte(insn, &insn->opcode);
case VEX_LOB_0F38:
insn->opcodeType = THREEBYTE_38;
return consumeByte(insn, &insn->opcode);
case VEX_LOB_0F3A:
insn->opcodeType = THREEBYTE_3A;
return consumeByte(insn, &insn->opcode);
}
} else if (insn->vectorExtensionType == TYPE_VEX_3B) {
switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
default:
// dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
// mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
return -1;
case VEX_LOB_0F:
insn->twoByteEscape = 0x0f;
insn->opcodeType = TWOBYTE;
return consumeByte(insn, &insn->opcode);
case VEX_LOB_0F38:
insn->twoByteEscape = 0x0f;
insn->threeByteEscape = 0x38;
insn->opcodeType = THREEBYTE_38;
return consumeByte(insn, &insn->opcode);
case VEX_LOB_0F3A:
insn->twoByteEscape = 0x0f;
insn->threeByteEscape = 0x3a;
insn->opcodeType = THREEBYTE_3A;
return consumeByte(insn, &insn->opcode);
}
} else if (insn->vectorExtensionType == TYPE_VEX_2B) {
insn->twoByteEscape = 0x0f;
insn->opcodeType = TWOBYTE;
return consumeByte(insn, &insn->opcode);
} else if (insn->vectorExtensionType == TYPE_XOP) {
switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
default:
// dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
// mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
return -1;
case XOP_MAP_SELECT_8:
// FIXME: twoByteEscape?
insn->opcodeType = XOP8_MAP;
return consumeByte(insn, &insn->opcode);
case XOP_MAP_SELECT_9:
// FIXME: twoByteEscape?
insn->opcodeType = XOP9_MAP;
return consumeByte(insn, &insn->opcode);
case XOP_MAP_SELECT_A:
// FIXME: twoByteEscape?
insn->opcodeType = XOPA_MAP;
return consumeByte(insn, &insn->opcode);
}
}
if (consumeByte(insn, ¤t))
return -1;
// save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
insn->firstByte = current;
if (current == 0x0f) {
// dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
insn->twoByteEscape = current;
if (consumeByte(insn, ¤t))
return -1;