Skip to main content

Binary

Struct Binary 

Source
pub struct Binary {
    pub path: Option<String>,
    pub format: BinaryFormat,
    pub arch: String,
    pub is_64: bool,
    pub little_endian: bool,
    pub bytes: Vec<u8>,
    pub symbols: Vec<Symbol>,
    /* private fields */
}
Expand description

In-memory parsed object representing a single executable format.

File bytes are kept so that vaddrs can be resolved even if sections were truncated on disk (NOBITS, sparse Mach-O, etc.) — readers return None rather than panic when ranges fall outside the loaded segment.

Fields§

§path: Option<String>§format: BinaryFormat§arch: String§is_64: bool

true when the target pointer ABI is 64-bit, false when it is 32-bit. Analyzers size their pointer reads off this flag.

§little_endian: bool§bytes: Vec<u8>§symbols: Vec<Symbol>

Implementations§

Source§

impl Binary

Source

pub fn parse(path: Option<&str>, bytes: Vec<u8>) -> Result<Self>

Parse a raw object file (ELF, PE, or single-arch Mach-O).

The input is cloned once internally so that the parsed object’s &[u8] references (via Elf<'_>, PE<'_>, Mach<'_>) stay valid until we finish copying them out into owned types.

§Errors

Returns Err when the input exceeds MAX_INPUT_BYTES, goblin cannot recognize the format, the object is truncated, an ar archive is passed (we do not dereference archive members in v0.1), or a Mach-O fat archive is passed (we only support single-arch slices). Any panic raised by goblin’s parser on a malformed object (e.g. Mach-O LC_MAIN load commands that trigger subtract-with-overflow in goblin 0.8.2) is also converted to Err, so a hostile object cannot abort the process.

Source

pub fn vaddr_to_offset(&self, vaddr: u64) -> Option<(usize, usize)>

Translate vaddr to (section_index, offset_within_section). Returns None if vaddr falls outside the span of any loaded section. If ranges overlap, the containing section with the highest start address (then the smallest span) wins.

Source

pub fn read_at_vaddr(&self, vaddr: u64, len: usize) -> Option<&[u8]>

Read len bytes at vaddr. Returns None if any byte falls outside the section bounds or the backing data is too short.

Source

pub fn read_ptr(&self, vaddr: u64) -> Option<u64>

Read a pointer-sized value (4 bytes on 32-bit, 8 on 64-bit) at vaddr, respecting the binary’s endianness.

Source

pub fn read_u32(&self, vaddr: u64) -> Option<u32>

Read a u32 (little- or big-endian per self.little_endian).

Source

pub fn vaddr_in_string_section(&self, vaddr: u64, len: u64) -> bool

true if vaddr..vaddr+len lies entirely inside a section classified as hosting string bytes (read-only data across formats).

Source

pub fn string_section_indices(&self) -> &[usize]

Indices into self.sections of sections classified as hosting string bytes (.rodata*, .rdata*, __cstring, …).

Source

pub fn is_string_section_index(&self, index: usize) -> bool

true when the section at index is classified as hosting string bytes.

Source

pub fn sections(&self) -> &[Section]

All sections parsed from the object file (not all are guaranteed to contain parseable bytes — some are NOBITS or sparse).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.