elf_parser_rs

Crates.ioelf_parser_rs
lib.rself_parser_rs
version0.1.0
sourcesrc
created_at2024-10-21 02:24:36.955933
updated_at2024-10-21 02:24:36.955933
descriptionAn lightweight ELF parser that parses ELF files and converts them into information needed for kernel building
homepage
repositoryhttps://github.com/Starry-OS/elf_parser_rs
max_upload_size
id1416823
size82,628
Firefly (Azure-stars)

documentation

https://docs.rs/elf_parser_rs

README

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

Commit count: 0

cargo fmt