Package ghidra.app.util.opinion

# Class MachoProgramBuilder

java.lang.Object
ghidra.app.util.opinion.MachoProgramBuilder

Direct Known Subclasses:
  `DyldCacheProgramBuilder`, `MachoPrelinkProgramBuilder`

public class MachoProgramBuilder extends Object
Builds up a Mach-O `Program` by parsing the Mach-O headers.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final String`
  `BLOCK_SOURCE_NAME`
  `protected FileBytes`
  `fileBytes`
  `protected Listing`
  `listing`
  `protected MessageLog`
  `log`
  `protected MachHeader`
  `machoHeader`
  `protected Memory`
  `memory`
  `protected TaskMonitor`
  `monitor`
  `protected Program`
  `program`
  `protected ByteProvider`
  `provider`
  `protected AddressSpace`
  `space`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `MachoProgramBuilder(Program program, ByteProvider provider, FileBytes fileBytes, MessageLog log, TaskMonitor monitor)`
  Creates a new `MachoProgramBuilder` based on the given information.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `build()`
  `static void`
  `buildProgram(Program program, ByteProvider provider, FileBytes fileBytes, MessageLog log, TaskMonitor monitor)`
  Builds up a Mach-O `Program`.
  `protected Namespace`
  `createNamespace(String namespaceName)`
  `protected void`
  `fixupProgramTree(String suffix)`
  Fixes up the Program Tree to better visualize the memory blocks that were split into sections
  `protected void`
  `markupChainedFixups(MachHeader header, List<Address> chainedFixups)`
  Markup the given `List` of chained fixups by creating pointers at their locations, if possible
  `protected void`
  `markupHeaders(MachHeader header, Address headerAddr)`
  `protected void`
  `markupLoadCommandData(MachHeader header, String source)`
  Marks up `LoadCommand` dadta
  `protected void`
  `markupProgramVars()`
  See crt.c from opensource.apple.com
  `protected void`
  `markupSections()`
  `protected void`
  `processAbsoluteSymbols()`
  `protected void`
  `processBindings(boolean doClassic, List<String> libraryPaths)`
  `List<Address>`
  `processChainedFixups(List<String> libraryPaths)`
  `protected void`
  `processEncryption()`
  Logs encrypted block ranges
  `protected void`
  `processEntryPoint()`
  Attempts to discover and set the entry point.
  `protected boolean`
  `processExports(MachHeader header)`
  `protected void`
  `processExternalRelocations()`
  Processes the external relocations from all `DynamicSymbolTableCommand`s.
  `protected List<String>`
  `processLibraries()`
  `protected void`
  `processLocalRelocations()`
  Processes the local relocations from all `DynamicSymbolTableCommand`s.
  `protected void`
  `processMemoryBlocks(MachHeader header, String source, boolean processSections, boolean allowZeroAddr)`
  Creates memory blocks for the given header.
  `protected void`
  `processNewExport(Address baseAddr, ExportTrie.ExportEntry export, String name)`
  `protected void`
  `processSectionRelocations()`
  Processes the section relocations from all `Section`s.
  `protected void`
  `processStubs()`
  `protected void`
  `processSymbolTables(MachHeader header, boolean processExports)`
  `protected void`
  `processUndefinedSymbols()`
  `protected void`
  `processUnsupportedLoadCommands()`
  Processes `LoadCommand`s that we haven't implemented yet.
  `protected void`
  `renameObjMsgSendRtpSymbol()`
  `protected void`
  `setCompiler()`
  `protected void`
  `setImageBase()`
  Sets the image base
  `protected void`
  `setProgramDescription()`
  `protected void`
  `setRelocatableProperty()`
  `protected Address`
  `setupHeaderAddr(Collection<SegmentCommand> segments)`
  Sets up the `MachHeader` in memory and returns its address.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### BLOCK_SOURCE_NAME

    public static final String BLOCK_SOURCE_NAME
    See Also:
      - Constant Field Values

  - ### machoHeader

    protected MachHeader machoHeader

  - ### program

    protected Program program

  - ### provider

    protected ByteProvider provider

  - ### fileBytes

    protected FileBytes fileBytes

  - ### log

    protected MessageLog log

  - ### monitor

    protected TaskMonitor monitor

  - ### memory

    protected Memory memory

  - ### listing

    protected Listing listing

  - ### space

    protected AddressSpace space

- ## Constructor Details

  - ### MachoProgramBuilder

    protected MachoProgramBuilder(Program program, ByteProvider provider, FileBytes fileBytes, MessageLog log, TaskMonitor monitor)
    Creates a new `MachoProgramBuilder` based on the given information.
    Parameters:
      `program` - The `Program` to build up.
      `provider` - The `ByteProvider` that contains the Mach-O's bytes.
      `fileBytes` - Where the Mach-O's bytes came from.
      `log` - The log.
      `monitor` - A cancelable task monitor.

- ## Method Details

  - ### buildProgram

    public static void buildProgram(Program program, ByteProvider provider, FileBytes fileBytes, MessageLog log, TaskMonitor monitor) throws Exception
    Builds up a Mach-O `Program`.
    Parameters:
      `program` - The `Program` to build up.
      `provider` - The `ByteProvider` that contains the Mach-O's bytes.
      `fileBytes` - Where the Mach-O's bytes came from.
      `log` - The log.
      `monitor` - A cancelable task monitor.
    Throws:
      `Exception` - if a problem occurs.

  - ### build

    protected void build() throws Exception
    Throws:
      `Exception`

  - ### setImageBase

    protected void setImageBase() throws Exception
    Sets the image base
    Throws:
      `Exception` - if there was a problem setting the image base

  - ### processMemoryBlocks

    protected void processMemoryBlocks(MachHeader header, String source, boolean processSections, boolean allowZeroAddr) throws Exception
    Creates memory blocks for the given header.
    Parameters:
      `header` - The Mach-O header to process for memory block creation.
      `source` - A name that represents where the memory blocks came from.
      `processSections` - True to split segments into their sections.
      `allowZeroAddr` - True if memory blocks at address 0 should be processed; otherwise, false.
    Throws:
      `Exception` - If there was a problem processing the memory blocks.

  - ### fixupProgramTree

    protected void fixupProgramTree(String suffix) throws Exception
    Fixes up the Program Tree to better visualize the memory blocks that were split into sections
    Parameters:
      `suffix` - An optional suffix that will get appended to tree segment and segment nodes
    Throws:
      `Exception` - if there was a problem fixing up the Program Tree

  - ### processEntryPoint

    protected void processEntryPoint() throws Exception
    Attempts to discover and set the entry point.
    Throws:
      `Exception` - If there was a problem discovering or setting the entry point.

  - ### processExports

    protected boolean processExports(MachHeader header) throws Exception
    Throws:
      `Exception`

  - ### processNewExport

    protected void processNewExport(Address baseAddr, ExportTrie.ExportEntry export, String name) throws AddressOutOfBoundsException, Exception
    Throws:
      `AddressOutOfBoundsException`
      `Exception`

  - ### processSymbolTables

    protected void processSymbolTables(MachHeader header, boolean processExports) throws Exception
    Throws:
      `Exception`

  - ### processStubs

    protected void processStubs() throws Exception
    Throws:
      `Exception`

  - ### processUndefinedSymbols

    protected void processUndefinedSymbols() throws Exception
    Throws:
      `Exception`

  - ### processAbsoluteSymbols

    protected void processAbsoluteSymbols() throws Exception
    Throws:
      `Exception`

  - ### processChainedFixups

    public List<Address> processChainedFixups(List<String> libraryPaths) throws Exception
    Throws:
      `Exception`

  - ### processBindings

    protected void processBindings(boolean doClassic, List<String> libraryPaths) throws Exception
    Throws:
      `Exception`

  - ### markupHeaders

    protected void markupHeaders(MachHeader header, Address headerAddr) throws Exception
    Throws:
      `Exception`

  - ### setupHeaderAddr

    protected Address setupHeaderAddr(Collection<SegmentCommand> segments) throws AddressOverflowException
    Sets up the `MachHeader` in memory and returns its address. If the header was not intended to reside in memory (like for Mach-O object files}, then this method will create an area in the "OTHER" address space for the header to live in.
    Parameters:
      `segments` - A `Collection` of `Mach-O segments`
    Returns:
      The `Address` of `MachHeader` in memory
    Throws:
      `AddressOverflowException` - if the address lies outside the address space

  - ### markupSections

    protected void markupSections() throws Exception
    Throws:
      `Exception`

  - ### processSectionRelocations

    protected void processSectionRelocations() throws CancelledException
    Processes the section relocations from all `Section`s.
    Throws:
      `CancelledException` - if the operation was cancelled.

  - ### processExternalRelocations

    protected void processExternalRelocations() throws CancelledException
    Processes the external relocations from all `DynamicSymbolTableCommand`s.
    Throws:
      `CancelledException` - if the operation was cancelled.

  - ### processLocalRelocations

    protected void processLocalRelocations() throws CancelledException
    Processes the local relocations from all `DynamicSymbolTableCommand`s.
    Throws:
      `CancelledException` - if the operation was cancelled.

  - ### processLibraries

    protected List<String> processLibraries() throws Exception
    Throws:
      `Exception`

  - ### processEncryption

    protected void processEncryption() throws Exception
    Logs encrypted block ranges
    Throws:
      `Exception` - if there was a problem detecting the encrypted block ranges

  - ### processUnsupportedLoadCommands

    protected void processUnsupportedLoadCommands() throws CancelledException
    Processes `LoadCommand`s that we haven't implemented yet.
    Throws:
      `CancelledException` - if the operation was cancelled.

  - ### markupLoadCommandData

    protected void markupLoadCommandData(MachHeader header, String source) throws Exception
    Marks up `LoadCommand` dadta
    Parameters:
      `header` - The Mach-O header
      `source` - A name that represents where the header came from (could be null)
    Throws:
      `Exception` - If there was a problem performing the markup

  - ### createNamespace

    protected Namespace createNamespace(String namespaceName)

  - ### markupChainedFixups

    protected void markupChainedFixups(MachHeader header, List<Address> chainedFixups) throws CancelledException
    Markup the given `List` of chained fixups by creating pointers at their locations, if possible
    Parameters:
      `header` - The Mach-O header
      `chainedFixups` - The `List` of chained fixups to markup
    Throws:
      `CancelledException` - if the operation was cancelled

  - ### markupProgramVars

    protected void markupProgramVars()
    See crt.c from opensource.apple.com

  - ### setRelocatableProperty

    protected void setRelocatableProperty()

  - ### setProgramDescription

    protected void setProgramDescription()

  - ### setCompiler

    protected void setCompiler()

  - ### renameObjMsgSendRtpSymbol

    protected void renameObjMsgSendRtpSymbol() throws DuplicateNameException, InvalidInputException
    Throws:
      `DuplicateNameException`
      `InvalidInputException`