Expand description
ELF Loader Library
This library provides functionality to load and execute a kernel or application from an ELF (Executable and Linkable Format) file. It is designed for use in a UEFI bootloader context, where the kernel is loaded from disk and executed directly from its entry point address.
§Overview
- Reads an ELF file from disk (e.g., from an EFI system partition)
- Parses the ELF file and loads its segments into memory at the addresses specified by the ELF headers
- Allocates memory using UEFI services, respecting the segment permissions
- Copies segment data from the file into memory, zero-filling any uninitialized data (BSS)
- Returns the entry point address and a callable function pointer to start the loaded kernel
§Usage
Call [load_kernel
] with the path to the ELF file. The function returns the entry point address and a function pointer you can call to transfer control to the loaded kernel.
§Safety
This code uses unsafe operations to copy memory and to transmute the entry point address into a function pointer. Ensure the ELF file is trusted and valid.
§ELF Entry Point
The entry point of an ELF file is a special address specified in the ELF header (the e_entry
field). When the loader finishes loading all segments, it transfers control to this address to start execution of the program or kernel.
§How the Entry Point is Set
- In Rust, you typically define the entry point function (e.g.,
fn _start()
) and mark it with#[no_mangle]
to prevent the compiler from renaming it. - The linker script (e.g.,
ENTRY(_start)
) tells the linker to set the ELF header’s entry point to the address of your_start
function.
When this loader returns the entry point address and function pointer, it is using the value from the ELF header, which should match your #[no_mangle]
entry function as set by your linker script.
For more details, see the documentation for your linker and the ENTRY()
directive in your linker script.