使用 readelf
分析 ELF 文件的头部和节区
在这一步中,你将学习如何使用 readelf
命令更详细地分析 ELF 文件的头部和节区。
让我们从检查 /bin/ls
二进制文件的 ELF 文件头部开始:
readelf -h /bin/ls
示例输出:
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
Version: 0x1
Entry point address: 0x4047e0
Start of program headers: 64 (bytes into file)
Start of section headers: 6472 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 28
Section header string table index: 27
此输出提供了关于 ELF 文件头部的详细信息,包括文件类别、数据编码、类型、机器架构以及各种偏移量和大小。
接下来,让我们探索 ELF 文件的节区:
readelf -S /bin/ls
示例输出:
There are 28 section headers, starting at offset 0x1998:
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.gnu.build-i NOTE 0000000000400254 00000254
0000000000000024 0000000000000000 A 0 0 4
...
此命令显示了节区头部,提供了关于 ELF 文件中各个节区的信息,例如它们的名称、类型、地址、大小和其他属性。
你可以通过使用 readelf -e
命令进一步探索节区,该命令显示完整的 ELF 文件信息,包括程序头部、节区头部和符号表:
readelf -e /bin/ls
此命令将提供 ELF 文件的全面视图,使你能够详细了解其结构和内容。