Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Analyzing ELF Binaries with readelf and objdump

Recommended resources

In our last lesson, we established the theoretical groundwork for the Executable and Linkable Format (ELF), dissecting its structure into headers, sections, and segments. Today, we transition from theory to practice. You will learn to wield the standard Linux command-line utilities—readelf and objdump—to parse, inspect, and understand the anatomy of real-world ELF binaries.

This hands-on session is designed to solidify your understanding of the concepts from the previous lesson. We will use these tools to find entry points, examine section properties, and even look at the raw machine code. Given your background in C and Assembly, this direct interaction with compiled binaries should provide a satisfying link between source code and its executable representation.

Initial Inspection with readelf

The primary tool for parsing ELF metadata is readelf. It is part of the GNU Binutils suite and is purpose-built to read and display information about ELF files in a human-readable format. Let's explore its most common uses.

For all the examples in this lesson, you can use a common system binary like /bin/ls or /bin/bash in your Linux lab environment.

The ELF Header: readelf -h

First, let's verify the information stored in the ELF header, which you'll recall is the file's main roadmap. The -h (or --file-header) flag displays the contents of the Elf64_Ehdr structure we discussed.

The video below gives a clear demonstration of using readelf to examine the ELF header, program headers (segments), and section headers.

In-depth: ELF - The Extensible & Linkable Format

This segment of the "In-depth: ELF" video by stacksmashing demonstrates how readelf is used to inspect the core components of an ELF file.

Please watch the following parts of the video, paying attention to how the command's output maps to the theoretical structures we've learned: ELF Header: This shows the use of readelf -h and its output. Program Headers (Segments): Here, the presenter uses readelf --segments to show the execution view. Section Headers: Finally, this part shows readelf --sections for the linking view.

After watching, try running readelf -h /bin/ls yourself. You should see output similar to this:

Example output of `readelf` showing the ELF Header and a snippet of the Section Headers for `/bin/ls`.

Notice how the fields correspond directly to the Elf64_Ehdr members from our previous lesson:

  • Magic: The 7f 45 4c 46 (.ELF) bytes that identify the file.
  • Class: ELF64 for a 64-bit binary.
  • Type: Usually EXEC (executable) or DYN (shared object or position-independent executable).
  • Entry point address: The virtual address where execution begins.
  • Start of program headers: The file offset to the Program Header Table (e_phoff).
  • Start of section headers: The file offset to the Section Header Table (e_shoff).

Segments and Sections: readelf -l and readelf -S

As the video demonstrated, readelf can show you both views of the file.

A reminder of the two views of an ELF file: the linking view (sections) for analysis and linking, and the execution view (segments) for the OS loader.
  • Execution View (Segments): Use readelf -l (or --segments) to list the program headers. The output shows each segment's type (LOAD, INTERP), its offset and size in the file, its target virtual address and size in memory, and its permissions (R, W, X). This table is the set of instructions the kernel follows to load the program into memory.

  • Linking View (Sections): Use readelf -S (or --sections) to list the section headers. This is the view you'll use most often for static analysis. The output details each section's name, type (PROGBITS, NOBITS), address, offset, size, and flags (A for alloc, W for write, X for execute). This allows you to quickly locate the code (.text), read-only data (.rodata), and writable data (.data).


Deeper Analysis with objdump

While readelf is excellent for metadata, objdump is a more versatile tool for inspecting the contents of sections, most notably for disassembling code.

The following chapter from "Practical Binary Analysis" provides excellent, practical examples of using objdump and other utilities in a scenario-driven way.

[PDF] Practical Binary Analysis - NoStarch

This chapter excerpt demonstrates how to use objdump to inspect section contents and disassemble code to understand a program's logic.

Please read the section titled Examining Instruction-Level Behavior Using objdump. Focus on how the author uses objdump with different flags: objdump -s --section .rodata: To dump the contents of a specific data section and find a string's address. objdump -d: To disassemble the code and analyze the instruction-level logic around a specific address.

