-
Notifications
You must be signed in to change notification settings - Fork 7
/
function.cpp
640 lines (524 loc) · 27.2 KB
/
function.cpp
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
#include "function.h"
#include <Zydis/Zydis.h>
#include <inttypes.h>
#include <string.h>
#include <sstream>
#include <Zycore/Format.h>
namespace
{
uint32_t get_le32(const uint8_t *data)
{
return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
}
// Copy of the disassmble function without any formatting.
static ZyanStatus UnasmDisassembleNoFormat(ZydisMachineMode machine_mode, ZyanU64 runtime_address, const void *buffer,
ZyanUSize length, ZydisDisassembledInstruction *instruction)
{
if (!buffer || !instruction) {
return ZYAN_STATUS_INVALID_ARGUMENT;
}
memset(instruction, 0, sizeof(*instruction));
instruction->runtime_address = runtime_address;
// Derive the stack width from the address width.
ZydisStackWidth stack_width;
switch (machine_mode) {
case ZYDIS_MACHINE_MODE_LONG_64:
stack_width = ZYDIS_STACK_WIDTH_64;
break;
case ZYDIS_MACHINE_MODE_LONG_COMPAT_32:
case ZYDIS_MACHINE_MODE_LEGACY_32:
stack_width = ZYDIS_STACK_WIDTH_32;
break;
case ZYDIS_MACHINE_MODE_LONG_COMPAT_16:
case ZYDIS_MACHINE_MODE_LEGACY_16:
case ZYDIS_MACHINE_MODE_REAL_16:
stack_width = ZYDIS_STACK_WIDTH_16;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZydisDecoder decoder;
ZYAN_CHECK(ZydisDecoderInit(&decoder, machine_mode, stack_width));
ZydisDecoderContext ctx;
ZYAN_CHECK(ZydisDecoderDecodeInstruction(&decoder, &ctx, buffer, length, &instruction->info));
ZYAN_CHECK(ZydisDecoderDecodeOperands(
&decoder, &ctx, &instruction->info, instruction->operands, instruction->info.operand_count));
return ZYAN_STATUS_SUCCESS;
}
ZydisFormatterFunc default_print_address_absolute;
static ZyanStatus UnasmFormatterPrintAddressAbsolute(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context)
{
unassemblize::Function *func = static_cast<unassemblize::Function *>(context->user_data);
uint64_t address;
ZYAN_CHECK(ZydisCalcAbsoluteAddress(context->instruction, context->operand, context->runtime_address, &address));
char hex_buff[32];
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
auto it = func->labels().find(address);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
} else if (address >= func->section_address() && address <= func->section_end()) {
// Probably a function if the address is in the current section.
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "sub_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "sub_%" PRIx64, address);
} else if (address >= func->executable().base_address() && address <= func->executable().end_address()) {
// Data is in another section?
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "off_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "off_%" PRIx64, address);
}
return default_print_address_absolute(formatter, buffer, context);
}
ZydisFormatterFunc default_print_address_relative;
static ZyanStatus UnasmFormatterPrintAddressRelative(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context)
{
unassemblize::Function *func = static_cast<unassemblize::Function *>(context->user_data);
uint64_t address;
ZYAN_CHECK(ZydisCalcAbsoluteAddress(context->instruction, context->operand, context->runtime_address, &address));
char hex_buff[32];
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
auto it = func->labels().find(address);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
} else if (address >= func->section_address() && address <= func->section_end()) {
// Probably a function if the address is in the current section.
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "sub_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "sub_%" PRIx64, address);
} else if (address >= func->executable().base_address() && address <= func->executable().end_address()) {
// Data if in another section?
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "off_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "off_%" PRIx64, address);
}
return default_print_address_relative(formatter, buffer, context);
}
ZydisFormatterFunc default_print_immediate;
static ZyanStatus UnasmFormatterPrintIMM(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context)
{
unassemblize::Function *func = static_cast<unassemblize::Function *>(context->user_data);
uint64_t address = context->operand->imm.value.u;
char hex_buff[32];
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
auto it = func->labels().find(address);
return ZyanStringAppendFormat(string, "offset %s", symbol.name.c_str());
} else if (address >= func->section_address() && address <= func->section_end()) {
// Probably a function if the address is in the current section.
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "offset %s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "offset sub_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "offset sub_%" PRIx64, address);
} else if (address >= func->executable().base_address() && address <= (func->executable().end_address())) {
// Data if in another section?
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "offset %s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "offset off_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "offset off_%" PRIx64, address);
}
return default_print_immediate(formatter, buffer, context);
}
ZydisFormatterFunc default_print_displacement;
static ZyanStatus UnasmFormatterPrintDISP(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context)
{
unassemblize::Function *func = static_cast<unassemblize::Function *>(context->user_data);
uint64_t address = context->operand->mem.disp.value;
char hex_buff[32];
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
auto it = func->labels().find(address);
return ZyanStringAppendFormat(string, "+%s", symbol.name.c_str());
} else if (address >= func->section_address() && address <= func->section_end()) {
// Probably a function if the address is in the current section.
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_nearest_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
if (symbol.value == address) {
return ZyanStringAppendFormat(string, "+%s", symbol.name.c_str());
} else {
uint64_t diff = address - symbol.value; // value should always be lower than requested address.
return ZyanStringAppendFormat(string, "+%s+0x%" PRIx64, symbol.name.c_str(), diff);
}
}
snprintf(hex_buff, sizeof(hex_buff), "sub_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "+sub_%" PRIx64, address);
} else if (address >= func->executable().base_address() && address <= (func->executable().end_address())) {
// Data if in another section?
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_nearest_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
if (symbol.value == address) {
return ZyanStringAppendFormat(string, "+%s", symbol.name.c_str());
} else {
uint64_t diff = address - symbol.value; // value should always be lower than requested address.
return ZyanStringAppendFormat(string, "+%s+0x%" PRIx64, symbol.name.c_str(), diff);
}
}
snprintf(hex_buff, sizeof(hex_buff), "off_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "+off_%" PRIx64, address);
}
return default_print_displacement(formatter, buffer, context);
}
ZydisFormatterFunc default_format_operand_ptr;
static ZyanStatus UnasmFormatterFormatOperandPTR(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context)
{
unassemblize::Function *func = static_cast<unassemblize::Function *>(context->user_data);
uint64_t address = context->operand->ptr.offset;
char hex_buff[32];
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
auto it = func->labels().find(address);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
} else if (address >= func->section_address() && address <= func->section_end()) {
// Probably a function if the address is in the current section.
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "sub_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "sub_%" PRIx64, address);
} else if (address >= func->executable().base_address() && address <= func->executable().end_address()) {
// Data if in another section?
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "%s", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "unk_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "unk_%" PRIx64, address);
}
return default_format_operand_ptr(formatter, buffer, context);
}
ZydisFormatterFunc default_format_operand_mem;
static ZyanStatus UnasmFormatterFormatOperandMEM(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context)
{
unassemblize::Function *func = static_cast<unassemblize::Function *>(context->user_data);
uint64_t address = context->operand->mem.disp.value;
char hex_buff[32];
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if ((context->operand->mem.type == ZYDIS_MEMOP_TYPE_MEM) || (context->operand->mem.type == ZYDIS_MEMOP_TYPE_VSIB)) {
ZYAN_CHECK(formatter->func_print_typecast(formatter, buffer, context));
}
ZYAN_CHECK(formatter->func_print_segment(formatter, buffer, context));
if (!symbol.name.empty()) {
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
auto it = func->labels().find(address);
return ZyanStringAppendFormat(string, "[%s]", symbol.name.c_str());
} else if (address >= func->section_address() && address <= func->section_end()) {
// Probably a function if the address is in the current section.
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "[%s]", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "sub_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "[sub_%" PRIx64 "]", address);
} else if (address >= func->executable().base_address() && address <= func->executable().end_address()) {
// Data if in another section?
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, ZYDIS_TOKEN_SYMBOL));
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
const unassemblize::Executable::Symbol &symbol = func->executable().get_symbol(address);
if (!symbol.name.empty()) {
func->add_dependency(symbol.name);
return ZyanStringAppendFormat(string, "[%s]", symbol.name.c_str());
}
snprintf(hex_buff, sizeof(hex_buff), "unk_%" PRIx64, address);
func->add_dependency(hex_buff);
return ZyanStringAppendFormat(string, "[unk_%" PRIx64 "]", address);
}
return default_format_operand_mem(formatter, buffer, context);
}
ZydisFormatterRegisterFunc default_format_print_reg;
static ZyanStatus UnasmFormatterFormatPrintRegister(
const ZydisFormatter *formatter, ZydisFormatterBuffer *buffer, ZydisFormatterContext *context, ZydisRegister reg)
{
// Copied from internal FormatterBase.h
#define ZYDIS_BUFFER_APPEND_TOKEN(buffer, type) \
if ((buffer)->is_token_list) { \
ZYAN_CHECK(ZydisFormatterBufferAppend(buffer, type)); \
}
if (reg >= ZYDIS_REGISTER_ST0 && reg <= ZYDIS_REGISTER_ST7) {
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_REGISTER);
ZyanString *string;
ZYAN_CHECK(ZydisFormatterBufferGetString(buffer, &string));
return ZyanStringAppendFormat(string, "st(%d)", reg - 69);
}
return default_format_print_reg(formatter, buffer, context, reg);
}
static ZyanStatus UnasmDisassembleCustom(ZydisMachineMode machine_mode, ZyanU64 runtime_address, const void *buffer,
ZyanUSize length, ZydisDisassembledInstruction *instruction, void *user_data, ZydisFormatterStyle style)
{
if (!buffer || !instruction) {
return ZYAN_STATUS_INVALID_ARGUMENT;
}
memset(instruction, 0, sizeof(*instruction));
instruction->runtime_address = runtime_address;
// Derive the stack width from the address width.
ZydisStackWidth stack_width;
switch (machine_mode) {
case ZYDIS_MACHINE_MODE_LONG_64:
stack_width = ZYDIS_STACK_WIDTH_64;
break;
case ZYDIS_MACHINE_MODE_LONG_COMPAT_32:
case ZYDIS_MACHINE_MODE_LEGACY_32:
stack_width = ZYDIS_STACK_WIDTH_32;
break;
case ZYDIS_MACHINE_MODE_LONG_COMPAT_16:
case ZYDIS_MACHINE_MODE_LEGACY_16:
case ZYDIS_MACHINE_MODE_REAL_16:
stack_width = ZYDIS_STACK_WIDTH_16;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZydisDecoder decoder;
ZYAN_CHECK(ZydisDecoderInit(&decoder, machine_mode, stack_width));
ZydisDecoderContext ctx;
ZYAN_CHECK(ZydisDecoderDecodeInstruction(&decoder, &ctx, buffer, length, &instruction->info));
ZYAN_CHECK(ZydisDecoderDecodeOperands(
&decoder, &ctx, &instruction->info, instruction->operands, instruction->info.operand_count));
ZydisFormatter formatter;
ZYAN_CHECK(ZydisFormatterInit(&formatter, style));
ZydisFormatterSetProperty(&formatter, ZYDIS_FORMATTER_PROP_FORCE_SIZE, ZYAN_TRUE);
default_print_address_absolute = (ZydisFormatterFunc)&UnasmFormatterPrintAddressAbsolute;
ZydisFormatterSetHook(
&formatter, ZYDIS_FORMATTER_FUNC_PRINT_ADDRESS_ABS, (const void **)&default_print_address_absolute);
default_print_immediate = (ZydisFormatterFunc)&UnasmFormatterPrintIMM;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_FUNC_PRINT_IMM, (const void **)&default_print_immediate);
default_print_address_relative = (ZydisFormatterFunc)&UnasmFormatterPrintAddressRelative;
ZydisFormatterSetHook(
&formatter, ZYDIS_FORMATTER_FUNC_PRINT_ADDRESS_REL, (const void **)&default_print_address_relative);
default_print_displacement = (ZydisFormatterFunc)&UnasmFormatterPrintDISP;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_FUNC_PRINT_DISP, (const void **)&default_print_displacement);
default_format_operand_ptr = (ZydisFormatterFunc)&UnasmFormatterFormatOperandPTR;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_FUNC_FORMAT_OPERAND_PTR, (const void **)&default_format_operand_ptr);
default_format_operand_mem = (ZydisFormatterFunc)&UnasmFormatterFormatOperandMEM;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_FUNC_FORMAT_OPERAND_MEM, (const void **)&default_format_operand_mem);
default_format_print_reg = (ZydisFormatterRegisterFunc)&UnasmFormatterFormatPrintRegister;
ZydisFormatterSetHook(&formatter, ZYDIS_FORMATTER_FUNC_PRINT_REGISTER, (const void **)&default_format_print_reg);
ZYAN_CHECK(ZydisFormatterFormatInstruction(&formatter,
&instruction->info,
instruction->operands,
instruction->info.operand_count_visible,
instruction->text,
sizeof(instruction->text),
runtime_address,
user_data));
return ZYAN_STATUS_SUCCESS;
}
} // namespace
void unassemblize::Function::disassemble(AsmFormat fmt)
{
if (m_executable.section_size(m_section.c_str()) == 0) {
return;
}
static bool in_jump_table;
ZyanUSize offset = m_startAddress - m_executable.section_address(m_section.c_str());
uint64_t runtime_address = m_startAddress;
ZyanUSize end_offset = m_endAddress - m_executable.section_address(m_section.c_str());
ZydisDisassembledInstruction instruction;
in_jump_table = false;
// Loop through function once to identify all jumps to local labels and create them.
while (ZYAN_SUCCESS(UnasmDisassembleNoFormat(ZYDIS_MACHINE_MODE_LEGACY_32,
runtime_address,
m_executable.section_data(m_section.c_str()) + offset,
96,
&instruction))
&& offset <= end_offset) {
uint64_t address;
if (instruction.info.raw.imm->is_relative) {
ZydisCalcAbsoluteAddress(&instruction.info, instruction.operands, runtime_address, &address);
if (address >= m_startAddress && address <= m_endAddress && m_labels.find(address) == m_labels.end()) {
std::stringstream stream;
stream << std::hex << address;
m_labels[address] = std::string("loc_") + stream.str();
m_executable.add_symbol(m_labels[address].c_str(), address);
}
}
offset += instruction.info.length;
runtime_address += instruction.info.length;
// If instruction is a nop or jmp, could be at an inline jump table.
if (instruction.info.mnemonic == ZYDIS_MNEMONIC_NOP || instruction.info.mnemonic == ZYDIS_MNEMONIC_JMP) {
uint64_t next_int = get_le32(m_executable.section_data(m_section.c_str()) + offset);
bool in_jump_table = false;
// Naive jump table detection attempt uint32_t representation happens to be in function address space.
while (next_int >= m_startAddress && next_int <= m_endAddress) {
// If this is first entry of jump table, create label to jump to.
if (!in_jump_table) {
if (runtime_address >= m_startAddress && runtime_address <= m_endAddress
&& m_labels.find(runtime_address) == m_labels.end()) {
std::stringstream stream;
stream << std::hex << runtime_address;
m_labels[runtime_address] = std::string("loc_") + stream.str();
m_executable.add_symbol(m_labels[runtime_address].c_str(), runtime_address);
}
in_jump_table = true;
}
if (m_labels.find(next_int) == m_labels.end()) {
std::stringstream stream;
stream << std::hex << next_int;
m_labels[next_int] = std::string("loc_") + stream.str();
m_executable.add_symbol(m_labels[next_int].c_str(), next_int);
}
offset += sizeof(uint32_t);
runtime_address += sizeof(uint32_t);
next_int = get_le32(m_executable.section_data(m_section.c_str()) + offset);
}
}
}
offset = m_startAddress - m_executable.section_address(m_section.c_str());
runtime_address = m_startAddress;
in_jump_table = false;
ZydisFormatterStyle style;
switch (fmt) {
case FORMAT_MASM:
style = ZYDIS_FORMATTER_STYLE_INTEL_MASM;
break;
case FORMAT_AGAS:
style = ZYDIS_FORMATTER_STYLE_ATT;
break;
case FORMAT_IGAS:
case FORMAT_DEFAULT:
style = ZYDIS_FORMATTER_STYLE_INTEL;
break;
}
while (ZYAN_SUCCESS(UnasmDisassembleCustom(ZYDIS_MACHINE_MODE_LEGACY_32,
runtime_address,
m_executable.section_data(m_section.c_str()) + offset,
96,
&instruction,
this,
style))
&& offset <= end_offset) {
if (m_labels.find(runtime_address) != m_labels.end()) {
m_dissassembly += m_labels[runtime_address];
m_dissassembly += ":\n";
}
m_dissassembly += " ";
m_dissassembly += instruction.text;
m_dissassembly += '\n';
offset += instruction.info.length;
runtime_address += instruction.info.length;
// If instruction is a nop or jmp, could be at an inline jump table.
if (instruction.info.mnemonic == ZYDIS_MNEMONIC_NOP || instruction.info.mnemonic == ZYDIS_MNEMONIC_JMP) {
uint64_t next_int = get_le32(m_executable.section_data(m_section.c_str()) + offset);
bool in_jump_table = false;
// Naive jump table detection attempt uint32_t representation happens to be in function address space.
while (next_int >= m_startAddress && next_int <= m_endAddress) {
// If this is first entry of jump table, create label to jump to.
if (!in_jump_table) {
const unassemblize::Executable::Symbol &symbol = m_executable.get_symbol(runtime_address);
if (!symbol.name.empty()) {
m_dissassembly += symbol.name;
m_dissassembly += ":\n";
}
in_jump_table = true;
}
const unassemblize::Executable::Symbol &symbol = m_executable.get_symbol(next_int);
if (!symbol.name.empty()) {
if (fmt == FORMAT_MASM) {
m_dissassembly += " DWORD ";
} else {
m_dissassembly += " .int ";
}
m_dissassembly += symbol.name;
m_dissassembly += "\n";
}
offset += sizeof(uint32_t);
runtime_address += sizeof(uint32_t);
next_int = get_le32(m_executable.section_data(m_section.c_str()) + offset);
}
}
}
}