ghidra.program.model.symbol.Reference Java Examples
The following examples show how to use
ghidra.program.model.symbol.Reference.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReferenceLineDispenser.java From ghidra with Apache License 2.0 | 6 votes |
ReferenceLineDispenser(Variable var, Program program, ProgramTextOptions options) { this.memory = program.getMemory(); this.referenceManager = program.getReferenceManager(); this.displayRefHeader = options.isShowReferenceHeaders(); this.header = "XREF"; this.headerWidth = options.getRefHeaderWidth(); this.prefix = options.getCommentPrefix(); this.width = options.getStackVarXrefWidth(); this.fillAmount = options.getStackVarPreNameWidth() + options.getStackVarNameWidth() + options.getStackVarDataTypeWidth() + options.getStackVarOffsetWidth() + options.getStackVarCommentWidth(); this.isHTML = options.isHTML(); List<Reference> xrefs = new ArrayList<Reference>(); List<Reference> offcuts = new ArrayList<Reference>(); XReferenceUtil.getVariableRefs(var, xrefs, offcuts); Address[] xrefAddr = extractFromAddr(xrefs); Address[] offcutsAddr = extractFromAddr(offcuts); processRefs(var.getFunction().getEntryPoint(), xrefAddr, offcutsAddr); }
Example #2
Source File: CodeManagerTest.java From ghidra with Apache License 2.0 | 6 votes |
@Test public void testCreateArrayPointersWithSomeNullsDoesntBail() throws Exception { Memory memory = program.getMemory(); memory.setBytes(addr(0x2000), bytes(0, 0, 1, 1)); Pointer p = new Pointer16DataType(); assertEquals(2, p.getLength()); Array pArray = new ArrayDataType(p, 2, 2); listing.createData(addr(0x2000), pArray, 4); Data data = listing.getDataAt(addr(0x2000)); assertEquals(2, data.getNumComponents()); assertEquals(addr(0x0000), data.getComponent(0).getValue()); assertEquals(addr(0x0101), data.getComponent(1).getValue()); Reference[] referencesFrom = data.getComponent(0).getReferencesFrom(); assertEquals(0, referencesFrom.length); referencesFrom = data.getComponent(1).getReferencesFrom(); assertEquals(1, referencesFrom.length); assertEquals(addr(0x0101), referencesFrom[0].getToAddress()); }
Example #3
Source File: InstructionPcodeOverride.java From ghidra with Apache License 2.0 | 6 votes |
/** * This constructor caches the primary and overriding "from" references of {@code instr}. * This cache is never updated; the assumption is that this object is short-lived * (duration of {@link PcodeEmit}) * @param instr the instruction */ public InstructionPcodeOverride(Instruction instr) { this.instr = instr; primaryOverridingReferences = new ArrayList<>(); for (Reference ref : instr.getReferencesFrom()) { if (!ref.isPrimary() || !ref.getToAddress().isMemoryAddress()) { continue; } RefType type = ref.getReferenceType(); if (type.isOverride()) { primaryOverridingReferences.add(ref); } else if (type.isCall() && primaryCallAddress == null) { primaryCallAddress = ref.getToAddress(); } } }
Example #4
Source File: VarnodeContext.java From ghidra with Apache License 2.0 | 6 votes |
/** * Check if the symbol at the address is read_only. * * @param addr - address of the symbol * * @return true if the block is read_only, and there are no write references. */ protected boolean isReadOnly(Address addr) { boolean readonly = false; MemoryBlock block = program.getMemory().getBlock(addr); if (block != null) { readonly = !block.isWrite(); // if the block says read-only, check the refs to the variable if (readonly) { ReferenceIterator refIter = program.getReferenceManager().getReferencesTo(addr); int count = 0; while (refIter.hasNext() && count < 100) { Reference ref = refIter.next(); if (ref.getReferenceType().isWrite()) { readonly = false; break; } count++; } } } return readonly; }
Example #5
Source File: ToAdapterV0.java From ghidra with Apache License 2.0 | 6 votes |
private byte getRefLevel(Record newRec) { try { RefList refList = new RefListV0(newRec, this, addrMap, null, null, false); Reference[] refs = refList.getAllRefs(); byte refLevel = (byte) -1; for (Reference ref : refs) { byte level = RefListV0.getRefLevel(ref.getReferenceType()); if (level > refLevel) { refLevel = level; } } return refLevel; } catch (IOException e) { throw new RuntimeException("IOException unexpected for ToAdapterV0 RefList"); } }
Example #6
Source File: ReferenceFunctionAlgorithm.java From ghidra with Apache License 2.0 | 6 votes |
@Override public int score(Function function, TaskMonitor monitor) throws CancelledException { Program program = function.getProgram(); ReferenceManager referenceManager = program.getReferenceManager(); AddressSetView body = function.getBody(); long maxIterations = body.getNumAddresses(); monitor.initialize(maxIterations); AddressIterator iterator = referenceManager.getReferenceSourceIterator(body, true); int referenceCount = 0; while (iterator.hasNext()) { monitor.checkCanceled(); Address address = iterator.next(); Reference[] referencesFrom = referenceManager.getReferencesFrom(address); referenceCount += referencesFrom.length; monitor.incrementProgress(1); artificialSleepForDemoPurposes(); } return referenceCount; }
Example #7
Source File: RemoveOffcutReferenceToCurrentInstructionScript.java From ghidra with Apache License 2.0 | 6 votes |
private void removeReferences(CodeUnit codeUnit) { Address address = currentAddress.add( 1 ); while ( address.compareTo( codeUnit.getMaxAddress() ) <= 0) { if ( monitor.isCancelled() ) { break; } Reference [] referencesTo = getReferencesTo(address); for ( Reference reference : referencesTo ) { if ( monitor.isCancelled() ) { break; } removeReference(reference); } address = address.add( 1 ); } }
Example #8
Source File: OverridePrototypeAction.java From ghidra with Apache License 2.0 | 6 votes |
private Function getCalledFunction(Program program, PcodeOp op) { if (op.getOpcode() != PcodeOp.CALL) { return null; } Address addr = op.getInput(0).getAddress(); FunctionManager functionManager = program.getFunctionManager(); Function function = functionManager.getFunctionAt(addr); if (function != null) { return function; } Address opAddr = op.getSeqnum().getTarget(); Reference[] references = program.getReferenceManager().getFlowReferencesFrom(opAddr); for (Reference ref : references) { if (ref.getReferenceType().isCall()) { function = functionManager.getFunctionAt(ref.getToAddress()); if (function != null) { return function; } } } return null; }
Example #9
Source File: InstructionStasher.java From ghidra with Apache License 2.0 | 6 votes |
public void restore() throws CodeUnitInsertionException { if (prototype == null) { return; } MemBuffer buf = new DumbMemBufferImpl(program.getMemory(), minAddress); ProcessorContext context = new ProgramProcessorContext(program.getProgramContext(), minAddress); program.getListing().createInstruction(minAddress, prototype, buf, context); for (Reference reference : referencesFrom) { if (reference.getSource() != SourceType.DEFAULT) { program.getReferenceManager().addReference(reference); } } }
Example #10
Source File: SelectBackRefsAction.java From ghidra with Apache License 2.0 | 6 votes |
private ProgramSelection getSelection(Program program, AddressSetView addressSetView){ AddressSet addressSet = new AddressSet(); AddressIterator refAddrIter = program.getReferenceManager().getReferenceDestinationIterator(addressSetView,true); while (refAddrIter.hasNext()) { Address reffedAddr = refAddrIter.next(); ReferenceIterator memRefIter = program.getReferenceManager().getReferencesTo(reffedAddr); while (memRefIter.hasNext()){ Reference memRef = memRefIter.next(); Address addr = memRef.getFromAddress(); if ( addr.isMemoryAddress() ) { addressSet.addRange(addr,addr); } } } return new ProgramSelection(addressSet); }
Example #11
Source File: MnemonicFieldFactory.java From ghidra with Apache License 2.0 | 6 votes |
private Address getReferenceAddress(CodeUnit cu) { Program program = cu.getProgram(); if (cu instanceof Data) { if (((Data) cu).getNumComponents() != 0) { return null; // outer composite/array type should ignore reference from component } } ReferenceManager referenceManager = program.getReferenceManager(); Reference[] referencesFrom = referenceManager.getReferencesFrom(cu.getMinAddress()); for (Reference reference : referencesFrom) { if (reference.isMemoryReference()) { return reference.getToAddress(); } } return null; }
Example #12
Source File: CallNode.java From ghidra with Apache License 2.0 | 6 votes |
protected Set<Reference> getReferencesFrom(Program program, AddressSetView addresses, TaskMonitor monitor) throws CancelledException { Set<Reference> set = new HashSet<Reference>(); ReferenceManager referenceManager = program.getReferenceManager(); AddressIterator addressIterator = addresses.getAddresses(true); while (addressIterator.hasNext()) { monitor.checkCanceled(); Address address = addressIterator.next(); Reference[] referencesFrom = referenceManager.getReferencesFrom(address); if (referencesFrom != null) { for (Reference reference : referencesFrom) { set.add(reference); } } } return set; }
Example #13
Source File: CallTreePlugin.java From ghidra with Apache License 2.0 | 6 votes |
/** * * Apparently, we create fake function markup for external functions. Thus, there is no * real function at that address and our plugin has to do some work to find out where * we 'hang' references to the external function, which is itself a Function. These * fake function will usually just be a pointer to another function. * * @param function the function to resolve; if it is not null, then it will be used * @param address the address for which to find a function * @return either the given function if non-null, or a function being referenced from the * given address. */ Function resolveFunction(Function function, Address address) { if (function != null) { return function; } // maybe we point to another function? FunctionManager functionManager = currentProgram.getFunctionManager(); ReferenceManager referenceManager = currentProgram.getReferenceManager(); Reference[] references = referenceManager.getReferencesFrom(address); for (Reference reference : references) { Address toAddress = reference.getToAddress(); Function toFunction = functionManager.getFunctionAt(toAddress); if (toFunction != null) { return toFunction; } } return null; }
Example #14
Source File: SelectForwardRefsAction.java From ghidra with Apache License 2.0 | 6 votes |
private ProgramSelection getSelection(Program program, AddressSetView addressSetView) { AddressSet addressSet = new AddressSet(); CodeUnitIterator iter = program.getListing().getCodeUnits(addressSetView, true); while (iter.hasNext()) { CodeUnit cu = iter.next(); Reference[] memRef = cu.getReferencesFrom(); for (Reference element : memRef) { Address addr = element.getToAddress(); if (addr.isMemoryAddress()) { addressSet.addRange(addr, addr); } } } return new ProgramSelection(addressSet); }
Example #15
Source File: EditReferenceDialog.java From ghidra with Apache License 2.0 | 6 votes |
public void initDialog(CodeUnit cu, int opIndex, int subIndex, Reference ref) { initializing = true; instrPanel.setCodeUnitLocation(cu, opIndex, subIndex, ref != null); if (ref != null) { configureEditReference(cu, ref); } else { configureAddReference(opIndex, subIndex); } initializing = false; activeRefPanel.requestFocus(); }
Example #16
Source File: CodeBrowserPlugin.java From ghidra with Apache License 2.0 | 6 votes |
private void showXrefs(ActionContext context) { TableService service = tool.getService(TableService.class); if (service == null) { return; } ListingActionContext lac = (ListingActionContext) context; ProgramLocation location = lac.getLocation(); if (location == null) { return; // not sure if this can happen } Set<Reference> refs = XReferenceUtil.getAllXrefs(location); XReferenceUtil.showAllXrefs(connectedProvider, tool, service, location, refs); }
Example #17
Source File: ShowConstantUse.java From ghidra with Apache License 2.0 | 6 votes |
private Function getReferencedFunction(Address faddr) { Function f = currentProgram.getFunctionManager().getFunctionAt(faddr); // couldn't find the function, see if there is an external ref there. if (f == null) { Reference[] referencesFrom = currentProgram.getReferenceManager().getReferencesFrom(faddr); for (int i = 0; i < referencesFrom.length; i++) { if (referencesFrom[i].isExternalReference()) { faddr = referencesFrom[i].getToAddress(); f = currentProgram.getFunctionManager().getFunctionAt(faddr); if (f != null) { break; } } } } return f; }
Example #18
Source File: CodeUnitDetails.java From ghidra with Apache License 2.0 | 6 votes |
private static String getRefInfo(Program pgm, Reference ref) { String typeStr = "Type: " + ref.getReferenceType(); String fromStr = " From: " + ref.getFromAddress(); String operandStr = ((ref.isMnemonicReference()) ? " Mnemonic" : (" Operand: " + ref.getOperandIndex())); String toStr = " To: " + DiffUtility.getUserToAddressString(pgm, ref); String sourceStr = " " + ref.getSource().toString(); String primaryStr = ((ref.isPrimary()) ? " Primary" : ""); String symbolStr = ""; long symbolID = ref.getSymbolID(); if (symbolID != -1) { Symbol sym = pgm.getSymbolTable().getSymbol(symbolID); if (sym != null) { symbolStr = " Symbol: " + sym.getName(true); } } return typeStr + fromStr + operandStr + toStr + sourceStr + primaryStr + symbolStr; }
Example #19
Source File: CodeUnitDetails.java From ghidra with Apache License 2.0 | 6 votes |
private static String getProgramRefDetails(Program pgm, Reference[] refs) { String indent = INDENT1; if (refs.length == 0) { return indent + "None"; } StringBuffer buf = new StringBuffer(); for (int i = 0; i < refs.length; i++) { if (refs[i].isExternalReference()) { buf.append(indent + "External Reference " + getRefInfo(pgm, refs[i]) + NEW_LINE); } else if (refs[i].isStackReference()) { buf.append(indent + "Stack Reference " + getRefInfo(pgm, refs[i]) + NEW_LINE); } else { buf.append(indent + "Reference " + getRefInfo(pgm, refs[i]) + NEW_LINE); } } return buf.toString(); }
Example #20
Source File: ReferenceLineDispenser.java From ghidra with Apache License 2.0 | 5 votes |
private Address [] extractFromAddr(List<Reference> refs) { Address [] addrs = new Address[refs.size()]; for (int i=0; i < addrs.length; i++) { addrs[i] = refs.get(i).getFromAddress(); } Arrays.sort(addrs); return addrs; }
Example #21
Source File: CodeManager64Test.java From ghidra with Apache License 2.0 | 5 votes |
@Test public void testCreateArrayPointers64() throws Exception { Memory memory = program.getMemory(); memory.setBytes(addr(0x2000), bytes(1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0)); Pointer p = new Pointer64DataType(); assertEquals(8, p.getLength()); Array pArray = new ArrayDataType(p, 3, 24); listing.createData(addr(0x2000), pArray, 24); Data data = listing.getDataAt(addr(0x2000)); assertEquals(3, data.getNumComponents()); assertEquals(addr(0x0000000100000001L), data.getComponent(0).getValue()); assertEquals(addr(0x0000000100000002L), data.getComponent(1).getValue()); assertEquals(addr(0x0000000100000003L), data.getComponent(2).getValue()); Reference[] referencesFrom = data.getComponent(0).getReferencesFrom(); assertEquals(1, referencesFrom.length); assertEquals(addr(0x0000000100000001L), referencesFrom[0].getToAddress()); referencesFrom = data.getComponent(1).getReferencesFrom(); assertEquals(1, referencesFrom.length); assertEquals(addr(0x0000000100000002L), referencesFrom[0].getToAddress()); // limit of 2 new 32 bit segments can be created from array of pointers referencesFrom = data.getComponent(2).getReferencesFrom(); assertEquals(1, referencesFrom.length); assertEquals(addr(0x0000000100000003L), referencesFrom[0].getToAddress()); }
Example #22
Source File: EditReferenceDialog.java From ghidra with Apache License 2.0 | 5 votes |
private void configureEditReference(CodeUnit cu, Reference ref) { setTitle("Edit Reference"); setHelpLocation(EDIT_HELP); applyButton.setText("Update"); memRefChoice.setEnabled(false); extRefChoice.setEnabled(false); stackRefChoice.setEnabled(false); regRefChoice.setEnabled(false); Address toAddress = ref.getToAddress(); if (toAddress.isRegisterAddress() || cu.getProgram().getRegister(toAddress) != null) { regRefPanel.initialize(cu, ref); regRefChoice.setSelected(true); regRefChoice.setEnabled(true); if (toAddress.isMemoryAddress()) { memRefPanel.initialize(cu, ref); memRefChoice.setEnabled(true); } } else if (toAddress.isStackAddress()) { stackRefPanel.initialize(cu, ref); stackRefChoice.setSelected(true); stackRefChoice.setEnabled(true); } else if (toAddress.isMemoryAddress()) { memRefPanel.initialize(cu, ref); memRefChoice.setSelected(true); memRefChoice.setEnabled(true); } else if (toAddress.isExternalAddress()) { extRefPanel.initialize(cu, ref); extRefChoice.setSelected(true); extRefChoice.setEnabled(true); } else { throw new AssertException("Unknown address type"); } }
Example #23
Source File: ArmThumbChangeDisassemblyTest.java From ghidra with Apache License 2.0 | 5 votes |
@Test public void testCorrectDisassembly() throws Exception { ProgramBuilder programBuilder = new ProgramBuilder("Test", ProgramBuilder._ARM); program = programBuilder.getProgram(); int txId = program.startTransaction("Add Memory");// leave open until tearDown programBuilder.createMemory(".text", "1000", 64).setExecute(true);// initialized programBuilder.setBytes("1000", "ff ff ff ea 01 c0 8f e2 1c ff 2f e1 82 08 30 b5 70 47"); programBuilder.disassemble("1000", 11, true); programBuilder.analyze(); // should disassemble as ARM, then transition to Thumb Address instrAddr = programBuilder.addr("100c"); Instruction instructionAt = program.getListing().getInstructionAt(instrAddr); Assert.assertNotEquals(null,instructionAt); assertEquals(6, program.getListing().getNumInstructions()); RegisterValue registerValue = program.getProgramContext().getRegisterValue(program.getRegister("TMode"), instrAddr); assertEquals(1,registerValue.getUnsignedValue().intValue()); // make sure reference put on operand 0, not mnemonic instrAddr = programBuilder.addr("1008"); instructionAt = program.getListing().getInstructionAt(instrAddr); Reference[] operandReferences = instructionAt.getOperandReferences(0); assertEquals(1,operandReferences.length); assertEquals(0x100cL, operandReferences[0].getToAddress().getOffset()); }
Example #24
Source File: ReferencesFromTableModel.java From ghidra with Apache License 2.0 | 5 votes |
public ReferencesFromTableModel(List<Reference> refs, ServiceProvider sp, Program program) { super("References", sp, program, null); this.refs = refs.stream().map(r -> { boolean offcut = ReferenceUtils.isOffcut(program, r.getToAddress()); return new IncomingReferenceEndpoint(r, offcut); }).collect(Collectors.toList()); addTableColumn(new ReferenceTypeTableColumn()); }
Example #25
Source File: VariableXRefFieldFactory.java From ghidra with Apache License 2.0 | 5 votes |
/** * @see ghidra.app.util.viewer.field.FieldFactory#getProgramLocation(int, int, ghidra.app.util.viewer.field.ListingField) */ @Override public ProgramLocation getProgramLocation(int row, int col, ListingField bf) { Object obj = bf.getProxy().getObject(); if (!(obj instanceof Variable)) { return null; } ListingTextField field = (ListingTextField) getField(bf.getProxy(), 0); if (field != null) { RowColLocation loc = field.screenToDataLocation(row, col); int index = loc.row(); Variable var = (Variable) obj; List<Reference> xrefs = new ArrayList<>(); List<Reference> offcuts = new ArrayList<>(); XReferenceUtil.getVariableRefs(var, xrefs, offcuts); Reference ref = null; if (index < xrefs.size()) { ref = xrefs.get(index); } else if (index < xrefs.size() + offcuts.size()) { ref = offcuts.get(index - xrefs.size()); } if (ref != null) { Address refAddr = ref.getFromAddress(); return new VariableXRefFieldLocation(var.getProgram(), var, refAddr, index, loc.col()); } } return null; }
Example #26
Source File: VariableXRefFieldMouseHandler.java From ghidra with Apache License 2.0 | 5 votes |
protected void showXRefDialog(Navigatable navigatable, ProgramLocation location, ServiceProvider serviceProvider) { TableService service = serviceProvider.getService(TableService.class); if (service == null) { return; } VariableLocation variableLocation = (VariableLocation) location; Variable variable = variableLocation.getVariable(); Set<Reference> refs = XReferenceUtil.getVariableRefs(variable); XReferenceUtil.showAllXrefs(navigatable, serviceProvider, service, location, refs); }
Example #27
Source File: ResultsState.java From ghidra with Apache License 2.0 | 5 votes |
private int findOpIndex(PcodeOp op, Varnode loc) { if (loc instanceof VarnodeOperation) { return -1; } Instruction instr = listing.getInstructionAt(op.getSeqnum().getTarget()); int numOperands = instr.getNumOperands(); for (int i = 0; i < numOperands; i++) { PcodeOp[] operandPcode = instr.getPcode(i); if (operandPcode == null || operandPcode.length == 0) { continue; } if (matchOpPcodeObjectAssignment(operandPcode, loc)) { return i; } } int opMatchCnt = 0; int lastOpMatch = -1; for (int i = 0; i < numOperands; i++) { // Check representation if (matchOpObject(instr, i, loc)) { ++opMatchCnt; lastOpMatch = i; } } if (opMatchCnt == 1) { return lastOpMatch; } return Reference.MNEMONIC; }
Example #28
Source File: AcyclicCallGraphBuilder.java From ghidra with Apache License 2.0 | 5 votes |
private boolean isStartFunction(Address address) { ReferenceManager referenceManager = program.getReferenceManager(); Iterable<Reference> referencesTo = referenceManager.getReferencesTo(address); for (Reference reference : referencesTo) { if (reference.isEntryPointReference()) { return true; } if (reference.getReferenceType().isCall()) { //Assume that any call implies that none of the references will be entry point reference. return false; } } return true; }
Example #29
Source File: GenericRefernenceBaseRelocationFixupHandler.java From ghidra with Apache License 2.0 | 5 votes |
private boolean hasMatchingReference(Program program, Address address, Address candiateRelocationValue) { CodeUnit cu = program.getListing().getCodeUnitContaining(address); Reference[] referencesFrom = cu.getReferencesFrom(); for (Reference reference : referencesFrom) { if (reference.getToAddress().equals(candiateRelocationValue)) { return true; } } return false; }
Example #30
Source File: FunctionLocation.java From ghidra with Apache License 2.0 | 5 votes |
@Override public boolean isValid(Program p) { if (!super.isValid(p)) { return false; } Listing listing = p.getListing(); if (!addr.equals(functionAddr)) { // ensure that inferred function reference is valid if (listing.getFunctionAt(addr) != null) { return false; } CodeUnit cu = listing.getCodeUnitAt(addr); if (!(cu instanceof Data)) { return false; } Data data = (Data) cu; if (!(data.getDataType() instanceof Pointer)) { return false; } Reference ref = data.getPrimaryReference(0); if (ref == null || !ref.getToAddress().equals(functionAddr)) { return false; } } return listing.getFunctionAt(functionAddr) != null; }