On this page

A binary isn’t a black box: it has a fingerprint, a table of contents, a symbol table, and a byte-by-byte layout. You just have to know which tool to ask.

Once a build finishes, you’re left with a file that runs—but explains nothing. You can read the source code with your own eyes, yet you can’t see what actually landed in machine code, how much space data takes up, where the linker wrote a string’s address, or which bytes correspond to which instruction. In this lesson, I build a hybrid C99 + NASM testbed and inspect it with six CLI utilities. This isn’t a textbook on the ELF specification—it’s the practical set of questions and commands I reach for every day.

Mental Model: Six Questions for One File

Question Tool What it shows
What kind of file is this? file word size, byte order, file type, architecture, linking mode
How big is it? size, size -A total size by access permissions and per-section breakdown
How is it laid out? readelf -h, -S, -l header, section table, memory map
Where are symbols and addresses? nm, readelf -r symbol names and unresolved relocations
What bytes encode the instructions? objdump -d disassembly in AT&T or Intel syntax
What’s in the file byte by byte? xxd -g 1 raw hex dump

None of these tools is the single “primary” one: file answers one question, readelf another, and objdump doesn’t replace nm. Below, I walk through this list on the same binary.

What to watch for in this lesson
As in lessons 00 and 01, the main goal here is learning how to interrogate a compiled binary using file, size, readelf, nm, objdump, and xxd. The lea and ret instructions, the 48 prefix, and relocation tables just serve as live specimens for the tools—we’ll cover them in detail across lessons 03–17.

Testbed: C99 Plus Assembly

The C99 side checks the first byte of a string returned by an assembly function:

probe.c
/* SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0 */

/* probe.c — C99 check of the first byte of the string from asm_marker(). */

#include <stdint.h>

extern const char *asm_marker(void);

int main(void)
{
    const char *marker = asm_marker();

    if (marker[0] != 'a') {
        return 1;
    }

    return 0;
}

The assembly function returns the address of a string living in the read-only .rodata section:

probe.asm
; SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

; probe.asm — asm_marker function: returns the address of a string in .rodata.

default rel

section .rodata
    marker: db "asm", 0

section .text
    global asm_marker

; ------------------------------------------------------------------------------
; const char *asm_marker(void)
; Input:    none
; Output:   rax = address of string marker
; Clobbers: nothing except rax (flags untouched)
; ------------------------------------------------------------------------------
asm_marker:
    lea rax, [marker]

    ret

section .note.GNU-stack noalloc noexec nowrite progbits

The build combines both object files into a single ELF executable:

$ nasm -f elf64 -g -F dwarf -o build/probe_asm.o probe.asm
$ gcc -std=c99 -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Werror -pedantic \
    -g -O0 -no-pie -c -o build/probe.o probe.c
$ gcc -no-pie -o build/probe build/probe.o build/probe_asm.o
$ ./build/probe; echo $?
0

That last command invokes gcc rather than ld directly, so it implicitly links CRT and libc. That’s why build/probe contains _start from crt1.o instead of just main; ld on its own links neither CRT nor libc.

An exit code of 0 means the string was found and its first byte matched 'a'. We’ll cover calling conventions and register roles later; right now, the binary itself is our focus.

Mechanics: Reading the File Layer by Layer

file: The File’s Fingerprint

$ file build/probe
build/probe: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=52738eb92ecedc4e98016fa535e2450593089a41, for GNU/Linux 4.4.0, with debug_info, not stripped

Each field answers a separate question:

  • ELF 64-bit — ELF class (64-bit addresses and header fields);
  • LSB — little-endian byte order (more in lesson 06);
  • executable — file type: an executable binary with fixed addresses, since it was built with -no-pie;
  • x86-64 — target architecture;
  • version 1 (SYSV) — ELF format version 1 and the System V standard (its calling rules are covered in lesson 18);
  • dynamically linked — the program loads external libraries (such as libc) at runtime;
  • interpreter /lib64/ld-linux-x86-64.so.2 — path to the dynamic loader: on launch, the kernel maps both the ELF binary and this loader, and hands control to ld-linux first; ld-linux loads the required .so libraries and then passes control to _start;
  • BuildID[sha1]=... — unique build hash used by debuggers to match debug symbols to an exact binary build;
  • for GNU/Linux 4.4.0 — minimum Linux kernel version and ABI target (more in lesson 18);
  • with debug_info — DWARF data is embedded;
  • not stripped — function and data symbol names are intact.

