Analyze ELF File Headers and Sections Using readelf
In this step, you will learn how to use the readelf
command to analyze the headers and sections of ELF files in more detail.
Let's start by examining the ELF file header of the /bin/ls
binary:
readelf -h /bin/ls
Example output:
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
This output provides detailed information about the ELF file header, including the file class, data encoding, type, machine architecture, and various offsets and sizes.
Next, let's explore the sections of the ELF file:
readelf -S /bin/ls
Example output:
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
...
This command displays the section headers, providing information about the various sections in the ELF file, such as their names, types, addresses, sizes, and other attributes.
You can further explore the sections by using the readelf -e
command, which displays the full ELF file information, including the program headers, section headers, and symbol table:
readelf -e /bin/ls
This command will provide a comprehensive view of the ELF file, allowing you to understand its structure and contents in detail.