Crates.io | elf_parser_rs |
lib.rs | elf_parser_rs |
version | 0.1.0 |
source | src |
created_at | 2024-10-21 02:24:36.955933 |
updated_at | 2024-10-21 02:24:36.955933 |
description | An lightweight ELF parser that parses ELF files and converts them into information needed for kernel building |
homepage | |
repository | https://github.com/Starry-OS/elf_parser_rs |
max_upload_size | |
id | 1416823 |
size | 82,628 |
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.
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();