GNU Binutils
The GNU Binutils are a collection of binary tools. The main ones are:
- ld - the GNU linker.
- as - the GNU assembler.
But they also include:
- addr2line - Converts addresses into filenames and line numbers.
- ar - A utility for creating, modifying and extracting from archives.
- c++filt - Filter to demangle encoded C++ symbols.
- dlltool - Creates files for building and using DLLs.
- gold - A new, faster, ELF only linker, still in beta test.
- gprof - Displays profiling information.
- nlmconv - Converts object code into an NLM.
- nm - Lists symbols from object files.
- objcopy - Copies and translates object files.
- objdump - Displays information from object files.
- ranlib - Generates an index to the contents of an archive.
- readelf - Displays information from any ELF format object file.
- size - Lists the section sizes of an object or archive file.
- strings - Lists printable strings from files.
- strip - Discards symbols.
- windmc - A Windows compatible message compiler.
- windres - A compiler for Windows resource files.
Most of these programs use BFD, the Binary File Descriptor library, to do low-level manipulation. Many of them also use the opcodes library to assemble and disassemble machine instructions.
The binutils have been ported to most major Unix variants as well as Wintel systems, and their main reason for existence is to give the GNU system (and GNU/Linux) the facility to compile and link programs.
其中:
addr2line 将指定地址转化为对应的文件名和行号,常用于分析和定位内存访问错误的问题,编译源文件时需要使用-g参数以生成带有调试信息的可执行文件
使用方法:
0、预先开启core dump选项,系统默认 是不开启的,需要使用命令ulimit 来开启core转储
ulimit -c 1024(设置core文件最大为1024KBytes)或者ulimit -c unlimited(不限制core文件大小)
1、使用参数-g编译源文件,并运行,由于开启了core转储,程序崩溃时就会产生core文件
gcc test.c -g -o test
2、使用dmesg命令查询core文件中记录的发成错误的地址(IP寄存器中的地址,例如值为0x0804812)
dmesg core
3、对错误地址和可执行文件使用工具addr2line定位源代码中出错行
addr2line 0x0804812 -f -e test
ar 创建修改、提取档案文件(库文件)
ar crs libtest.a test.o
gprof 程序性能检测工具
它记录了每个函数被调用的次数以及其执行的时间,通过此工具可以快速锁定执行时间最多的函数并对其优化nm 从目标文件中列出符号表(文件名、函数名)
00000000 T main 其中: 00000000 表示地址 T 表示代码段 main 表示符号名
符号类型说明:
A 符号的地址是绝对的,不会因为进一步的连接而改变
B/b 符号位于bss段
D/d 符号位于data段
R/r 符号位于rdata段
T/t 符号位于代码段
objcopy 对目标文件进行格式转换
objdump 显示目标文件中的信息
objdump -d test.o 显示汇编代码
objdump -S test.o 显示汇编代码及其源码