Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

ELF Loader

GitHub description: A minimal educational x86-64 Linux ELF loader written in C, with segment mapping, PIE support, memory protection, and process-stack setup.

elf-loader loads and starts a 64-bit ELF executable directly from user space. It reads the executable into memory, validates its ELF header, maps each loadable segment at the expected virtual address, applies the segment permissions, prepares a Linux-style initial stack, and transfers control to the program's entry point.

Features

  • Validates the ELF magic number and 64-bit file class.
  • Maps PT_LOAD program segments on 4 KiB page boundaries.
  • Converts ELF segment flags to mprotect(2) permissions.
  • Handles ET_DYN executables with a randomized load base.
  • Builds argc, argv, envp, and essential auxiliary-vector entries on a new stack.
  • Transfers execution with the required x86-64 stack alignment.

Requirements

  • A 64-bit Linux system
  • GCC and GNU Make
  • A statically linked x86-64 ELF executable to load

Build and run

make
./elf-loader <static-elf-binary> [arguments...]

For example, first build a small static test program, then run it through the loader:

gcc -static -o hello hello.c
./elf-loader ./hello one two

The target receives its own path as argv[0], followed by any additional arguments. Use make clean to remove the compiled loader, or make pack to create ../src.zip from the project files.

How it works

The loader memory-maps the input file, reads its ELF and program headers, and allocates anonymous memory for every loadable segment. File-backed bytes are copied into place while the remaining memory is zero-initialized for sections such as .bss. It then creates a separate stack containing the argument and environment vectors plus the AT_PHDR, AT_PHENT, AT_PHNUM, AT_PAGESZ, AT_ENTRY, and AT_RANDOM auxiliary values. Finally, a short assembly sequence replaces %rsp, clears %rbp, and jumps to the ELF entry address.

Limitations

This is a learning project rather than a replacement for the Linux kernel loader. It targets ELF64 executables on x86-64 Linux and is intended for static binaries. It does not provide a dynamic linker, resolve relocations or shared libraries, process PT_INTERP, or perform comprehensive ELF bounds and error checking. Because it uses fixed-address mappings, run only binaries you trust in an isolated development environment.

License

The source is provided under the BSD 3-Clause License, as indicated by its SPDX header.

About

A minimal educational x86-64 Linux ELF loader written in C, with segment mapping, PIE support, memory protection, and process-stack setup.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages