-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
130 lines (116 loc) · 3.02 KB
/
main.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
#include <stdio.h>
//#include <malloc.h>
#include <libvex.h>
#include <pyvex.h>
#include <main_globals.h>
#include <setjmp.h>
#include <string.h>
#include <stdlib.h>
#include "bin2vex.h"
#include "utils.h"
#define MAX_INST_BYTES 16
int BUFF_AREA;
VexArchInfo vai_guest;
VexGuestExtents vge;
VexTranslateArgs vta;
VexTranslateResult vtr;
VexAbiInfo vbi;
VexArch arch;
/*static*/
/*void log_bytes ( const HChar* bytes, SizeT nbytes )*/
/*{*/
/*fwrite ( bytes, 1, nbytes, stdout );*/
/*}*/
char *msg_buffer = NULL;
size_t msg_capacity = 0, msg_current_size = 0;
jmp_buf jumpout;
VexControl vc;
void dump_arch_info(VexArchInfo vai) {
printf("hwcaps = %d\n", vai.hwcaps);
}
int main(int argc, char** argv){
void (*init)();
IRSB (*lift)(VexArch guest,
VexArchInfo archinfo,
unsigned char *insn_start,
unsigned long long insn_addr,
unsigned int max_insns,
unsigned int max_bytes,
int opt_level,
int traceflags,
int allow_lookback);
IRSB *irsb;
uint32_t inst_num = 0;
if(argc <= 3) {
printf("usage: %s [x86|x64|ARM] <inst_binary_file> [inst_num]\n", argv[0]);
exit(0);
}
inst_num = atoi(argv[3]);
char* arch = argv[1];
printf("target arch is %s.\n", arch);
printf("decode first %d instructions.\n", inst_num);
//import bin_flow to data
char* bin_file = argv[2];
printf("load binary file: %s\n", bin_file);
size_t file_size;
uint8_t* inst_data = load_file_data(bin_file, &file_size);
if(inst_data != NULL) {
printf("Load binary file OK, file size = %d\n", file_size);
}
else {
printf("load binary file failed.\n");
exit(-1);
}
switch(arch[1])
{
case '8':
init_bin2vex(VexArchX86);
break;
case '6':
init_bin2vex(VexArchAMD64);
break;
case 'R':
case 'r':
init_bin2vex(VexArchARM);
break;
default:
printf("unsupported architecture.\n");
exit(0);
}
int64_t code_size = file_size;
uint64_t inst_addr = 0x400400;
int i = 0;
while(1) {
if(inst_num != 0 && i >= inst_num) break;
if(code_size <= 0) {
printf("all code decoded, break.\n");
break;
}
printf("\nInstruction %d: \n", i);
irsb = bin2vex(inst_data, inst_addr);
char* dis = disassemble_inst(inst_data, code_size, inst_addr, arch);
if(dis != NULL) printf(dis);
ppIRSB(irsb);
for(int j = 0; j < irsb->stmts_used; j ++) {
IRStmt* stmt = irsb->stmts[j];
if(stmt->tag == Ist_IMark) {
inst_data += stmt->Ist.IMark.len;
inst_addr += stmt->Ist.IMark.len;
code_size -= stmt->Ist.IMark.len;
}
//ppIRStmt(stmt);
//printf("\n");
}
printf("next instruction addr = %x\n", inst_addr);
i ++;
}
return 0;
}
void array_merge(unsigned char des[] , unsigned char src[] , int inx)
{
int i = 0;
for (;i<512;i++)
{
des[i+inx]=src[i];
}
}