rallhook 0.7.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ /*
2
+ insts.h
3
+
4
+ diStorm3 - Powerful disassembler for X86/AMD64
5
+ http://ragestorm.net/distorm/
6
+ distorm at gmail dot com
7
+ Copyright (C) 2010 Gil Dabah
8
+
9
+ This program is free software: you can redistribute it and/or modify
10
+ it under the terms of the GNU General Public License as published by
11
+ the Free Software Foundation, either version 3 of the License, or
12
+ (at your option) any later version.
13
+
14
+ This program is distributed in the hope that it will be useful,
15
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ GNU General Public License for more details.
18
+
19
+ You should have received a copy of the GNU General Public License
20
+ along with this program. If not, see <http://www.gnu.org/licenses/>
21
+ */
22
+
23
+
24
+ #ifndef INSTS_H
25
+ #define INSTS_H
26
+
27
+ #include "instructions.h"
28
+
29
+ /* Root Trie DB */
30
+ extern _InstNode Instructions;
31
+ /* 3DNow! Trie DB */
32
+ extern _InstNode Table_0F_0F;
33
+ /* AVX related: */
34
+ extern _InstNode Table_0F, Table_0F_38, Table_0F_3A;
35
+
36
+ #endif /* INSTS_H */
@@ -0,0 +1,1270 @@
1
+ /*
2
+ operands.c
3
+
4
+ diStorm3 - Powerful disassembler for X86/AMD64
5
+ http://ragestorm.net/distorm/
6
+ distorm at gmail dot com
7
+ Copyright (C) 2010 Gil Dabah
8
+
9
+ This program is free software: you can redistribute it and/or modify
10
+ it under the terms of the GNU General Public License as published by
11
+ the Free Software Foundation, either version 3 of the License, or
12
+ (at your option) any later version.
13
+
14
+ This program is distributed in the hope that it will be useful,
15
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ GNU General Public License for more details.
18
+
19
+ You should have received a copy of the GNU General Public License
20
+ along with this program. If not, see <http://www.gnu.org/licenses/>
21
+ */
22
+
23
+
24
+ #include "../config.h"
25
+
26
+ #include "operands.h"
27
+ #include "x86defs.h"
28
+ #include "../mnemonics.h"
29
+
30
+
31
+ typedef enum {OPERAND_SIZE_NONE = 0, OPERAND_SIZE8, OPERAND_SIZE16, OPERAND_SIZE32, OPERAND_SIZE64, OPERAND_SIZE80, OPERAND_SIZE128, OPERAND_SIZE256} _OperandSizeType;
32
+ uint16_t _OPSIZETOINT[] = {0, 8, 16, 32, 64, 80, 128, 256};
33
+
34
+ /* A helper function to fix the 8 bits register if REX is used (to support SIL, DIL, etc). */
35
+ static unsigned int _FASTCALL_ operands_fix_8bit_rex_base(unsigned int reg)
36
+ {
37
+ if ((reg >= 4) && (reg < 8)) return reg + REGS8_REX_BASE - 4;
38
+ return reg + REGS8_BASE;
39
+ }
40
+
41
+ /* A helper function to set operand's type and size. */
42
+ static void _FASTCALL_ operands_set_ts(_Operand* op, _OperandType type, uint16_t size)
43
+ {
44
+ op->type = type;
45
+ op->size = size;
46
+ }
47
+
48
+ /* A helper function to set operand's type, size and index. */
49
+ static void _FASTCALL_ operands_set_tsi(_Operand* op, _OperandType type, uint16_t size, unsigned int index)
50
+ {
51
+ op->type = type;
52
+ op->index = (uint8_t)index;
53
+ op->size = size;
54
+ }
55
+
56
+ /* A helper function to read an unsigned integer from the stream safely. */
57
+ static int _FASTCALL_ read_stream_safe_uint(_CodeInfo* ci, void* result, unsigned int size)
58
+ {
59
+ ci->codeLen -= size;
60
+ if (ci->codeLen < 0) return FALSE;
61
+ switch (size)
62
+ {
63
+ case 1: *(uint8_t*)result = *(uint8_t*)ci->code; break;
64
+ case 2: *(uint16_t*)result = RUSHORT(ci->code); break;
65
+ case 4: *(uint32_t*)result = RULONG(ci->code); break;
66
+ case 8: *(uint64_t*)result = RULLONG(ci->code); break;
67
+ }
68
+ ci->code += size;
69
+ return TRUE;
70
+ }
71
+
72
+ /* A helper function to read a signed integer from the stream safely. */
73
+ static int _FASTCALL_ read_stream_safe_sint(_CodeInfo* ci, int64_t* result, unsigned int size)
74
+ {
75
+ ci->codeLen -= size;
76
+ if (ci->codeLen < 0) return FALSE;
77
+ switch (size)
78
+ {
79
+ case 1: *result = *(int8_t*)ci->code; break;
80
+ case 2: *result = RSHORT(ci->code); break;
81
+ case 4: *result = RLONG(ci->code); break;
82
+ case 8: *result = RLLONG(ci->code); break;
83
+ }
84
+ ci->code += size;
85
+ return TRUE;
86
+ }
87
+
88
+ /*
89
+ * SIB decoding is the most confusing part when decoding IA-32 instructions.
90
+ * This explanation should clear up some stuff.
91
+ *
92
+ * ! When base == 5, use EBP as the base register !
93
+ * if (rm == 4) {
94
+ * if mod == 01, decode SIB byte and ALSO read a 8 bits displacement.
95
+ * if mod == 10, decode SIB byte and ALSO read a 32 bits displacement.
96
+ * if mod == 11 <-- EXCEPTION, this is a general-purpose register and mustn't lead to SIB decoding!
97
+ * ; So far so good, now the confusing part comes in with mod == 0 and base=5, but no worry.
98
+ * if (mod == 00) {
99
+ * decode SIB byte WITHOUT any displacement.
100
+ * EXCEPTION!!! when base == 5, read a 32 bits displacement, but this time DO NOT use (EBP) BASE at all!
101
+ * }
102
+ *
103
+ * NOTE: base could specify None (no base register) if base==5 and mod==0, but then you also need DISP32.
104
+ * }
105
+ */
106
+ static void operands_extract_sib(_DInst* di, _OperandNumberType opNum,
107
+ _PrefixState* ps, _DecodeType effAdrSz,
108
+ unsigned int sib, unsigned int mod)
109
+ {
110
+ unsigned int scale = 0, index = 0, base = 0;
111
+ unsigned int vrex = ps->vrex;
112
+ uint8_t* pIndex = NULL;
113
+
114
+ _Operand* op = &di->ops[opNum];
115
+
116
+ /*
117
+ * SIB bits:
118
+ * |7---6-5----3-2---0|
119
+ * |SCALE| INDEX| BASE|
120
+ * |------------------|
121
+ */
122
+ scale = (sib >> 6) & 3;
123
+ index = (sib >> 3) & 7;
124
+ base = sib & 7;
125
+
126
+ /*
127
+ * The following fields: base/index/scale/disp8/32 are ALL optional by specific rules!
128
+ * The idea here is to keep the indirection as a simple-memory type.
129
+ * Because the base is optional, and we might be left with only one index.
130
+ * So even if there's a base but no index, or vice versa, we end up with one index register.
131
+ */
132
+
133
+ /* In 64 bits the REX prefix might affect the index of the SIB byte. */
134
+ if (vrex & PREFIX_EX_X) {
135
+ ps->usedPrefixes |= INST_PRE_REX;
136
+ index += EX_GPR_BASE;
137
+ }
138
+
139
+ if (index == 4) { /* No index is used. Use SMEM. */
140
+ op->type = O_SMEM;
141
+ pIndex = &op->index;
142
+ } else {
143
+ op->type = O_MEM;
144
+ pIndex = &di->base;
145
+ /* No base, unless it is updated below. E.G: [EAX*4] has no base reg. */
146
+ di->base = R_NONE;
147
+ }
148
+
149
+ if (base != 5) {
150
+ if (vrex & PREFIX_EX_B) ps->usedPrefixes |= INST_PRE_REX;
151
+ *pIndex = effAdrSz == Decode64Bits ? REGS64_BASE : REGS32_BASE;
152
+ *pIndex += (uint8_t)(base + ((vrex & PREFIX_EX_B) ? EX_GPR_BASE : 0));
153
+ } else if (mod != 0) {
154
+ /*
155
+ * if base == 5 then you have to decode according to MOD.
156
+ * mod(00) - disp32.
157
+ * mod(01) - disp8 + rBP
158
+ * mod(10) - disp32 + rBP
159
+ * mod(11) - not possible, it's a general-purpose register.
160
+ */
161
+
162
+ if (vrex & PREFIX_EX_B) ps->usedPrefixes |= INST_PRE_REX;
163
+ if (effAdrSz == Decode64Bits) *pIndex = REGS64_BASE + 5 + ((vrex & PREFIX_EX_B) ? EX_GPR_BASE : 0);
164
+ else *pIndex = REGS32_BASE + 5 + ((vrex & PREFIX_EX_B) ? EX_GPR_BASE : 0);
165
+ } else if (index == 4) {
166
+ /* 32bits displacement only. */
167
+ op->type = O_DISP;
168
+ return;
169
+ }
170
+
171
+ if (index != 4) { /* In 64 bits decoding mode, if index == R12, it's valid! */
172
+ if (effAdrSz == Decode64Bits) op->index = (uint8_t)(REGS64_BASE + index);
173
+ else op->index = (uint8_t)(REGS32_BASE + index);
174
+ di->scale = scale != 0 ? (1 << scale) : 0;
175
+ }
176
+ }
177
+
178
+ /*
179
+ * This seems to be the hardest part in decoding the operands.
180
+ * If you take a look carefully at Table 2-2. 32-Bit Addressing Forms with the ModR/M Byte,
181
+ * you will understand it's easy to decode the operands.
182
+
183
+ * First we check the DT, so we can decide according to which Table in the documentation we are supposed to decode.
184
+ * Then we follow the specific table whether it's 16 bits or 32/64 bits.
185
+
186
+ * Don't forget that Operand Size AND Address Size prefixes may change the decoding!
187
+
188
+ * Some instructions force the use of RM16 or other specific types, so take it into account.
189
+ */
190
+ static int operands_extract_modrm(_CodeInfo* ci,
191
+ _DInst* di, _OpType type,
192
+ _OperandNumberType opNum, _PrefixState* ps,
193
+ _DecodeType effOpSz, _DecodeType effAdrSz,
194
+ int* lockableInstruction, unsigned int mod, unsigned int rm,
195
+ _iflags instFlags)
196
+ {
197
+ unsigned int vrex = ps->vrex, sib = 0, base = 0;
198
+ _Operand* op = &di->ops[opNum];
199
+ uint16_t size = 0;
200
+
201
+ if (mod == 3) {
202
+ /*
203
+ * General-purpose register is handled the same way in 16/32/64 bits decoding modes.
204
+ * NOTE!! that we have to override the size of the register, since it was set earlier as Memory and not Register!
205
+ */
206
+ op->type = O_REG;
207
+ /* Start with original size which was set earlier, some registers have same size of memory and depend on it. */
208
+ size = op->size;
209
+ switch(type)
210
+ {
211
+ case OT_RFULL_M16:
212
+ case OT_RM_FULL:
213
+ switch (effOpSz)
214
+ {
215
+ case Decode16Bits:
216
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
217
+ if (vrex & PREFIX_EX_B) {
218
+ ps->usedPrefixes |= INST_PRE_REX;
219
+ rm += EX_GPR_BASE;
220
+ }
221
+ size = 16;
222
+ rm += REGS16_BASE;
223
+ break;
224
+ case Decode32Bits:
225
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
226
+ if (vrex & PREFIX_EX_B) {
227
+ ps->usedPrefixes |= INST_PRE_REX;
228
+ rm += EX_GPR_BASE;
229
+ }
230
+ size = 32;
231
+ rm += REGS32_BASE;
232
+ break;
233
+ case Decode64Bits:
234
+ /* A fix for SMSW RAX which use the REX prefix. */
235
+ if (type == OT_RFULL_M16) ps->usedPrefixes |= INST_PRE_REX;
236
+ /* CALL NEAR/PUSH/POP defaults to 64 bits. --> INST_64BITS, REX isn't required, thus ignored anyways. */
237
+ if (instFlags & INST_PRE_REX) ps->usedPrefixes |= INST_PRE_REX;
238
+ /* Include REX is used for REX.B. */
239
+ if (vrex & PREFIX_EX_B) {
240
+ ps->usedPrefixes |= INST_PRE_REX;
241
+ rm += EX_GPR_BASE;
242
+ }
243
+ size = 64;
244
+ rm += REGS64_BASE;
245
+ break;
246
+ }
247
+ break;
248
+ case OT_R32_64_M8:
249
+ /* FALL THROUGH, decode 32 or 64 bits register. */
250
+ case OT_R32_64_M16:
251
+ /* FALL THROUGH, decode 32 or 64 bits register. */
252
+ case OT_RM32_64: /* Take care specifically in MOVNTI/MOVD/CVT's instructions, making it _REG64 with REX or if they are promoted. */
253
+ if (vrex & PREFIX_EX_B) {
254
+ ps->usedPrefixes |= INST_PRE_REX;
255
+ rm += EX_GPR_BASE;
256
+ }
257
+ /* Is it a promoted instruction? (only INST_64BITS is set and REX isn't required.) */
258
+ if ((ci->dt == Decode64Bits) && ((instFlags & (INST_64BITS | INST_PRE_REX)) == INST_64BITS)) {
259
+ size = 64;
260
+ rm += REGS64_BASE;
261
+ break;
262
+ }
263
+ /* Give a chance to REX.W. Because if it was a promoted instruction we don't care about REX.W anyways. */
264
+ if (vrex & PREFIX_EX_W) {
265
+ ps->usedPrefixes |= INST_PRE_REX;
266
+ size = 64;
267
+ rm += REGS64_BASE;
268
+ } else {
269
+ size = 32;
270
+ rm += REGS32_BASE;
271
+ }
272
+ break;
273
+ case OT_RM16_32: /* Used only with MOVZXD instruction to support 16 bits operand. */
274
+ if (vrex & PREFIX_EX_B) {
275
+ ps->usedPrefixes |= INST_PRE_REX;
276
+ rm += EX_GPR_BASE;
277
+ }
278
+ /* Is it 16 bits operand size? */
279
+ if (ps->decodedPrefixes & INST_PRE_OP_SIZE) {
280
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
281
+ size = 16;
282
+ rm += REGS16_BASE;
283
+ } else {
284
+ size = 32;
285
+ rm += REGS32_BASE;
286
+ }
287
+ break;
288
+ case OT_RM16:
289
+ if (vrex & PREFIX_EX_B) {
290
+ ps->usedPrefixes |= INST_PRE_REX;
291
+ rm += EX_GPR_BASE;
292
+ }
293
+ rm += REGS16_BASE;
294
+ break;
295
+ case OT_RM8:
296
+ if (ps->prefixExtType == PET_REX) {
297
+ ps->usedPrefixes |= INST_PRE_REX;
298
+ rm = operands_fix_8bit_rex_base(rm + ((vrex & PREFIX_EX_B) ? EX_GPR_BASE : 0));
299
+ } else rm += REGS8_BASE;
300
+ break;
301
+ case OT_MM32:
302
+ case OT_MM64:
303
+ /* MMX doesn't support extended registers. */
304
+ size = 64;
305
+ rm += MMXREGS_BASE;
306
+ break;
307
+
308
+ case OT_XMM16:
309
+ case OT_XMM32:
310
+ case OT_XMM64:
311
+ case OT_XMM128:
312
+ if (vrex & PREFIX_EX_B) {
313
+ ps->usedPrefixes |= INST_PRE_REX;
314
+ rm += EX_GPR_BASE;
315
+ }
316
+ size = 128;
317
+ rm += SSEREGS_BASE;
318
+ break;
319
+
320
+ case OT_RM32:
321
+ case OT_R32_M8:
322
+ case OT_R32_M16:
323
+ if (vrex & PREFIX_EX_B) {
324
+ ps->usedPrefixes |= INST_PRE_REX;
325
+ rm += EX_GPR_BASE;
326
+ }
327
+ size = 32;
328
+ rm += REGS32_BASE;
329
+ break;
330
+
331
+ case OT_YMM256:
332
+ if (vrex & PREFIX_EX_B) rm += EX_GPR_BASE;
333
+ rm += AVXREGS_BASE;
334
+ break;
335
+ case OT_YXMM64_256:
336
+ case OT_YXMM128_256:
337
+ if (vrex & PREFIX_EX_B) rm += EX_GPR_BASE;
338
+ if (vrex & PREFIX_EX_L) {
339
+ size = 256;
340
+ rm += AVXREGS_BASE;
341
+ } else {
342
+ size = 128;
343
+ rm += SSEREGS_BASE;
344
+ }
345
+ break;
346
+ case OT_WXMM32_64:
347
+ case OT_LXMM64_128:
348
+ if (vrex & PREFIX_EX_B) rm += EX_GPR_BASE;
349
+ size = 128;
350
+ rm += SSEREGS_BASE;
351
+ break;
352
+
353
+ case OT_WRM32_64:
354
+ case OT_REG32_64_M8:
355
+ case OT_REG32_64_M16:
356
+ if (vrex & PREFIX_EX_B) rm += EX_GPR_BASE;
357
+ if (vrex & PREFIX_EX_W) {
358
+ size = 64;
359
+ rm += REGS64_BASE;
360
+ } else {
361
+ size = 32;
362
+ rm += REGS32_BASE;
363
+ }
364
+ break;
365
+
366
+ default: return FALSE;
367
+ }
368
+ op->size = size;
369
+ op->index = (uint8_t)rm;
370
+ return TRUE;
371
+ }
372
+
373
+ /* Memory indirection decoding ahead:) */
374
+
375
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
376
+ if (lockableInstruction && (ps->decodedPrefixes & INST_PRE_LOCK)) *lockableInstruction = TRUE;
377
+
378
+ if (effAdrSz == Decode16Bits) {
379
+ /* Decoding according to Table 2-1. (16 bits) */
380
+ if ((mod == 0) && (rm == 6)) {
381
+ /* 6 is a special case - only 16 bits displacement. */
382
+ op->type = O_DISP;
383
+ di->dispSize = 16;
384
+ if (!read_stream_safe_sint(ci, (int64_t*)&di->disp, sizeof(int16_t))) return FALSE;
385
+ } else {
386
+ /*
387
+ * Create the O_MEM for 16 bits indirection that requires 2 registers, E.G: [BS+SI].
388
+ * or create O_SMEM for a single register indirection, E.G: [BP].
389
+ */
390
+ static uint8_t MODS[] = {R_BX, R_BX, R_BP, R_BP, R_SI, R_DI, R_BP, R_BX};
391
+ static uint8_t MODS2[] = {R_SI, R_DI, R_SI, R_DI};
392
+ if (rm < 4) {
393
+ op->type = O_MEM;
394
+ di->base = MODS[rm];
395
+ op->index = MODS2[rm];
396
+ } else {
397
+ op->type = O_SMEM;
398
+ op->index = MODS[rm];
399
+ }
400
+
401
+ if (mod == 1) { /* 8 bits displacement + indirection */
402
+ di->dispSize = 8;
403
+ if (!read_stream_safe_sint(ci, (int64_t*)&di->disp, sizeof(int8_t))) return FALSE;
404
+ } else if (mod == 2) { /* 16 bits displacement + indirection */
405
+ di->dispSize = 16;
406
+ if (!read_stream_safe_sint(ci, (int64_t*)&di->disp, sizeof(int16_t))) return FALSE;
407
+ }
408
+ }
409
+
410
+ if ((rm == 2) || (rm == 3) || ((rm == 6) && (mod != 0))) {
411
+ /* BP's default segment is SS, so ignore it. */
412
+ prefixes_use_segment(INST_PRE_SS, ps, ci->dt, di);
413
+ } else {
414
+ /* Ignore default DS segment. */
415
+ prefixes_use_segment(INST_PRE_DS, ps, ci->dt, di);
416
+ }
417
+ } else { /* Decode32Bits or Decode64Bits! */
418
+ /* Remember that from a 32/64 bits ModR/M byte a SIB byte could follow! */
419
+ if ((mod == 0) && (rm == 5)) {
420
+
421
+ /* 5 is a special case - only 32 bits displacement, or RIP relative. */
422
+ di->dispSize = 32;
423
+ if (!read_stream_safe_sint(ci, (int64_t*)&di->disp, sizeof(int32_t))) return FALSE;
424
+
425
+ if (ci->dt == Decode64Bits) {
426
+ /* In 64 bits decoding mode depsite of the address size, a RIP-relative address it is. */
427
+ op->type = O_SMEM;
428
+ op->index = R_RIP;
429
+ } else {
430
+ /* Absolute address: */
431
+ op->type = O_DISP;
432
+ }
433
+ } else {
434
+ if (rm == 4) {
435
+ /* 4 is a special case - SIB byte + disp8/32 follows! */
436
+ /* Read SIB byte. */
437
+ if (!read_stream_safe_uint(ci, &sib, sizeof(int8_t))) return FALSE;
438
+ operands_extract_sib(di, opNum, ps, effAdrSz, sib, mod);
439
+ } else {
440
+ op->type = O_SMEM;
441
+ if (vrex & PREFIX_EX_B) {
442
+ ps->usedPrefixes |= INST_PRE_REX;
443
+ rm += EX_GPR_BASE;
444
+ }
445
+
446
+ if (effAdrSz == Decode64Bits) op->index = (uint8_t)(REGS64_BASE + rm);
447
+ else op->index = (uint8_t)(REGS32_BASE + rm);
448
+ }
449
+
450
+ if (mod == 1) {
451
+ di->dispSize = 8;
452
+ if (!read_stream_safe_sint(ci, (int64_t*)&di->disp, sizeof(int8_t))) return FALSE;
453
+ } else if ((mod == 2) || ((sib & 7) == 5)) { /* If there is no BASE, read DISP32! */
454
+ di->dispSize = 32;
455
+ if (!read_stream_safe_sint(ci, (int64_t*)&di->disp, sizeof(int32_t))) return FALSE;
456
+ }
457
+ }
458
+
459
+ /* Get the base register. */
460
+ base = op->index;
461
+ if (di->base != R_NONE) base = di->base;
462
+ else if (di->scale >= 2) base = 0; /* If it's only an index but got scale, it's still DS. */
463
+ /* Default for EBP/ESP is SS segment. 64 bits mode ignores DS anyway. */
464
+ if ((base == R_EBP) || (base == R_ESP)) prefixes_use_segment(INST_PRE_SS, ps, ci->dt, di);
465
+ else prefixes_use_segment(INST_PRE_DS, ps, ci->dt, di);
466
+ }
467
+
468
+ return TRUE;
469
+ }
470
+
471
+
472
+ /*
473
+ * This function is reponsible to textually format a required operand according to its type.
474
+ * It is vital to understand that there are other operands than what the ModR/M byte specifies.
475
+
476
+ * Only by decoding the operands of an instruction which got a LOCK prefix, we could tell whether it may use the LOCK prefix.
477
+ * According to Intel, LOCK prefix must precede some specific instructions AND in their memory destination operand form (which means first operand).
478
+ * LOCK INC EAX, would generate an exception, but LOCK INC [EAX] is alright.
479
+ * Also LOCK ADD BX, [BP] would generate an exception.
480
+
481
+ * Return code:
482
+ * TRUE - continue parsing the instruction and its operands, everything went right 'till now.
483
+ * FALSE - not enough bytes, or invalid operands.
484
+ */
485
+
486
+ int operands_extract(_CodeInfo* ci, _DInst* di, _InstInfo* ii,
487
+ _OpType type, _OperandNumberType opNum,
488
+ unsigned int modrm, _PrefixState* ps, _DecodeType effOpSz,
489
+ _DecodeType effAdrSz, int* lockableInstruction)
490
+ {
491
+ unsigned int mod = 0, reg = 0, rm = 0, vexV = ps->vexV;
492
+ unsigned int vrex = ps->vrex, typeHandled = TRUE;
493
+ _iflags instFlags = ii->flags;
494
+ _Operand* op = &di->ops[opNum];
495
+
496
+ /* Used to indicate the size of the MEMORY INDIRECTION only. */
497
+ _OperandSizeType opSize = OPERAND_SIZE_NONE;
498
+
499
+ /*
500
+ * ModRM bits:
501
+ * |7-6-5--------3-2-0|
502
+ * |MOD|REG/OPCODE|RM |
503
+ * |------------------|
504
+ */
505
+ mod = (modrm >> 6) & 3; /* Mode(register-indirection, disp8+reg+indirection, disp16+reg+indirection, general-purpose register) */
506
+ reg = (modrm >> 3) & 7; /* Register(could be part of the opcode itself or general-purpose register) */
507
+ rm = modrm & 7; /* Specifies which general-purpose register or disp+reg to use. */
508
+
509
+ /* -- Memory Indirection Operands (that cannot be a general purpose register) -- */
510
+ switch (type)
511
+ {
512
+ case OT_MEM64_128: /* Used only by CMPXCHG8/16B. */
513
+ /* Make a specific check when the type is OT_MEM64_128 since the lockable CMPXCHG8B uses this one... */
514
+ if (lockableInstruction && (ps->decodedPrefixes & INST_PRE_LOCK)) *lockableInstruction = TRUE;
515
+ if (effOpSz == Decode64Bits) {
516
+ ps->usedPrefixes |= INST_PRE_REX;
517
+ opSize = OPERAND_SIZE128;
518
+ } else opSize = OPERAND_SIZE64;
519
+ break;
520
+ case OT_MEM32: opSize = OPERAND_SIZE32; break;
521
+ case OT_MEM32_64:
522
+ /* Used by MOVNTI. Default size is 32bits, 64bits with REX. */
523
+ if (effOpSz == Decode64Bits) {
524
+ ps->usedPrefixes |= INST_PRE_REX;
525
+ opSize = OPERAND_SIZE64;
526
+ } else opSize = OPERAND_SIZE32;
527
+ break;
528
+ case OT_MEM64: opSize = OPERAND_SIZE64; break;
529
+ case OT_MEM128: opSize = OPERAND_SIZE128; break;
530
+ case OT_MEM16_FULL: /* The size indicates about the second item of the pair. */
531
+ switch (effOpSz)
532
+ {
533
+ case Decode16Bits:
534
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
535
+ opSize = OPERAND_SIZE16;
536
+ break;
537
+ case Decode32Bits:
538
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
539
+ opSize = OPERAND_SIZE32;
540
+ break;
541
+ case Decode64Bits:
542
+ /* Mark usage of REX only if it was required. */
543
+ if ((instFlags & (INST_64BITS | INST_PRE_REX)) == (INST_64BITS | INST_PRE_REX)) ps->usedPrefixes |= INST_PRE_REX;
544
+ opSize = OPERAND_SIZE64;
545
+ break;
546
+ }
547
+ break;
548
+ case OT_MEM16_3264: /* The size indicates about the second item of the pair. */
549
+ if (ci->dt == Decode64Bits) opSize = OPERAND_SIZE64;
550
+ else opSize = OPERAND_SIZE32;
551
+ break;
552
+ case OT_MEM_OPT:
553
+ /* Since the MEM is optional, only when mod != 3, then return true as if the operand was alright. */
554
+ if (mod == 0x3) return TRUE;
555
+ break;
556
+ case OT_FPUM16: opSize = OPERAND_SIZE16; break;
557
+ case OT_FPUM32: opSize = OPERAND_SIZE32; break;
558
+ case OT_FPUM64: opSize = OPERAND_SIZE64; break;
559
+ case OT_FPUM80: opSize = OPERAND_SIZE80; break;
560
+ case OT_LMEM128_256:
561
+ if (vrex & PREFIX_EX_L) opSize = OPERAND_SIZE256;
562
+ else opSize = OPERAND_SIZE128;
563
+ break;
564
+ case OT_MEM: /* Size is unknown, but still handled. */ break;
565
+ default: typeHandled = FALSE; break;
566
+ }
567
+ if (typeHandled) {
568
+ /* All of the above types can't use a general-purpose register (a MOD of 3)!. */
569
+ if (mod == 0x3) {
570
+ if (lockableInstruction) *lockableInstruction = FALSE;
571
+ return FALSE;
572
+ }
573
+ op->size = _OPSIZETOINT[opSize];
574
+ return operands_extract_modrm(ci, di, type, opNum, ps, effOpSz, effAdrSz, lockableInstruction, mod, rm, instFlags);
575
+ }
576
+
577
+ /* -- Memory Indirection Operands (that can be a register) -- */
578
+ typeHandled = TRUE;
579
+ switch (type)
580
+ {
581
+ case OT_RM_FULL:
582
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
583
+ /* PUSH/JMP/CALL are automatically promoted to 64 bits! */
584
+ if (effOpSz == Decode32Bits) {
585
+ opSize = OPERAND_SIZE32;
586
+ break;
587
+ } else if (effOpSz == Decode64Bits) {
588
+ /* Mark usage of REX only if it was required. */
589
+ if ((instFlags & INST_64BITS) == 0) ps->usedPrefixes |= INST_PRE_REX;
590
+ opSize = OPERAND_SIZE64;
591
+ break;
592
+ }
593
+ /* FALL THROUGH BECAUSE dt==Decoded16Bits @-<----*/
594
+ case OT_RM16:
595
+ /* If we got here not from OT_RM16, then the prefix was used. */
596
+ if (type != OT_RM16) ps->usedPrefixes |= INST_PRE_OP_SIZE;
597
+ opSize = OPERAND_SIZE16;
598
+ break;
599
+ case OT_RM32_64:
600
+ /* The default size is 32, which can be 64 with a REX only. */
601
+ if (effOpSz == Decode64Bits) {
602
+ opSize = OPERAND_SIZE64;
603
+ /* Mark REX prefix as used if non-promoted instruction. */
604
+ if ((instFlags & (INST_64BITS | INST_PRE_REX)) == (INST_64BITS | INST_PRE_REX)) {
605
+ ps->usedPrefixes |= INST_PRE_REX;
606
+ }
607
+ } else opSize = OPERAND_SIZE32;
608
+ break;
609
+ case OT_RM16_32:
610
+ /* Ignore REX, it's either 32 or 16 bits RM. */
611
+ if (ps->decodedPrefixes & INST_PRE_OP_SIZE) {
612
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
613
+ /* Assume: We are in 64bits when we have this operand used. */
614
+ opSize = OPERAND_SIZE16;
615
+ } else opSize = OPERAND_SIZE32;
616
+ break;
617
+ case OT_WXMM32_64:
618
+ case OT_WRM32_64:
619
+ if (vrex & PREFIX_EX_W) opSize = OPERAND_SIZE64;
620
+ else opSize = OPERAND_SIZE32;
621
+ break;
622
+ case OT_YXMM64_256:
623
+ if (vrex & PREFIX_EX_L) opSize = OPERAND_SIZE256;
624
+ else opSize = OPERAND_SIZE64;
625
+ break;
626
+ case OT_YXMM128_256:
627
+ if (vrex & PREFIX_EX_L) opSize = OPERAND_SIZE256;
628
+ else opSize = OPERAND_SIZE128;
629
+ break;
630
+ case OT_LXMM64_128:
631
+ if (vrex & PREFIX_EX_L) opSize = OPERAND_SIZE128;
632
+ else opSize = OPERAND_SIZE64;
633
+ break;
634
+ case OT_RFULL_M16:
635
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
636
+ opSize = OPERAND_SIZE16;
637
+ break;
638
+
639
+ case OT_RM8:
640
+ case OT_R32_M8:
641
+ case OT_R32_64_M8:
642
+ case OT_REG32_64_M8:
643
+ opSize = OPERAND_SIZE8;
644
+ break;
645
+
646
+ case OT_XMM16:
647
+ case OT_R32_M16:
648
+ case OT_R32_64_M16:
649
+ case OT_REG32_64_M16:
650
+ opSize = OPERAND_SIZE16;
651
+ break;
652
+
653
+ case OT_RM32:
654
+ case OT_MM32:
655
+ case OT_XMM32:
656
+ opSize = OPERAND_SIZE32;
657
+ break;
658
+
659
+ case OT_MM64:
660
+ case OT_XMM64:
661
+ opSize = OPERAND_SIZE64;
662
+ break;
663
+
664
+ case OT_XMM128: opSize = OPERAND_SIZE128; break;
665
+ case OT_YMM256: opSize = OPERAND_SIZE256; break;
666
+ default: typeHandled = FALSE; break;
667
+ }
668
+ if (typeHandled) {
669
+ /* Fill size of memory dereference for operand. */
670
+ op->size = _OPSIZETOINT[opSize];
671
+ return operands_extract_modrm(ci, di, type, opNum, ps, effOpSz, effAdrSz, lockableInstruction, mod, rm, instFlags);
672
+ }
673
+
674
+ /* Simple operand type (no ModRM byte). */
675
+ switch (type)
676
+ {
677
+ case OT_IMM8:
678
+ operands_set_ts(op, O_IMM, 8);
679
+ if (!read_stream_safe_uint(ci, &di->imm.byte, sizeof(int8_t))) return FALSE;
680
+ break;
681
+ case OT_IMM_FULL: /* 16, 32 or 64, depends on prefixes. */
682
+ if (effOpSz == Decode16Bits) {
683
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
684
+ /* FALL THROUGH */
685
+ case OT_IMM16: /* Force 16 bits imm. */
686
+ operands_set_ts(op, O_IMM, 16);
687
+ if (!read_stream_safe_uint(ci, &di->imm.word, sizeof(int16_t))) return FALSE;
688
+ break;
689
+ /*
690
+ * Extension: MOV imm64, requires REX.
691
+ * Make sure it needs the REX.
692
+ * REX must be present because op size function takes it into consideration.
693
+ */
694
+ } else if ((effOpSz == Decode64Bits) &&
695
+ ((instFlags & (INST_64BITS | INST_PRE_REX)) == (INST_64BITS | INST_PRE_REX))) {
696
+ ps->usedPrefixes |= INST_PRE_REX;
697
+
698
+ operands_set_ts(op, O_IMM, 64);
699
+ if (!read_stream_safe_uint(ci, &di->imm.qword, sizeof(int64_t))) return FALSE;
700
+ break;
701
+ } else ps->usedPrefixes |= INST_PRE_OP_SIZE;
702
+ /* FALL THROUGH BECAUSE dt==Decoded32Bits @-<----*/
703
+ case OT_IMM32:
704
+ op->type = O_IMM;
705
+ if (ci->dt == Decode64Bits) {
706
+ /* Imm32 is sign extended to 64 bits! */
707
+ op->size = 32;
708
+ di->flags |= FLAG_IMM_SIGNED;
709
+ if (!read_stream_safe_sint(ci, &di->imm.sqword, sizeof(int32_t))) return FALSE;
710
+ } else {
711
+ op->size = 32;
712
+ if (!read_stream_safe_uint(ci, &di->imm.dword, sizeof(int32_t))) return FALSE;
713
+ }
714
+ break;
715
+ case OT_SEIMM8: /* Sign extended immediate. */
716
+ /*
717
+ * PUSH SEIMM8 can be prefixed by operand size:
718
+ * Input stream: 66, 6a, 55
719
+ * 64bits DT: push small 55
720
+ * 32bits DT: push small 55
721
+ * 16bits DT: push large 55
722
+ * small/large indicates the size of the eSP pointer advancement.
723
+ * Check the instFlags (ii->flags) if it can be operand-size-prefixed and if the prefix exists.
724
+ */
725
+ op->type = O_IMM;
726
+ if ((instFlags & INST_PRE_OP_SIZE) && (ps->decodedPrefixes & INST_PRE_OP_SIZE)) {
727
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
728
+ switch (ci->dt)
729
+ {
730
+ case Decode16Bits: op->size = 32; break;
731
+ case Decode32Bits:
732
+ case Decode64Bits:
733
+ op->size = 16;
734
+ break;
735
+ }
736
+ } else op->size = 8;
737
+ di->flags |= FLAG_IMM_SIGNED;
738
+ if (!read_stream_safe_sint(ci, &di->imm.sqword, sizeof(int8_t))) return FALSE;
739
+ break;
740
+ case OT_IMM16_1:
741
+ operands_set_ts(op, O_IMM1, 16);
742
+ if (!read_stream_safe_uint(ci, &di->imm.ex.i1, sizeof(int16_t))) return FALSE;
743
+ break;
744
+ case OT_IMM8_1:
745
+ operands_set_ts(op, O_IMM1, 8);
746
+ if (!read_stream_safe_uint(ci, &di->imm.ex.i1, sizeof(int8_t))) return FALSE;
747
+ break;
748
+ case OT_IMM8_2:
749
+
750
+ operands_set_ts(op, O_IMM2, 8);
751
+ if (!read_stream_safe_uint(ci, &di->imm.ex.i2, sizeof(int8_t))) return FALSE;
752
+ break;
753
+ case OT_REG8:
754
+ operands_set_ts(op, O_REG, 8);
755
+ if (ps->prefixExtType) {
756
+ /*
757
+ * If REX prefix is valid then we will have to use low bytes.
758
+ * This is a PASSIVE behaviour changer of REX prefix, it affects operands even if its value is 0x40 !
759
+ */
760
+ ps->usedPrefixes |= INST_PRE_REX;
761
+ op->index = (uint8_t)operands_fix_8bit_rex_base(reg + ((vrex & PREFIX_EX_R) ? EX_GPR_BASE : 0));
762
+ } else op->index = (uint8_t)(REGS8_BASE + reg);
763
+ break;
764
+ case OT_REG16:
765
+ operands_set_tsi(op, O_REG, 16, REGS16_BASE + reg);
766
+ break;
767
+ case OT_REG_FULL:
768
+ switch (effOpSz)
769
+ {
770
+ case Decode16Bits:
771
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
772
+ if (vrex & PREFIX_EX_R) {
773
+ ps->usedPrefixes |= INST_PRE_REX;
774
+ reg += EX_GPR_BASE;
775
+ }
776
+ operands_set_tsi(op, O_REG, 16, REGS16_BASE + reg);
777
+ break;
778
+ case Decode32Bits:
779
+ if (vrex & PREFIX_EX_R) {
780
+ ps->usedPrefixes |= INST_PRE_REX;
781
+ reg += EX_GPR_BASE;
782
+ } else ps->usedPrefixes |= INST_PRE_OP_SIZE;
783
+ operands_set_tsi(op, O_REG, 32, REGS32_BASE + reg);
784
+ break;
785
+ case Decode64Bits: /* rex must be presented. */
786
+ ps->usedPrefixes |= INST_PRE_REX;
787
+ operands_set_tsi(op, O_REG, 64, REGS64_BASE + reg + ((vrex & PREFIX_EX_R) ? EX_GPR_BASE : 0));
788
+ break;
789
+ }
790
+ break;
791
+ case OT_REG32:
792
+ if (vrex & PREFIX_EX_R) {
793
+ ps->usedPrefixes |= INST_PRE_REX;
794
+ reg += EX_GPR_BASE;
795
+ }
796
+ operands_set_tsi(op, O_REG, 32, REGS32_BASE + reg);
797
+ break;
798
+ case OT_REG32_64: /* Handle CVT's, MOVxX and MOVNTI instructions which could be extended to 64 bits registers with REX. */
799
+ if (vrex & PREFIX_EX_R) {
800
+ ps->usedPrefixes |= INST_PRE_REX;
801
+ reg += EX_GPR_BASE;
802
+ }
803
+
804
+ /* Is it a promoted instruction? (only INST_64BITS is set and REX isn't required.) */
805
+ if ((ci->dt == Decode64Bits) && ((instFlags & (INST_64BITS | INST_PRE_REX)) == INST_64BITS)) {
806
+ operands_set_tsi(op, O_REG, 64, REGS64_BASE + reg);
807
+ break;
808
+ }
809
+ /* Give a chance to REX.W. Because if it was a promoted instruction we don't care about REX.W anyways. */
810
+ if (vrex & PREFIX_EX_W) {
811
+ ps->usedPrefixes |= INST_PRE_REX;
812
+ operands_set_tsi(op, O_REG, 64, REGS64_BASE + reg);
813
+ } else operands_set_tsi(op, O_REG, 32, REGS32_BASE + reg);
814
+ break;
815
+ case OT_FREG32_64_RM: /* Force decoding mode. Used for MOV CR(n)/DR(n) which defaults to 64 bits operand size in 64 bits. */
816
+ if (vrex & PREFIX_EX_B) {
817
+ ps->usedPrefixes |= INST_PRE_REX;
818
+ rm += EX_GPR_BASE;
819
+ }
820
+
821
+ if (ci->dt == Decode64Bits) operands_set_tsi(op, O_REG, 64, REGS64_BASE + rm);
822
+ else operands_set_tsi(op, O_REG, 32, REGS32_BASE + rm);
823
+ break;
824
+ case OT_MM: /* MMX register */
825
+ operands_set_tsi(op, O_REG, 64, MMXREGS_BASE + reg);
826
+ break;
827
+ case OT_MM_RM: /* MMX register, this time from the RM field */
828
+ operands_set_tsi(op, O_REG, 64, MMXREGS_BASE + rm);
829
+ break;
830
+ case OT_REGXMM0: /* Implicit XMM0 operand. */
831
+ reg = 0;
832
+ vrex = 0;
833
+ /* FALL THROUGH */
834
+ case OT_XMM: /* SSE register */
835
+ if (vrex & PREFIX_EX_R) {
836
+ ps->usedPrefixes |= INST_PRE_REX;
837
+ reg += EX_GPR_BASE;
838
+ }
839
+ operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + reg);
840
+ break;
841
+ case OT_XMM_RM: /* SSE register, this time from the RM field */
842
+ if (vrex & PREFIX_EX_B) {
843
+ ps->usedPrefixes |= INST_PRE_REX;
844
+ rm += EX_GPR_BASE;
845
+ }
846
+ operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + rm);
847
+ break;
848
+ case OT_CREG:
849
+ /*
850
+ * Don't parse if the reg exceeds the bounds of the array.
851
+ * Most of the CR's are not implemented, so if there's no matching string, the operand is invalid.
852
+ */
853
+ if (vrex & PREFIX_EX_R) {
854
+ ps->usedPrefixes |= INST_PRE_REX;
855
+ reg += EX_GPR_BASE;
856
+ } else if ((ci->dt == Decode32Bits) && (ps->decodedPrefixes & INST_PRE_LOCK)) {
857
+ /*
858
+ * NOTE: In 32 bits decoding mode,
859
+ * if the lock prefix is set before MOV CR(n) it will become the 4th bit of the REG field like REX.R in 64 bits.
860
+ */
861
+ reg += EX_GPR_BASE;
862
+ ps->usedPrefixes |= INST_PRE_LOCK;
863
+ }
864
+ /* Ignore some registers which do not exist. */
865
+ if ((reg >= CREGS_MAX) || (reg == 1) || ((reg >= 5) && (reg <= 7))) return FALSE;
866
+
867
+ op->type = O_REG;
868
+ if (ci->dt == Decode64Bits) op->size = 64;
869
+ else op->size = 32;
870
+ op->index = (uint8_t)(CREGS_BASE + reg);
871
+ break;
872
+ case OT_DREG:
873
+ /*
874
+ * In 64 bits there are 16 debug registers.
875
+ * but accessing any of dr8-15 which aren't implemented will cause an #ud.
876
+ */
877
+ if ((reg == 4) || (reg == 5) || (vrex & PREFIX_EX_R)) return FALSE;
878
+
879
+ op->type = O_REG;
880
+ if (ci->dt == Decode64Bits) op->size = 64;
881
+ else op->size = 32;
882
+ op->index = (uint8_t)(DREGS_BASE + reg);
883
+ break;
884
+ case OT_SREG: /* Works with REG16 only! */
885
+ /* If lockableInstruction pointer is non-null we know it's the first operand. */
886
+ if (lockableInstruction && (reg == 1)) return FALSE; /* Can't MOV CS, <REG>. */
887
+ /*Don't parse if the reg exceeds the bounds of the array. */
888
+ if (reg <= SEG_REGS_MAX - 1) operands_set_tsi(op, O_REG, 16, SREGS_BASE + reg);
889
+ else return FALSE;
890
+ break;
891
+ case OT_SEG:
892
+ op->type = O_REG;
893
+ /* Size of reg is always 16, it's up to caller to zero extend it to operand size. */
894
+ op->size = 16;
895
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
896
+ /*
897
+ * Extract the SEG from ii->flags this time!!!
898
+ * Check whether an operand size prefix is used.
899
+ */
900
+ switch (instFlags & INST_PRE_SEGOVRD_MASK)
901
+ {
902
+ case INST_PRE_ES: op->index = R_ES; break;
903
+ case INST_PRE_CS: op->index = R_CS; break;
904
+ case INST_PRE_SS: op->index = R_SS; break;
905
+ case INST_PRE_DS: op->index = R_DS; break;
906
+ case INST_PRE_FS: op->index = R_FS; break;
907
+ case INST_PRE_GS: op->index = R_GS; break;
908
+ }
909
+ break;
910
+ case OT_ACC8:
911
+ operands_set_tsi(op, O_REG, 8, R_AL);
912
+ break;
913
+ case OT_ACC16:
914
+ operands_set_tsi(op, O_REG, 16, R_AX);
915
+ break;
916
+ case OT_ACC_FULL_NOT64: /* No REX.W support for IN/OUT. */
917
+ vrex &= ~PREFIX_EX_W;
918
+ case OT_ACC_FULL:
919
+ if (effOpSz == Decode16Bits) {
920
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
921
+ operands_set_tsi(op, O_REG, 16, R_AX);
922
+ } else if (effOpSz == Decode32Bits) {
923
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
924
+ operands_set_tsi(op, O_REG, 32, R_EAX);
925
+ } else { /* Decode64Bits */
926
+ /* Only non-promoted instructions need REX in order to decode in 64 bits. */
927
+ /* MEM-OFFSET MOV's are NOT automatically promoted to 64 bits. */
928
+ if (~instFlags & INST_64BITS) {
929
+ ps->usedPrefixes |= INST_PRE_REX;
930
+ }
931
+ operands_set_tsi(op, O_REG, 64, R_RAX);
932
+ }
933
+ break;
934
+ case OT_PTR16_FULL:
935
+ /* ptr16:full - full is size of operand size to read, therefore Operand Size Prefix affects this. So we need to handle it. */
936
+ if (effOpSz == Decode16Bits) {
937
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
938
+ ci->codeLen -= sizeof(int16_t)*2;
939
+ if (ci->codeLen < 0) return FALSE;
940
+
941
+ operands_set_ts(op, O_PTR, 16);
942
+ di->imm.ptr.off = RUSHORT(ci->code); /* Read offset first. */
943
+ di->imm.ptr.seg = RUSHORT((ci->code + sizeof(int16_t))); /* And read segment. */
944
+
945
+ ci->code += sizeof(int16_t)*2;
946
+ } else { /* Decode32Bits, for Decode64Bits this instruction is invalid. */
947
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
948
+ ci->codeLen -= sizeof(int32_t) + sizeof(int16_t);
949
+ if (ci->codeLen < 0) return FALSE;
950
+
951
+ operands_set_ts(op, O_PTR, 32);
952
+ di->imm.ptr.off = RULONG(ci->code); /* Read 32bits offset this time. */
953
+ di->imm.ptr.seg = RUSHORT((ci->code + sizeof(int32_t))); /* And read segment, 16 bits. */
954
+
955
+ ci->code += sizeof(int32_t) + sizeof(int16_t);
956
+ }
957
+ break;
958
+ case OT_RELCB:
959
+ case OT_RELC_FULL:
960
+
961
+ if (type == OT_RELCB) {
962
+ operands_set_ts(op, O_PC, 8);
963
+ if (!read_stream_safe_sint(ci, &di->imm.sqword, sizeof(int8_t))) return FALSE;
964
+ } else { /* OT_RELC_FULL */
965
+
966
+ /* Yep, operand size prefix affects relc also. */
967
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
968
+ if (effOpSz == Decode16Bits) {
969
+ operands_set_ts(op, O_PC, 16);
970
+ if (!read_stream_safe_sint(ci, &di->imm.sqword, sizeof(int16_t))) return FALSE;
971
+ } else { /* Decode32Bits or Decode64Bits = for now they are the same */
972
+ operands_set_ts(op, O_PC, 32);
973
+ if (!read_stream_safe_sint(ci, &di->imm.sqword, sizeof(int32_t))) return FALSE;
974
+ }
975
+ }
976
+
977
+ /* Support for hint, see if there's a segment override. */
978
+ if ((ii->opcodeId >= I_JO) && (ii->opcodeId <= I_JG)) {
979
+ if (ps->decodedPrefixes & INST_PRE_CS) {
980
+ ps->usedPrefixes |= INST_PRE_CS;
981
+ di->flags |= FLAG_HINT_NOT_TAKEN;
982
+ } else if (ps->decodedPrefixes & INST_PRE_DS) {
983
+ ps->usedPrefixes |= INST_PRE_DS;
984
+ di->flags |= FLAG_HINT_TAKEN;
985
+ }
986
+ }
987
+ break;
988
+ case OT_MOFFS8:
989
+ op->size = 8;
990
+ /* FALL THROUGH, size won't be changed. */
991
+ case OT_MOFFS_FULL:
992
+ op->type = O_DISP;
993
+ if (op->size == 0) {
994
+ /* Calculate size of operand (same as ACC size). */
995
+ switch (effOpSz)
996
+ {
997
+ case Decode16Bits: op->size = 16; break;
998
+ case Decode32Bits: op->size = 32; break;
999
+ case Decode64Bits: op->size = 64; break;
1000
+ }
1001
+ }
1002
+
1003
+ prefixes_use_segment(INST_PRE_DS, ps, ci->dt, di);
1004
+
1005
+ /*
1006
+ * Just a pointer to a BYTE, WORD, DWORD, QWORD. Works only with ACC8/16/32/64 respectively.
1007
+ * MOV [0x1234], AL ; MOV AX, [0x1234] ; MOV EAX, [0x1234], note that R/E/AX will be chosen by OT_ACC_FULL.
1008
+ */
1009
+ if (effAdrSz == Decode16Bits) {
1010
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1011
+
1012
+ di->dispSize = 16;
1013
+ if (!read_stream_safe_uint(ci, &di->disp, sizeof(int16_t))) return FALSE;
1014
+ } else if (effAdrSz == Decode32Bits) {
1015
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1016
+
1017
+ di->dispSize = 32;
1018
+ if (!read_stream_safe_uint(ci, &di->disp, sizeof(int32_t))) return FALSE;
1019
+ } else { /* Decode64Bits */
1020
+ di->dispSize = 64;
1021
+ if (!read_stream_safe_uint(ci, &di->disp, sizeof(int64_t))) return FALSE;
1022
+ }
1023
+ break;
1024
+ case OT_CONST1:
1025
+ operands_set_ts(op, O_IMM, 8);
1026
+ di->imm.byte = 1;
1027
+ break;
1028
+ case OT_REGCL:
1029
+ operands_set_tsi(op, O_REG, 8, R_CL);
1030
+ break;
1031
+
1032
+ case OT_FPU_SI:
1033
+ /* Low 3 bits specify the REG, similar to the MODR/M byte reg. */
1034
+ operands_set_tsi(op, O_REG, 32, FPUREGS_BASE + (*(ci->code-1) & 7));
1035
+ break;
1036
+ case OT_FPU_SSI:
1037
+ operands_set_tsi(op, O_REG, 32, R_ST0);
1038
+ operands_set_tsi(op + 1, O_REG, 32, FPUREGS_BASE + (*(ci->code-1) & 7));
1039
+ break;
1040
+ case OT_FPU_SIS:
1041
+ operands_set_tsi(op, O_REG, 32, FPUREGS_BASE + (*(ci->code-1) & 7));
1042
+ operands_set_tsi(op + 1, O_REG, 32, R_ST0);
1043
+ break;
1044
+
1045
+ /*
1046
+ * Special treatment for Instructions-Block:
1047
+ * INC/DEC (only 16/32 bits) /PUSH/POP/XCHG instructions, which get their REG from their own binary code.
1048
+
1049
+ * Notice these instructions are 1 or 2 byte long,
1050
+ * code points after the byte which represents the instruction itself,
1051
+ * thus, even if the instructions are 2 bytes long it will read its last byte which contains the REG info.
1052
+ */
1053
+ case OT_IB_RB:
1054
+ /* Low 3 bits specify the REG, similar to the MODR/M byte reg. */
1055
+ operands_set_ts(op, O_REG, 8);
1056
+ reg = *(ci->code-1) & 7;
1057
+ if (vrex & PREFIX_EX_B) {
1058
+ ps->usedPrefixes |= INST_PRE_REX;
1059
+ op->index = (uint8_t)operands_fix_8bit_rex_base(reg + EX_GPR_BASE);
1060
+ } else if (ps->prefixExtType == PET_REX) {
1061
+ ps->usedPrefixes |= INST_PRE_REX;
1062
+ op->index = (uint8_t)operands_fix_8bit_rex_base(reg);
1063
+ } else op->index = (uint8_t)(REGS8_BASE + reg);
1064
+ break;
1065
+ case OT_IB_R_FULL:
1066
+ reg = *(ci->code-1) & 7;
1067
+ switch (effOpSz)
1068
+ {
1069
+ case Decode16Bits:
1070
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
1071
+ if (vrex & PREFIX_EX_B) {
1072
+ ps->usedPrefixes |= INST_PRE_REX;
1073
+ reg += EX_GPR_BASE;
1074
+ }
1075
+ operands_set_tsi(op, O_REG, 16, REGS16_BASE + reg);
1076
+ break;
1077
+ case Decode32Bits:
1078
+ if (vrex & PREFIX_EX_B) {
1079
+ ps->usedPrefixes |= INST_PRE_REX;
1080
+ reg += EX_GPR_BASE;
1081
+ } else ps->usedPrefixes |= INST_PRE_OP_SIZE;
1082
+ operands_set_tsi(op, O_REG, 32, REGS32_BASE + reg);
1083
+ break;
1084
+ case Decode64Bits:
1085
+ /*
1086
+ * Automatically promoted instruction can drop REX prefix if not required.
1087
+ * PUSH/POP defaults to 64 bits. --> INST_64BITS
1088
+ * MOV imm64 / BSWAP requires REX.W to be 64 bits --> INST_64BITS | INST_PRE_REX
1089
+ */
1090
+ if ((instFlags & INST_64BITS) && ((instFlags & INST_PRE_REX) == 0)) {
1091
+ if (vrex & PREFIX_EX_B) {
1092
+ ps->usedPrefixes |= INST_PRE_REX;
1093
+ reg += EX_GPR_BASE;
1094
+ }
1095
+ } else {
1096
+ ps->usedPrefixes |= INST_PRE_REX;
1097
+ reg += (vrex & PREFIX_EX_B) ? EX_GPR_BASE : 0;
1098
+ }
1099
+ operands_set_tsi(op, O_REG, 64, REGS64_BASE + reg);
1100
+ break;
1101
+ }
1102
+ break;
1103
+
1104
+ /*
1105
+ * Special treatment for repeatable instructions.
1106
+
1107
+ * We want the following output:
1108
+ * If there's only the REP/NZ prefix, we won't output anything (All operands are implicit).
1109
+ * If there's an operand size prefix, we will change the suffix letter of the mnemonic, which specifies the size of operand to the required one.
1110
+ * If there's a segment override prefix, we will output the segment and the used index register (EDI/ESI).
1111
+ * If there's an address size prefix, we will output the (segment if needed and) the used and inverted index register (DI/SI).
1112
+
1113
+ * Example:
1114
+ * :: Decoding in 16 bits mode! ::
1115
+ * AD ~ LODSW
1116
+ * 66 AD ~ LODSD
1117
+ * F3 AC ~ REP LODSB
1118
+ * F3 66 AD ~ REP LODSD
1119
+ * F3 3E AC ~ REP LODS BYTE DS:[SI]
1120
+ * F3 67 AD ~ REP LODS WORD [ESI]
1121
+
1122
+ * The basic form of a repeatable instruction has its operands hidden and has a suffix letter
1123
+ * which implies on the size of operation being done.
1124
+ * Therefore, we cannot change the mnemonic here when we encounter another prefix and its not the decoder's responsibility to do so.
1125
+ * That's why the caller is responsible to add the suffix letter if no other prefixes are used.
1126
+ * And all we are doing here is formatting the operand correctly.
1127
+ */
1128
+ case OT_REGI_ESI:
1129
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1130
+
1131
+ op->type = O_SMEM;
1132
+
1133
+ /* This might be a 16, 32 or 64 bits instruction, depends on the decoding mode. */
1134
+ if (instFlags & INST_16BITS) {
1135
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
1136
+
1137
+ if (effOpSz == Decode16Bits) op->size = 16;
1138
+ else if ((effOpSz == Decode64Bits) && (instFlags & INST_64BITS)) {
1139
+ ps->usedPrefixes |= INST_PRE_REX;
1140
+ op->size = 64;
1141
+ } else op->size = 32;
1142
+ } else op->size = 8;
1143
+
1144
+ /*
1145
+ * Clear segment in case OT_REGI_EDI was parsed earlier,
1146
+ * DS can be overridden and therefore has precedence.
1147
+ */
1148
+ di->segment = 0;
1149
+ prefixes_use_segment(INST_PRE_DS, ps, ci->dt, di);
1150
+
1151
+ if (effAdrSz == Decode16Bits) op->index = R_SI;
1152
+ else if (effAdrSz == Decode32Bits) op->index = R_ESI;
1153
+ else op->index = R_RSI;
1154
+ break;
1155
+ case OT_REGI_EDI:
1156
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1157
+
1158
+ op->type = O_SMEM;
1159
+
1160
+ /* This might be a 16 or 32 bits instruction, depends on the decoding mode. */
1161
+ if (instFlags & INST_16BITS) {
1162
+ ps->usedPrefixes |= INST_PRE_OP_SIZE;
1163
+
1164
+ if (effOpSz == Decode16Bits) op->size = 16;
1165
+ else if ((effOpSz == Decode64Bits) && (instFlags & INST_64BITS)) {
1166
+ ps->usedPrefixes |= INST_PRE_REX;
1167
+ op->size = 64;
1168
+ } else op->size = 32;
1169
+ } else op->size = 8;
1170
+
1171
+ /* Note: The [rDI] operand can't be prefixed by a segment override, therefore we don't set usedPrefixes. */
1172
+ if ((opNum == ONT_1) && (ci->dt != Decode64Bits)) di->segment = R_ES | SEGMENT_DEFAULT; /* No ES in 64 bits mode. */
1173
+
1174
+ if (effAdrSz == Decode16Bits) op->index = R_DI;
1175
+ else if (effAdrSz == Decode32Bits) op->index = R_EDI;
1176
+ else op->index = R_RDI;
1177
+ break;
1178
+
1179
+ /* Used for In/Out instructions varying forms. */
1180
+ case OT_REGDX:
1181
+ /* Simple single IN/OUT instruction. */
1182
+ operands_set_tsi(op, O_REG, 16, R_DX);
1183
+ break;
1184
+
1185
+ /* Used for INVLPGA instruction. */
1186
+ case OT_REGECX:
1187
+ operands_set_tsi(op, O_REG, 32, R_ECX);
1188
+ break;
1189
+ case OT_REGI_EBXAL:
1190
+ /* XLAT BYTE [rBX + AL] */
1191
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1192
+
1193
+ prefixes_use_segment(INST_PRE_DS, ps, ci->dt, di);
1194
+
1195
+ /* Size of deref is always 8 for xlat. */
1196
+ operands_set_tsi(op, O_MEM, 8, R_AL);
1197
+
1198
+ if (effAdrSz == Decode16Bits) di->base = R_BX;
1199
+ else if (effAdrSz == Decode32Bits) di->base = R_EBX;
1200
+ else {
1201
+ ps->usedPrefixes |= INST_PRE_REX;
1202
+ di->base = R_RBX;
1203
+ }
1204
+ break;
1205
+ case OT_REGI_EAX:
1206
+ /*
1207
+ * Implicit rAX as memory indirection operand. Used by AMD's SVM instructions.
1208
+ * Since this is a memory indirection, the default address size in 64bits decoding mode is 64.
1209
+ */
1210
+
1211
+ if (effAdrSz == Decode64Bits) operands_set_tsi(op, O_SMEM, 64, R_RAX);
1212
+ else if (effAdrSz == Decode32Bits) {
1213
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1214
+ operands_set_tsi(op, O_SMEM, 32, R_EAX);
1215
+ } else {
1216
+ ps->usedPrefixes |= INST_PRE_ADDR_SIZE;
1217
+ operands_set_tsi(op, O_SMEM, 16, R_AX);
1218
+ }
1219
+ break;
1220
+ case OT_VXMM:
1221
+ operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + vexV);
1222
+ break;
1223
+ case OT_XMM_IMM:
1224
+ ci->codeLen -= sizeof(int8_t);
1225
+ if (ci->codeLen < 0) return FALSE;
1226
+
1227
+ if (ci->dt == Decode32Bits) reg = (*ci->code >> 4) & 0x7;
1228
+ else reg = (*ci->code >> 4) & 0xf;
1229
+ operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + reg);
1230
+
1231
+ ci->code += sizeof(int8_t);
1232
+ break;
1233
+ case OT_YXMM:
1234
+ if (vrex & PREFIX_EX_R) reg += EX_GPR_BASE;
1235
+ if (ps->vrex & PREFIX_EX_L) operands_set_tsi(op, O_REG, 256, AVXREGS_BASE + reg);
1236
+ else operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + reg);
1237
+ break;
1238
+ case OT_YXMM_IMM:
1239
+ ci->codeLen -= sizeof(int8_t);
1240
+ if (ci->codeLen < 0) return FALSE;
1241
+
1242
+ if (ci->dt == Decode32Bits) reg = (*ci->code >> 4) & 0x7;
1243
+ else reg = (*ci->code >> 4) & 0xf;
1244
+
1245
+ if (ps->vrex & PREFIX_EX_L) operands_set_tsi(op, O_REG, 256, AVXREGS_BASE + reg);
1246
+ else operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + reg);
1247
+
1248
+ ci->code += sizeof(int8_t);
1249
+ break;
1250
+ case OT_YMM:
1251
+ if (vrex & PREFIX_EX_R) reg += EX_GPR_BASE;
1252
+ operands_set_tsi(op, O_REG, 256, AVXREGS_BASE + reg);
1253
+ break;
1254
+ case OT_VYMM:
1255
+ operands_set_tsi(op, O_REG, 256, AVXREGS_BASE + vexV);
1256
+ break;
1257
+ case OT_VYXMM:
1258
+ if (ps->vrex & PREFIX_EX_L) operands_set_tsi(op, O_REG, 256, AVXREGS_BASE + vexV);
1259
+ else operands_set_tsi(op, O_REG, 128, SSEREGS_BASE + vexV);
1260
+ break;
1261
+ case OT_WREG32_64:
1262
+ if (vrex & PREFIX_EX_R) reg += EX_GPR_BASE;
1263
+ if (ps->vrex & PREFIX_EX_W) operands_set_tsi(op, O_REG, 64, REGS64_BASE + reg);
1264
+ else operands_set_tsi(op, O_REG, 32, REGS32_BASE + reg);
1265
+ break;
1266
+ default: return FALSE;
1267
+ }
1268
+
1269
+ return TRUE;
1270
+ }