皇上,还记得我吗?我就是1999年那个Linux伊甸园啊-----24小时滚动更新开源资讯,全年无休!

GNU binutils 里的九种武器

二进制分析是计算机行业中最被低估的技能。

想象一下,在无法访问软件的源代码时,但仍然能够理解软件的实现方式,在其中找到漏洞,并且更厉害的是还能修复错误。所有这些都是在只有二进制文件时做到的。这听起来就像是超能力,对吧?

你也可以拥有这样的超能力,GNU 二进制实用程序(binutils)就是一个很好的起点。GNU binutils 是一个二进制工具集,默认情况下所有 Linux 发行版中都会安装这些二进制工具。

二进制分析是计算机行业中最被低估的技能。它主要由恶意软件分析师、反向工程师和使用底层软件的人使用。

本文探讨了 binutils 可用的一些工具。我使用的是 RHEL,但是这些示例应该在任何 Linux 发行版上可以运行。

  1. [~]# cat /etc/redhat-release
  2. Red Hat Enterprise Linux Server release 7.6 (Maipo)
  3. [~]#
  4. [~]# uname -r
  5. 3.10.0-957.el7.x86_64
  6. [~]#

请注意,某些打包命令(例如 rpm)在基于 Debian 的发行版中可能不可用,因此请使用等效的 dpkg 命令替代。

软件开发的基础知识

在开源世界中,我们很多人都专注于源代码形式的软件。当软件的源代码随时可用时,很容易获得源代码的副本,打开喜欢的编辑器,喝杯咖啡,然后就可以开始探索了。

但是源代码不是在 CPU 上执行的代码,在 CPU 上执行的是二进制或者说是机器语言指令。二进制或可执行文件是编译源代码时获得的。熟练的调试人员深谙通常这种差异。

编译的基础知识

在深入研究 binutils 软件包本身之前,最好先了解编译的基础知识。

编译是将程序从某种编程语言(如 C/C++)的源代码(文本形式)转换为机器代码的过程。

机器代码是 CPU(或一般而言,硬件)可以理解的 1 和 0 的序列,因此可以由 CPU 执行或运行。该机器码以特定格式保存到文件,通常称为可执行文件或二进制文件。在 Linux(和使用 Linux 兼容二进制的 BSD)上,这称为 ELF可执行和可链接格式Executable and Linkable Format)。

在生成给定的源文件的可执行文件或二进制文件之前,编译过程将经历一系列复杂的步骤。以这个源程序(C 代码)为例。打开你喜欢的编辑器,然后键入以下程序:

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. printf("Hello World\n");
  5. return 0;
  6. }

步骤 1:用 cpp 预处理

C 预处理程序(cpp)用于扩展所有宏并将头文件包含进来。在此示例中,头文件 stdio.h 将被包含在源代码中。stdio.h 是一个头文件,其中包含有关程序内使用的 printf 函数的信息。对源代码运行 cpp,其结果指令保存在名为 hello.i 的文件中。可以使用文本编辑器打开该文件以查看其内容。打印 “hello world” 的源代码在该文件的底部。

  1. [testdir]# cat hello.c
  2. #include <stdio.h>
  3. int main(void)
  4. {
  5. printf("Hello World\n");
  6. return 0;
  7. }
  8. [testdir]#
  9. [testdir]# cpp hello.c > hello.i
  10. [testdir]#
  11. [testdir]# ls -lrt
  12. total 24
  13. -rw-r--r--. 1 root root 76 Sep 13 03:20 hello.c
  14. -rw-r--r--. 1 root root 16877 Sep 13 03:22 hello.i
  15. [testdir]#

步骤 2:用 gcc 编译

在此阶段,无需创建目标文件就将步骤 1 中生成的预处理源代码转换为汇编语言指令。这个阶段使用 GNU 编译器集合(gcc)。对 hello.i 文件运行带有 -S 选项的 gcc 命令后,它将创建一个名为 hello.s 的新文件。该文件包含该 C 程序的汇编语言指令。

