A loader is a system utility and an essential component of an operating system responsible for taking an executable file (generated by the linker) from secondary storage (disk) and placing it into the main memory (RAM) for execution. It acts as the final bridge between static code on your disk and a running process in the CPU.
Core Functions of a Loader
The loader typically performs four fundamental tasks to prepare a program:
- Allocation: Reserving the required space in main memory based on the program's size.
- Linking: Resolving symbolic references between different modules or libraries that were not settled during static linking.
- Relocation: Adjusting internal memory addresses within the code so it can run correctly at its assigned location in RAM.
- Loading: Physically copying the machine code and data into the allocated memory space.
Types of Loaders
- Absolute Loader: Loads the program into a fixed, predetermined memory location. It is simple but inflexible because the program cannot be moved if that specific memory spot is occupied.
- Relocating Loader: Allows a program to be loaded anywhere in available memory by modifying addresses on the fly during the loading process.
- Dynamic Loader: Loads libraries and modules only when they are actually needed during program execution, which saves memory.
- Bootstrap Loader: A specialized loader located in ROM that starts as soon as the computer is turned on; its only job is to load the primary operating system into RAM.
Example Scenario
When you double-click a game icon on Windows:
- The OS Loader (invoked by a system call like
execve()on Linux) reads the game's executable file. - It checks for dependencies (like
DirectX.dll) and finds space for them in memory. - It allocates RAM for the game's code, player data, and sound files.
- Finally, it jumps the CPU's instruction pointer to the game's "entry point," and the game starts running.
No comments:
Post a Comment