πŸš€ What is a Loader?

A Loader is a part of the Operating System responsible for:

Loading an executable file (machine code) from storage into main memory (RAM) and preparing it for execution by the CPU.

Without the loader, compiled code would just sit on diskβ€”never executed.


🧠 Responsibilities of a Loader

TaskDescription
🧾 Loads program into RAMReads the binary executable from disk into memory.
πŸ—ΊοΈ Allocates memoryAllocates memory for code, data, stack, and heap sections.
🧭 Sets up the program counterInitializes CPU registers, especially PC (Program Counter) to point to main().
πŸ”— Handles linking (in some systems)Resolves addresses of external symbols (functions/libraries).
πŸ“¦ Initializes environmentPrepares runtime environment: args, env variables, etc.
▢️ Starts executionHands control to the entry point of the program (like _start or main)

πŸ” Where Does the Loader Fit?

[Source Code] β†’ [Compiler] β†’ [Executable File (.exe/.out)] 
                           ↓
                    [Loader (OS)]
                           ↓
                  [Program in RAM, Ready to Run]

🧩 Loader vs Linker (Often Confused)

FeatureLoaderLinker
πŸ“¦ PurposeLoads program into memoryCombines multiple object files into a single executable
🧠 When?At runtimeAt compile time (static) or load time (dynamic)
πŸ“ InputExecutable file (e.g., ELF, PE)Object files (.o, .obj)
πŸ”§ Resolves addresses?Yes, if dynamic linkingYes, during linking phase

πŸ§ͺ Real-World Examples

OSLoader Component
Linuxld-linux.so, execve() syscall
WindowsWindows PE Loader
Unixexec, fork, ld.so
EmbeddedBootloaders (like U-Boot) do this job at power-on

πŸ“œ Types of Loading (Interview-Level Detail)

TypeDescription
Absolute LoadingLoad program at a fixed address. Rare today.
Relocatable LoadingLoad at any memory address. Relocation logic adjusts addresses.
Dynamic LoadingLoad parts of the program (like libraries) only when needed, not at startup.

🧠 Interview-Ready Definition:

A Loader is a system program in the OS that loads an executable into memory, sets up the runtime environment, resolves addresses (if needed), and starts execution by transferring control to the program’s entry point.