你可以使用任何编辑器或 cat 命令查看其内容。

  1. [testdir]#
  2. [testdir]# gcc -Wall -S hello.i
  3. [testdir]#
  4. [testdir]# ls -l
  5. total 28
  6. -rw-r--r--. 1 root root 76 Sep 13 03:20 hello.c
  7. -rw-r--r--. 1 root root 16877 Sep 13 03:22 hello.i
  8. -rw-r--r--. 1 root root 448 Sep 13 03:25 hello.s
  9. [testdir]#
  10. [testdir]# cat hello.s
  11. .file "hello.c"
  12. .section .rodata
  13. .LC0:
  14. .string "Hello World"
  15. .text
  16. .globl main
  17. .type main, @function
  18. main:
  19. .LFB0:
  20. .cfi_startproc
  21. pushq %rbp
  22. .cfi_def_cfa_offset 16
  23. .cfi_offset 6, -16
  24. movq %rsp, %rbp
  25. .cfi_def_cfa_register 6
  26. movl $.LC0, %edi
  27. call puts
  28. movl $0, %eax
  29. popq %rbp
  30. .cfi_def_cfa 7, 8
  31. ret
  32. .cfi_endproc
  33. .LFE0:
  34. .size main, .-main
  35. .ident "GCC: (GNU) 4.8.5 20150623 (Red Hat 4.8.5-36)"
  36. .section .note.GNU-stack,"",@progbits
  37. [testdir]#

步骤 3:用 as 汇编

汇编器的目的是将汇编语言指令转换为机器语言代码,并生成扩展名为 .o 的目标文件。此阶段使用默认情况下在所有 Linux 平台上都可用的 GNU 汇编器。

  1. testdir]# as hello.s -o hello.o
  2. [testdir]#
  3. [testdir]# ls -l
  4. total 32
  5. -rw-r--r--. 1 root root 76 Sep 13 03:20 hello.c
  6. -rw-r--r--. 1 root root 16877 Sep 13 03:22 hello.i
  7. -rw-r--r--. 1 root root 1496 Sep 13 03:39 hello.o
  8. -rw-r--r--. 1 root root 448 Sep 13 03:25 hello.s
  9. [testdir]#

现在,你有了第一个 ELF 格式的文件;但是,还不能执行它。稍后,你将看到“目标文件object file”和“可执行文件executable file”之间的区别。

  1. [testdir]# file hello.o
  2. hello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

步骤 4:用 ld 链接

这是编译的最后阶段,将目标文件链接以创建可执行文件。可执行文件通常需要外部函数,这些外部函数通常来自系统库(libc)。

你可以使用 ld 命令直接调用链接器;但是,此命令有些复杂。相反,你可以使用带有 -v(详细)标志的 gcc 编译器,以了解链接是如何发生的。(使用 ld命令进行链接作为一个练习,你可以自行探索。)

  1. [testdir]# gcc -v hello.o
  2. Using built-in specs.
  3. COLLECT_GCC=gcc
  4. COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
  5. Target: x86_64-redhat-linux
  6. Configured with: ../configure --prefix=/usr --mandir=/usr/share/man [...] --build=x86_64-redhat-linux
  7. Thread model: posix
  8. gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
  9. COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:[...]:/usr/lib/gcc/x86_64-redhat-linux/
  10. LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/
  11. COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64'
  12. /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu [...]/../../../../lib64/crtn.o
  13. [testdir]#

运行此命令后,你应该看到一个名为 a.out 的可执行文件:

  1. [testdir]# ls -l
  2. total 44
  3. -rwxr-xr-x. 1 root root 8440 Sep 13 03:45 a.out
  4. -rw-r--r--. 1 root root 76 Sep 13 03:20 hello.c
  5. -rw-r--r--. 1 root root 16877 Sep 13 03:22 hello.i
  6. -rw-r--r--. 1 root root 1496 Sep 13 03:39 hello.o
  7. -rw-r--r--. 1 root root 448 Sep 13 03:25 hello.s

对 a.out 运行 file 命令,结果表明它确实是 ELF 可执行文件:

  1. [testdir]# file a.out
  2. a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=48e4c11901d54d4bf1b6e3826baf18215e4255e5, not stripped

运行该可执行文件,看看它是否如源代码所示工作:

  1. [testdir]# ./a.out Hello World

工作了!在幕后发生了很多事情它才在屏幕上打印了 “Hello World”。想象一下在更复杂的程序中会发生什么。

探索 binutils 工具

