-
Notifications
You must be signed in to change notification settings - Fork 0
/
modrm.c
216 lines (190 loc) · 6.2 KB
/
modrm.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "modrm.h"
#include "prefix.h"
#include "registers.h"
#include "immediates.h"
#include "sib.h"
static char * get_effective_addr_16(int index){
char * strings[] = {"bx + si", "bx + di", "bp + si", "bp + di", "si", "di", "bp", "bx"};
return strings[index];
}
void check_modrm_reg(unsigned char * inst, int operand_size, int address_size, struct prefixes * prfx){
int modrm = inst[0];
int reg_num = (modrm & REG) >> 3;
char * reg;
if(prfx->REX != 0){
// REX bits need to be in 4th position from right
u_int8_t rex_r = (prfx->REX & REX_R) << 1;
// Get extension bit from rex prefix
reg_num = reg_num| rex_r;
}
reg = get_register(reg_num, operand_size, prfx);
}
// Should not be called when instruction has REX.W bit set
void modrm_rm_16(unsigned char * inst, int operand_size, int address_size){
int modrm = inst[0];
char * rm;
u_int8_t mod = (modrm & MODRM) >> 6;
u_int8_t modrm_rm = (modrm & RM);
switch(mod){
case 0:
switch(modrm_rm){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("memory access [%s]\n", get_effective_addr_16(modrm_rm));
break;
case 6:
printf("disp 0x%lx\n", get_displacement(&inst[1], operand_size, 0));
break;
case 7:
printf("memory access [%s]\n", get_effective_addr_16(modrm_rm));
break;
default:
assert(0);
}
break;
case 1:
switch(modrm_rm){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
printf("memory access [%s", get_effective_addr_16(modrm_rm));
get_ones_comp_disp(&inst[1], 8, 0);
break;
default:
assert(0);
}
break;
case 2:
switch(modrm_rm){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
printf("memory access [%s", get_effective_addr_16(modrm_rm));
get_ones_comp_disp(&inst[1], operand_size, 0);
break;
default:
assert(0);
}
break;
case 3:
// both operands are registers
rm = get_register(modrm_rm, operand_size, 0);
break;
default:
assert(0);
}
return;
}
void check_modrm_rm(unsigned char * inst, int operand_size, int address_size, struct prefixes * prfx){
if(address_size == 16){
assert(prfx->REX & REX_W == 0);
return modrm_rm_16(inst, operand_size, address_size);
}
int modrm = inst[0];
char * rm;
u_int8_t mod = (modrm & MODRM) >> 6;
u_int8_t modrm_rm = (modrm & RM);
if(prfx->REX != 0){
// Get extension bit from rex prefix (even if REX.W not set, registers can still be extended in 64 bit mode
u_int8_t rex_b = (prfx->REX & REX_B) << 3;
modrm_rm = modrm_rm | rex_b;
}
switch(mod){
case 0:
switch(modrm_rm & 0x7){
case 0:
case 1:
case 2:
case 3:
printf("memory access [%s]\n", get_register(modrm_rm, operand_size, prfx));
break;
case 4:
printf("memory access [");
check_sib(&inst[1], operand_size, prfx);
printf("]");
break;
case 5:
printf("memory access [rip");
get_ones_comp_disp(&inst[1], 32, 0);
break;
case 6:
case 7:
printf("memory access [%s]\n", get_register(modrm_rm, operand_size, prfx));
break;
default:
assert(0);
}
break;
case 1:
switch(modrm_rm & 0x7){
case 0:
case 1:
case 2:
case 3:
printf("memory access [%s", get_register(modrm_rm, operand_size, prfx));
get_ones_comp_disp(&inst[1], 8, 0);
break;
case 4:
printf("memory access [");
check_sib(&inst[1], operand_size, prfx);
get_ones_comp_disp(&inst[2], 8, 0);
break;
case 5:
case 6:
case 7:
printf("memory access [%s", get_register(modrm_rm, operand_size, prfx));
get_ones_comp_disp(&inst[1], 8, 0);
break;
default:
assert(0);
}
break;
case 2:
switch(modrm_rm & 0x7){
case 0:
case 1:
case 2:
case 3:
printf("memory access [%s", get_register(modrm_rm, operand_size, prfx));
get_ones_comp_disp(&inst[1], 32, 0);
break;
case 4:
printf("memory access [");
check_sib(&inst[1], operand_size, prfx);
get_ones_comp_disp(&inst[2], 32, 0);
break;
case 5:
case 6:
case 7:
printf("memory access [%s", get_register(modrm_rm, operand_size, prfx));
get_ones_comp_disp(&inst[1], 32, 0);
break;
default:
assert(0);
}
break;
case 3:
rm = get_register(modrm_rm, operand_size, prfx);
break;
default:
assert(0);
}
return;
}