-
Notifications
You must be signed in to change notification settings - Fork 5
/
bin2vex.c
76 lines (67 loc) · 1.96 KB
/
bin2vex.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
#include <stdio.h>
#include <libvex.h>
#include <pyvex.h>
#include <main_globals.h>
#include <stdint.h>
#include <capstone/capstone.h>
#include <stdlib.h>
#define MAX_INST_BYTES 16
static VexArchInfo vai_guest;
static VexArch arch_guest;
void init_bin2vex(VexArch arch) {
vex_init();
LibVEX_default_VexArchInfo(&vai_guest);
vai_guest.endness = 0x601;
vta.archinfo_host.hwcaps = 4064;
arch_guest = arch;
}
IRSB* bin2vex(uint8_t* inst_data, uint64_t inst_addr) {
//IRSB* irsb = vex_lift(arch_guest, vai_guest, inst_data, inst_addr, 1, MAX_INST_BYTES, 1, VEX_TRACE_FE|VEX_TRACE_OPT1|VEX_TRACE_INST|VEX_TRACE_OPT2|VEX_TRACE_ASM/*255 to trace all*/, 0);
IRSB* irsb = vex_lift(arch_guest, vai_guest, inst_data, inst_addr, 1, MAX_INST_BYTES, 1, 0, 0);
if(irsb == NULL){
fprintf(stderr, "vex_lift error.\n");
exit(-1);
}
return irsb;
}
void print_irsb(IRSB* irsb) {
ppIRSB(irsb);
}
char* disassemble_inst(const uint8_t* code, uint32_t code_size, uint64_t base_address, char* arch){
static char dis_str[1024];
char* ret = NULL;
csh handle;
cs_insn *insn = NULL;
switch(arch[1])
{
case '8':
if (cs_open(CS_ARCH_X86, CS_MODE_32, &handle) != CS_ERR_OK)
return false;
break;
case '6':
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
return false;
break;
case 'R':
case 'r':
if (cs_open(CS_ARCH_ARM, CS_MODE_ARM, &handle) != CS_ERR_OK)
return false;
break;
default:
printf("unsupported architecture.\n");
return false;
}
// if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
// return false;
int count = cs_disasm(handle, code, code_size, base_address, 1, &insn);
if (count > 0) {
sprintf(dis_str, "%lx \t%s\t%s\t\t\n", insn->address, insn->mnemonic, insn->op_str);
ret = dis_str;
cs_free(insn, 1);
}
else {
printf("ERROR: Failed to disassemble given code!\n");
}
cs_close(&handle);
return ret;
}