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
c.build.1— the compilation pipeline.
Core Concept
Sections
An object file is divided into named sections, each a contiguous region:
| Section | Contents |
|---|---|
.text | machine code |
.data | initialized non-const data |
.rodata | read-only data (string literals, const) |
.bss | uninitialized data (zero at load, no bytes in file) |
.symtab | symbol table |
.rela.text | relocations for .text |
Symbol table
The symbol table maps names to definitions/references:
- Defined symbols: a name and its address/size within a section (e.g., a
function body or global variable).
- Undefined symbols: references to names defined elsewhere (e.g., a call
to printf).
- Each symbol has a binding (global, local) and a section index.
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:
.textcontainingf's code;.datacontainingglobal = 5;- a symbol
f(global, defined in.text); - a symbol
global(global, defined in.data); - an undefined symbol
ext; - a relocation in
.textforext.
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
- Confusing a symbol *reference* with a symbol *definition* (the source of
"undefined reference").
- Assuming section order in an object matches source order (the linker decides
final layout).
Undefined Behavior
- None inherent to sections/symbols/relocations; they are metadata.
Portability
- The *concept* of sections, symbols, and relocations is universal, but the
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
- Use
readelf/nm/objdumpto diagnose link errors and inspect what an
object contains.
- Understand
.bssto explain why uninitialized globals do not bloat the
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).
Related Concepts
c.build.1— pipeline.c.build.3— linking.c.link.1— object files and formats.
References
- ELF specification and GNU binutils documentation (
readelf,nm,
objdump).
Verification
- Section/symbol/relocation concepts match ELF and general object-file
structure. VERIFIED
- No example was executed during generation unless noted. `Execution not
verified.`
Progress
- [ ] Read
- [ ] Understand
- [ ] Complete examples
- [ ] Complete exercises
- [ ] Complete deep challenge
Concept checkboxes
- [ ] Sections (.text/.data/.bss/.rodata)
- [ ] Symbol tables
- [ ] Relocations
- [ ] Defined vs. undefined symbols
Mastery levels
| Concept | Current level (0–8) | Target level |
|---|---|---|
| c.build.symbols | 0 | 6 |
| c.build.relocations | 0 | 6 |
| c.build.sections | 0 | 6 |