diff --git a/src/elf.rs b/src/elf.rs index 4ec1c788e..d0c156c03 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -383,7 +383,7 @@ impl Executable { aligned = AlignedMemory::<{ HOST_ALIGN }>::from_slice(bytes); aligned.as_slice() }; - let elf = Elf64::parse(bytes)?; + let mut elf = Elf64::parse(bytes)?; let enabled_sbpf_versions = loader.get_config().enabled_sbpf_versions.clone(); let sbpf_version = if *enabled_sbpf_versions.end() == SBPFVersion::V1 { // Emulates a bug in the version dispatcher until we enable the first other version @@ -403,6 +403,8 @@ impl Executable { return Err(ElfError::UnsupportedSBPFVersion); } if sbpf_version == SBPFVersion::V1 { + elf.parse_sections()?; + elf.parse_dynamic()?; Self::load_with_lenient_parser(&elf, bytes, loader) } else { Self::load_with_strict_parser(&elf, bytes, loader).map_err(|err| err.into()) diff --git a/src/elf_parser/mod.rs b/src/elf_parser/mod.rs index 385a551ae..214997013 100644 --- a/src/elf_parser/mod.rs +++ b/src/elf_parser/mod.rs @@ -246,7 +246,7 @@ impl<'a> Elf64<'a> { }) .transpose()?; - let mut parser = Self { + let parser = Self { elf_bytes, file_header, program_header_table, @@ -260,9 +260,6 @@ impl<'a> Elf64<'a> { dynamic_symbol_names_section_header: None, }; - parser.parse_sections()?; - parser.parse_dynamic()?; - Ok(parser) } @@ -291,7 +288,8 @@ impl<'a> Elf64<'a> { self.dynamic_relocations_table } - fn parse_sections(&mut self) -> Result<(), ElfParserError> { + /// Parses the section header table. + pub fn parse_sections(&mut self) -> Result<(), ElfParserError> { macro_rules! section_header_by_name { ($self:expr, $section_header:expr, $section_name:expr, $($name:literal => $field:ident,)*) => { @@ -326,7 +324,8 @@ impl<'a> Elf64<'a> { Ok(()) } - fn parse_dynamic(&mut self) -> Result<(), ElfParserError> { + /// Parses the dynamic section. + pub fn parse_dynamic(&mut self) -> Result<(), ElfParserError> { let mut dynamic_table: Option<&[Elf64Dyn]> = None; // try to parse PT_DYNAMIC