size: How Much Space It Takes

$ size build/probe
   text	   data	    bss	    dec	    hex	filename
   1137	    488	      8	   1633	    661	build/probe

Without flags, size doesn’t print “file size”—it prints byte totals grouped into three permission categories:

  • text — all allocated read-only and executable sections;
  • data — mutable data with initial values;
  • bss — mutable data zero-initialized at startup.

dec and hex are the sum of those three numbers in decimal and hexadecimal. The file itself is larger on disk because it also contains headers, padding, and debug info.

The per-section breakdown shows where those numbers come from:

$ size -A build/probe | grep -E 'section|\.(text|rodata|data|bss)\b|\.comment|\.debug_info|Total'
section              size      addr
.text                 296   4198432
.rodata                 8   4202496
.data                  16   4210688
.bss                    8   4210704
.comment               27         0
.debug_info           312         0
Total                2610

text = 1137 is the sum of all read-only allocated sections, not just .text (296 bytes) on its own. That total includes auxiliary sections such as:

  • .interp — the string holding the dynamic linker path;
  • .dynsym — the symbol table needed for dynamic linking;
  • .eh_frame — stack-unwinding tables used for exception handling and debugging.

Total = 2610 exceeds text + data + bss = 1633 because the full tally also counts non-loaded sections like .comment (the compiler version string) and every .debug_* section (some of them, along with auxiliary linker tables, were filtered out by grep, which is why the displayed rows sum to less than 2610). Their address is 0—the kernel doesn’t map them into RAM, though they still sit on disk. Here, .debug_info is uncompressed: in readelf -SW it has no C flag, and its Size field is 000138, matching the 312 bytes reported by size. Note that Total on another machine or in a different directory may differ from 2610 by tens of bytes: with -g, GCC records the build directory path (DW_AT_comp_dir) in DWARF as a string inside .debug_line_str, so path length changes debug section sizes.

size -A prints sizes and addresses in decimal (296 = 0x128, 4198432 = 0x401020), whereas readelf shows those same numbers in hex.

The 16 bytes of .data and 8 bytes of .bss don’t come from probe.c: CRT object files bring them in, and the first 4 bytes of .rodata are occupied by the internal _IO_stdin_used marker.

Under the hood: where the extra bytes in sections come from (CRT)
  • a 4-byte zero object from crt1.o plus __dso_handle from crtbegin.o (8 bytes), together with alignment padding, produce 16 bytes in .data;
  • the 1-byte completed.0 flag from crtbegin.o plus section-end alignment yields 8 bytes in .bss;
  • the first 4 bytes of .rodata hold the internal _IO_stdin_used marker from crt1.o, pushing marker to +4 bytes (we’ll inspect the raw bytes below in the xxd section).

readelf: How the File Is Laid Out

The ELF header answers “what is this?” and “how many tables live inside?”:

$ readelf -hW build/probe | grep -E 'Magic|Class|Data|Type|Machine|Entry point|Number of'
  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
  Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64
  Entry point address:               0x401020
  Number of program headers:         15
  Number of section headers:         35

