Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleSmartLoader — ELF Loader with Demand Paging

A smart ELF loader written in C that loads and executes ELF binaries using demand paging — memory for a program's segments is allocated lazily, only when the program actually accesses it, rather than all upfront. This mirrors how real operating systems handle program loading efficiently.

Built as part of my Operating Systems coursework, extending a basic ELF loader to handle page faults and on-demand memory allocation.


What Makes It "Smart"

A basic loader copies all segments into memory before running the program. This smart loader instead:

  1. Loads lazily — segments are not pre-loaded into memory
  2. Catches page faults — when the program accesses an unmapped address, a SIGSEGV handler triggers
  3. Allocates on demand — the handler maps in exactly the page(s) needed at that moment
  4. Resumes execution — the program continues as if the memory was always there
  5. Reports statistics — tracks total page faults and allocated pages to show the efficiency gain

Tech & Concepts

  • Language: C
  • Core OS concepts: demand paging, page faults, signal handling, virtual memory, ELF format
  • System calls: mmap, mprotect, signal handling (SIGSEGV via sigaction)
  • Build: Makefile-based

Getting Started

Prerequisites

  • GCC and make
  • A Linux/Unix environment

Build & Run

# Clone the repository
git clone https://github.com/Goyamjain06/OS_projectttt.git
cd OS_projectttt

# Build using the Makefile
make

# Run the smart loader on a sample ELF binary
./simplesmartloader sum.elf

The loader executes the binary and reports page-fault statistics (number of page faults, pages allocated, internal fragmentation).


Project Structure

.
├── loader.c            # Core smart loader: ELF parsing + page-fault handler
├── loader.h            # Loader interface and data structures
├── simplesmartloader   # Compiled loader executable
├── fib.c / sum.c       # Sample programs (source)
├── sum.elf / sum.o     # Compiled test binaries
└── Makefile            # Build configuration

How It Works (High Level)

When the loader starts a program, it does not copy the segments into memory. Instead it registers a SIGSEGV signal handler. As soon as the program tries to access an address that isn't mapped yet, the CPU raises a page fault, the handler runs, and it uses mmap to map in just the page containing that address — loading the corresponding chunk of the ELF segment. Control then returns to the faulting instruction, which now succeeds. Over the run, the loader counts how many page faults occurred and how much memory was actually needed.


What I Learned

  • How demand paging saves memory by loading pages only when accessed
  • Writing a SIGSEGV handler to intercept and resolve page faults
  • Using mmap for fine-grained, page-aligned memory allocation
  • The trade-off between page-fault overhead and memory efficiency
  • [Add your own line — e.g. debugging alignment issues, measuring fragmentation]

Future Improvements

  • Support for multiple loadable segments with different permissions
  • Page eviction / replacement policy
  • Detailed per-segment fault statistics

About

A simple ELF loader implemented in C that demonstrates the process of loading and executing ELF binaries. Includes step-by-step handling of program headers, memory mapping, and dynamic linking basics.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages