Struct lief::macho::Binary

pub struct Binary { /* private fields */ }
Expand description

This is the main interface to read and write Mach-O binary attributes.

Note that this structure implements the generic::Binary trait from which other generic functions are exposed

Implementations§

§

impl Binary

pub fn header(&self) -> Header<'_>

Return the main Mach-O header

pub fn commands(&self) -> CommandsIter<'_>

Return an iterator over the different crate::macho::Commands used by the Mach-O binary

pub fn sections(&self) -> Sections<'_>

Return an iterator over the different crate::macho::Section of the binary

pub fn segments(&self) -> Segments<'_>

Return an iterator over the different crate::macho::commands::Segment (LC_SEGMENT/LC_SIGNATURE) of the binary.

pub fn libraries(&self) -> Libraries<'_>

Return an iterator over the crate::macho::commands::Dylib used by this binary

pub fn relocations(&self) -> Relocations<'_>

Return an iterator over the different crate::macho::Relocation of this binary

pub fn rpaths(&self) -> RPaths<'_>

Return an iterator over the different crate::macho::commands::RPath of this binary

pub fn symbols(&self) -> Symbols<'_>

Return an iterator over the different crate::macho::Symbol of this binary

pub fn dyld_info(&self) -> Option<DyldInfo<'_>>

Return the LC_DYLD_INFO/LC_DYLD_INFO_ONLY command if present

pub fn uuid(&self) -> Option<UUID<'_>>

Return the LC_UUID command if present

pub fn main_command(&self) -> Option<Main<'_>>

Return the LC_MAIN command if present

pub fn dylinker(&self) -> Option<Dylinker<'_>>

Return the LC_LOAD_DYLINKER/LC_ID_DYLINKER command if present

pub fn function_starts(&self) -> Option<FunctionStarts<'_>>

Return the LC_FUNCTION_STARTS command if present

pub fn source_version(&self) -> Option<SourceVersion<'_>>

Return the LC_SOURCE_VERSION command if present

pub fn thread_command(&self) -> Option<ThreadCommand<'_>>

Return the LC_THREAD/LC_UNIXTHREAD command if present

pub fn rpath(&self) -> Option<RPath<'_>>

Return the LC_RPATH command if present

pub fn routine(&self) -> Option<Routine<'_>>

Return the LC_ROUTINE/LC_ROUTINE64 command if present

pub fn symbol_command(&self) -> Option<SymbolCommand<'_>>

Return the LC_SYMTAB command if present

pub fn dynamic_symbol(&self) -> Option<DynamicSymbolCommand<'_>>

Return the LC_DYSYMTAB command if present

pub fn code_signature(&self) -> Option<CodeSignature<'_>>

Return the LC_CODE_SIGNATURE command if present

pub fn code_signature_dir(&self) -> Option<CodeSignatureDir<'_>>

Return the LC_DYLIB_CODE_SIGN_DRS command if present

pub fn data_in_code(&self) -> Option<DataInCode<'_>>

Return the LC_DATA_IN_CODE command if present

pub fn segment_split_info(&self) -> Option<SegmentSplitInfo<'_>>

Return the LC_SEGMENT_SPLIT_INFO command if present

pub fn encryption_info(&self) -> Option<EncryptionInfo<'_>>

Return the LC_ENCRYPTION_INFO/LC_ENCRYPTION_INFO_64 command if present

pub fn sub_framework(&self) -> Option<SubFramework<'_>>

Return the LC_SUB_FRAMEWORK command if present

pub fn subclients(&self) -> SubClients<'_>

Return the LC_SUBCLIENT command if present

pub fn dyld_environment(&self) -> Option<DyldEnvironment<'_>>

Return the LC_DYLD_ENVIRONMENT command if present

pub fn build_version(&self) -> Option<BuildVersion<'_>>

Return the LC_BUILD_VERSION command if present

pub fn dyld_chained_fixups(&self) -> Option<DyldChainedFixups<'_>>

Return the LC_DYLD_CHAINED_FIXUPS command if present

pub fn dyld_exports_trie(&self) -> Option<DyldExportsTrie<'_>>

Return the LC_DYLD_EXPORTS_TRIE command if present

pub fn two_level_hints(&self) -> Option<TwoLevelHints<'_>>

Return the LC_TWOLEVEL_HINTS command if present

