常用的GUN二进制工具集

GNU Binutils是一系列二进制工具,包括addr2line、ar、gprof等,用于程序分析和维护。addr2line帮助定位内存错误,ar管理档案文件,gprof提供性能分析。其他工具如nm、objcopy、objdump、readelf、size、strings和strip分别用于显示符号表、格式转换、显示目标文件信息、查看段大小、提取字符串常量和删除调试信息等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 丢弃可执行文件中的调试信息,提升性能、减少文件占用

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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值