CD Notes
Detailed guide to program loading, memory allocation, relocation, and execution setup
The loader is a system software component that reads an executable file from disk and loads it into memory for execution. It's the final step in the compilation/linking process that prepares a program for CPU execution.
What is a Loader?
A loader is a program (often part of the operating system) that:
- Reads executable files from disk
- Allocates memory sections
- Transfers control to the program's entry point
- Sets up runtime environment
Loader Functions
1. Allocation
Allocate memory sections for code, data, stack, and heap:
2. Relocation
Adjusting final addresses if the program wasn't linked for a specific address:
If executable linked for 0x400000 but loaded at 0x500000
All addresses adjusted: 0x400000 → 0x500000
References updated
CODE @ 0x400100 → 0x500100
DATA @ 0x400500 → 0x500500
3. Linking (Dynamic Linking at Runtime)
Resolving references to dynamically linked libraries:
4. Transfer of Control
Pass control to program's entry point:
| IP/PC register | _start or main() address |
| SP register | top of stack |
| Other registers | initialized values |
Loading Process
Executable File Format
ELF Format (Linux)
ELF Header
Magic number (0x7F 'E' 'L' 'F')
Architecture (x86-64, ARM, etc.)
Entry point
Program header offset
Section header offset
Program Headers
Type, offset, virtual address, size
.text segment
.data segment
.bss segment
Sections
.text (code)
.data (initialized data)
.bss (uninitialized data)
.symtab (symbol table)
.strtab (string table)
...
String tables, symbol tables, etc.
PE Format (Windows)
Runtime Environment Setup
Before execution, the loader sets up:
Program Execution Flow
Interview Q&A
Q: What does a loader do? A: A loader reads executable files from disk, allocates memory, loads code and data sections, resolves dynamic references, and transfers control to the program's entry point.
Q: What's the difference between linker and loader? A: The linker combines object files into executable files at compile time. The loader reads executable files from disk and loads them into memory at runtime for execution.
Q: What is relocation in loading? A: Relocation in loading adjusts addresses if the program is loaded at a different memory location than it was linked for. All addresses are offset accordingly.
Q: What is dynamic linking? A: Dynamic linking resolves references to shared libraries at runtime rather than at link time. Libraries are loaded into memory when needed.
Q: What information does executable file header contain? A: The header contains magic number, architecture, entry point address, file format version, program and section header offsets, and other metadata needed to load the file.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for The Loader.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, language, processing, system, loader
Related Compiler Design Topics