pub fn linker_opt_hint(&self) -> Option<LinkerOptHint<'_>>

Return the LC_LINKER_OPTIMIZATION_HINT command if present

pub fn atom_info(&self) -> Option<AtomInfo<'_>>

Return the LC_ATOM_INFO command if present

pub fn function_variants(&self) -> Option<FunctionVariants<'_>>

Return the LC_FUNCTION_VARIANTS command if present

pub fn function_variant_fixups(&self) -> Option<FunctionVariantFixups<'_>>

Return the LC_FUNCTION_VARIANT_FIXUPS command if present

pub fn version_min(&self) -> Option<VersionMin<'_>>

Return the LC_VERSION_MIN_MACOSX/VERSION_MIN_IPHONEOS command if present

pub fn support_arm64_ptr_auth(&self) -> bool

Check if the binary is supporting ARM64 pointer authentication (arm64e)

pub fn bindings(&self) -> BindingsInfo<'_>

Return an iterator over the bindings located in DyldInfo or DyldChainedFixups

pub fn symbol_stubs(&self) -> Stubs<'_>

Return an iterator over the symbol stubs.

These stubs are involved when calling an imported function and are similar to the ELF’s plt/got mechanism.

There are located in sections like: __stubs,__auth_stubs,__symbol_stub,__picsymbolstub4

pub fn objc_metadata(&self) -> Option<Metadata<'_>>

Return Objective-C metadata if present

pub fn platform(&self) -> Platform

Return the platform for which this Mach-O has been compiled for

pub fn is_ios(&self) -> bool

True if this binary targets iOS

pub fn is_macos(&self) -> bool

True if this binary targets macOS

pub fn find_library(&self, name: &str) -> Option<Dylib<'_>>

Try to find the library with the given library name.

This function tries to match the fullpath of the DylibCommand or the library name suffix.

pub fn get_int_from_virtual_address<T>(&self, addr: u64) -> Result<T, Error>where T: Num + FromPrimitive + ToPrimitive,

Get the integer value at the given virtual address

pub fn write<P: AsRef<Path>>(&mut self, output: P)

Write back the current MachO binary into the file specified in parameter

pub fn write_with_config<P: AsRef<Path>>(&mut self, output: P, config: Config)

Write back the current MachO binary into the file specified in parameter with the configuration provided in the second parameter.

pub fn add_command(&mut self, command: impl Command) -> Option<Commands<'_>>

Insert a new command

pub fn add_library<'a>(&'a mut self, libname: &str) -> Dylib<'a>

Insert a new shared library through a LC_LOAD_DYLIB command

pub fn remove_commands_by_type(&mut self, ty: LoadCommandTypes) -> bool

Remove all commands that have the given type

pub fn functions(&self) -> Functions<'_>

pub fn notes(&self) -> Notes<'_>

Return an iterator over the LC_NOTE commands

pub fn fileset_name(&self) -> String

Name associated with the LC_FILESET_ENTRY for this MachO. For instance: com.apple.kec.corecrypto

pub fn fileset_addr(&self) -> u64

Original address associated with the LC_FILESET_ENTRY for this MachO.

pub fn filesets(&self) -> FilesetBinaries<'_>

Return an iterator over the Binary associated with the LC_FILESET_ENTRY commands

pub fn virtual_address_to_offset(&self, address: u64) -> Result<u64, Error>

Convert the given virtual address into an offset

pub fn segment_from_offset(&self, offset: u64) -> Option<Segment<'_>>

Return the Segment associated with the given offset

pub fn segment_from_virtual_address(&self, va: u64) -> Option<Segment<'_>>

Return the Segment associated with the given virtual address

pub fn section_from_virtual_address(&self, va: u64) -> Option<Section<'_>>

Return the Section associated with the given virtual address

pub fn get_segment(&self, name: String) -> Option<Segment<'_>>

Return the Segment matching the given name

pub fn get_section( &self, segment_name: String, section_name: String ) -> Option<Section<'_>>

Return the Section embedded in the given segment’s name

pub fn fat_offset(&self) -> u64

Return the offset of this binary in the FAT binary

pub fn overlay(&self) -> &[u8]

Return the overlay data (if any)

pub fn is_valid_addr(&self, address: u64) -> bool

Check if the given address is valid for this binary

pub fn has_symbol(&self, name: &str) -> bool

Check if the binary contains a symbol with the given name

