Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PE utils for sections and directories #420

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/read/pe/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::{cmp, iter, slice, str};

use crate::endian::LittleEndian as LE;
use crate::pe;
use crate::pe::ImageSectionHeader;
use crate::read::{
self, CompressedData, CompressedFileRange, ObjectSection, ObjectSegment, ReadError, ReadRef,
Relocation, Result, SectionFlags, SectionIndex, SectionKind, SegmentFlags,
Expand Down Expand Up @@ -324,6 +325,11 @@ impl<'data> SectionTable<'data> {
self.iter()
.find_map(|section| section.pe_data_containing(data, va))
}

/// Return the section that contains a given virtual address.
pub fn section_containing(&self, va: u32) -> Option<&'data ImageSectionHeader> {
self.iter().find(|section| section.contains_rva(va))
}
}

impl pe::ImageSectionHeader {
Expand Down Expand Up @@ -378,6 +384,18 @@ impl pe::ImageSectionHeader {
data.read_bytes_at(offset.into(), size.into()).ok()
}

/// Tests whether a given RVA is part of this section
pub fn contains_rva(&self, va: u32) -> bool {
let section_va = self.virtual_address.get(LE);
match va.checked_sub(section_va) {
None => false,
Some(offset) => {
// Address must be within section (and not at its end).
offset < self.virtual_size.get(LE)
}
}
}

/// Return the section data if it contains the given virtual address.
///
/// Also returns the virtual address of that section.
Expand Down