Reading — step 1 of 5
Read
~1 min readStatic Loading
Sections (Linker View)
Sections are how the linker sees the file. Loaders can mostly ignore them.
Common sections:
.text: executable code..rodata: read-only data (string literals, const arrays)..data: initialized writable data (global variables with initializers)..bss: uninitialized writable data (zero-init globals; takes no file space)..symtab: symbol table (function/var names + addresses)..strtab: string table (names referenced by symtab)..shstrtab: section header string table..dynsym: dynamic symbol table (used by dynamic linker)..dynstr: dynamic string table..rela.text/.rel.text: relocations for .text..dynamic: tags for dynamic linker (DT_NEEDED, DT_RPATH, ...)..plt,.got: dynamic linking machinery..eh_frame,.eh_frame_hdr: stack unwinding info..note.gnu.build-id: unique build identifier..comment: compiler info.
Section header:
struct SectionHeader {
uint32_t name; // offset into .shstrtab
uint32_t type; // SHT_PROGBITS, SHT_SYMTAB, ...
uint64_t flags; // SHF_ALLOC, SHF_WRITE, SHF_EXECINSTR
uint64_t addr; // virtual address (if loaded)
uint64_t offset; // file offset
uint64_t size;
uint32_t link;
uint32_t info;
uint64_t addralign;
uint64_t entsize;
};
Linking process:
- Read .symtab from each input .o.
- Resolve undefined symbols (find in another .o or library).
- Combine sections: all .text from all inputs → output .text.
- Apply relocations: patch addresses now that final positions known.
- Write executable.
Stripping (strip foo): remove debug + symbol info. Smaller binary, harder to debug.
Sections vs Segments:
- Sections: arbitrary categorization. Many.
- Segments: contiguous loadable chunks. Few (typically 3-5).
- Multiple sections can map to one segment (linker decides).
readelf -S foo: list sections. readelf -l foo: list segments.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…