7 releases
0.3.2 | Feb 26, 2025 |
---|---|
0.3.1 | Feb 22, 2025 |
0.2.2 | Feb 20, 2025 |
0.1.0 | Sep 26, 2024 |
#117 in No standard library
840 downloads per month
135KB
297 lines
Contains (ELF exe/lib, 240KB) tests/ld-linux-x86-64.so.2, (ELF exe/lib, 25KB) tests/elf_static
kernel-elf-parser
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 kernel_elf_parser::{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 = kernel_elf_parser::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