Each header field answers a specific question:

  • Magic — the magic signature at the very start of the file, which the Linux kernel (execve) and file use to recognize ELF immediately (corrupt even one of these first four bytes and the kernel refuses to execute the file). The first byte is the non-printable control code 0x7f (ASCII DEL—chosen deliberately so no plain text file matches an ELF header), followed by the ASCII codes for E (45), L (4c), and F (46). In the Magic: row, readelf prints all 16 bytes of the e_ident identifier, where \x7fELF is immediately followed by ELF class (02), byte order (01), ELF version (01), and OS ABI (00);
  • Class: ELF64 — 64-bit addresses and header fields;
  • Data: 2's complement, little endian — signed integers use two’s complement, and byte order is little-endian;
  • Type: EXEC — an executable with fixed addresses (unlike a relocatable REL object or a position-independent PIE binary, which has type DYN);
  • Entry point address: 0x401020 — virtual address of the first instruction to execute (the first 4 KiB starting at 0x400000 hold ELF headers, and code begins on the next page at 0x401000);
  • Number of program headers: 15 — number of entries in the loader’s memory map;
  • Number of section headers: 35 — number of entries in the file’s section table.

Entry point isn’t main: CRT provides the entry point (_start from crt1.o), which in turn calls main. We’ll write our own _start in lesson 04.

The file is built from sections. To avoid wading through all 35 entries, I filter down to the ones we care about:

$ readelf -SW build/probe | grep -E '\.(text|rodata|data|bss)\b'
  [10] .text             PROGBITS        0000000000401020 001020 000128 00  AX  0   0 16
  [12] .rodata           PROGBITS        0000000000402000 002000 000008 00   A  0   0  4
  [23] .data             PROGBITS        0000000000404000 003000 000010 00  WA  0   0  8
  [24] .bss              NOBITS          0000000000404010 003010 000008 00  WA  0   0  1

Columns read left to right:

  • Nr — entry index;
  • Name — section name;
  • Type — PROGBITS (bytes exist in the file) or NOBITS (no bytes stored in the file; memory is zeroed on load);
  • Address — virtual address;
  • Off — offset inside the file;
  • Size — section size;
  • ES — table entry size (0 for these sections);
  • Flg — access flags: A means the section is allocated in process memory, X allows instruction execution, W allows writes;
  • Lk and Inf — index links to related sections for metadata tables (for instance, .symtab points via Lk to .strtab holding its symbol name strings, while .rela.text points via Inf to .text, the section it patches; regular code and data sections have no links, so these are zero);
  • Al — alignment.

Section flags:

  • .text has AX: code is readable and executable;
  • .rodata has only A: writes are forbidden;
  • .data and .bss have WA: memory is writable.

NOBITS on .bss explains why it takes zero bytes on disk even though its size is 8.

Sections are grouped into segments, which are what the kernel actually maps:

$ readelf -lW build/probe | grep -E 'LOAD|GNU_STACK'
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x000558 0x000558 R   0x1000
  LOAD           0x001000 0x0000000000401000 0x0000000000401000 0x000155 0x000155 R E 0x1000
  LOAD           0x002000 0x0000000000402000 0x0000000000402000 0x000160 0x000160 R   0x1000
  LOAD           0x002e28 0x0000000000403e28 0x0000000000403e28 0x0001e8 0x0001f0 RW  0x1000
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10

The four LOAD entries define a memory map with distinct permissions:

  • first — read-only headers and metadata tables (R);
  • second — code with R E permissions (read and execute);
  • third — read-only .rodata (R);
  • fourth — writable RW data (read and write).

Permissions are enforced per memory page, which is why code, read-only data, and writable data live in separate segments—otherwise you couldn’t forbid writes to .text or .rodata. The 0x1000 alignment is 4096 bytes, the x86-64 page size; because the kernel maps memory in pages, segment boundaries are multiples of 4 KiB.

On the last LOAD segment, FileSiz and MemSiz differ: 0x1e8 vs. 0x1f0. The 0x1f0−0x1e8=8\mathtt{0x1f0} - \mathtt{0x1e8} = 8-byte difference is .bss, which doesn’t exist in the file but gets zeroed in memory. The GNU_STACK line showing RW without E confirms that the stack is non-executable.

The fourth LOAD segment starts at offset 0x2e28 and virtual address 0x403e28, ahead of .data itself (0x404000), because linker metadata sections (.init_array, .fini_array, .dynamic, .got) sit right before .data in the same RW segment. Debug sections (.debug_*) stay on disk only:

 File on disk (offsets)                   Virtual memory (LOAD segments)
