The ELF format

The ELF header

ELF (Executable and Linkable Format) is what nasm -f elf64 and ld produce. A header identifies the file, then program headers tell the loader where to map each segment, and section headers tell the linker where each section lives.

nasm -f elf64 -o hello.o hello.asm readelf -h hello.o

readelf and objdump

readelf shows structure: -h the header, -S the section table, -s the symbol table. objdump -d disassembles the code back to instructions, which is how you check what your assembler actually emitted:

readelf -S hello.o objdump -d hello

Symbols

global makes a symbol visible to the linker; everything else stays local to the object. extern imports a symbol from another object. The symbol table is also how gdb maps addresses back to your labels.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does ELF stand for?

    Answer

    Executable and Linkable Format

    ELF is the Executable and Linkable Format used by Linux and many Unix systems.

  2. Which readelf option prints the ELF header?

    Answer

    -h

    -h is the header; -S shows sections, -s symbols and -d dynamic information.

  3. Which readelf option prints the section table?

    Answer

    -S

    The capital S selects the section headers.

  4. What do program headers tell the loader?

    Answer

    Where to map each segment

    Program headers describe the loadable segments and their addresses and permissions.

  5. What does objdump -d hello show?

    Answer

    The disassembly of the code

    -d decodes the machine code back into instructions.

  6. Which directive makes a symbol visible to the linker?

    Answer

    global

    global exports the symbol in the object symbol table.

  7. Which directive imports a symbol from another object?

    Answer

    extern

    extern declares a name this object uses but does not define.

  8. How does gdb map an address back to a label?

    Answer

    Through the symbol table in the file

    The symbol table holds the name and address of each symbol, and debug information adds the lines.

  9. Which part of an ELF file identifies the architecture and the file type?

    Answer

    The ELF header

    The header holds the class, the byte order, the machine and the type of the file.

  10. What does the section table tell the linker?

    Answer

    Where each section lives and how big it is

    Each entry names a section and gives its offset, size, address and flags.

The code on this page was assembled with NASM 3.01 and inspected with GNU readelf and objdump 2.45.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev