Skip to main content

rustubs/proc/
loader.rs

1//! a simple loader for statically linked elf.
2use crate::arch::x86_64::paging::get_root;
3use crate::arch::x86_64::paging::map_vma;
4use crate::black_magic;
5use crate::fs;
6use crate::mm::vmm::{VMArea, VMPerms, VMType};
7use crate::proc::task::Task;
8use alloc::string::String;
9use core::ops::Range;
10use core::str::FromStr;
11use xmas_elf::header::HeaderPt2;
12use xmas_elf::program::{ProgramHeader, Type};
13use xmas_elf::ElfFile;
14pub fn cat_elf(f: &fs::File) {
15	let elf = ElfFile::new(f.file).unwrap();
16	println!("{:?}", elf.header);
17}
18
19// this loads a file into task address space
20// half baked!
21// 0. find and parse elf
22// 1. creates VMAs
23// 2. create paging structs and copy memory if necessary
24pub fn load(file: &fs::File) -> Option<u64> {
25	let task = Task::current().unwrap();
26	let mm = &mut task.mm;
27	let elf = ElfFile::new(file.file).ok()?;
28	let pt_root = get_root();
29
30	let mut header_ok = true;
31	for hdr in elf.program_iter() {
32		// you stupid rust force people to do 20000 levels of
33		// indentations of if let and match. Why can't we have nice
34		// thing?
35		if let ProgramHeader::Ph32(_) = hdr {
36			println!("not an 64 bit elf header");
37			header_ok = false;
38			break;
39		}
40		// I know for sure now this is Ph64, I just want to unwrap it. Why the
41		// heck can't I do it? Why the heck do I need yet another level of
42		// indentation???
43		let h = match hdr {
44			ProgramHeader::Ph64(h) => h,
45			_ => panic!(),
46		};
47
48		// skip non-load sections
49		if h.get_type() != Ok(Type::Load) {
50			continue;
51		}
52
53		println!(
54			"{:?} VA:{:#X}+{:#X}, FILE:{}+{:#X}",
55			h.type_, h.virtual_addr, h.mem_size, h.offset, h.file_size,
56		);
57		if h.mem_size != h.file_size {
58			println!("BSS not supported, yet");
59			header_ok = false;
60			break;
61		}
62		let fstart = h.offset as usize;
63		let fend = fstart + h.file_size as usize;
64		if fstart >= file.file.len() || fend >= file.file.len() {
65			println!("bad size");
66		}
67		// black magic in sight! this converts a reference to static lifetime,
68		// which is UB, but I know what I'm doing here. The file backing ARE
69		// static!
70		let vma = VMArea {
71			vm_range: Range::<u64> {
72				start: h.virtual_addr,
73				end: h.virtual_addr + h.mem_size,
74			},
75			tag: String::from_str("USER BITS").unwrap(),
76			user_perms: VMPerms::all(),
77			backing: VMType::FILE(unsafe {
78				black_magic::make_static(&file.file[fstart..fend])
79			}),
80		};
81		let res = unsafe { map_vma(pt_root, &vma, true) };
82		if !res {
83			println!("failed to push vma: {:#X?}", &vma);
84			return None;
85		}
86		mm.vmas.push(vma);
87	}
88
89	if !header_ok {
90		println!("bad header");
91		return None;
92	}
93	match &elf.header.pt2 {
94		HeaderPt2::Header64(pt2) => return Some(pt2.entry_point),
95		_ => {
96			println!("bad header, not entry point");
97			return None;
98		}
99	};
100}