上面这个练习为使用 binutils 软件包中的工具提供了良好的背景。我的系统带有 binutils 版本 2.27-34;你的 Linux 发行版上的版本可能有所不同。

  1. [~]# rpm -qa | grep binutils
  2. binutils-2.27-34.base.el7.x86_64

binutils 软件包中提供了以下工具:

  1. [~]# rpm -ql binutils-2.27-34.base.el7.x86_64 | grep bin/
  2. /usr/bin/addr2line
  3. /usr/bin/ar
  4. /usr/bin/as
  5. /usr/bin/c++filt
  6. /usr/bin/dwp
  7. /usr/bin/elfedit
  8. /usr/bin/gprof
  9. /usr/bin/ld
  10. /usr/bin/ld.bfd
  11. /usr/bin/ld.gold
  12. /usr/bin/nm
  13. /usr/bin/objcopy
  14. /usr/bin/objdump
  15. /usr/bin/ranlib
  16. /usr/bin/readelf
  17. /usr/bin/size
  18. /usr/bin/strings
  19. /usr/bin/strip

上面的编译练习已经探索了其中的两个工具:用作汇编器的 as 命令,用作链接器的 ld 命令。继续阅读以了解上述 GNU binutils 软件包工具中的其他七个。

readelf:显示 ELF 文件信息

上面的练习提到了术语“目标文件”和“可执行文件”。使用该练习中的文件,通过带有 -h(标题)选项的 readelf 命令,以将文件的 ELF 标题转储到屏幕上。请注意,以 .o 扩展名结尾的目标文件显示为 Type: REL (Relocatable file)(可重定位文件):

  1. [testdir]# readelf -h hello.o
  2. ELF Header:
  3. Magic: 7f 45 4c 46 02 01 01 00 [...]
  4. [...]
  5. Type: REL (Relocatable file)
  6. [...]

如果尝试执行此目标文件,会收到一条错误消息,指出无法执行。这仅表示它尚不具备在 CPU 上执行所需的信息。

请记住,你首先需要使用 chmod 命令在对象文件上添加 x(可执行位),否则你将得到“权限被拒绝”的错误。

  1. [testdir]# ./hello.o
  2. bash: ./hello.o: Permission denied
  3. [testdir]# chmod +x ./hello.o
  4. [testdir]#
  5. [testdir]# ./hello.o
  6. bash: ./hello.o: cannot execute binary file

如果对 a.out 文件尝试相同的命令,则会看到其类型为 EXEC (Executable file)(可执行文件)。

  1. [testdir]# readelf -h a.out
  2. ELF Header:
  3. Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  4. Class: ELF64
  5. [...] Type: EXEC (Executable file)

如上所示,该文件可以直接由 CPU 执行:

  1. [testdir]# ./a.out Hello World

readelf 命令可提供有关二进制文件的大量信息。在这里,它会告诉你它是 ELF 64 位格式,这意味着它只能在 64 位 CPU 上执行,而不能在 32 位 CPU 上运行。它还告诉你它应在 X86-64(Intel/AMD)架构上执行。该二进制文件的入口点是地址 0x400430,它就是 C 源程序中 main 函数的地址。

在你知道的其他系统二进制文件上尝试一下 readelf 命令,例如 ls。请注意,在 RHEL 8 或 Fedora 30 及更高版本的系统上,由于安全原因改用了位置无关可执行文件position independent executablePIE),因此你的输出(尤其是 Type:)可能会有所不同。

  1. [testdir]# readelf -h /bin/ls
  2. ELF Header:
  3. Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
  4. Class: ELF64
  5. Data: 2's complement, little endian
  6. Version: 1 (current)
  7. OS/ABI: UNIX - System V
  8. ABI Version: 0
  9. Type: EXEC (Executable file)

使用 ldd 命令了解 ls 命令所依赖的系统库,如下所示:

  1. [testdir]# ldd /bin/ls
  2. linux-vdso.so.1 => (0x00007ffd7d746000)
  3. libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f060daca000)
  4. libcap.so.2 => /lib64/libcap.so.2 (0x00007f060d8c5000)
  5. libacl.so.1 => /lib64/libacl.so.1 (0x00007f060d6bc000)
  6. libc.so.6 => /lib64/libc.so.6 (0x00007f060d2ef000)
  7. libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f060d08d000)
  8. libdl.so.2 => /lib64/libdl.so.2 (0x00007f060ce89000)
  9. /lib64/ld-linux-x86-64.so.2 (0x00007f060dcf1000)
  10. libattr.so.1 => /lib64/libattr.so.1 (0x00007f060cc84000)
  11. libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f060ca68000)

