use core::marker::PhantomData;
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, RelocationMap, Result, SectionFlags, SectionIndex, SectionKind, SegmentFlags,
};
use super::{ImageNtHeaders, PeFile, SectionTable};
pub type PeSegmentIterator32<'data, 'file, R = &'data [u8]> =
PeSegmentIterator<'data, 'file, pe::ImageNtHeaders32, R>;
pub type PeSegmentIterator64<'data, 'file, R = &'data [u8]> =
PeSegmentIterator<'data, 'file, pe::ImageNtHeaders64, R>;
#[derive(Debug)]
pub struct PeSegmentIterator<'data, 'file, Pe, R = &'data [u8]>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
pub(super) file: &'file PeFile<'data, Pe, R>,
pub(super) iter: slice::Iter<'data, pe::ImageSectionHeader>,
}
impl<'data, 'file, Pe, R> Iterator for PeSegmentIterator<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
type Item = PeSegment<'data, 'file, Pe, R>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|section| PeSegment {
file: self.file,
section,
})
}
}
pub type PeSegment32<'data, 'file, R = &'data [u8]> =
PeSegment<'data, 'file, pe::ImageNtHeaders32, R>;
pub type PeSegment64<'data, 'file, R = &'data [u8]> =
PeSegment<'data, 'file, pe::ImageNtHeaders64, R>;
#[derive(Debug)]
pub struct PeSegment<'data, 'file, Pe, R = &'data [u8]>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
file: &'file PeFile<'data, Pe, R>,
section: &'data pe::ImageSectionHeader,
}
impl<'data, 'file, Pe, R> PeSegment<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
pub fn pe_file(&self) -> &'file PeFile<'data, Pe, R> {
self.file
}
pub fn pe_section(&self) -> &'data pe::ImageSectionHeader {
self.section
}
}
impl<'data, 'file, Pe, R> read::private::Sealed for PeSegment<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
}
impl<'data, 'file, Pe, R> ObjectSegment<'data> for PeSegment<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
#[inline]
fn address(&self) -> u64 {
u64::from(self.section.virtual_address.get(LE)).wrapping_add(self.file.common.image_base)
}
#[inline]
fn size(&self) -> u64 {
u64::from(self.section.virtual_size.get(LE))
}
#[inline]
fn align(&self) -> u64 {
self.file.section_alignment()
}
#[inline]
fn file_range(&self) -> (u64, u64) {
let (offset, size) = self.section.pe_file_range();
(u64::from(offset), u64::from(size))
}
fn data(&self) -> Result<&'data [u8]> {
self.section.pe_data(self.file.data)
}
fn data_range(&self, address: u64, size: u64) -> Result<Option<&'data [u8]>> {
Ok(read::util::data_range(
self.data()?,
self.address(),
address,
size,
))
}
#[inline]
fn name_bytes(&self) -> Result<Option<&[u8]>> {
self.section
.name(self.file.common.symbols.strings())
.map(Some)
}
#[inline]
fn name(&self) -> Result<Option<&str>> {
let name = self.section.name(self.file.common.symbols.strings())?;
Ok(Some(
str::from_utf8(name)
.ok()
.read_error("Non UTF-8 PE section name")?,
))
}
#[inline]
fn flags(&self) -> SegmentFlags {
let characteristics = self.section.characteristics.get(LE);
SegmentFlags::Coff { characteristics }
}
}
pub type PeSectionIterator32<'data, 'file, R = &'data [u8]> =
PeSectionIterator<'data, 'file, pe::ImageNtHeaders32, R>;
pub type PeSectionIterator64<'data, 'file, R = &'data [u8]> =
PeSectionIterator<'data, 'file, pe::ImageNtHeaders64, R>;
#[derive(Debug)]
pub struct PeSectionIterator<'data, 'file, Pe, R = &'data [u8]>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
pub(super) file: &'file PeFile<'data, Pe, R>,
pub(super) iter: iter::Enumerate<slice::Iter<'data, pe::ImageSectionHeader>>,
}
impl<'data, 'file, Pe, R> Iterator for PeSectionIterator<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
type Item = PeSection<'data, 'file, Pe, R>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(index, section)| PeSection {
file: self.file,
index: SectionIndex(index + 1),
section,
})
}
}
pub type PeSection32<'data, 'file, R = &'data [u8]> =
PeSection<'data, 'file, pe::ImageNtHeaders32, R>;
pub type PeSection64<'data, 'file, R = &'data [u8]> =
PeSection<'data, 'file, pe::ImageNtHeaders64, R>;
#[derive(Debug)]
pub struct PeSection<'data, 'file, Pe, R = &'data [u8]>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
pub(super) file: &'file PeFile<'data, Pe, R>,
pub(super) index: SectionIndex,
pub(super) section: &'data pe::ImageSectionHeader,
}
impl<'data, 'file, Pe, R> PeSection<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
pub fn pe_file(&self) -> &'file PeFile<'data, Pe, R> {
self.file
}
pub fn pe_section(&self) -> &'data pe::ImageSectionHeader {
self.section
}
}
impl<'data, 'file, Pe, R> read::private::Sealed for PeSection<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
}
impl<'data, 'file, Pe, R> ObjectSection<'data> for PeSection<'data, 'file, Pe, R>
where
Pe: ImageNtHeaders,
R: ReadRef<'data>,
{
type RelocationIterator = PeRelocationIterator<'data, 'file, R>;
#[inline]
fn index(&self) -> SectionIndex {
self.index
}
#[inline]
fn address(&self) -> u64 {
u64::from(self.section.virtual_address.get(LE)).wrapping_add(self.file.common.image_base)
}
#[inline]
fn size(&self) -> u64 {
u64::from(self.section.virtual_size.get(LE))
}
#[inline]
fn align(&self) -> u64 {
self.file.section_alignment()
}
#[inline]
fn file_range(&self) -> Option<(u64, u64)> {
let (offset, size) = self.section.pe_file_range();
if size == 0 {
None
} else {
Some((u64::from(offset), u64::from(size)))
}
}
fn data(&self) -> Result<&'data [u8]> {
self.section.pe_data(self.file.data)
}
fn data_range(&self, address: u64, size: u64) -> Result<Option<&'data [u8]>> {
Ok(read::util::data_range(
self.data()?,
self.address(),
address,
size,
))
}
#[inline]
fn compressed_file_range(&self) -> Result<CompressedFileRange> {
Ok(CompressedFileRange::none(self.file_range()))
}
#[inline]
fn compressed_data(&self) -> Result<CompressedData<'data>> {
self.data().map(CompressedData::none)
}
#[inline]
fn name_bytes(&self) -> Result<&'data [u8]> {
self.section.name(self.file.common.symbols.strings())
}
#[inline]
fn name(&self) -> Result<&'data str> {
let name = self.name_bytes()?;
str::from_utf8(name)
.ok()
.read_error("Non UTF-8 PE section name")
}
#[inline]
fn segment_name_bytes(&self) -> Result<Option<&[u8]>> {
Ok(None)
}
#[inline]
fn segment_name(&self) -> Result<Option<&str>> {
Ok(None)
}
#[inline]
fn kind(&self) -> SectionKind {
self.section.kind()
}
fn relocations(&self) -> PeRelocationIterator<'data, 'file, R> {
PeRelocationIterator(PhantomData)
}
fn relocation_map(&self) -> read::Result<RelocationMap> {
RelocationMap::new(self.file, self)
}
fn flags(&self) -> SectionFlags {
SectionFlags::Coff {
characteristics: self.section.characteristics.get(LE),
}
}
}
impl<'data> SectionTable<'data> {
pub fn pe_file_range_at(&self, va: u32) -> Option<(u32, u32)> {
self.iter().find_map(|section| section.pe_file_range_at(va))
}
pub fn pe_data_at<R: ReadRef<'data>>(&self, data: R, va: u32) -> Option<&'data [u8]> {
self.iter().find_map(|section| section.pe_data_at(data, va))
}
pub fn pe_data_containing<R: ReadRef<'data>>(
&self,
data: R,
va: u32,
) -> Option<(&'data [u8], u32)> {
self.iter()
.find_map(|section| section.pe_data_containing(data, va))
}
pub fn section_containing(&self, va: u32) -> Option<&'data ImageSectionHeader> {
self.iter().find(|section| section.contains_rva(va))
}
}
impl pe::ImageSectionHeader {
pub fn pe_file_range(&self) -> (u32, u32) {
let offset = self.pointer_to_raw_data.get(LE);
let size = cmp::min(self.virtual_size.get(LE), self.size_of_raw_data.get(LE));
(offset, size)
}
pub fn pe_file_range_at(&self, va: u32) -> Option<(u32, u32)> {
let section_va = self.virtual_address.get(LE);
let offset = va.checked_sub(section_va)?;
let (section_offset, section_size) = self.pe_file_range();
if offset < section_size {
Some((section_offset.checked_add(offset)?, section_size - offset))
} else {
None
}
}
pub fn pe_address_range(&self) -> (u32, u32) {
(self.virtual_address.get(LE), self.virtual_size.get(LE))
}
pub fn pe_data<'data, R: ReadRef<'data>>(&self, data: R) -> Result<&'data [u8]> {
let (offset, size) = self.pe_file_range();
data.read_bytes_at(offset.into(), size.into())
.read_error("Invalid PE section offset or size")
}
pub fn pe_data_at<'data, R: ReadRef<'data>>(&self, data: R, va: u32) -> Option<&'data [u8]> {
let (offset, size) = self.pe_file_range_at(va)?;
data.read_bytes_at(offset.into(), size.into()).ok()
}
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) => {
offset < self.virtual_size.get(LE)
}
}
}
pub fn pe_data_containing<'data, R: ReadRef<'data>>(
&self,
data: R,
va: u32,
) -> Option<(&'data [u8], u32)> {
let section_va = self.virtual_address.get(LE);
let offset = va.checked_sub(section_va)?;
let (section_offset, section_size) = self.pe_file_range();
if offset < section_size {
let section_data = data
.read_bytes_at(section_offset.into(), section_size.into())
.ok()?;
Some((section_data, section_va))
} else {
None
}
}
}
#[derive(Debug)]
pub struct PeRelocationIterator<'data, 'file, R = &'data [u8]>(
PhantomData<(&'data (), &'file (), R)>,
);
impl<'data, 'file, R> Iterator for PeRelocationIterator<'data, 'file, R> {
type Item = (u64, Relocation);
fn next(&mut self) -> Option<Self::Item> {
None
}
}