-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstring.c
267 lines (228 loc) · 6.13 KB
/
string.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
#define DEFINE_OPCODES_STR
#include "minx86dec/opcodes_str.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
const char *regnames[9][32+1] = {
/* 0 */ { NULL },
/* 1 */ { "AL", "CL", "DL", "BL", "AH", "CH", "DH", "BH",
"R8B", "R9B", "R10B", "R11B", "R12B", "R13B", "R14B", "R15B",
"?", "?", "?", "?", "SPL", "BPL", "SIL", "DIL", },
/* 2 */ { "AX", "CX", "DX", "BX", "SP", "BP", "SI", "DI",
"R8W", "R9W", "R10W", "R11W", "R12W", "R13W", "R14W", "R15W" },
/* 3 */ { NULL },
/* 4 */ { "EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI",
"R8D", "R9D", "R10D", "R11D", "R12D", "R13D", "R14D", "R15D" },
/* 5 */ { NULL },
/* 6 */ { NULL },
/* 7 */ { NULL },
/* 8 */ { "RAX", "RCX", "RDX", "RBX", "RSP", "RBP", "RSI", "RDI",
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15",
"RIP" }
};
const char *sregnames[8] = {
"ES", "CS", "SS", "DS",
"FS", "GS", "", ""
};
const char *memptrsizes[17] = {
"", /* 0 */
"(BYTE*)", /* 1 */
"(WORD*)", /* 2 */
"",
"(DWORD*)", /* 4 */
"",
"(3WORD*)", /* 6 */
"",
"(QWORD*)", /* 8 */
"",
"(TWORD*)", /* 10 */
"",
"",
"",
"",
"",
"(DQWORD*)" /* 16 */
};
static void minx86dec_memref_print(struct minx86dec_argv *a,char *output) {
char doop=0;
if (a->segment >= 0)
output += sprintf(output,"%s:",sregnames[a->segment]);
else if (a->segment == MX86_SEG_IMM)
output += sprintf(output,"0x%04X:",a->segval);
#if 1 /* DEBUG: there must be a segment register reference, or else */
else {
fprintf(stderr,"Memory reference without segment\n");
abort();
}
#endif
if (a->size >= 0 && a->size <= 16)
output += sprintf(output,"%s[",memptrsizes[a->size]);
else
output += sprintf(output,"(BYTE[%u])[",a->size);
if (a->memregs) {
int c;
/* assume [0] is valid */
if (a->scalar)
output += sprintf(output,"%s*%d",regnames[a->memregsz][a->memreg[0]],1<<a->scalar);
else
output += sprintf(output,"%s",regnames[a->memregsz][a->memreg[0]]);
doop++;
for (c=1;c < a->memregs;c++) {
*output++ = '+';
output += sprintf(output,"%s",regnames[a->memregsz][a->memreg[c]]);
doop++;
}
}
if (a->memref_base || a->memregs == 0) {
uint32_t pofs = a->memref_base;
char sgn = '+';
if ((pofs & 0x80000000) && a->memregs != 0) {
pofs = (uint32_t)(-pofs);
sgn = '-';
}
if (doop++ || sgn == '-') *output++ = sgn;
output += sprintf(output,"0x%X",pofs);
}
*output++ = ']';
*output = 0;
}
void minx86dec_regprint(struct minx86dec_argv *a,char *output) {
const char *x;
output[0] = 0;
switch (a->regtype) {
case MX86_RT_NONE:
minx86dec_memref_print(a,output);
break;
case MX86_RT_IMM:
if (a->segment == MX86_SEG_IMM)
output += sprintf(output,"0x%04X:",a->segval);
sprintf(output,"0x%X",a->value);
break;
case MX86_RT_REG:
if (a->size >= 0 && a->size <= 8 && a->reg >= 0 && a->reg <= 32)
x = regnames[a->size][a->reg];
else
x = NULL;
#if 1 /* DEBUG */
if (x == NULL) {
fprintf(stderr,"BUG: MX86_RT_REG with size=%d reg=%d which does not exist\n",
a->size,a->reg);
abort();
}
#endif
strcpy(output,x != NULL ? x : "(null)");
break;
case MX86_RT_SREG:
strcpy(output,sregnames[a->reg]);
break;
case MX86_RT_ST:
sprintf(output,"st(%d)",a->reg);
break;
case MX86_RT_MMX:
sprintf(output,"MM%d",a->reg);
break;
case MX86_RT_SSE:
if (a->size >= 32) sprintf(output,"YMM%d",a->reg);
else sprintf(output,"XMM%d",a->reg);
break;
case MX86_RT_CR:
sprintf(output,"CR%d",a->reg);
break;
case MX86_RT_DR:
sprintf(output,"DR%d",a->reg);
break;
case MX86_RT_TR:
sprintf(output,"TR%d",a->reg);
break;
}
}
static void minx86dec_memref_print_x64(struct minx86dec_argv_x64 *a,char *output) {
char doop=0;
if (a->segment >= 0)
output += sprintf(output,"%s:",sregnames[a->segment]);
else if (a->segment == MX86_SEG_IMM)
output += sprintf(output,"0x%04X:",a->segval);
if (a->size >= 0 && a->size <= 16)
output += sprintf(output,"%s[",memptrsizes[a->size]);
else
output += sprintf(output,"(BYTE[%u])[",a->size);
if (a->memregs) {
int c;
/* assume [0] is valid */
if (a->scalar)
output += sprintf(output,"%s*%d",regnames[a->memregsz][a->memreg[0]],1<<a->scalar);
else
output += sprintf(output,"%s",regnames[a->memregsz][a->memreg[0]]);
doop++;
for (c=1;c < a->memregs;c++) {
*output++ = '+';
output += sprintf(output,"%s",regnames[a->memregsz][a->memreg[c]]);
doop++;
}
}
if (a->memref_base || a->memregs == 0) {
uint64_t pofs = a->memref_base;
char sgn = '+';
if ((pofs & 0x8000000000000000ULL) && a->memregs != 0) {
pofs = (uint64_t)(-pofs);
sgn = '-';
}
if (doop++ || sgn == '-') *output++ = sgn;
output += sprintf(output,"0x%llX",(unsigned long long)pofs);
}
*output++ = ']';
*output = 0;
}
void minx86dec_regprint_x64(struct minx86dec_argv_x64 *a,char *output) {
const char *x;
output[0] = 0;
switch (a->regtype) {
case MX86_RT_NONE:
minx86dec_memref_print_x64(a,output);
break;
case MX86_RT_IMM:
if (a->segment == MX86_SEG_IMM)
output += sprintf(output,"0x%04X:",a->segval);
sprintf(output,"0x%llX",(unsigned long long)a->value);
break;
case MX86_RT_REG:
if (a->size >= 0 && a->size <= 8 && a->reg >= 0 && a->reg <= 32)
x = regnames[a->size][a->reg];
else
x = NULL;
if (x == NULL) {
fprintf(stderr,"BUG: MX86_RT_REG with size=%d reg=%d which does not exist\n",a->size,a->reg);
abort();
}
strcpy(output,x != NULL ? x : "(null)");
break;
case MX86_RT_SREG:
strcpy(output,sregnames[a->reg]);
break;
case MX86_RT_ST:
sprintf(output,"st(%d)",a->reg);
break;
case MX86_RT_MMX:
sprintf(output,"MM%d",a->reg);
break;
case MX86_RT_SSE:
if (a->size >= 32) sprintf(output,"YMM%d",a->reg);
else sprintf(output,"XMM%d",a->reg);
break;
case MX86_RT_CR:
sprintf(output,"CR%d",a->reg);
break;
case MX86_RT_DR:
sprintf(output,"DR%d",a->reg);
break;
case MX86_RT_TR:
sprintf(output,"TR%d",a->reg);
break;
}
}
void minx86dec_init_instruction(struct minx86dec_instruction *i) {
memset(i,0,sizeof(*i));
}
void minx86dec_init_instruction_x64(struct minx86dec_instruction_x64 *i) {
memset(i,0,sizeof(*i));
}