From e28a9f9b05313c38392dae5d8e7272e4c4838d91 Mon Sep 17 00:00:00 2001 From: Nick Condron Date: Sat, 21 Jan 2023 00:11:10 -0500 Subject: [PATCH] Replace panic! with Option --- src/obj/elf.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/obj/elf.rs b/src/obj/elf.rs index 98d2f0ed..c80b26dc 100644 --- a/src/obj/elf.rs +++ b/src/obj/elf.rs @@ -17,12 +17,12 @@ use crate::obj::{ ObjSymbolFlagSet, ObjSymbolFlags, }; -fn to_obj_section_kind(kind: SectionKind) -> ObjSectionKind { +fn to_obj_section_kind(kind: SectionKind) -> Option { match kind { - SectionKind::Text => ObjSectionKind::Code, - SectionKind::Data | SectionKind::ReadOnlyData => ObjSectionKind::Data, - SectionKind::UninitializedData => ObjSectionKind::Bss, - _ => panic!("Unhandled section kind {kind:?}"), + SectionKind::Text => Some(ObjSectionKind::Code), + SectionKind::Data | SectionKind::ReadOnlyData => Some(ObjSectionKind::Data), + SectionKind::UninitializedData => Some(ObjSectionKind::Bss), + _ => None, } } @@ -73,18 +73,14 @@ fn filter_sections(obj_file: &File<'_>) -> Result> { if section.size() == 0 { continue; } - if section.kind() != SectionKind::Text - && section.kind() != SectionKind::Data - && section.kind() != SectionKind::ReadOnlyData - && section.kind() != SectionKind::UninitializedData - { + let Some(kind) = to_obj_section_kind(section.kind()) else { continue; - } + }; let name = section.name().context("Failed to process section name")?; let data = section.uncompressed_data().context("Failed to read section data")?; result.push(ObjSection { name: name.to_string(), - kind: to_obj_section_kind(section.kind()), + kind, address: section.address(), size: section.size(), data: data.to_vec(),