Skip to content

Starry-OS/kernel-elf-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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 =
    kernel_elf_parser::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();

About

A simple ELF parser written in rust that is useful for kernel

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages