gnu binutils使用方法指南

原文:http://zqwt.012.blog.163.com/blog/static/120446842010111364250978/

GNU binutils是一组二进制工具集,包括ar nm objcopy objdump gprof ld ranlib size strings strip addr2line。下面将对它们的用法做一个简单的介绍。

1、    ar

ar用于建立、修改、提取档案文件(archive)archive是一个包含多个被包含文件的单一文件(也称之为库文件),其结构保证了可以从中检索并得到原始的被包含文件(称之为archive中的member)。member的原始文件内容、模式(权限)、时间戳、所有者和组等属性都被保存在 archive中。member被提取后,他们的属性被恢复到初始状态。ar主要用于创建C库文件

<1>    创建静态库

l         生成目标文件:$ gcc -Wall -c file1.c file2.c file3.c  // 不用指定生成.o文件名(默认生成file1.o, file2.o, file3.o)

l         .o目标文件创建静态连接库:$ ar rv libNAME.a file1.o file2.o file3.oar生成了libNAME.a库,并列出库中的文件。r : flie1.o, file2,o, file3.o插入archive,如故原先archive中已经存在某文件,则先将该文件删除。v : 显示ar操作的附加信息

<2>    创建动态库(利用gcc,未用ar)

l         生成目标文件$ gcc -Wall -c -fpic file1.c file2.c file3.c -fpic指定生成的.o目标文件可被重定址. picposition idependent code的缩写位置无关代码.

l         生成动态库文件:$ gcc -shared -o libNAME.so file1.o file2.o file3.o一般地连接器使用main()函数作为程序入口但在动态共享库中没有这样的入口所以就要指定-shared选项来避免编译器显示出错信息。实际上上述的两条命令可以合并为下面这条:$ gcc -Wall -shared -fpic -o libNAME.so file1.c file2.c file3.c,此后,将main函数所在的程序与libNAME.so连接。

    至此,与动态库连接的函数编译成了一个可执行文件。貌似成功了,但还差最后一步。如果直接运行该程序,会给出这样的错误信息:

  error while loading shared libraries: libhello.so:

  cannot open shared object file: No such file or directory

  这是因为与动态库连接的程序在运行时,首先将该动态库加载到内存中,而gcc默认加载动态库文件所在目录为/usr/local/lib, /usr/lib。刚才的程序虽然能编译成功,但如果我们自己建立的动态库没有位于默认目录中,则执行时会应为无法找到它而失败。

  解决办法:改变加载路径对应的环境变量,然后再执行。

  export LD_LIBRARY_PATH=动态库所在目录:$LD_LIBRARY_PATH

  查看archive内容

  $ ar tv archiveNAME

  t : 显示archivemember的内容,若不指定member,则列出所有。

  v : t结合使用时,显示member的详细信息。

要想进了解ar的详细选项,参考aron-line manual

 

2、    nm

    nm用来列出目标文件中的符号可以帮助程序员定位和分析执行程序和目标文件中的符号信息和它的属性。如果没有目标文件作为参数传递给nm, nm假定目标文件为a.out.

    这里用一个简单的示例程序来介绍nm的用法:

main.c:

  int main(int argc, char *argv[])

  {

                 hello();

                 bye();

                 return 0;

  }

hello.c:

  void hello(void)

  {

             printf("hello!\n");

  }

bye.c:

  void bye(void)

  {

             printf("good bye!\n");

  }

运行下列命令:

$ gcc -Wall -c main.c hello.c bye.c gcc生成main.o, hello.o, bye.o三个目标文件(这里没有声明函数原型,加了-Wall,gcc会给出警告)

$ nm main.o hello.o bye.o

结果显示如下:

main.o:

U bye

U hello

00000000 T main

hello.o:

00000000 T hello

U puts

bye.o:

00000000 T bye

U puts

结合这些输出结果,以及程序代码,可以知道:

对于main.o, byehello未被定义, main被定义了

对于hello.o, hello被定义了, puts未被定义

对于bye.o, bye被定义了,puts未被定义

几个值得注意的问题:

<1>    "目标文件".o文件库文件最终的可执行文件。.o 是编译后的目标文件,即含有最终编译出的机器码,但它里面所引用的其他文件中函数的内存位置尚未定义.

<2>    如果用nm查看可执行文件输出会比较多仔细研究输出可以对nm用法有更清醒的认识.

<3>    在上述hello.c, bye.c调用的是printf(), nm输出中显示调用的是puts(), 说明最终程序实际调用的puts(),如果令hello.cbye.c中的printf()使用格式化输出,nm显示调用printf(). ( : printf("%d", 1); )

关于nm的参数选项,参考on-line manual

 

3、    objcopy

  objcopy可以将一种格式的目标文件转化为另外一种格式的目标文件。它使用GNU BFD库进行读/写目标文件。使用BFD, objcopy就能将原格式的目标文件转化为不同格式的目标文件。

  以我们在nm中使用的hello.o目标文件和hello可执行为例:

$ file hello.o hello

    file命令用来判别文件类型输出如下:

  hello.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped

  hello: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux < xmlnamespace prefix ="st1" ns ="urn:schemas-microsoft-com:office:smarttags" />2.2.0, dynamically linked (uses shared libs), not stripped

  现在运行objcopy来改变hello的文件类型:原先它是ELF格式的可执行程序现将它转换为srec格式.srec格式文件是Motolora S-Record格式的文件主要用来在主机和目标机之间传输数据.

  $ objcopy -O srec hello hello_srec

  $ file hello.o hello

  file命令结果:hello_srec: Motorola S-Record; binary data in text format

注意objcopy的格式"-O"指定输出文件类型; 输入文件名和输出文件名位于命令末尾关于objcopy命令的详细选项参考on-line manual

 

4、    objdump

  objdump用来显示目标文件的信息可以通过选项控制显示那些特定信息objdump一个最大的用处恐怕就是将C代码反汇编了. 在嵌入式软件开发过程中也可以用它查看执行文件或库文件的信息.

下面我们用上文提到的hello可执行文件和hello_srec可执行文件为例介绍objdump的简单用法:

<1>    -f:显示目标文件的头文件概要信息.

$ objdump -f hello hello_srec

输出如下:

hello: file format elf32-i386

architecture: i386, flags 0x00000112:

EXEC_P, HAS_SYMS, D_PAGED

start address 0x080482c0

hello_srec: file format srec

architecture: UNKNOWN!, flags 0x00000000:

start address 0x00000000080482c0

<2>    生成反汇编代码

$ objdump -d hello.o

显示如下:

hello.o: file format elf32-i386

Disassembly of section .text:

00000000 <hello>:

0: 55 push %ebp

1: 89 e5         mov %esp,%ebp

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

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

9: 68 00 00 00 00 push $0x0

e: e8 fc ff ff ff     call f <hello+0xf>

13: 83 c4 10     add $0x10,%esp

16: c9           leave

17: c3           ret

    -d显示目标文件中机器指令使用的汇编语言,只反汇编那些应该含有指令机器码的节(显示.text)如果用-D,则反汇编所有节的内容。关于objcopy命令的详细选项参考on-line manual

 

5、    readelf

    readelf用来显示ELF格式目标文件的信息.可通过参数选项来控制显示哪些特定信息.(注意: readelf不支持显示archive文档也不支持64位的ELF文件)。下面利用先前的hello可执行文件演示readelf的简单用法:

$ readelf -h hello

ELF Header:

Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00

Class: ELF32

Data: 2's complement, little endian

Version: 1 (current)

OS/ABI: UNIX - System V

ABI Version: 0

Type: EXEC (Executable file)

Machine: Intel 80386

Version: 0x1

Entry point address: 0x80482c0

Start of program headers: 52 (bytes into file)

Start of section headers: 3848 (bytes into file)

Flags: 0x0

Size of this header: 52 (bytes)

Size of program headers: 32 (bytes)

Number of program headers: 7

Size of section headers: 40 (bytes)

Number of section headers: 34

Section header string table index: 31

注意:readelf只能用于ELF格式目标文件且选项中至少要指定一个(V, H)的选项!

 

6、    gprof

  gprof被用来测量程序的性能。它记录每个函数被调用的次数以及相应的执行时间。这样就能锁定程序执行时花费时间最多的部分对程序的优化就可集中于对它们的优化。用一个简单的数值计算程序来掩饰gprof的用法:

collatz.c

#include <stdio.h>

/* Computes the length of Collatz sequences */

unsigned int step (unsigned int x)

{

  if (x % 2 == 0) {

             return (x / 2);

  } else {

             return (3 * x + 1);

  }

}

unsigned int nseq (unsigned int x0)

{

  unsigned int i = 1, x;

  if (x0 == 1 || x0 == 0)

  return i;

  x = step (x0);

  while (x != 1 && x != 0) {

             x = step (x);

             i++;

  }

  return i;

}

int main (void)

{

  unsigned int i, m = 0, im = 0;

  for (i = 1; i < 500000; i++) {

             unsigned int k = nseq (i);

             if (k > m) {

             m = k;

                im = i;

             printf ("sequence length = %u for %u\n", m, im);

       }

  }

  return 0;

}

    先将collatz.c编译成目标文件collatz.o gcc通过-pg选项来打开gprof支持

  $ gcc -Wall -c -pg collatz.c

  $ gcc -Wall -pg -o collatz collatz.o

  注意:两条命令都要加 "-pg"选项。前一条命令生成collatz.o目标文件。后一条命令生成可执行文件,该可执行文件中包含了记录函数执行时间的指令。

  生成collatz可执行文件后,现执行它,结果与一般程序的执行无疑。但此时在PWD目录生成一个名为"gmon.out"的文件,gprof通过它来分析程序的执行。

  如果不先执行程序,而直接用gprof来分析它,会提示“gmon.out: No such file or directory”

  gprof用法:

$ gprof ./collatz

关于gprof更多的描述,参考gprofon-line manual

 

7、    ld

         ld的用法和概念可以参考链接:http://www.schwedler.com/old/HTML/ld_toc.html#SEC1

 

8、    ranlib

Usage: ranlib [options] archive

Function: Generate an index to speed access to archives

The options are:

@<file>                      Read options from <file>

-h --help                    Print this help message

-V --version                 Print version information

ranlib: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big srec symbolsrec tekhex binary ihex trad-core

 

9、    size

Usage: size [option(s)] [file(s)]

Function: Displays the sizes of sections inside binary files

If no input file(s) are specified, a.out is assumed

The options are:

-A|-B     --format={sysv|berkeley}  Select output style (default is berkeley)

-o|-d|-x  --radix={8|10|16}         Display numbers in octal, decimal or hex

-t        --totals                  Display the total sizes (Berkeley only)

--common                  Display total size for *COM* syms

--target=<bfdname>        Set the binary file format

@<file>                   Read options from <file>

-h        --help                    Display this information

-v        --version                 Display the program's version

 

size: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big srec symbolsrec tekhex binary ihex trad-core

 

10、 strings

Usage: strings [option(s)] [file(s)]

Function: Display printable strings in [file(s)] (stdin by default)

The options are:

-a - --all                Scan the entire file, not just the data section

-f --print-file-name      Print the name of the file before each string

-n --bytes=[number]       Locate & print any NUL-terminated sequence of at

-<number>                 least [number] characters (default 4).

-t --radix={o,d,x}        Print the location of the string in base 8, 10 or 16

-o                        An alias for --radix=o

-T --target=<BFDNAME>     Specify the binary file format

-e --encoding={s,S,b,l,B,L} Select character size and endianness:

s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit

@<file>                   Read options from <file>

-h --help                 Display this information

-v --version              Print the program's version number

strings: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big srec symbolsrec tekhex binary ihex trad-core

11、 strip

Usage: strip <option(s)> in-file(s)

Function: Removes symbols and sections from files

The options are:

-I --input-target=<bfdname>      Assume input file is in format <bfdname>

-O --output-target=<bfdname>     Create an output file in format <bfdname>

-F --target=<bfdname>            Set both input and output format to <bfdname>

-p --preserve-dates              Copy modified/access timestamps to the output

-R --remove-section=<name>       Remove section <name> from the output

-s --strip-all                   Remove all symbol and relocation information

-g -S -d --strip-debug           Remove all debugging symbols & sections

--strip-unneeded              Remove all symbols not needed by relocations

--only-keep-debug             Strip everything but the debug information

-N --strip-symbol=<name>         Do not copy symbol <name>

-K --keep-symbol=<name>          Do not strip symbol <name>

--keep-file-symbols           Do not strip file symbol(s)

-w --wildcard                    Permit wildcard in symbol comparison

-x --discard-all                 Remove all non-global symbols

-X --discard-locals              Remove any compiler-generated symbols

-v --verbose                     List all object files modified

-V --version                     Display this program's version number

-h --help                        Display this output

--info                        List object formats & architectures supported

-o <file>                        Place stripped output into <file>

strip: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big srec symbolsrec tekhex binary ihex trad-core

    UNIX下文件压缩命令compress大家都比较熟悉了,它的压缩率比较高, tar命令结合使用来做数据备份是最合适不过了。但compress压缩也有缺点,就是被压缩后的文件需要用命令uncompress解压后才能正常使用。而用strip命令就没有这个问题,它能清除执行文件中不必要的标示符及调试信息,可减小文件大小而不影响正常使用。但与compress 不同的是,文件一旦strip后就不能恢复原样了,所以strip是一个减肥工具而不是压缩工具。而且,被strip后的文件不包含调试信息,就不能用dbx来调试程序了。现在让我们来具体效果如何,看下面一个程序:

#include

main()

{

  printf("hello, world\n");

}

  cc编译以后长度为46176字节,用strip后变为了30648,足足减小了 1/3,而且仍然可以正常执行。除了用strip外,用共享库也是减小执行文件长度的方法。

 

注:

1.如果文件大小没有减小,那就是已经strip过了.

2.cc 编译时加上"-s"参数,具有同样的作用。

 

12、 addr2line

Usage: addr2line [option(s)] [addr(s)]

Function: Convert addresses into line number/file name pairs.

addr2line用于把debug版本中得地址变成对应的C language Code,这是一个GNU工具

addr2line -e abc.debug 0x081d3ec0

可以工作

addr2line -e abc.release 0x081d3ec0

不能工作

所以说应该发布版本时至少应该有两个文件,release 版本和debug版本,而release版本仅仅是debug版本少了debugging信息。

If no addresses are specified on the command line, they will be read from stdin

The options are:

@<file>                Read options from <file>

-b --target=<bfdname>  Set the binary file format

-e --exe=<executable>  Set the input file name (default is a.out)

-i --inlines           Unwind inlined functions

-j --section=<name>    Read section-relative offsets instead of addresses

-s --basenames         Strip directory names

-f --functions         Show function names

-C --demangle[=style]  Demangle function names

-h --help              Display this information

-v --version           Display the program's version

 

addr2line: supported targets: elf32-i386 a.out-i386-linux efi-app-ia32 elf32-little elf32-big srec symbolsrec tekhex binary ihex trad-core

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值