在这里主要是提供了两个接口。disasm_armv7和disasm_thumb,前者主要是对armv7进行反汇编,该接口封装了darm提供给的darm_armv7_disasm。后者主要是对thumb和thumb2进行反汇编,该接口封装了darm提供给的darm_thumb_disasm和darm_thumb_disasm。
自己定义的接口声明如下:
int disasm_armv7(const uint8_t* g_buf, uint32_t vaddr, uint32_t offset, uint32_t size);
int disasm_thumb(const uint8_t* g_buf, uint32_t vaddr, uint32_t offset, uint32_t size);
const uint8_t* g_buf //这里主要是指获取的文件缓冲区buffer起始位置
uint32_t vaddr //这里是指映射到内存中的函数的起始位置
uint32_t offset //该函数在文件中的偏移
uint32_t size //函数的大小
具体函数实现如下:
int disasm_armv7(const uint8_t* g_buf, uint32_t vaddr, uint32_t offset, uint32_t size)
{
const uint32_t *code_32 = (const uint32_t *) &g_buf[offset];
darm_t d; darm_str_t str;
for (uint32_t idx = 0; idx < size / sizeof(uint32_t);
idx++, vaddr += sizeof(uint32_t)) {
//disasm armv7
if(darm_armv7_disasm(&d, code_32[idx]) < 0) {
printf("#%06x %08x (..)\n", vaddr, code_32[idx]);
continue;
}
//translate opcodes into strings
if(darm_str2(&d, &str, 1) < 0) {
printf("#%06x %08x (..)\n", vaddr, code_32[idx]);
continue;
}
printf("#%06x %08x %s\n", vaddr, code_32[idx], str.total);
}
return 0;
}
解析结果:armv7
#008d10 e59fc05c ldr r12, [pc, #92]
#008d14 e92d4800 push {r11,lr}
#008d18 e59f3058 ldr r3, [pc, #88]
#008d1c e28db004 add r11, sp, #4
#008d20 e24dd010 sub sp, sp, #16
#008d24 e08fc00c add r12, pc, r12
#008d28 e79c3003 ldr r3, [r12, r3]
#008d2c e50b3014 str r3, [r11, #-20]
int disasm_thumb(const uint8_t* g_buf, uint32_t vaddr, uint32_t offset, uint32_t size){
const uint16_t *code = (const uint16_t *) &g_buf[offset];
darm_t d; darm_str_t str;
for (uint32_t idx = 0; idx < size / sizeof(uint32_t);
idx++, vaddr += 2) {
if(darm_thumb_disasm(&d, *(code+idx)) < 0) {
//if darm_thumb_disasm() failed, then calling darm_thumb2_disasm() function
if(darm_thumb2_disasm(&d, code[idx], code[idx+1]) < 0){
continue;
}else{
if(darm_str2(&d, &str, 1) < 0) {
printf("#%06x %04x (..)\n", vaddr, code[idx]);
continue;
}
printf("#%06x %04x%04x %s\n", vaddr, code[idx+1], code[idx], str.total);
idx++;
vaddr += 2;
}
continue;
}
if(darm_str2(&d, &str, 1) < 0) {
printf("#%06x %04x (..)\n", vaddr, code[idx]);
continue;
}
printf("#%06x %04x %s\n", vaddr, code[idx], str.total);
}
}
解析结果: thumb和thumb2
#008da0 4601 mov r1, r0
#008da2 4808 ldr r0, [pc, #32]
#008da4 b508 push {r3,lr}
#008da6 4478 add r0, r0, pc
#008da8 eea2f7ff blx #+-700
#008dac 4806 ldr r0, [pc, #24]
#008dae 4478 add r0, r0, pc
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课