C Mastery / Symbol Tables, Relocations, and Sections
Part 5 — Compilation, Linking, and Building

Symbol Tables, Relocations, and Sections

This chapter explains the three key structures inside object files: sections, symbol tables, and relocations. These are what the linker manipulates to build an executable.

Why This Matters

Object files are not just machine code; they carry metadata that makes separate compilation and linking possible. Understanding sections, symbols, and relocations is how you debug link errors, understand executable formats, and reason about memory layout.

Prerequisites

Core Concept

Sections

An object file is divided into named sections, each a contiguous region:

SectionContents
.textmachine code
.datainitialized non-const data
.rodataread-only data (string literals, const)
.bssuninitialized data (zero at load, no bytes in file)
.symtabsymbol table
.rela.textrelocations for .text

Symbol table

The symbol table maps names to definitions/references:

function body or global variable).

to printf).

Relocations

A relocation is a note: "this address in the object refers to symbol X; patch it at link time." When the linker assigns final addresses, it fills in these references.

Examples

Inspecting an object file (GNU tools)

cc -c main.c -o main.o
readelf -S main.o     # sections
readelf -s main.o     # symbols
readelf -r main.o     # relocations

Object file contents, conceptually

For:

extern int ext;
int global = 5;

int f(void) { return ext + global; }

The object has:

How It Works

The assembler groups code/data into sections, records symbols, and emits relocations wherever an address depends on a symbol whose final value is unknown. The linker merges sections from all objects, lays them out, assigns final addresses, and applies relocations.

Variations

BSS has no file bytes

.bss occupies no space in the object file; it is zeroed at load time. Only its *size* is recorded.

Debug sections

With -g, the compiler emits .debug_* sections (DWARF), used by debuggers (c.debug.2).

Common Mistakes

"undefined reference").

final layout).

Undefined Behavior

Portability

exact section names and formats differ (ELF on Linux, Mach-O on macOS, PE on Windows — c.build.5).

Under the Hood

readelf/objdump reveal the raw structures. The linker reads all objects, resolves symbols, merges sections, and writes the final executable format.

Practical Usage

object contains.

binary.

Exercises

1. Compile a small program to an object and inspect its sections, symbols, and relocations with readelf. 2. Identify which section each global/function lives in. 3. Create an undefined-reference error and find it in the symbol table.

Deep Challenge

Explain, step by step, how the linker turns a .rela.text relocation for an external function call into a final address, and what changes if the target is in a shared library (introducing GOT/PLT — see c.link.2).

References

objdump).

Verification

structure. VERIFIED

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.build.symbols06
c.build.relocations06
c.build.sections06