diff --git a/tool/microkit/src/elf.rs b/tool/microkit/src/elf.rs index badb3eec..5f342f86 100644 --- a/tool/microkit/src/elf.rs +++ b/tool/microkit/src/elf.rs @@ -220,12 +220,12 @@ impl ElfFile { pub fn from_path(path: &Path) -> Result { 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; @@ -241,13 +241,7 @@ impl ElfFile { hdr_size = std::mem::size_of::(); 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 @@ -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); @@ -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 diff --git a/tool/microkit/src/loader.rs b/tool/microkit/src/loader.rs index 30c64ff5..2a1ec446 100644 --- a/tool/microkit/src/loader.rs +++ b/tool/microkit/src/loader.rs @@ -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, diff --git a/tool/microkit/src/main.rs b/tool/microkit/src/main.rs index d3c9a3cb..719ba9ab 100644 --- a/tool/microkit/src/main.rs +++ b/tool/microkit/src/main.rs @@ -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 @@ -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) = @@ -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 { @@ -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: '{}'",