Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions tool/microkit/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ impl ElfFile {
pub fn from_path(path: &Path) -> Result<ElfFile, String> {
let bytes = match fs::read(path) {
Ok(bytes) => bytes,
Err(err) => return Err(format!("Failed to read ELF '{}': {}", path.display(), err)),
Err(err) => return Err(format!("failed to read ELF: {}", err)),
};

let magic = &bytes[0..4];
if magic != ELF_MAGIC {
return Err(format!("ELF '{}': magic check failed", path.display()));
return Err("magic check failed".to_string());
}

let word_size;
Expand All @@ -241,13 +241,7 @@ impl ElfFile {
hdr_size = std::mem::size_of::<ElfHeader64>();
word_size = 64;
}
_ => {
return Err(format!(
"ELF '{}': invalid class '{}'",
path.display(),
class
))
}
_ => return Err(format!("invalid class '{}'", class)),
};

// Now need to read the header into a struct
Expand All @@ -260,17 +254,16 @@ impl ElfFile {
assert!(hdr.ident_class == *class);

if hdr.ident_data != 1 {
return Err(format!(
"ELF '{}': incorrect endianness, only little endian architectures are supported",
path.display()
));
return Err(
"incorrect endianness, only little endian architectures are supported".to_string(),
);
}

let entry = hdr.entry;

// Read all the segments
if hdr.phnum == 0 {
return Err(format!("ELF '{}': has no program headers", path.display()));
return Err("has no program headers".to_string());
}

let mut segments = Vec::with_capacity(hdr.phnum as usize);
Expand Down Expand Up @@ -325,18 +318,12 @@ impl ElfFile {
}

if shstrtab_shent.is_none() {
return Err(format!(
"ELF '{}': unable to find string table section",
path.display()
));
return Err("unable to find string table section".to_string());
}

assert!(symtab_shent.is_some());
if symtab_shent.is_none() {
return Err(format!(
"ELF '{}': unable to find symbol table section",
path.display()
));
return Err("unable to find symbol table section".to_string());
}

// Reading the symbol table
Expand Down
9 changes: 8 additions & 1 deletion tool/microkit/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ impl<'a> Loader<'a> {
unreachable!("internal error: x86_64 does not support creating a loader image");
}

let loader_elf = ElfFile::from_path(loader_elf_path).unwrap();
let loader_elf = ElfFile::from_path(loader_elf_path).unwrap_or_else(|e| {
eprintln!(
"ERROR: failed to parse loader ELF ({}): {}",
loader_elf_path.display(),
e
);
std::process::exit(1);
});
let sz = loader_elf.word_size;
let magic = match sz {
32 => 0x5e14dead,
Expand Down
42 changes: 36 additions & 6 deletions tool/microkit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,14 @@ fn main() -> Result<(), String> {
}
};

let capdl_initialiser_elf = ElfFile::from_path(&capdl_init_elf_path).unwrap();
let capdl_initialiser_elf = ElfFile::from_path(&capdl_init_elf_path).unwrap_or_else(|e| {
eprintln!(
"ERROR: failed to parse initialiser ELF ({}): {}",
capdl_init_elf_path.display(),
e
);
std::process::exit(1);
});

// Only relevant for ARM and RISC-V.
// Determine how much physical memory is available to the kernel after it boots but before dropping
Expand All @@ -581,7 +588,14 @@ fn main() -> Result<(), String> {
match kernel_config.arch {
Arch::X86_64 => (None, None, None),
Arch::Aarch64 | Arch::Riscv64 => {
let kernel_elf = ElfFile::from_path(&kernel_elf_path).unwrap();
let kernel_elf = ElfFile::from_path(&kernel_elf_path).unwrap_or_else(|e| {
eprintln!(
"ERROR: failed to parse kernel ELF ({}): {}",
kernel_elf_path.display(),
e
);
std::process::exit(1);
});

// Now determine how much memory we have after the kernel boots.
let (available_memory, kernel_boot_region) =
Expand All @@ -594,7 +608,14 @@ fn main() -> Result<(), String> {
}
};

let monitor_elf = ElfFile::from_path(&monitor_elf_path)?;
let monitor_elf = ElfFile::from_path(&monitor_elf_path).unwrap_or_else(|e| {
eprintln!(
"ERROR: failed to parse monitor ELF ({}): {}",
monitor_elf_path.display(),
e
);
std::process::exit(1);
});

let mut search_paths = vec![std::env::current_dir().unwrap()];
for path in args.search_paths {
Expand All @@ -608,9 +629,18 @@ fn main() -> Result<(), String> {
// Get the elf files for each pd:
for pd in &system.protection_domains {
match get_full_path(&pd.program_image, &search_paths) {
Some(path) => {
system_elfs.push(ElfFile::from_path(&path)?);
}
Some(path) => match ElfFile::from_path(&path) {
Ok(elf) => system_elfs.push(elf),
Err(e) => {
eprintln!(
"ERROR: failed to parse ELF '{}' for PD '{}': {}",
path.display(),
pd.name,
e
);
std::process::exit(1);
}
},
None => {
return Err(format!(
"unable to find program image: '{}'",
Expand Down
Loading