C++ Program Loading
Understand how C++ programs are loaded and executed by the operating system.
Best viewed on desktop for optimal interactive experience
Program Loading & Execution
When you run a C++ program, the OS performs complex steps to transform the executable file into a running process.
Program Loading Process
Executable File
ProcessingBinary file on disk (ELF format)
Kernel Loads File
OS reads ELF headers and validates
Create Process
Allocate process control block (PCB)
Map Segments
Map code and data into virtual memory
Load Dynamic Linker
Load ld-linux.so for dynamic linking
Resolve Libraries
Load and link shared libraries
Initialize Runtime
Set up stack, heap, and globals
Execute main()
Transfer control to user code
Memory Usage Progression
Memory Layout
Once loaded, your program has a well-defined memory structure:
Process Memory Layout
Virtual Address Space (x86-64)
Stack
Return addresses, parameters, local vars
Common Contents:
0x7FFFFFFFFFFF
Memory Permissions
Useful Commands
cat /proc/self/maps
View memory mappmap <pid>
Process memorysize ./program
Segment sizesulimit -s
Stack limitLoading Steps
-
Read ELF headers
- Validate executable format
- Check architecture compatibility
- Find entry point
-
Create process
- Allocate Process Control Block (PCB)
- Assign Process ID (PID)
- Create virtual address space
-
Map segments
- Map code (.text) as read-execute
- Map data (.data, .bss) as read-write
- Set up memory protections
-
Load dynamic linker
- If dynamically linked, load ld.so
- Resolve library dependencies
- Perform relocations
-
Initialize runtime
- Set up stack and heap
- Run global constructors
- Initialize Thread Local Storage
-
Transfer control
- Jump to _start (not main!)
- C runtime setup
- Finally call main()
Memory Regions
Stack & Heap
- Stack: Local variables, function calls
- Heap: Dynamic allocations
Code & Data
- .text: Executable instructions
- .data: Initialized globals
- .bss: Uninitialized globals
- .rodata: Constants
Memory Mapping
- Shared libraries
- Memory-mapped files
- Thread stacks
Before main()
Your code doesn't run first:
// Global constructors run before main class Global { Global() { std::cout << "Before main!\n"; } } g; int main() { std::cout << "In main\n"; return 0; } // Output: // Before main! // In main
Runtime Environment
Process Information
# View process memory cat /proc/self/maps # Check memory usage pmap <pid> # Monitor runtime strace ./program
Environment Variables
# Dynamic linker paths LD_LIBRARY_PATH=/custom/lib # Preload libraries LD_PRELOAD=./hook.so # Debug loading LD_DEBUG=all ./program
Loading Topics
Explore specific aspects:
- π Stack vs Heap - Memory allocation strategies
- π Dynamic Loading - Runtime library loading
- πΊοΈ Virtual Memory - Address translation
Related Topics
The complete journey:
- Compilation - Source to object files
- Linking - Object files to executable
- Loading - Executable to process (you are here)
Advanced Topics
- Smart Pointers - Modern memory management
- RAII - Resource management
- Thread Safety - Concurrent execution