对 libc 库文件运行 readelf 以查看它是哪种文件。正如它指出的那样,它是一个 DYN (Shared object file)(共享对象文件),这意味着它不能直接执行;必须由内部使用了该库提供的任何函数的可执行文件使用它。

  1. [testdir]# readelf -h /lib64/libc.so.6
  2. ELF Header:
  3. Magic: 7f 45 4c 46 02 01 01 03 00 00 00 00 00 00 00 00
  4. Class: ELF64
  5. Data: 2's complement, little endian
  6. Version: 1 (current)
  7. OS/ABI: UNIX - GNU
  8. ABI Version: 0
  9. Type: DYN (Shared object file)

size:列出节的大小和全部大小

size 命令仅适用于目标文件和可执行文件,因此,如果尝试在简单的 ASCII 文件上运行它,则会抛出错误,提示“文件格式无法识别”。

  1. [testdir]# echo "test" > file1
  2. [testdir]# cat file1
  3. test
  4. [testdir]# file file1
  5. file1: ASCII text
  6. [testdir]# size file1
  7. size: file1: File format not recognized

现在,在上面的练习中,对目标文件和可执行文件运行 size 命令。请注意,根据 size 命令的输出可以看出,可执行文件(a.out)的信息要比目标文件(hello.o)多得多:

  1. [testdir]# size hello.o
  2. text data bss dec hex filename
  3. 89 0 0 89 59 hello.o
  4. [testdir]# size a.out
  5. text data bss dec hex filename
  6. 1194 540 4 1738 6ca a.out

但是这里的 textdata 和 bss 节是什么意思?

text 节是指二进制文件的代码部分,其中包含所有可执行指令。data 节是所有初始化数据所在的位置,bss 节是所有未初始化数据的存储位置。(LCTT 译注:一般来说,在静态的映像文件中,各个部分称之为section,而在运行时的各个部分称之为segment,有时统称为段。)

比较其他一些可用的系统二进制文件的 size 结果。

对于 ls 命令:

  1. [testdir]# size /bin/ls
  2. text data bss dec hex filename
  3. 103119 4768 3360 111247 1b28f /bin/ls

只需查看 size 命令的输出,你就可以看到 gcc 和 gdb 是比 ls 大得多的程序:

  1. [testdir]# size /bin/gcc
  2. text data bss dec hex filename
  3. 755549 8464 81856 845869 ce82d /bin/gcc
  4. [testdir]# size /bin/gdb
  5. text data bss dec hex filename
  6. 6650433 90842 152280 6893555 692ff3 /bin/gdb

strings:打印文件中的可打印字符串

在 strings 命令中添加 -d 标志以仅显示 data 节中的可打印字符通常很有用。

hello.o 是一个目标文件,其中包含打印出 Hello World 文本的指令。因此,strings 命令的唯一输出是 Hello World

  1. [testdir]# strings -d hello.o
  2. Hello World

另一方面,在 a.out(可执行文件)上运行 strings 会显示在链接阶段该二进制文件中包含的其他信息:

  1. [testdir]# strings -d a.out
  2. /lib64/ld-linux-x86-64.so.2
  3. !^BU
  4. libc.so.6
  5. puts
  6. __libc_start_main
  7. __gmon_start__
  8. GLIBC_2.2.5
  9. UH-0
  10. UH-0
  11. =(
  12. []A\A]A^A_
  13. Hello World
  14. ;*3$"

objdump:显示目标文件信息

另一个可以从二进制文件中转储机器语言指令的 binutils 工具称为 objdump。使用 -d 选项,可从二进制文件中反汇编出所有汇编指令。

