From ffaba54a4ebc946f8c07fa3a042cedf1e4bb42af Mon Sep 17 00:00:00 2001 From: Dominik Boehi Date: Sun, 31 Jan 2021 06:46:40 +0100 Subject: [PATCH] elf: Return an empty file range for SHT_NOBITS sections (#253) * Return an empty file range for SHT_NOBITS sections ELF sections with type SHT_NOBITS only exist in memory, and don't contain any data in the ELF file itself. Fixes #252. * Return an option from SectionHeader::file_range() Some sections don't occupy any space in the file, for these sections we now return None instead of a range. --- src/elf/section_header.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/elf/section_header.rs b/src/elf/section_header.rs index 5c04a303..0e7ddb5a 100644 --- a/src/elf/section_header.rs +++ b/src/elf/section_header.rs @@ -426,9 +426,16 @@ if_alloc! { sh_entsize: 0, } } - /// Returns this section header's file offset range - pub fn file_range(&self) -> Range { - self.sh_offset as usize..(self.sh_offset as usize).saturating_add(self.sh_size as usize) + /// Returns this section header's file offset range, + /// if the section occupies space in fhe file. + pub fn file_range(&self) -> Option> { + // Sections with type SHT_NOBITS have no data in the file itself, + // they only exist in memory. + if self.sh_type == SHT_NOBITS { + None + } else { + Some(self.sh_offset as usize..(self.sh_offset as usize).saturating_add(self.sh_size as usize)) + } } /// Returns this section header's virtual memory range pub fn vm_range(&self) -> Range {