┌─────────────────────────┐              ┌───────────────────────────────────┐
│ Headers, .interp ...    │ ──LOAD 1──►  │ 0x400000  [R  ] headers           │
├─────────────────────────┤              ├───────────────────────────────────┤
│ .text (296 B)           │ ──LOAD 2──►  │ 0x401000  [R E] code              │
├─────────────────────────┤              ├───────────────────────────────────┤
│ .rodata (8 B)           │ ──LOAD 3──►  │ 0x402000  [R  ] constants         │
├─────────────────────────┤              ├───────────────────────────────────┤
│ .dynamic, .got, .data   │ ──LOAD 4──►  │ 0x403e28  [RW ] mutable +         │
├─────────────────────────┤              │           .bss (8 B zeros in RAM) │
│ .symtab, .debug_*       │ (not in RAM) └───────────────────────────────────┘
└─────────────────────────┘

nm: Symbols and Addresses

Function and data names live in the symbol table. In the C99 object file, we can see the call isn’t resolved yet:

$ nm -n build/probe.o
                 U asm_marker
0000000000000000 T main

U means undefined—the symbol is declared, though its definition lives in another object file. T marks a global function in .text. The asm_marker symbol is defined in the assembly object file:

$ nm -n build/probe_asm.o
0000000000000000 T asm_marker
0000000000000000 r marker

T on asm_marker means an exported function; r on marker means local read-only data. Both addresses are still zero: those are offsets within the object file. After linking, the addresses become real:

$ nm -n build/probe | grep -E ' (T|t) (main|asm_marker)$| (r|R) marker$'
0000000000401116 T main
0000000000401140 T asm_marker
0000000000402004 r marker

readelf -r: Who Patches the Addresses

Until a string’s address is known, the linker needs to be told where to write it. Those unresolved slots are recorded as relocations:

$ readelf -rW build/probe.o | sed -n '/\.rela\.text/,/^$/p'
Relocation section '.rela.text' at offset 0x498 contains 1 entry:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000000009  0000000900000004 R_X86_64_PLT32         0000000000000000 asm_marker - 4

$ readelf -rW build/probe_asm.o | sed -n '/\.rela\.text/,/^$/p'
Relocation section '.rela.text' at offset 0x8c0 contains 1 entry:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
0000000000000003  0000000200000002 R_X86_64_PC32          0000000000000000 .rodata - 4

Relocation fields:

  • Offset — position of the field inside .text that the linker will overwrite;
  • Type — address calculation formula: R_X86_64_PC32 means a plain offset relative to the current instruction, while R_X86_64_PLT32 means a call through the PLT (we cover the full mechanics of dynamic linking, PLT, and GOT in lesson 17). In probe.o, the compiler doesn’t know yet whether asm_marker lives in a neighboring object file or an external .so library, so it emits PLT32 conservatively; after linking with probe_asm.o, the linker sees the function is local and patches in a direct call with no stub;
  • Symbol's Name — target of the relocation;
  • Addend - 4 — adjustment for the width of the 4-byte displacement field: at runtime rip points to the end of the lea instruction rather than the start of the field, so we subtract the field’s start address plus its 4 bytes from the target address;
  • once linked, the target of the relocation in probe_asm.o becomes 0x402004 <marker>, which objdump prints in a comment next to lea.

In the assembly object, Offset is 3: lea takes three bytes before its 4-byte displacement field. The Info column packs the symbol index and relocation type into one number; for reading, Type and Symbol's Name are all you need. Static relocations in .rela.text disappear from the final binary once all local code and data addresses are patched in (only dynamic slots in .rela.dyn and .rela.plt remain for external library functions, covered in lesson 17).

The linker and the CPU compute the offset in two independent steps:

  1. The linker takes the target symbol’s virtual address, adds Addend (here −4), and subtracts the start address of the 4-byte displacement field inside the instruction. The result—disp32—is written into that 4-byte slot.
  2. At runtime, the CPU adds disp32 to the address of the next instruction to obtain the final target address.
