常用嵌入式Linux二进制调试工具(1)

http://www.top-e.org/jiaoshi/html/?162.html

Linux系统中有大量的工具可用于ELF文件的二进制调试,常用的工具在GNU binutils包中可以找到,注意你可能需要这些工具的x86版本和arm版本,以便在调试环境中能够调试x86 ELF文件和arm ELF文件——与交叉编译器arm-linux-gcc类似,我们需要所谓的“交叉调试工具”,你可以通过互联网下载别人已经编译好的crosstool,或者自己重新编译(configure时指--target=arm-linux)。

GNU binutils包在GNU的官方网站提供下载:http://www.gnu.org/software/binutils/,特别的,更多跟arm相关的信息和工具可以看看gnu arm网站:http://www.gnuarm.org/

我们将常用的ELF调试工具归纳介绍如下。由于这些工具的x86版本和arm版本使用起来基本没有区别,这里也不作区分。读者在使用的时候请根据使用对象的类型(用FILE命令查看)自行区分。

Ø       AR

用来建立、修改、提取静态库文件。静态库文件包含多个可重定位目标文件,其结构保证了可以恢复原始目标文件内容。比如:

$ gcc –c file1.c file2.c

$ ar rcs libxx.a file1.o file2.o

这里我们先用gcc编译得到file1.o file2.o两个目标文件,然后用ar命令生成静态库libxx.a

当你希望查看静态库中包含了哪些目标文件时,可以用选项-x解开静态库文件:

$ ar x libxx.a

 

Ø       NM

列出目标文件的符号表中定义的符号。常见的链接或者运行时发生的unresolved symbol类型的错误可以用NM来辅助调试。比如用NM结合GREP来查看变量或函数是否被定义或引用:

$ nm [xx.o, or yy.a, or zz.so] | grep [your symbol]

对于C++程序,可以使用选项-C来进行所谓的demangle——C++编译器一般会将变量名或函数名进行修饰(mangle),加上类信息、参数信息等,变成比较难以辨认的符号,而-C选项的demangle则可将其恢复为比较正常的符号。比如下面很简单的C++程序:

#include <iostream>

 

int main()

{

    std::cout<<"Hello World!"<<std::endl;

}

编译之后用nm来查看:

$ g++ -c hello.cpp

$ nm hello.o

00000094 t _GLOBAL__I_main

0000003e t _Z41__static_initialization_and_destruction_0ii

         U _ZNSolsEPFRSoS_E

         U _ZNSt8ios_base4InitC1Ev

         U _ZNSt8ios_base4InitD1Ev

         U _ZSt4cout

         U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_

00000000 b _ZSt8__ioinit

         U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc

         U __cxa_atexit

         U __dso_handle

         U __gxx_personality_v0

0000007c t __tcf_0

00000000 T main

这时这些mangle之后的C++符号是比较难以辨认的,如果使用nm –C进行demangle就好多了:

$ nm -C hello.o

00000094 t _GLOBAL__I_main

0000003e t __static_initialization_and_destruction_0(int, int)

         U std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))

         U std::ios_base::Init::Init[in-charge]()

         U std::ios_base::Init::~Init [in-charge]()

         U std::cout

         U std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)

00000000 b std::__ioinit

         U std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

         U __cxa_atexit

         U __dso_handle

         U __gxx_personality_v0

0000007c t __tcf_0

00000000 T main

-C选项在其他一些二进制调试工具中也有提供,使用C++开发的读者可以多加注意,毕竟demangle之后的符号可读性要强很多。

 

Ø       OBJDUMP

objdump是所有二进制工具之母,能够显示一个目标文件中所有的信息,通常我们用它来反汇编.text节中的二进制指令。

比如对上面的hello.o反汇编的结果如下:

# objdump -d hello.o

 

hello.o:     file format elf32-i386

 

Disassembly of section .text:

 

00000000 <main>:

   0:   55                      push   %ebp

   1:   89 e5                   mov    %esp,%ebp

   3:   83 ec 08                sub    $0x8,%esp

   6:   83 e 4 f 0                and    $0xfffffff0,%esp

   9:   b8 00 00 00 00          mov    $0x0,%eax

   e:   29 c 4                   sub    %eax,%esp

  10:   83 ec 08                sub    $0x8,%esp

  13:   68 00 00 00 00          push   $0x0

  18:   83 ec 0c                 sub    $0xc,%esp

  1b:   68 00 00 00 00          push   $0x0

  20:   68 00 00 00 00          push   $0x0

  25:   e8 fc ff ff ff          call   26 <main+0x26>

  2a :   83 c 4 14                add    $0x14,%esp

  2d:   50                      push   %eax

  2e:   e8 fc ff ff ff          call   2f <main+0x 2f >

  33:   83 c 4 10                add    $0x10,%esp

  36:   b8 00 00 00 00          mov    $0x0,%eax

  3b:   c9                      leave 

  3c :   c3                      ret   

  3d:   90                      nop   

  ...

注意这里用的目标文件hello.o和工具objdump都是x86版本的,生成的反汇编代码是Unix系统上传统的AT&T汇编,而不是多数人更熟悉的Intel汇编。

如果你用ARM格式的hello.o,及针对ARM的交叉调试工具arm-linux-objdump,得到的则是ARM汇编:

$ arm-linux-objdump -d hello.o

 

hello.o:     file format elf32-littlearm

 

Disassembly of section .text:

00000180 <main>:

 180:   e 1a 0c 00d        mov     ip, sp

 184:   e92dd800        stmdb   sp!, {fp, ip, lr, pc}

 188:   e24cb004        sub     fp, ip, #4      ; 0x4

  18c :   e 59f 0014        ldr     r0, [pc, #20]   ; 1a 8 <.text+0x 1a 8>

 190:   e 59f 1014        ldr     r1, [pc, #20]   ; 1ac <.text+0x 1ac >

 194:   ebfffffe        bl      194 <main+0x14>

 198:   e 59f 1010        ldr     r1, [pc, #16]   ; 1b0 <.text+0x1b0>

  19c :   ebfffffe        bl      19c <main+0x 1c >

  1a 0:   e 3a 00000        mov     r0, #0  ; 0x0

  1a 4:   e89da800        ldmia   sp, {fp, sp, pc}

        ...

在主机的模拟环境中进行调试时,你可以用AT&T汇编来作为参考,但涉及到与CPU体系结构有关的代码时,最好还是反汇编得到ARM汇编格式的代码,这样更为准确一些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值