From 1e779c520a90daa642a67cf3b986536aa50ad5ef Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Thu, 18 Feb 2021 20:26:06 +0000 Subject: [PATCH] aya: obj: implement sane defaults for license and kernel version Default to license=GPL and kernel_version=any --- aya/src/obj/mod.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/aya/src/obj/mod.rs b/aya/src/obj/mod.rs index 807fc718..1a628c2b 100644 --- a/aya/src/obj/mod.rs +++ b/aya/src/obj/mod.rs @@ -93,15 +93,17 @@ impl Object { let obj = object::read::File::parse(data).map_err(|e| ParseError::ElfError(e))?; let endianness = obj.endianness(); - let section = obj - .section_by_name("license") - .ok_or(ParseError::MissingLicense)?; - let license = parse_license(Section::try_from(§ion)?.data)?; + let license = if let Some(section) = obj.section_by_name("license") { + parse_license(Section::try_from(§ion)?.data)? + } else { + CString::new("GPL").unwrap() + }; - let section = obj - .section_by_name("version") - .ok_or(ParseError::MissingKernelVersion)?; - let kernel_version = parse_version(Section::try_from(§ion)?.data, endianness)?; + let kernel_version = if let Some(section) = obj.section_by_name("version") { + parse_version(Section::try_from(§ion)?.data, endianness)? + } else { + KernelVersion::Any + }; let mut bpf_obj = Object::new(endianness, license, kernel_version); @@ -215,18 +217,12 @@ pub enum ParseError { #[error("error parsing ELF data")] ElfError(#[from] object::read::Error), - #[error("no license specified")] - MissingLicense, - #[error("invalid license `{data:?}`: missing NULL terminator")] MissingLicenseNullTerminator { data: Vec }, #[error("invalid license `{data:?}`")] InvalidLicense { data: Vec }, - #[error("missing kernel version")] - MissingKernelVersion, - #[error("invalid kernel version `{data:?}`")] InvalidKernelVersion { data: Vec },