#elf #kernel #starry

no-std elf_parser_rs

An lightweight ELF parser that parses ELF files and converts them into information needed for kernel building

1 unstable release

0.1.0 Oct 21, 2024

#125 in No standard library

Download history 173/week @ 2024-10-21 4/week @ 2024-10-28 7/week @ 2024-11-04

184 downloads per month

GPL-3.0-or-later OR Apache-2…

37KB
591 lines

Contains (ELF exe/lib, 25KB) tests/elf_static, (ELF exe/lib, 16KB) tests/elf_dynamic

elf_parser_rs

Crates.io Docs.rs CI

A lightweight ELF parser written in Rust, providing assistance for loading applications into the kernel.

It reads the data of the ELF file, and generates Sections, Relocations, Segments and so on.

It also generate a layout of the user stack according to the given user parameters and environment variables,which will be used for loading a given application into the physical memory of the kernel.

Examples

let args: Vec<String> = vec!["arg1".to_string(), "arg2".to_string(), "arg3".to_string()];
let envs: Vec<String> = vec!["LOG=file".to_string()];

// The highest address of the user stack.
let ustack_end = 0x4000_0000;
let ustack_size = 0x1_0000;
let ustack_bottom = ustack_end - ustack_size;

let stack_data =
    elf_parser_rs::app_stack_region(&args, &envs, &auxv, ustack_bottom.into(), ustack_size);
assert_eq!(stack_data[0..8], [3, 0, 0, 0, 0, 0, 0, 0]);

uspace.map_alloc(ustack_bottom, ustack_size, MappingFlags::READ | MappingFlags::WRITE | MappingFlags::USER)?;

// Copy the stack data to the user stack.
// After initialization, the stack layout is as follows: <https://articles.manugarg.com/aboutelfauxiliaryvectors.html>
unsafe {
    core::ptr::copy_nonoverlapping(
        stack_data.as_ptr(),
        phys_to_virt(ustack_size).as_mut_ptr(),
        stack_data.len(),
    );
}

ucontext.sp = ustack_end - stack_data.len();

Dependencies

~1MB
~17K SLoC