回想一下,编译是将源代码指令转换为机器代码的过程。机器代码仅由 1 和 0 组成,人类难以阅读。因此,它有助于将机器代码表示为汇编语言指令。汇编语言是什么样的?请记住,汇编语言是特定于体系结构的;由于我使用的是 Intel(x86-64)架构,因此如果你使用 ARM 架构编译相同的程序,指令将有所不同。

  1. [testdir]# objdump -d hello.o
  2. hello.o: file format elf64-x86-64
  3. Disassembly of section .text:
  4. 0000000000000000
  5. :
  6. 0: 55 push %rbp
  7. 1: 48 89 e5 mov %rsp,%rbp
  8. 4: bf 00 00 00 00 mov $0x0,%edi
  9. 9: e8 00 00 00 00 callq e
  10. e: b8 00 00 00 00 mov $0x0,%eax
  11. 13: 5d pop %rbp
  12. 14: c3 retq

该输出乍一看似乎令人生畏,但请花一点时间来理解它,然后再继续。回想一下,.text 节包含所有的机器代码指令。汇编指令可以在第四列中看到(即 pushmovcallqpopretq 等)。这些指令作用于寄存器,寄存器是 CPU 内置的存储器位置。本示例中的寄存器是 rbprspedieax 等,并且每个寄存器都有特殊的含义。

现在对可执行文件(a.out)运行 objdump 并查看得到的内容。可执行文件的 objdump 的输出可能很大,因此我使用 grep 命令将其缩小到 main 函数:

  1. [testdir]# objdump -d a.out | grep -A 9 main\>
  2. 000000000040051d
  3. :
  4. 40051d: 55 push %rbp
  5. 40051e: 48 89 e5 mov %rsp,%rbp
  6. 400521: bf d0 05 40 00 mov $0x4005d0,%edi
  7. 400526: e8 d5 fe ff ff callq 400400
  8. 40052b: b8 00 00 00 00 mov $0x0,%eax
  9. 400530: 5d pop %rbp
  10. 400531: c3 retq

请注意,这些指令与目标文件 hello.o 相似,但是其中包含一些其他信息:

  • 目标文件 hello.o 具有以下指令:callq e
  • 可执行文件 a.out 由以下指令组成,该指令带有一个地址和函数:callq 400400 <puts@plt>    上面的汇编指令正在调用 puts 函数。请记住,你在源代码中使用了一个 printf 函数。编译器插入了对 puts 库函数的调用,以将 Hello World 输出到屏幕。

查看 put 上方一行的说明:

  • 目标文件 hello.o 有个指令 movmov $0x0,%edi
  • 可执行文件 a.out 的 mov 指令带有实际地址($0x4005d0)而不是 $0x0mov $0x4005d0,%edi

该指令将二进制文件中地址 $0x4005d0 处存在的内容移动到名为 edi 的寄存器中。

这个存储位置的内容中还能是别的什么吗?是的,你猜对了:它就是文本 Hello, World。你是如何确定的?

readelf 命令使你可以将二进制文件(a.out)的任何节转储到屏幕上。以下要求它将 .rodata(这是只读数据)转储到屏幕上:

  1. [testdir]# readelf -x .rodata a.out
  2. Hex dump of section '.rodata':
  3. 0x004005c0 01000200 00000000 00000000 00000000 ....
  4. 0x004005d0 48656c6c 6f20576f 726c6400 Hello World.

你可以在右侧看到文本 Hello World,在左侧可以看到其二进制格式的地址。它是否与你在上面的 mov 指令中看到的地址匹配?是的,确实匹配。

strip:从目标文件中剥离符号

该命令通常用于在将二进制文件交付给客户之前减小二进制文件的大小。

请记住,由于重要信息已从二进制文件中删除,因此它会妨碍调试。但是,这个二进制文件可以完美地执行。

对 a.out 可执行文件运行该命令,并注意会发生什么。首先,通过运行以下命令确保二进制文件没有被剥离(not stripped):

  1. [testdir]# file a.out
  2. a.out: ELF 64-bit LSB executable, x86-64, [......] not stripped

另外,在运行 strip 命令之前,请记下二进制文件中最初的字节数:

  1. [testdir]# du -b a.out
  2. 8440 a.out

现在对该可执行文件运行 strip 命令,并使用 file 命令以确保正常完成:

  1. [testdir]# strip a.out
  2. [testdir]# file a.out a.out: ELF 64-bit LSB executable, x86-64, [......] stripped

剥离该二进制文件后,此小程序的大小从之前的 8440 字节减小为 6296 字节。对于这样小的一个程序都能有这么大的空间节省,难怪大型程序经常被剥离。

  1. [testdir]# du -b a.out
  2. 6296 a.out

