Skip to content

Commit

Permalink
elf: Return an empty file range for SHT_NOBITS sections (#253)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Tiwalun committed Jan 31, 2021
1 parent c0a421f commit ffaba54
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/elf/section_header.rs
Expand Up @@ -426,9 +426,16 @@ if_alloc! {
sh_entsize: 0,
}
}
/// Returns this section header's file offset range
pub fn file_range(&self) -> Range<usize> {
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<Range<usize>> {
// 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<usize> {
Expand Down

0 comments on commit ffaba54

Please sign in to comment.