Skip to content

Commit

Permalink
read: add ObjectSegment::flags (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
everettjf authored Jan 1, 2022
1 parent 56299a3 commit 3c7a3d0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 10 deletions.
25 changes: 25 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ pub enum FileFlags {
},
}

/// Segment flags that are specific to each file format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SegmentFlags {
/// No segment flags.
None,
/// ELF segment flags.
Elf {
/// `p_flags` field in the segment header.
p_flags: u32,
},
/// Mach-O segment flags.
MachO {
/// `maxprot` field in the segment header.
maxprot: u32,
/// `initprot` field in the segment header.
initprot: u32,
},
/// COFF segment flags.
Coff {
/// `Characteristics` field in the segment header.
characteristics: u32,
},
}

/// Section flags that are specific to each file format.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
Expand Down
8 changes: 6 additions & 2 deletions src/read/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::read::{
self, Architecture, BinaryFormat, CodeView, ComdatKind, CompressedData, CompressedFileRange,
Error, Export, FileFlags, FileKind, Import, Object, ObjectComdat, ObjectKind, ObjectMap,
ObjectSection, ObjectSegment, ObjectSymbol, ObjectSymbolTable, ReadRef, Relocation, Result,
SectionFlags, SectionIndex, SectionKind, SymbolFlags, SymbolIndex, SymbolKind, SymbolMap,
SymbolMapName, SymbolScope, SymbolSection,
SectionFlags, SectionIndex, SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind,
SymbolMap, SymbolMapName, SymbolScope, SymbolSection,
};
#[allow(unused_imports)]
use crate::{AddressSize, Endian, Endianness};
Expand Down Expand Up @@ -563,6 +563,10 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSegment<'data> for Segment<'data, 'f
fn name(&self) -> Result<Option<&str>> {
with_inner!(self.inner, SegmentInternal, |x| x.name())
}

fn flags(&self) -> SegmentFlags {
with_inner!(self.inner, SegmentInternal, |x| x.flags())
}
}

/// An iterator of the sections of a `File`.
Expand Down
8 changes: 7 additions & 1 deletion src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::pe;
use crate::read::util::StringTable;
use crate::read::{
self, CompressedData, CompressedFileRange, Error, ObjectSection, ObjectSegment, ReadError,
ReadRef, Result, SectionFlags, SectionIndex, SectionKind,
ReadRef, Result, SectionFlags, SectionIndex, SectionKind, SegmentFlags,
};

use super::{CoffFile, CoffRelocationIterator};
Expand Down Expand Up @@ -189,6 +189,12 @@ impl<'data, 'file, R: ReadRef<'data>> ObjectSegment<'data> for CoffSegment<'data
.read_error("Non UTF-8 COFF section name")
.map(Some)
}

#[inline]
fn flags(&self) -> SegmentFlags {
let characteristics = self.section.characteristics.get(LE);
SegmentFlags::Coff { characteristics }
}
}

/// An iterator over the sections of a `CoffFile`.
Expand Down
8 changes: 7 additions & 1 deletion src/read/elf/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{mem, slice, str};
use crate::elf;
use crate::endian::{self, Endianness};
use crate::pod::Pod;
use crate::read::{self, Bytes, ObjectSegment, ReadError, ReadRef};
use crate::read::{self, Bytes, ObjectSegment, ReadError, ReadRef, SegmentFlags};

use super::{ElfFile, FileHeader, NoteIterator};

Expand Down Expand Up @@ -128,6 +128,12 @@ where
fn name(&self) -> read::Result<Option<&str>> {
Ok(None)
}

#[inline]
fn flags(&self) -> SegmentFlags {
let p_flags = self.segment.p_flags(self.file.endian);
SegmentFlags::Elf { p_flags }
}
}

