Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleSmartLoader - An Upgraded SimpleLoader in C

Design Document for Operating Systems (OS) Assignment 4


Group Members & Contribution

Modifications made to loader.c from Assignment 1


2024138 - Atharva Singh Velpula

Contributions

  • find_segment()
  • loader_cleanup()
  • my_handler()
  • load_and_run_elf()

2024343 - Mayank Yadav

Contributions

  • my_handler()
  • load_and_run_elf()

Joint Contribution

Both members contributed to:

  • Error handling
  • Bug fixing

Implementation Details

  • The codebase is well documented using:

    • Comments
    • Appropriate variable names
    • Meaningful function names

loader.c

ELF Loading Workflow

Reading ELF Data

The loader:

  • Reads the ELF file using:

    • File descriptors
    • lseek()
    • read()
  • Stores:

    • ELF Header (EHDR)
    • Program Header Table (PHDR) entries

for later use.


Entry Point Execution

Instead of:

  • Allocating memory beforehand
  • Copying all segment data immediately

the loader directly:

  • Typecasts e_entry from the ELF header into the _start() function.
  • Calls _start() directly.

Lazy Loading Mechanism

Page Fault Trigger

When _start() begins execution:

  • The program attempts to access virtual memory addresses that are not yet mapped.
  • This causes a page fault.
  • The operating system raises:
SIGSEGV

Signal Handler Logic

Fault Address Retrieval

The signal handler:

  • Receives the SIGSEGV signal.
  • Uses:
siginfo_t* info

to retrieve the faulty memory address.


Segment Identification

The loader iterates through the Program Header Table to:

  • Find the segment containing the faulty address.

Condition

If no matching segment is found:

  • The fault is treated as a genuine segmentation fault.
  • The loader cannot handle it.

Page Allocation & Mapping

If the segment is found:

Segment Information

Using the phdr entries, the loader determines:

  • Start of the segment
  • End of the segment
  • Start address of the page containing the faulting address

Page Fault Tracking

  • Page fault count is incremented by 1.

Memory Mapping

The loader maps:

  • One page (4 KB) using:
mmap()

Segment Copying

The required segment data is copied into memory using:

  • lseek()
  • read()

.bss Handling

Condition

p_memsz > p_filesz

This indicates the presence of:

  • .bss section
  • Uninitialized data

Action

The extra memory region is zero-initialized using:

memset()

Internal Fragmentation Calculation

Internal fragmentation is updated using:

(size of page - size of segment)

Reason

  • A full 4 KB page is always allocated.
  • Some segments may occupy less than one page.

Page Allocation Tracking

  • Total page allocations are incremented by 1 for every mapped page.

Execution Continuation

After handling the page fault:

  • Execution resumes from the _start() function.

  • The process continues:

    • Until termination
    • Or until another page fault occurs

Program Termination

Upon completion:

  • The return value of _start() is printed.
  • Required statistics are displayed.
  • Cleanup functions are executed.

find_segment()

Functionality

  • Iterates through Program Header Table entries.
  • Finds the segment containing the faulting virtual address.
  • Used by the signal handler during page fault resolution.

my_handler()

Functionality

Handles:

  • SIGSEGV page faults

Responsibilities

  • Determine faulting address
  • Identify corresponding ELF segment
  • Allocate and map required page
  • Copy segment data into memory
  • Resume execution

loader_cleanup()

Functionality

  • Frees allocated resources.
  • Closes file descriptors.
  • Cleans up loader state before termination.

Lazy Loading Workflow

Program starts
      ↓
Read ELF headers
      ↓
Call _start() directly
      ↓
Page fault occurs (SIGSEGV)
      ↓
Signal handler invoked
      ↓
Find corresponding segment
      ↓
Map required page using mmap()
      ↓
Load page contents from ELF file
      ↓
Resume execution
      ↓
Repeat on future page faults
      ↓
Program terminates

Key System Calls & Functions Used

  • open()
  • read()
  • lseek()
  • mmap()
  • memset()
  • signal()
  • sigaction()

Core Features

  • Lazy loading of ELF segments
  • Demand paging simulation
  • Page fault handling using signals
  • Dynamic page allocation
  • Internal fragmentation tracking
  • Page allocation statistics
  • Efficient memory usage

Notes

  • Unlike Assignment 1, segments are not preloaded entirely into memory.

  • Pages are loaded only when accessed.

  • The implementation mimics:

    • Demand paging
    • Lazy loading mechanisms used in modern operating systems.
  • SIGSEGV is intentionally used as a mechanism for page fault handling.

  • Memory allocation occurs page-by-page instead of segment-by-segment

About

SimpleSmartLoader - An Upgraded SimpleLoader in C

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages