常用的GUN二进制工具集

来源:互联网 发布:编程软件win10不支持 编辑:程序博客网 时间:2024/06/03 10:34

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 从目标文件中列出符号表(文件名、函数名)

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    显示汇编代码及其源码

readelf 显示elf格式目标文件的信息

详细说明

size 获取目标文件中各种段的大小

strings 获取目标文件中所有字符串常量

strip 丢弃可执行文件中的调试信息,提升性能、减少文件占用

注:应该在程序发布时再考虑是否丢弃调试信息







原创粉丝点击