addr2line:转换地址到文件名和行号

addr2line 工具只是在二进制文件中查找地址,并将其与 C 源代码程序中的行进行匹配。很酷,不是吗?

为此编写另一个测试程序;只是这一次确保使用 gcc 的 -g 标志进行编译,这将为二进制文件添加其它调试信息,并包含有助于调试的行号(由源代码中提供):

  1. [testdir]# cat -n atest.c
  2. 1 #include <stdio.h>
  3. 2
  4. 3 int globalvar = 100;
  5. 4
  6. 5 int function1(void)
  7. 6 {
  8. 7 printf("Within function1\n");
  9. 8 return 0;
  10. 9 }
  11. 10
  12. 11 int function2(void)
  13. 12 {
  14. 13 printf("Within function2\n");
  15. 14 return 0;
  16. 15 }
  17. 16
  18. 17 int main(void)
  19. 18 {
  20. 19 function1();
  21. 20 function2();
  22. 21 printf("Within main\n");
  23. 22 return 0;
  24. 23 }

用 -g 标志编译并执行它。正如预期:

  1. [testdir]# gcc -g atest.c
  2. [testdir]# ./a.out
  3. Within function1
  4. Within function2
  5. Within main

现在使用 objdump 来标识函数开始的内存地址。你可以使用 grep 命令来过滤出所需的特定行。函数的地址在下面突出显示(55 push %rbp 前的地址):

  1. [testdir]# objdump -d a.out | grep -A 2 -E 'main>:|function1>:|function2>:'
  2. 000000000040051d :
  3. 40051d: 55 push %rbp
  4. 40051e: 48 89 e5 mov %rsp,%rbp
  5. --
  6. 0000000000400532 :
  7. 400532: 55 push %rbp
  8. 400533: 48 89 e5 mov %rsp,%rbp
  9. --
  10. 0000000000400547
  11. :
  12. 400547: 55 push %rbp
  13. 400548: 48 89 e5 mov %rsp,%rbp

现在,使用 addr2line 工具从二进制文件中的这些地址映射到 C 源代码匹配的地址:

  1. [testdir]# addr2line -e a.out 40051d
  2. /tmp/testdir/atest.c:6
  3. [testdir]#
  4. [testdir]# addr2line -e a.out 400532
  5. /tmp/testdir/atest.c:12
  6. [testdir]#
  7. [testdir]# addr2line -e a.out 400547
  8. /tmp/testdir/atest.c:18

它说 40051d 从源文件 atest.c 中的第 6 行开始,这是 function1 的起始大括号({)开始的行。function2 和 main 的输出也匹配。

nm:列出目标文件的符号

使用上面的 C 程序测试 nm 工具。使用 gcc 快速编译并执行它。

  1. [testdir]# gcc atest.c
  2. [testdir]# ./a.out
  3. Within function1
  4. Within function2
  5. Within main

现在运行 nm 和 grep 获取有关函数和变量的信息:

  1. [testdir]# nm a.out | grep -Ei 'function|main|globalvar'
  2. 000000000040051d T function1
  3. 0000000000400532 T function2
  4. 000000000060102c D globalvar
  5. U __libc_start_main@@GLIBC_2.2.5
  6. 0000000000400547 T main

你可以看到函数被标记为 T,它表示 text 节中的符号,而变量标记为 D,表示初始化的 data 节中的符号。

想象一下在没有源代码的二进制文件上运行此命令有多大用处?这使你可以窥视内部并了解使用了哪些函数和变量。当然,除非二进制文件已被剥离,这种情况下它们将不包含任何符号,因此 nm 就命令不会很有用,如你在此处看到的:

  1. [testdir]# strip a.out
  2. [testdir]# nm a.out | grep -Ei 'function|main|globalvar'
  3. nm: a.out: no symbols

结论

GNU binutils 工具为有兴趣分析二进制文件的人提供了许多选项,这只是它们可以为你做的事情的冰山一角。请阅读每种工具的手册页,以了解有关它们以及如何使用它们的更多信息。


via: https://opensource.com/article/19/10/gnu-binutils

作者:Gaurav Kamathe 选题:lujun9972 译者:wxy 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

转自 https://linux.cn/article-11441-1.html