disp32=addr(sym)+A−addr(field)target=ripnext+disp32 \text{disp32} = \text{addr}(\text{sym}) + A - \text{addr}(\text{field}) \\[4pt] \text{target} = \text{rip}_{\text{next}} + \text{disp32}
where
disp32\text{disp32}
32-bit displacement written by the linker into the instruction’s field.
addr(sym)\text{addr}(\text{sym})
final virtual address of the target symbol.
AA
Addend correction from the relocation table; here -4.
addr(field)\text{addr}(\text{field})
start address of the 4-byte displacement field inside the instruction.
target\text{target}
address at which the processor continues execution after a jump.
ripnext\text{rip}_{\text{next}}
address of the instruction immediately following the current one.

For probe_asm.o, the field starts at 0x401143: lea uses three bytes starting from 0x401140. This layout diagram shows where A = -4 comes from in the formula:

address     bytes            meaning
0x401140    48 8d 05         lea rax, [rip + disp32]
0x401143    bd 0e 00 00      disp32 field: 4 bytes, addr(field)
0x401147    —                rip_next = addr(field) + 4

The linker knows the symbol’s address and the field’s start address, but not rip_next: the CPU measures the offset from the next instruction, not from the field. Those exact four bytes are what A = -4 (the length of the displacement field itself) compensates for:

disp32=addr(sym)−4−addr(field)=addr(sym)−(addr(field)+4)=addr(sym)−ripnext \begin{aligned} \text{disp32} &= \text{addr}(\text{sym}) - 4 - \text{addr}(\text{field}) \\[2pt] &= \text{addr}(\text{sym}) - \bigl(\text{addr}(\text{field}) + 4\bigr) \\[2pt] &= \text{addr}(\text{sym}) - \text{rip}_{\text{next}} \end{aligned}

With that substitution, the linker’s math matches what the CPU will do: add disp32 to the next instruction’s address. Plugging in the numbers from our testbed:

0x402004−4−0x401143=0xebd,0x401147+0xebd=0x402004. \mathtt{0x402004} - 4 - \mathtt{0x401143} = \mathtt{0xebd}, \\[4pt] \mathtt{0x401147} + \mathtt{0xebd} = \mathtt{0x402004}.

The target is the exact address of marker reported by nm.

objdump: Bytes and Instructions

By default, the disassembler prints AT&T syntax:

$ objdump -d build/probe | sed -n '/<asm_marker>:/,/^$/p'
0000000000401140 <asm_marker>:
  401140:	48 8d 05 bd 0e 00 00 	lea    0xebd(%rip),%rax        # 402004 <marker>
  401147:	c3                   	ret

In AT&T syntax, percent signs mark registers and the source precedes the destination: 0xebd(%rip),%rax reads as “compute offset 0xebd from rip and store it in rax.” Passing -M intel makes the same code look familiar after NASM:

$ objdump -d -M intel build/probe | sed -n '/<asm_marker>:/,/^$/p'
0000000000401140 <asm_marker>:
  401140:	48 8d 05 bd 0e 00 00 	lea    rax,[rip+0xebd]        # 402004 <marker>
  401147:	c3                   	ret

Only the notation differs: Intel syntax puts the destination on the left, drops % from registers, and wraps displacements in square brackets. The bytes are identical:

  • 48 — 64-bit operand prefix: switches lea to 64-bit width (prefixes are covered in lesson 03);
  • 8d — opcode for the lea instruction;
  • 05 — addressing-mode byte for “offset relative to rip”;
  • bd 0e 00 00 — the displacement itself;
  • c3 — the single-byte ret instruction.

The bytes bd 0e 00 00 encode 0xebd: least significant byte first, which is little-endian order (covered in lesson 06).

xxd: Byte-by-Byte Hex Dump

The file header begins with the exact bytes readelf showed:

$ xxd -g 1 -l 16 build/probe
00000000: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00  .ELF............

