readelf命令指南

readelf 是 Linux 系统中用于分析 ELF(Executable and Linkable Format)格式文件(如可执行文件、目标文件、共享库等)的强大工具。它可以显示 ELF 文件的各种信息,例如头部信息、节区(section)、符号表、动态链接信息等。以下是 readelf 命令的常用操作举例和总结。



常用选项总结

选项功能示例命令
-h显示 ELF 文件头部信息readelf -h a.out
-S显示节区表readelf -S a.out
-d显示动态段信息readelf -d a.out
-s显示符号表readelf -s a.out
-l显示程序头(段信息)readelf -l a.out
-r显示重定位表readelf -r a.out
-a显示所有信息readelf -a a.out
-W宽格式输出(避免截断)readelf -dW a.out

常用操作举例

1. 查看 ELF 文件头部信息

  • 命令:
    readelf -h your_executable
    
  • 示例输出:
    ELF Header:
      Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
      Class:                             ELF64
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              EXEC (Executable file)
      Machine:                           Advanced Micro Devices X86-64
      Entry point address:               0x400430
    
  • 用途:显示 ELF 文件的基本属性,如位数(32/64位)、字节序、文件类型(可执行文件或共享库)等。

2. 查看节区(Sections)信息

  • 命令:
    readelf -S your_executable
    
  • 示例输出:
    There are 31 section headers, starting at offset 0x3b58:
    Section Headers:
      [Nr] Name              Type             Address           Offset
           Size              EntSize          Flags  Link  Info  Align
      [ 0]                   NULL             0000000000000000  00000000
           0000000000000000  0000000000000000           0     0     0
      [ 1] .interp           PROGBITS         0000000000400238  00000238
           000000000000001c  0000000000000000   A       0     0     1
      [ 2] .note.ABI-tag     NOTE             0000000000400254  00000254
           0000000000000020  0000000000000000   A       0     0     4
    
  • 用途:列出所有节区(如 .text.data.bss 等)的名称、类型、大小、对齐方式等。

3. 查看动态链接信息

  • 命令:
    readelf -d your_executable
    
  • 示例输出:
    Dynamic section at offset 0xe08 contains 25 entries:
      Tag        Type                         Name/Value
     0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
     0x000000000000000f (RPATH)              Library rpath: [/usr/local/lib]
     0x000000000000001d (RUNPATH)            Library runpath: [/usr/local/lib]
     0x000000000000000c (INIT)               0x400300
    
  • 用途:显示动态链接相关的条目,如依赖的动态库(NEEDED)、运行时路径(RPATHRUNPATH)等。
  • Notos: ldd命令也可以看见详细的动态链接情况 - ldd命令总结

4. 查看符号表

  • 命令:
    readelf -s your_executable
    
  • 示例输出:
    Symbol table '.dynsym' contains 10 entries:
       Num:    Value          Size Type    Bind   Vis      Ndx Name
         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5
         2: 0000000000400500    32 FUNC    GLOBAL DEFAULT   13 main
    
  • 用途:列出文件中定义或引用的符号(如函数名、变量名),包括绑定类型(全局/本地)、节区索引等。

5. 查看程序头(Program Headers)

  • 命令:
    readelf -l your_executable
    
  • 示例输出:
    Elf file type is EXEC (Executable file)
    Entry point 0x400430
    There are 9 program headers, starting at offset 64
    
    Program Headers:
      Type           Offset             VirtAddr           PhysAddr
                     FileSiz            MemSiz              Flags  Align
      PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                     0x00000000000001f8 0x00000000000001f8  R      0x8
      INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                     0x000000000000001c 0x000000000000001c  R      0x1
        [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
    
  • 用途:显示程序加载时的段信息(如代码段、数据段),包括虚拟地址、大小、权限等。

6. 查看重定位表

  • 命令:
    readelf -r your_executable
    
  • 示例输出:
    Relocation section '.rela.dyn' at offset 0x408 contains 2 entries:
      Offset          Info           Type           Sym. Value    Sym. Name + Addend
    000000600ff0  000200000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
    000000601000  000300000006 R_X86_64_GLOB_DAT 0000000000000000 _ITM_deregisterTMCloneTable + 0
    
  • 用途:显示重定位条目,通常用于动态链接时解析符号地址。

7. 查看所有信息

  • 命令:
    readelf -a your_executable
    
  • 用途:一次性显示 ELF 文件的所有详细信息,包括头部、节区、符号表、动态信息等(输出较长)。

使用技巧与注意事项

  1. 结合 grep 过滤输出

    • 例如,仅查看动态库依赖:
      readelf -d your_executable | grep NEEDED
      
    • 查看 RPATH
      readelf -d your_executable | grep RPATH
      
  2. ldd 的区别

    • readelf 显示文件本身的静态信息(如嵌入的路径或库名)。
    • ldd 显示运行时动态链接器解析后的实际路径。
  3. 适用范围

    • readelf 适用于 ELF 格式文件(Linux/Unix 系统),不适用于 Windows 的 PE 格式(.exe)或 macOS 的 Mach-O 格式。
  4. 权限要求

    • 确保对目标文件有读取权限,否则需要 sudo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值