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;
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		println!(
48			"{:?} VA:{:#X}+{:#X}, FILE:{}+{:#X}",
49			h.type_, h.virtual_addr, h.mem_size, h.offset, h.file_size,
50		);
51		if h.mem_size != h.file_size {
52			println!("BSS not supported, yet");
53			header_ok = false;
54			break;
55		}
56		let fstart = h.offset as usize;
57		let fend = fstart + h.file_size as usize;
58		if fstart >= file.file.len() || fend >= file.file.len() {
59			println!("bad size");
60		}
61		// black magic in sight! this converts a reference to static lifetime,
62		// which is UB, but I know what I'm doing here. The file backing ARE
63		// static!
64		let vma = VMArea {
65			vm_range: Range::<u64> {
66				start: h.virtual_addr,
67				end: h.virtual_addr + h.mem_size,
68			},
69			tag: String::from_str("USER BITS").unwrap(),
70			user_perms: VMPerms::all(),
71			backing: VMType::FILE(unsafe {
72				black_magic::make_static(&file.file[fstart..fend])
73			}),
74		};
75		let res = unsafe { map_vma(pt_root, &vma, true) };
76		if !res {
77			println!("failed to push vma: {:#X?}", &vma);
78			return None;
79		}
80		mm.vmas.push(vma);
81	}
82
83	if !header_ok {
84		println!("bad header");
85		return None;
86	}
87	match &elf.header.pt2 {
88		HeaderPt2::Header64(pt2) => return Some(pt2.entry_point),
89		_ => {
90			println!("bad header, not entry point");
91			return None;
92		}
93	};
94}