Overview
The ReferenceManager interface provides methods for creating, retrieving, and managing references between code units in a Ghidra program. References include memory references, stack references, register references, and external references.Interface
Package:ghidra.program.model.symbolLocation:
Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/symbol/ReferenceManager.java
Constants
MNEMONIC
Operand index which corresponds to the instruction/data mnemonic.Adding References
addReference()
Adds a memory, stack, register, or external reference.reference- Reference to be added
addMemoryReference()
Adds a memory reference. The first memory reference on an operand becomes primary by default.fromAddr- Address of the code unit where the reference occurstoAddr- Address of the location being referenced (memory, stack, or register)type- Reference type (how the location is being referenced)source- The source of this reference (e.g.,SourceType.USER_DEFINED,SourceType.ANALYSIS)opIndex- The operand index
IllegalArgumentException if unsupported RefType is specified
All non-memory references will be removed from the specified operand.
addOffsetMemReference()
Adds an offset memory reference.fromAddr- Address for the “from” locationtoAddr- Address of the location being referencedtoAddrIsBase- If true, toAddr is treated as base address; else treated as (base+offset)offset- Value added to a base address to get the toAddrtype- Reference typesource- The source of this referenceopIndex- The operand index
addShiftedMemReference()
Adds a shifted memory reference where the “to” address is computed as a value shifted by some number of bits.fromAddr- Source/from memory addresstoAddr- Destination/to memory addressshiftValue- Number of bits to shifttype- Reference typesource- The source of this referenceopIndex- The operand index
addStackReference()
Adds a reference to a stack location.fromAddr- “From” address within a functionopIndex- Operand indexstackOffset- Stack offset of the referencetype- Reference typesource- The source of this reference
If a reference already exists for the fromAddr and opIndex, it will be replaced.
addRegisterReference()
Adds a reference to a register.fromAddr- “From” addressopIndex- Operand indexregister- Register to add the reference totype- Reference typesource- The source of this reference
addExternalReference() - By Library Name
Adds an external reference to an external symbol by library name.fromAddr- From memory address (source of the reference)libraryName- Name of external programextLabel- Label within the external program (may be null if extAddr is not null)extAddr- Memory address within the external program (may be null)source- The source of this referenceopIndex- Operand indextype- Reference type
InvalidInputException- If libraryName is invalid or null, or invalid extLabel specifiedDuplicateNameException- If another non-Library namespace has the same nameIllegalArgumentException- If invalid extAddr was specified
addExternalReference() - By Namespace
Adds an external reference using an external namespace.fromAddr- From memory addressextNamespace- External namespace containing the named external labelextLabel- Label within the external program (may be null if extAddr is not null)extAddr- Address within the external program (may be null)source- The source of this referenceopIndex- Operand indextype- Reference type
InvalidInputException- If invalid extLabel specifiedDuplicateNameException- If another non-Library namespace has the same nameIllegalArgumentException- If invalid extAddr was specified
addExternalReference() - By External Location
Adds an external reference using an ExternalLocation object.fromAddr- From memory addressopIndex- Operand indexlocation- External locationsource- The source of this referencetype- Reference type
InvalidInputException if the input is invalid
Removing References
removeAllReferencesFrom() - Address Range
Removes all references where “from address” is in the given range.beginAddr- The first address in the rangeendAddr- The last address in the range
removeAllReferencesFrom() - Single Address
Removes all stack, external, and memory references from the given address.fromAddr- The address of the code unit from which to remove all references
removeAllReferencesTo()
Removes all stack, external, and memory references to the given address.toAddr- The address for which all references to should be removed
delete()
Deletes the given reference object.ref- The reference to be deleted
Retrieving References
getReference()
Returns the reference with the given from and to addresses and operand index.fromAddr- The address of the code unit making the referencetoAddr- The address being referred toopIndex- The operand index
getReferencesFrom() - All References
Returns all references “from” the specified address.addr- Address of code-unit making the references
getReferencesFrom() - By Operand
Returns all references “from” the given fromAddr and operand.fromAddr- The address from which to get referencesopIndex- The operand from which to get references
getReferencesTo() - Address
Returns an iterator over all references that have the given address as their “to” address.addr- The address that all references in the iterator refer to
getReferencesTo() - Variable
Returns all references to the given variable.var- Variable to retrieve references to
Only data references to storage are considered.
getFlowReferencesFrom()
Returns all flow references from the given address.addr- The address of the code unit to get all flows from
getExternalReferences()
Returns an iterator over all external space references.getPrimaryReferenceFrom()
Returns the primary reference from the given address and operand index.addr- From addressopIndex- Operand index
Reference Iterators
getReferenceIterator()
Returns a forward iterator over references starting with the specified fromAddr.startAddr- The first from address to consider
getReferenceSourceIterator() - Starting Address
Returns an iterator over addresses that are the “from” address in a reference.startAddr- Address to position iteratorforward-trueto iterate in forward direction
getReferenceSourceIterator() - Address Set
Returns an iterator over “from” addresses restricted by the given address set.addrSet- The set of addresses to restrict the iterator (or null for all addresses)forward-trueto iterate in forward direction
getReferenceDestinationIterator() - Starting Address
Returns an iterator over addresses that are the “to” address in a reference.startAddr- Start of iteratorforward-trueto iterate in forward direction
getReferenceDestinationIterator() - Address Set
Returns an iterator over “to” addresses restricted by the given address set.addrSet- The set of addresses to restrict the iterator (or null for all addresses)forward-trueto iterate in forward direction
Query Methods
hasReferencesFrom() - By Operand
Checks if there are any memory references at the given address and operand index.fromAddr- The address of the code unit being testedopIndex- The index of the operand being tested
true if one or more references are defined
This is a rather inefficient method as it must examine all references from the specified fromAddr.
hasReferencesFrom() - Address
Checks if there are any memory references at the given address.fromAddr- The address of the code unit being tested
true if one or more references are defined
hasReferencesTo()
Checks if a memory reference exists with the given “to” address.toAddr- Address being referred to
true if specified toAddr has one or more references to it
hasFlowReferencesFrom()
Checks if the given address has flow references from it.addr- The address to test for flow references
true if the address has flow references from it
Reference Counts
getReferenceCountTo()
Returns the number of references to the specified address.toAddr- The address being referenced
getReferenceCountFrom()
Returns the number of references from the specified address.fromAddr- The address of the code unit making the reference
getReferenceDestinationCount()
Returns the number of references for “to” addresses.getReferenceSourceCount()
Returns the number of references for “from” addresses.Modifying References
setPrimary()
Sets the reference’s primary attribute.ref- The reference to modifyisPrimary-trueto make the reference primary,falseto make it non-primary
updateRefType()
Updates the reference type on a memory reference.ref- Reference to be updatedrefType- New reference type
setAssociation()
Associates the given reference with the given symbol.s- The symbol to associate with the given referenceref- The reference to associate with the given symbol
IllegalArgumentException if the reference doesn’t exist or its “to” address doesn’t match the symbol’s address
Applies to memory references only where a specified label symbol must have an address matching the reference to-address. Stack and register reference associations to variable symbols are always inferred.
removeAssociation()
Removes any symbol associations with the given reference.ref- The reference for which any symbol association is to be removed
IllegalArgumentException if the reference doesn’t exist
Variable Methods
getReferencedVariable()
Returns the referenced function variable.reference- Variable reference
getReferenceLevel()
Returns the reference level for references to the given address.toAddr- The address at which to find the highest reference level
Usage Examples
Adding Memory References
Adding External References
Querying References
Iterating Over All References
Working with Flow References
See Also
- Function Manager - Managing functions in a program
- Analyzers - Creating custom analyzers
- Decompiler - Decompiling functions
