Crate kernel_elf_parser

Source
Expand description

§kernel-elf-parser

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 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]);

Structs§

AuxvEntry
Represents an entry in the auxiliary vector.
ELFPH
ELF Program Header applied to the kernel
ELFParser
A wrapper for the ELF file data with some useful methods.

Enums§

AuxvType
Represents the type of an auxiliary vector entry.

Functions§

app_stack_region
Generate initial stack frame for user stack