From c70e09fc2d75a5d702ac9f8cf9ce07c01ac1cc14 Mon Sep 17 00:00:00 2001 From: Dominik Boehi Date: Sun, 24 Jan 2021 20:57:27 +0100 Subject: [PATCH 1/2] 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. --- src/elf/section_header.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/elf/section_header.rs b/src/elf/section_header.rs index 5c04a303..8d1b23f2 100644 --- a/src/elf/section_header.rs +++ b/src/elf/section_header.rs @@ -428,7 +428,13 @@ if_alloc! { } /// 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) + // Sections with type SHT_NOBITS have no data in the file itself, + // they only exist in memory. + if self.sh_type == SHT_NOBITS { + self.sh_offset as usize..self.sh_offset as usize + } else { + 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 { From 035af82e106df95382334ee04f38e11a1055c790 Mon Sep 17 00:00:00 2001 From: Dominik Boehi Date: Mon, 25 Jan 2021 19:29:29 +0100 Subject: [PATCH 2/2] 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 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/elf/section_header.rs b/src/elf/section_header.rs index 8d1b23f2..0e7ddb5a 100644 --- a/src/elf/section_header.rs +++ b/src/elf/section_header.rs @@ -426,14 +426,15 @@ if_alloc! { sh_entsize: 0, } } - /// Returns this section header's file offset range - pub fn file_range(&self) -> Range { + /// 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 { - self.sh_offset as usize..self.sh_offset as usize + None } else { - self.sh_offset as usize..(self.sh_offset as usize).saturating_add(self.sh_size as usize) + 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