Skip to content

Commit

Permalink
Add x86,x64,ARM options.
Browse files Browse the repository at this point in the history
  • Loading branch information
Helio404 committed Jul 16, 2018
1 parent b5aa7b1 commit 2549a7f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
26 changes: 23 additions & 3 deletions bin2vex.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,34 @@ void print_irsb(IRSB* irsb) {
ppIRSB(irsb);
}

char* disassemble_inst(const uint8_t* code, uint32_t code_size, uint64_t base_address){
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;

if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
return false;
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) {
Expand Down
2 changes: 1 addition & 1 deletion bin2vex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {

void init_bin2vex(VexArch arch);
IRSB* bin2vex(uint8_t* inst_data, uint64_t inst_addr) ;
char* disassemble_inst(const uint8_t* code, uint32_t code_size, uint64_t base_address);
char* disassemble_inst(const uint8_t* code, uint32_t code_size, uint64_t base_address, char* arch);
void print_irsb(IRSB* irsb);
#ifdef __cplusplus
}
Expand Down
33 changes: 24 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ int main(int argc, char** argv){

IRSB *irsb;
uint32_t inst_num = 0;
if(argc <= 1) {
printf("usage: %s <inst_binary_file> [inst_num]\n", argv[0]);
if(argc <= 3) {
printf("usage: %s [x86|x64|ARM] <inst_binary_file> [inst_num]\n", argv[0]);
exit(0);
}
if(argc >= 3) {
inst_num = atoi(argv[2]);
printf("decode first %d instructions.\n", inst_num);
}
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[1];
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);
Expand All @@ -70,8 +70,23 @@ int main(int argc, char** argv){
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);
}

init_bin2vex(VexArchAMD64);
int64_t code_size = file_size;
uint64_t inst_addr = 0x400400;
int i = 0;
Expand All @@ -83,7 +98,7 @@ int main(int argc, char** argv){
}
printf("\nInstruction %d: \n", i);
irsb = bin2vex(inst_data, inst_addr);
char* dis = disassemble_inst(inst_data, code_size, inst_addr);
char* dis = disassemble_inst(inst_data, code_size, inst_addr, arch);
if(dis != NULL) printf(dis);
ppIRSB(irsb);

Expand Down

0 comments on commit 2549a7f

Please sign in to comment.