-g 1 groups output one byte at a time; -l 16 limits the dump to 16 bytes. Header fields:

  • 7f 45 4c 46 — the \x7fELF signature;
  • 02 — ELF64 class;
  • 01 — little-endian;
  • 01 — format version;
  • 00 — standard EI_OSABI value for System V (baseline Unix compatibility without OS-specific extensions).

The remaining zeros are reserved padding in the header. The column on the right shows those same bytes as printable ASCII characters, with dots replacing non-printable bytes.

We can inspect the .rodata string using the offset from readelf: .rodata starts at Off = 002000 and spans 8 bytes. Dumping the whole section:

$ xxd -g 1 -s 0x2000 -l 8 build/probe
00002000: 01 00 02 00 61 73 6d 00                          ....asm.

The first four bytes are the _IO_stdin_used constant from crt1.o, holding 0x00020001: since the late 1990s, glibc has used it to distinguish modern binaries from legacy ones built against an older internal FILE * layout in stdio. It has nothing to do with our marker string, though it still takes up the first four bytes of the section. Next come the character codes:

  • 61, 73, 6d — a, s, m in ASCII;
  • 00 — the trailing null terminator byte.

Aliases: Four Commands Instead of Ten Flags

Long commands quickly pile up flags, so I keep short aliases in a separate file and source it from ~/.bashrc or ~/.zshrc:

aliases.sh
# SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0

# aliases.sh — shortcuts for inspecting binaries.
# Usage: source aliases.sh (in ~/.bashrc or ~/.zshrc).

alias elf='readelf -W'
alias disasm='objdump -d -M intel --no-show-raw-insn'
alias hex='xxd -g 1'
alias gdb-clean='gdb -q --nx'

elf enables wide output, though it doesn’t choose what to display: -W is a width modifier, not an action. You still specify the view separately:

  • elf -h build/probe — header;
  • elf -S build/probe — sections;
  • elf -l build/probe — segments;
  • elf -r build/probe.o — relocations;
  • disasm — Intel syntax without raw bytes, when you only want instructions;
  • hex — shorthand for xxd -g 1; just append -s and -l;
  • gdb-clean — a helper for lesson 05 that launches the debugger without reading local config files (we don’t run GDB in this lesson).

When I need raw bytes, I call objdump -d -M intel directly. Running readelf -W build/probe without an action flag prints the Usage help text. GDB itself is covered in lesson 05.

After editing your shell rc file, open a new shell instance: an already running shell won’t reload the file. Verify with type disasm; if the shell prints not found, the alias isn’t loaded.

Common Pitfalls

  1. "readelf prints usage help instead of inspecting the file."
    Fact: readelf -W build/probe outputs Usage: readelf <option(s)> elf-file(s).
    Cause: -W only widens lines; it doesn’t select any data to display.
    Fix: Add an action flag—readelf -hW, readelf -SW, readelf -lW, or readelf -rW.

  2. "readelf output is too long and drowns out what I need."
    Fact: Dozens of auxiliary sections and the Key to Flags legend flood the terminal.
    Cause: The utility prints everything it finds.
    Fix: Filter by section names (grep -E '\.(text|rodata|data|bss)\b') and don’t copy the legend into notes—flag meanings live in the Flg column.

  3. "objdump shows %rax instead of rax."
    Fact: By default, the disassembler outputs AT&T syntax.
    Cause: Historical GNU default.
    Fix: Add -M intel so operand order and register names match NASM.

  4. "nm shows U asm_marker with no address."
    Fact: In probe.o, the symbol is marked U, whereas in probe it’s marked T with an address.
    Cause: The object file hasn’t been linked yet, so no address has been assigned.
    Fix: Run nm on the linked binary or on the object file where the symbol is defined.

  5. “The xxd dump doesn’t match the expected string.”
    Fact: Offset 0x2000 yields 01 00 02 00 61 73 6d 00 instead of starting with asm.
    Cause: The first 4 bytes are _IO_stdin_used from crt1.o; the string starts at offset 0x2004.
    Fix: Check the section’s Off in readelf -SW, account for linker startup objects, and verify file signatures via the leading bytes.

