#elf #kernel #starry #elf-file #env-var

no-std elf_parser_rs

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

2 unstable releases

new 0.2.0 Feb 19, 2025
0.1.0 Oct 21, 2024

#217 in No standard library

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

135KB
270 lines

Contains (ELF exe/lib, 240KB) tests/ld-linux-x86-64.so.2, (ELF exe/lib, 25KB) tests/elf_static

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

use std::collections::BTreeMap;
use elf_parser_rs::{AuxvEntry, AuxvType};
let args: Vec<String> = vec!["arg1".to_string(), "arg2".to_string(), "arg3".to_string()];
let envs: Vec<String> = vec!["LOG=file".to_string()];
let mut auxv: [AuxvEntry; 17] = [
    AuxvEntry::new(AuxvType::PHDR, 0x1000),
    AuxvEntry::new(AuxvType::PHENT, 1024),
    AuxvEntry::new(AuxvType::PHNUM, 10),
    AuxvEntry::new(AuxvType::PAGESZ, 0x1000),
    AuxvEntry::new(AuxvType::BASE, 0),
    AuxvEntry::new(AuxvType::FLAGS, 0),
    AuxvEntry::new(AuxvType::ENTRY, 0x1000),
    AuxvEntry::new(AuxvType::HWCAP, 0),
    AuxvEntry::new(AuxvType::CLKTCK, 100),
    AuxvEntry::new(AuxvType::PLATFORM, 0),
    AuxvEntry::new(AuxvType::UID, 0),
    AuxvEntry::new(AuxvType::EUID, 0),
    AuxvEntry::new(AuxvType::GID, 0),
    AuxvEntry::new(AuxvType::EGID, 0),
    AuxvEntry::new(AuxvType::RANDOM, 0),
    AuxvEntry::new(AuxvType::EXECFN, 0),
    AuxvEntry::new(AuxvType::NULL, 0),
];
// The highest address of the user stack.
let ustack_end = 0x4000_0000;
let ustack_size = 0x1_0000;
let ustack_start = ustack_end - ustack_size;

let stack_data =
    elf_parser_rs::app_stack_region(&args, &envs, &mut auxv, ustack_start.into(), ustack_size);

// args length
assert_eq!(stack_data[0..8], [3, 0, 0, 0, 0, 0, 0, 0]);

Dependencies

~1MB
~17K SLoC