/// A trait for generic access to `ProgramHeader32` and `ProgramHeader64`.
Expand Down
9 changes: 8 additions & 1 deletion src/read/macho/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{result, slice, str};
use crate::endian::{self, Endianness};
use crate::macho;
use crate::pod::Pod;
use crate::read::{self, ObjectSegment, ReadError, ReadRef, Result};
use crate::read::{self, ObjectSegment, ReadError, ReadRef, Result, SegmentFlags};

use super::{LoadCommandData, MachHeader, MachOFile, Section};

Expand Down Expand Up @@ -133,6 +133,13 @@ where
.read_error("Non UTF-8 Mach-O segment name")?,
))
}

#[inline]
fn flags(&self) -> SegmentFlags {
let maxprot = self.internal.segment.maxprot(self.file.endian);
let initprot = self.internal.segment.initprot(self.file.endian);
SegmentFlags::MachO { maxprot, initprot }
}
}

#[derive(Debug, Clone, Copy)]
Expand Down
8 changes: 7 additions & 1 deletion src/read/pe/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::endian::LittleEndian as LE;
use crate::pe;
use crate::read::{
self, CompressedData, CompressedFileRange, ObjectSection, ObjectSegment, ReadError, ReadRef,
Relocation, Result, SectionFlags, SectionIndex, SectionKind,
Relocation, Result, SectionFlags, SectionIndex, SectionKind, SegmentFlags,
};

use super::{ImageNtHeaders, PeFile, SectionTable};
Expand Down Expand Up @@ -123,6 +123,12 @@ where
.read_error("Non UTF-8 PE section name")?,
))
}

#[inline]
fn flags(&self) -> SegmentFlags {
let characteristics = self.section.characteristics.get(LE);
SegmentFlags::Coff { characteristics }
}
}

/// An iterator over the sections of a `PeFile32`.
Expand Down
7 changes: 5 additions & 2 deletions src/read/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use alloc::vec::Vec;
use crate::read::{
self, Architecture, CodeView, ComdatKind, CompressedData, CompressedFileRange, Export,
FileFlags, Import, ObjectKind, ObjectMap, Relocation, Result, SectionFlags, SectionIndex,
SectionKind, SymbolFlags, SymbolIndex, SymbolKind, SymbolMap, SymbolMapName, SymbolScope,
SymbolSection,
SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind, SymbolMap, SymbolMapName,
SymbolScope, SymbolSection,
};
use crate::Endianness;

Expand Down Expand Up @@ -261,6 +261,9 @@ pub trait ObjectSegment<'data>: read::private::Sealed {
///
/// Returns an error if the name is not UTF-8.
fn name(&self) -> Result<Option<&str>>;

/// Return the flags of segment.
fn flags(&self) -> SegmentFlags;
}

/// A section defined in an object file.
Expand Down
9 changes: 7 additions & 2 deletions src/read/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::read::{
self, Architecture, ComdatKind, CompressedData, CompressedFileRange, Error, Export, FileFlags,
Import, NoDynamicRelocationIterator, Object, ObjectComdat, ObjectKind, ObjectSection,
ObjectSegment, ObjectSymbol, ObjectSymbolTable, ReadError, ReadRef, Relocation, Result,
SectionFlags, SectionIndex, SectionKind, SymbolFlags, SymbolIndex, SymbolKind, SymbolScope,
SymbolSection,
SectionFlags, SectionIndex, SectionKind, SegmentFlags, SymbolFlags, SymbolIndex, SymbolKind,
SymbolScope, SymbolSection,
};

const SECTION_CUSTOM: usize = 0;
Expand Down Expand Up @@ -503,6 +503,11 @@ impl<'data, 'file, R> ObjectSegment<'data> for WasmSegment<'data, 'file, R> {
fn name(&self) -> Result<Option<&str>> {
unreachable!()
}

#[inline]
fn flags(&self) -> SegmentFlags {
unreachable!()
}
}

/// An iterator over the sections of a `WasmFile`.
Expand Down

0 comments on commit 3c7a3d0

Please sign in to comment.