pub fn get_symbol(&self, name: &str) -> Option<Symbol<'_>>

Return the symbol with the given name (if any)

pub fn has_section(&self, name: &str) -> bool

Check if the binary contains a section with the given name

pub fn section_from_offset(&self, offset: u64) -> Option<Section<'_>>

Return the Section associated with the given offset

pub fn has_segment(&self, name: &str) -> bool

Check if the binary contains a segment with the given name

pub fn has_command_type(&self, ty: LoadCommandTypes) -> bool

Check if the binary has a command with the given type

pub fn get_command_type(&self, ty: LoadCommandTypes) -> Option<Commands<'_>>

Get a command with the given type

pub fn remove_command(&mut self, index: u32) -> bool

Remove the command at the given index

pub fn remove_section(&mut self, name: &str, clear: bool)

Remove a section by name

pub fn remove_section_from_segment( &mut self, segname: &str, secname: &str, clear: bool )

Remove a section by segment name and section name

pub fn remove_signature(&mut self) -> bool

Remove the LC_CODE_SIGNATURE command

pub fn remove_symbol(&mut self, name: &str) -> bool

Remove a symbol by name

pub fn can_remove(&self, sym: &Symbol<'_>) -> bool

Check if the given symbol can be safely removed

pub fn can_remove_symbol(&self, name: &str) -> bool

Check if the symbol with the given name can be safely removed

pub fn unexport_name(&mut self, name: &str) -> bool

Unexport the symbol with the given name

pub fn unexport_symbol(&mut self, sym: &Symbol<'_>) -> bool

Unexport the given symbol

pub fn add_exported_function( &mut self, address: u64, name: &str ) -> Option<ExportInfo<'_>>

Add a new exported function

pub fn add_local_symbol( &mut self, address: u64, name: &str ) -> Option<Symbol<'_>>

Add a new local symbol

pub fn shift(&mut self, value: u64) -> Result<(), Error>

Shift the content of the binary

pub fn shift_linkedit(&mut self, width: u64) -> Result<(), Error>

Shift the __LINKEDIT segment

Trait Implementations§

§

impl Binary for Binary

§

fn entrypoint(&self) -> u64

Binary’s entrypoint
§

fn imagebase(&self) -> u64

Default base address where the binary should be mapped
§

fn virtual_size(&self) -> u64

Size of the binary when mapped in memory
§

fn is_pie(&self) -> bool

Whether the current binary is an executable and position independent
§

fn has_nx(&self) -> bool

Whether the binary defines a non-executable stack
§

fn original_size(&self) -> u64

Original file size of the binary
§

fn debug_info(&self) -> Option<DebugInfo<'_>>

Return the debug info if present. It can be either a crate::pdb::DebugInfo or crate::dwarf::DebugInfo. Read more
§

fn disassemble(&self, address: u64, size: u64) -> InstructionsIt<'_>

Disassemble code starting at the given virtual address and with the given size. Read more
§

fn disassemble_symbol(&self, name: &str) -> InstructionsIt<'_>

Disassemble code for the given symbol name Read more
§

fn disassemble_address(&self, address: u64) -> InstructionsIt<'_>

Disassemble code at the given virtual address Read more
§

fn disassemble_slice(&self, slice: &[u8], address: u64) -> InstructionsIt<'_>

Disassemble code provided by the given slice at the specified address parameter. Read more
§

fn assemble(&mut self, address: u64, asm: &str) -> Vec<u8>

Assemble and patch the provided assembly code at the specified address. Read more
§

fn assemble_with_config( &mut self, address: u64, asm: &str, config: &AssemblerConfig ) -> Vec<u8>

Same as Binary::assemble but this function takes an extra AssemblerConfig that is used to configure the assembly engine: dialect, symbols definitions.
§

fn page_size(&self) -> u64

Default memory page size according to the architecture and the format of the current binary
§

fn offset_to_virtual_address( &self, offset: u64, slide: u64 ) -> Result<u64, Error>

Convert the given offset into a virtual address. Read more
§

fn load_debug_info(&mut self, path: &Path) -> Option<DebugInfo<'_>>

Load and associate an external debug file (e.g., DWARF or PDB) with this binary. Read more
§

impl Debug for Binary

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Binary

§

impl !Send for Binary

§

impl !Sync for Binary

§

impl Unpin for Binary

§

impl UnwindSafe for Binary

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.