Lab

Goal. Build the hybrid probe.c + probe.asm testbed, inspect it with all six tools, and explain every non-trivial field.

Contract. Build build/probe.o, build/probe_asm.o, and build/probe using the commands in the Testbed section. Then collect six measurements:

  1. file build/probe and size -A build/probe.
  2. readelf -SW build/probe filtered down to the four core sections.
  3. readelf -lW build/probe filtered for LOAD and GNU_STACK.
  4. nm -n on both object files and the linked binary.
  5. readelf -rW build/probe_asm.o and objdump -d -M intel build/probe.
  6. xxd -g 1 -s 0x2000 -l 8 build/probe.

Verification criteria.

  1. file reports ELF 64-bit LSB executable, dynamically linked, and with debug_info.
  2. In readelf -SW, .bss has type NOBITS, and in size -A, Total exceeds text + data + bss.
  3. In readelf -lW, the last LOAD segment has FileSiz smaller than MemSiz by the exact size of .bss.
  4. In nm, asm_marker changes from U to T with a real address after linking.
  5. In readelf -rW, lea uses R_X86_64_PC32, and Offset equals the opcode length before the displacement field.
  6. xxd -g 1 -s 0x2000 -l 8 shows 01 00 02 00 followed by 61 73 6d 00.
Hints, expected output, and walkthrough

Expected values for this testbed:

text
file:   ELF 64-bit LSB executable, x86-64, dynamically linked, with debug_info, not stripped
size:   text 1137, data 488, bss 8, dec 1633  # sample output; numbers may vary
size -A: .text 296, .rodata 8, .data 16, .bss 8, Total 2610
sections: .text PROGBITS AX, .rodata PROGBITS A, .data PROGBITS WA, .bss NOBITS WA
segments: last LOAD FileSiz 0x1e8, MemSiz 0x1f0 (diff 8 = .bss)
nm:     probe.o → U asm_marker; probe → T asm_marker 0x401140
reloc:  .rela.text, Offset 3, R_X86_64_PC32, .rodata - 4
bytes:  48 8d 05 bd 0e 00 00 (lea), then c3 (ret)
xxd:    0x2000 → 01 00 02 00 (_IO_stdin_used), 0x2004 → 61 73 6d 00 ("asm\0")

Walkthrough:

  • .bss has type NOBITS, so it holds no bytes on disk: it gets zeroed in memory and only increases MemSiz in LOAD;
  • a U symbol in an object file is normal—the linker hasn’t resolved its address yet;
  • relocation offset 3 is the three bytes 48 8d 05 preceding the 4-byte displacement field;
  • Addend - 4 is needed because rip points to the end of the instruction, not the start of the field.

Address check: 0x401147 + 0xebd = 0x402004, which matches marker. If your numbers differ, check your compiler version and keep the commands as-is: exact addresses depend on layout, while the relationships (FileSiz, MemSiz, Offset, Addend) always hold.

Lab source code, Makefile, and automated tests for this lesson: lessons/02-terminal-tools.

Summary

  1. file answers “what kind of file is this?” without replacing the rest of the toolkit.
  2. size groups bytes by access permissions, while size -A shows each section and includes non-loaded debug data.
  3. readelf -h reads the header, -S the sections, and -l the segments; legends and noise can be filtered out.
  4. nm shows symbol names and states: U before linking, T with a real address afterward.
  5. readelf -r reveals where the linker will patch an address, while objdump shows bytes and instructions in AT&T or Intel syntax.
  6. xxd -g 1 ties raw file bytes directly to header fields and .rodata contents.
Next Up
We’ve inspected the binary: we can see where code and data live, which symbols remain, and where addresses point. What actually happens to those bytes during execution isn’t clear yet. In lesson 03, I break down the x86-64 register file, the rax/eax/ax/ah/al sub-register windows, the zero-extension rule, and the mov r8b, ah error.