Skip to content

Commit

Permalink
elf: Allow parsing SectionHeader from raw bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Lissy committed Feb 13, 2024
1 parent 30c0c33 commit ea7aacf
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/elf/section_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,20 @@ if_alloc! {
self.sh_addr as usize..(self.sh_addr as usize).saturating_add(self.sh_size as usize)
}
/// Parse `count` section headers from `bytes` at `offset`, using the given `ctx`
/// Assuming this is read from the whole file, it will check offset.
#[cfg(feature = "endian_fd")]
pub fn parse(bytes: &[u8], mut offset: usize, mut count: usize, ctx: Ctx) -> error::Result<Vec<SectionHeader>> {
use scroll::Pread;
// Zero offset means no section headers, not even the null section header.
if offset == 0 {
return Ok(Vec::new());
}
Self::parse_lossy(bytes, offset, count, ctx)
}
/// Parse `count` section headers from `bytes` at `offset`, using the given `ctx`
/// without performing any offset checking to allow parsing relatively
#[cfg(feature = "endian_fd")]
pub fn parse_lossy(bytes: &[u8], mut offset: usize, mut count: usize, ctx: Ctx) -> error::Result<Vec<SectionHeader>> {
use scroll::Pread;
let empty_sh = bytes.gread_with::<SectionHeader>(&mut offset, ctx)?;
if count == 0 as usize {
// Zero count means either no section headers if offset is also zero (checked
Expand Down

0 comments on commit ea7aacf

Please sign in to comment.