Class DecompileCallback

java.lang.Object
ghidra.app.decompiler.DecompileCallback

public class DecompileCallback extends Object
Routines that the decompiler invokes to gather info during decompilation of a function.
  • Field Details

  • Constructor Details

  • Method Details

    • setFunction

      public void setFunction(Function func, Address entry, DecompileDebug dbg)
      Establish function and debug context for next decompilation
      Parameters:
      func - is the function to be decompiled
      entry - is the function's entry address
      dbg - is the debugging context (or null)
    • getNativeMessage

      public String getNativeMessage()
      Returns:
      the last message from the decompiler
    • getBytes

      public byte[] getBytes(Address addr, int size)
      Get bytes from the program's memory image. Any exceptions are caught, resulting in null being returned. The decompiler treats a null as a DataUnavailError but will continue to process the function.
      Parameters:
      addr - is the starting address to fetch bytes from
      size - is the number of bytes to fetch
      Returns:
      the bytes matching the query or null if the query can't be met
    • getComments

      public void getComments(Address addr, int types, Encoder resultEncoder) throws IOException
      Collect any/all comments for the function starting at the indicated address. Filter based on selected comment types.
      Parameters:
      addr - is the indicated address
      types - is the set of flags
      resultEncoder - will contain the collected comments
      Throws:
      IOException - for errors in the underlying stream
    • getPcode

      public void getPcode(Address addr, PatchEncoder resultEncoder)
      Generate p-code ops for the instruction at the given address. Any exceptions are caught, resulting in an empty result. The decompiler interprets these as a BadDataError, but will continue to process the function.
      Parameters:
      addr - is the given address
      resultEncoder - will contain the generated p-code ops
    • encodeInstruction

      public static void encodeInstruction(Encoder encoder, Address addr, PcodeOp[] ops, int fallthruoffset, int paramshift, AddressFactory addrFactory) throws IOException
      Encode a list of pcode, representing an entire Instruction, to the stream
      Parameters:
      encoder - is the stream encoder
      addr - is the Address to associate with the Instruction
      ops - is the pcode ops
      fallthruoffset - number of bytes after instruction start that pcode flow falls into
      paramshift - special instructions for injection use
      addrFactory - is the address factory for recovering address space names
      Throws:
      IOException - for errors in the underlying stream
    • getPcodeInject

      public void getPcodeInject(String nm, Decoder paramDecoder, int type, Encoder resultEncoder) throws DecoderException, UnknownInstructionException, IOException, MemoryAccessException, NotFoundException
      Generate p-code ops for a named injection payload
      Parameters:
      nm - is the name of the payload
      paramDecoder - contains the context
      type - is the type of payload
      resultEncoder - will contain the generated p-code ops
      Throws:
      DecoderException - for problems decoding the injection context
      UnknownInstructionException - if there is no instruction at the injection site
      IOException - for errors encoding the injection result
      NotFoundException - if an expected aspect of the injection is not present in context
      MemoryAccessException - for problems establishing the injection context
    • getCPoolRef

      public void getCPoolRef(long[] refs, Encoder resultEncoder) throws IOException
      Look up details of a specific constant pool reference
      Parameters:
      refs - is the constant id (which may consist of multiple integers)
      resultEncoder - will contain the reference details
      Throws:
      IOException - for errors in the underlying stream while encoding results
    • getCodeLabel

      public String getCodeLabel(Address addr) throws IOException
      Return the first symbol name at the given address
      Parameters:
      addr - is the given address
      Returns:
      the symbol or null if no symbol is found
      Throws:
      IOException - for errors trying to encode the symbol
    • isNameUsed

      public boolean isNameUsed(String name, long startId, long stopId)
      Decide if a given name is used by any namespace between a starting namespace and a stopping namespace. I.e. check for a name collision along a specific namespace path. Currently, Ghidra is inefficient at calculating this perfectly, so this routine calculates an approximation that can occasionally indicate a collision when there isn't.
      Parameters:
      name - is the given name to check for collisions
      startId - is the id specifying the starting namespace
      stopId - is the id specifying the stopping namespace
      Returns:
      true if the name (likely) occurs in one of the namespaces on the path
    • getNamespacePath

      public void getNamespacePath(long id, Encoder resultEncoder) throws IOException
      Write a description of the formal namespace path to the given namespace
      Parameters:
      id - is the ID of the given namespace
      resultEncoder - is where to write the encoded result
      Throws:
      IOException - for errors in the underlying stream
    • getMappedSymbols

      public void getMappedSymbols(Address addr, Encoder resultEncoder) throws IOException
      Describe data or functions at the given address; either function, reference, data, or hole. Called by the native decompiler to query the GHIDRA database about any symbols at the given address.
      Parameters:
      addr - is the given address
      resultEncoder - is where to write encoded description
      Throws:
      IOException - for errors encoding the result
    • getExternalRef

      public void getExternalRef(Address addr, Encoder resultEncoder) throws IOException
      Get a description of an external reference at the given address
      Parameters:
      addr - is the given address
      resultEncoder - will contain the resulting description
      Throws:
      IOException - for errors encoding the result
    • getDataType

      public void getDataType(String name, long id, Encoder resultEncoder) throws IOException
      Get a description of a data-type given its name and type id
      Parameters:
      name - is the name of the data-type
      id - is the type id
      resultEncoder - will contain the resulting description
      Throws:
      IOException - for errors in the underlying stream while encoding
    • getRegister

      public void getRegister(String name, Encoder resultEncoder) throws IOException
      Return a description of the register with the given name
      Parameters:
      name - is the given name
      resultEncoder - is where to write the description
      Throws:
      IOException - for errors writing to the underlying stream
    • getRegisterName

      public String getRegisterName(Address addr, int size)
      Given a storage location, return the register name for that location, or null if there is no register there.
      Parameters:
      addr - is the starting address of the storage location
      size - is the size of storage in bytes
      Returns:
      the register name or null
    • getTrackedRegisters

      public void getTrackedRegisters(Address addr, Encoder resultEncoder) throws IOException
      Get "tracked" register values, constant values associated with a specific register at a specific point in the code.
      Parameters:
      addr - is the "point" in the code to look for tracked values
      resultEncoder - will hold the resulting description of registers and values
      Throws:
      IOException - for errors in the underlying stream writing the result
    • getUserOpName

      public String getUserOpName(int index)
      Get the name of a user op given its index
      Parameters:
      index - is the given index
      Returns:
      the userop name or null
    • getStringData

      public DecompileCallback.StringData getStringData(Address addr, int maxChars, String dtName, long dtId)
      Check for a string at the given address and return a UTF8 encoded byte array. If there is already data present at the address, use this to determine the string encoding. Otherwise use the data-type info passed in to determine the encoding. Check that the bytes at the address represent a valid string encoding that doesn't exceed the maximum character limit passed in. Return null if the string is invalid. Return the string translated into a UTF8 byte array otherwise. A (valid) empty string is returned as a zero length array.
      Parameters:
      addr - is the given address
      maxChars - is the maximum character limit
      dtName - is the name of a character data-type
      dtId - is the id associated with the character data-type
      Returns:
      the UTF8 encoded byte array or null