Let's break down the key capabilities of objdump.

Disassembling Code: objdump -d

The most powerful feature of objdump for a reverse engineer is its disassembler. Using objdump -d /path/to/binary will print the assembly code for all executable sections. This is your first direct look at the machine instructions. Since you are familiar with Assembly, you'll find this output invaluable for understanding what a program actually does, free from the abstractions of the source code.

You can combine it with grep to quickly find the disassembly for a specific function, for example: objdump -d /bin/ls | grep -A20 '<main>:'.

Examining Section Contents: objdump -s

As you saw in the reading, objdump -s (or --full-contents) dumps the full contents of sections, showing you the raw hexadecimal bytes and their ASCII representation. This is useful for examining data sections like .rodata to find embedded strings or constants, or .data to see the initial values of global variables.

Displaying Symbols: objdump -t

The -t flag displays the file's symbol table. This includes functions, global variables, and other named entities. For dynamically linked binaries, -T is often more useful as it shows the dynamic symbols, which are the functions the binary needs to import from shared libraries at runtime.


Essential Companion Tools: strings and nm

Alongside readelf and objdump, a few other command-line tools are indispensable for initial binary triage.

[PDF] Practical Binary Analysis - NoStarch

This reading covers two essential utilities: nm for symbols and strings for quick hints.

First, read the section Parsing Symbols with nm. Pay close attention to the use of the -D flag for dynamic symbols and --demangle for C++ functions. Next, read Looking for Hints with strings. This demonstrates how strings can provide immediate clues about a binary's functionality.

  • strings: This simple utility scans a file and prints any sequence of printable characters of a certain minimum length (default is 4). It is one of the very first tools you should run on an unknown binary. It can reveal hardcoded file paths, URLs, error messages, and other embedded text that can give you instant clues about the program's purpose.

  • nm: This tool is specifically designed to list symbols from object files and executables. Its key advantage, as noted in the reading, is its ability to demangle C++ function names with the --demangle option. This converts the compiler-generated mangled names back into human-readable function signatures, revealing the function's name and parameter types.


Practical Exercise

Let's put these tools to use. Create a file named simple.c with the following C code:




#include <stdio.h>

const char* message = "Hello, Reverse Engineering!";

int main() {
    printf("%s\n", message);
    return 0;
}

Now, open your terminal and perform the following steps:

  1. Compile the program:
    gcc -o simple simple.c

  2. Analyze the executable simple:

    • Use readelf -h simple to find the Entry point address.
    • Use readelf -S simple to find the address and size of the .text and .rodata sections.
    • Use strings simple. Can you find the "Hello, Reverse Engineering!" message?
    • Use objdump -s --section=.rodata simple. Verify that the string is in this section.
    • Use objdump -d simple. Find the disassembly for the main function. Observe the call instruction that invokes printf.
    • Use nm simple. Find the symbols for main and message. What are their addresses? Do they match what you found in the other tools?

This exercise will reinforce how the different tools provide complementary views of the same underlying file structure.


Conclusion

You are now equipped with the essential command-line toolkit for the initial static analysis of ELF binaries. These tools allow you to quickly dissect an executable without even running it, providing a wealth of information about its structure, dependencies, and content.

Here are the key takeaways on our primary tools:

  • readelf: Your go-to tool for parsing ELF-specific metadata. Use it to inspect the ELF header (-h), program headers/segments (-l), and section headers (-S).
  • objdump: A powerful general-purpose tool for inspecting binary content. Its most important feature is the disassembler (-d), but it's also used for dumping section data (-s) and viewing symbols (-t).
  • strings: A simple but effective utility for finding embedded text, perfect for initial triage.
  • nm: The specialist for listing symbols, with the crucial ability to demangle C++ names.

In our next lesson, we will move from inspection to modification. You will learn to use a hex editor to manually inspect and alter the raw bytes of a binary, giving you the power to patch programs directly at their lowest level.