分析软件缺陷,有时候需要把elf文件反汇编为汇编代码,然后分析汇编代码,需要把汇编代码和c语言代码对应起来。
如果一个函数比较长,那么人工把汇编代码和c语言代码对应起来,费时费力。怎么快速把汇编代码和c语言代码对应起来?
首先,编译程序的时候使用选项“-g”生成调试信息。
gcc test.c -o test.elf -g执行“readelf -s test.elf”,可以看到多个名称以“.debug_”开头的节,如下。
$ readelf -s test.elfthere are 35 section headers, starting at offset 0x1508:section headers: [nr] name type address offset size entsize flags link info align ... [27] .debug_aranges progbits 0000000000000000 0000106b 0000000000000030 0000000000000000 0 0 1 [28] .debug_info progbits 0000000000000000 0000109b 0000000000000173 0000000000000000 0 0 1 [29] .debug_abbrev progbits 0000000000000000 0000120e 0000000000000096 0000000000000000 0 0 1 [30] .debug_line progbits 0000000000000000 000012a4 0000000000000048 0000000000000000 0 0 1 [31] .debug_str progbits 0000000000000000 000012ec 00000000000000d2 0000000000000001 ms 0 0 1 ...key to flags: w (write), a (alloc), x (execute), m (merge), s (strings), l (large) i (info), l (link order), g (group), t (tls), e (exclude), x (unknown) o (extra os processing required) o (os specific), p (processor specific)使用objdump工具把elf文件反汇编为汇编代码的时候,使用选项“-s”把源代码和汇编代码混合,并且输出到一个文本文件。
objdump -s test.elf > test_asm.txt打开文本文件,下面是一个例子。
000000000040056a :void func2(int *ptr, int oldval, int newval){ 40056a: 55 push %rbp 40056b: 48 89 e5 mov %rsp,%rbp 40056e: 48 83 ec 20 sub $0x20,%rsp 400572: 48 89 7d e8 mov %rdi,-0x18(%rbp) 400576: 89 75 e4 mov %esi,-0x1c(%rbp) 400579: 89 55 e0 mov %edx,-0x20(%rbp) bool b; b = __atomic_compare_exchange_n(ptr, &oldval, newval, 40057c: 8b 4d e0 mov -0x20(%rbp),%ecx 40057f: 48 8b 75 e8 mov -0x18(%rbp),%rsi 400583: 48 8d 55 e4 lea -0x1c(%rbp),%rdx 400587: 8b 02 mov (%rdx),%eax 400589: f0 0f b1 0e lock cmpxchg %ecx,(%rsi) 40058d: 89 c1 mov %eax,%ecx 40058f: 0f 94 c0 sete %al 400592: 84 c0 test %al,%al 400594: 75 02 jne 400598 400596: 89 0a mov %ecx,(%rdx) 400598: 88 45 ff mov %al,-0x1(%rbp) false, __atomic_acquire, __atomic_relaxed); printf(b=%d\\n, b); 40059b: 0f b6 45 ff movzbl -0x1(%rbp),%eax 40059f: 89 c6 mov %eax,%esi 4005a1: bf 54 06 40 00 mov $0x400654,%edi 4005a6: b8 00 00 00 00 mov $0x0,%eax 4005ab: e8 60 fe ff ff callq 400410 } 4005b0: c9 leaveq 4005b1: c3 retq
为何盈利能力持续大幅下降?海信电器给出解释
华为与京东方深度合作 自主柔性OLED屏供应100万块
车载网关产品解析(附:车载网关详细应用案例及部署流程)
石墨烯芯片能否弯道超车?
不同电压和功率等级的三菱电机SiC功率器件介绍
怎么快速把汇编代码和C语言代码对应起来呢?
【回顾往年CES】艾洛维在展会发布两款4K激光电视,带来超强临感体验
机器人在工业自动化领域中的应用
苹果WWDC发布会将至:定档今晚凌晨1点
基于Cortex-M3的STM32微控制器处理先进电机控制方法
无人驾驶等于自动驾驶吗
三星Galaxy Watch官方渲染图曝光,与Gear S3 Classic差异不大
联电首款悬架阻尼控制电磁阀顺利下线
SA662GrA│SA662GrB│SA662GrC钢板(舞钢执行美标标准)
采用PowerFill外延硅工艺的电源器件
如何应用PM2.5传感器技术对工厂有害颗粒物进行安全监测
让智能家居与智能社会更有实际意思的落地应用
晶丰明源IC产品BP系列LED驱动IC
科学技术才是第一生产力,这一次中国又站在了世界的顶峰!
对于无线充电技术新能源汽车真的需要吗