Java Code Examples for ghidra.program.model.listing.CodeUnit#getMinAddress()

The following examples show how to use ghidra.program.model.listing.CodeUnit#getMinAddress() . 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: OffcutReferenceCountToAddressTableColumn.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public Integer getValue(Address address, Settings settings, Program program,
		ServiceProvider serviceProvider) throws IllegalArgumentException {
	int count = 0;
	if (address.isMemoryAddress()) {
		CodeUnit codeUnit = program.getListing().getCodeUnitContaining(address);
		if (codeUnit != null) {
			AddressSet set =
				new AddressSet(codeUnit.getMinAddress(), codeUnit.getMaxAddress());
			set.deleteRange(address, address);
			ReferenceManager referenceManager = program.getReferenceManager();
			AddressIterator it = referenceManager.getReferenceDestinationIterator(set, true);
			while (it.hasNext()) {
				it.next();
				count++;
			}
		}
	}
	return Integer.valueOf(count);
}
 
Example 2
Source File: MemoryBlockStartFieldFactory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Overridden to ensure that we return a {@link MemoryBlockStartFieldLocation} instance.
 * 
 * @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 proxyObject = bf.getProxy().getObject();
	if (!(proxyObject instanceof CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) proxyObject;

	String[] comments;
	List<AttributedString> attributedStrings = createBlockStartText(cu);
	if (attributedStrings == null) {
		comments = new String[0];
	}
	else {
		comments = new String[attributedStrings.size()];
		for (int i = 0; i < comments.length; i++) {
			comments[i] = attributedStrings.get(i).getText();
		}
	}

	return new MemoryBlockStartFieldLocation(cu.getProgram(), cu.getMinAddress(), null, row,
		col, comments, 0);
}
 
Example 3
Source File: SpaceFieldFactory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * @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 CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) obj;

	int[] cpath = null;
	if (obj instanceof Data) {
		cpath = ((Data) obj).getComponentPath();
	}

	return new SpaceFieldLocation(cu.getProgram(), cu.getMinAddress(), null, cpath, row);
}
 
Example 4
Source File: AddressFieldFactory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField lf) {
	Object obj = lf.getProxy().getObject();
	if (!(obj instanceof CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) obj;

	Address addr = cu.getMinAddress();

	int[] cpath = null;
	if (cu instanceof Data) {
		cpath = ((Data) cu).getComponentPath();
	}

	return new AddressFieldLocation(cu.getProgram(), addr, cpath, addr.toString(), col);
}
 
Example 5
Source File: AddressFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getAddressString(CodeUnit cu) {
	Address addr = cu.getMinAddress();
	AddressSpace space = addr.getAddressSpace();
	if (displayBlockName) {
		String text = addr.toString(false, padZeros ? 16 : minHexDigits);
		MemoryBlock block = cu.getProgram().getMemory().getBlock(addr);
		if (block != null) {
			return block.getName() + ":" + text;
		}
	}
	return addr.toString(space.showSpaceName(), padZeros ? 16 : minHexDigits);
}
 
Example 6
Source File: ExternalDisassemblyFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	ProxyObj<?> proxy = bf.getProxy();
	Object obj = proxy.getObject();
	if (!(obj instanceof CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) obj;

	return new ExternalDisassemblyFieldLocation(cu.getProgram(), cu.getMinAddress(), row, col);
}
 
Example 7
Source File: ProgramMemoryUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Checks a programs memory for direct references to the CodeUnit indicated.
 * Direct references are only found at addresses that match the indicated alignment. 
 * @param program the program whose memory is to be checked.
 * @param alignment direct references are to only be found at the indicated alignment in memory.
 * @param codeUnit the code unit to to search for references to.
 * @param monitor a task monitor for progress or to allow canceling.
 * @return list of addresses referring directly to the toAddress.
 */
public static List<Address> findDirectReferencesCodeUnit(Program program, int alignment,
		CodeUnit codeUnit, TaskMonitor monitor) {

	if (monitor == null) {
		monitor = TaskMonitorAdapter.DUMMY_MONITOR;
	}

	AddressSet toAddressSet =
		new AddressSet((codeUnit.getMinAddress()), codeUnit.getMaxAddress());

	List<ReferenceAddressPair> directReferenceList = new ArrayList<>();
	List<Address> results = new ArrayList<>();

	try {
		ProgramMemoryUtil.loadDirectReferenceList(program, alignment,
			toAddressSet.getMinAddress(), toAddressSet, directReferenceList, monitor);
	}
	catch (CancelledException e) {
		return Collections.emptyList();
	}

	for (ReferenceAddressPair rap : directReferenceList) {
		if (monitor.isCancelled()) {
			return null;
		}
		Address fromAddr = rap.getSource();
		if (!results.contains(fromAddr)) {
			results.add(fromAddr);
		}
	}

	return results;
}
 
Example 8
Source File: ListingDiffHighlightProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Highlight[] getOperandDiffHighlights(String text, CodeUnit codeUnit,
		int cursorTextOffset) {
	Address minAddress = codeUnit.getMinAddress();
	AddressSetView unmatchedDiffs = (isListing1) ? listingDiff.getListing1UnmatchedCode()
			: listingDiff.getListing2UnmatchedCode();
	if (unmatchedDiffs.contains(minAddress)) {
		return EMPTY_HIGHLIGHT;
	}
	Color operandDiffsBackgroundColor = comparisonOptions.getOperandDiffsBackgroundColor();
	AddressSetView codeUnitDiffs = (isListing1) ? listingDiff.getListing1CodeUnitDiffs()
			: listingDiff.getListing2CodeUnitDiffs();
	// Get intersection of Code Unit Diff addresses and this code unit's addresses
	AddressSet diffSet = new AddressSet(codeUnit.getMinAddress(), codeUnit.getMaxAddress());
	diffSet = diffSet.intersect(codeUnitDiffs);
	if (!diffSet.isEmpty()) {
		CodeUnit matchingCodeUnit = listingDiff.getMatchingCodeUnit(codeUnit, isListing1);
		if (listingDiff.doesEntireOperandSetDiffer(codeUnit, matchingCodeUnit)) {
			return entireTextHighlight(text, cursorTextOffset, operandDiffsBackgroundColor);
		}
		Pair[] pairs = getOperandPairs(text, codeUnit);
		int numOperands = codeUnit.getNumOperands();
		if (pairs.length != numOperands) {
			return entireTextHighlight(text, cursorTextOffset, operandDiffsBackgroundColor);
		}
		int[] diffOpIndices = listingDiff.getOperandsThatDiffer(codeUnit, matchingCodeUnit);
		ArrayList<Highlight> highlights = new ArrayList<>();
		for (int diffOpIndex : diffOpIndices) {
			// Highlight each operand that differs.
			highlights.add(new Highlight(pairs[diffOpIndex].start, pairs[diffOpIndex].end,
				operandDiffsBackgroundColor));
		}
		return highlights.toArray(new Highlight[highlights.size()]);
	}
	return EMPTY_HIGHLIGHT;
}
 
Example 9
Source File: ListingDiffHighlightProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Highlight[] getMnemonicDiffHighlights(String text, CodeUnit codeUnit,
		int cursorTextOffset) {
	Address minAddress = codeUnit.getMinAddress();
	AddressSetView unmatchedDiffs = (isListing1) ? listingDiff.getListing1UnmatchedCode()
			: listingDiff.getListing2UnmatchedCode();
	if (unmatchedDiffs.contains(minAddress)) {
		return EMPTY_HIGHLIGHT;
	}
	Color mnemonicDiffsBackgroundColor = comparisonOptions.getMnemonicDiffsBackgroundColor();
	AddressSetView codeUnitDiffs = (isListing1) ? listingDiff.getListing1CodeUnitDiffs()
			: listingDiff.getListing2CodeUnitDiffs();
	// Get intersection of Code Unit Diff addresses and this code unit's addresses
	AddressSet diffSet = new AddressSet(codeUnit.getMinAddress(), codeUnit.getMaxAddress());
	diffSet = diffSet.intersect(codeUnitDiffs);
	if (!diffSet.isEmpty()) {
		CodeUnit otherCodeUnit = listingDiff.getMatchingCodeUnit(codeUnit, isListing1);
		if (otherCodeUnit == null) {
			return entireTextHighlight(text, cursorTextOffset, mnemonicDiffsBackgroundColor);
		}
		// Highlight the mnemonic if they differ.
		boolean sameMnemonics =
			codeUnit.getMnemonicString().equals(otherCodeUnit.getMnemonicString());
		if (!sameMnemonics) {
			return entireTextHighlight(text, cursorTextOffset, mnemonicDiffsBackgroundColor);
		}
	}
	return EMPTY_HIGHLIGHT;
}
 
Example 10
Source File: ListingDiffHighlightProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Highlight[] getByteDiffHighlights(String text, CodeUnit codeUnit,
		int cursorTextOffset) {
	Address minAddress = codeUnit.getMinAddress();
	AddressSetView unmatchedDiffs = (isListing1) ? listingDiff.getListing1UnmatchedCode()
			: listingDiff.getListing2UnmatchedCode();
	if (unmatchedDiffs.contains(minAddress)) {
		return EMPTY_HIGHLIGHT;
	}
	Color byteDiffsBackgroundColor = comparisonOptions.getByteDiffsBackgroundColor();
	AddressSetView byteDiffs =
		(isListing1) ? listingDiff.getListing1ByteDiffs() : listingDiff.getListing2ByteDiffs();
	// Get intersection of Byte Diff addresses and this code unit's addresses
	AddressSet diffSet = new AddressSet(codeUnit.getMinAddress(), codeUnit.getMaxAddress());
	diffSet = diffSet.intersect(byteDiffs);
	if (!diffSet.isEmpty()) {
		ArrayList<Highlight> highlights = new ArrayList<>();
		// Get Highlight for each byte that differs.
		for (AddressRange addressRange : diffSet) {
			Address rangeMinAddress = addressRange.getMinAddress();
			long minByteIndex = rangeMinAddress.subtract(minAddress);
			int startIndex = (int) minByteIndex * NUM_CHARACTERS_PER_BYTE;
			Address rangeMaxAddress = addressRange.getMaxAddress();
			long maxByteIndex = rangeMaxAddress.subtract(minAddress);
			int endIndex = (int) (maxByteIndex * NUM_CHARACTERS_PER_BYTE) + 1;
			highlights.add(new Highlight(startIndex, endIndex, byteDiffsBackgroundColor));
		}
		return highlights.toArray(new Highlight[highlights.size()]);
	}
	return EMPTY_HIGHLIGHT;
}
 
Example 11
Source File: CodeUnitProxy.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a proxy for a code unit
 * @param program the program containing the code unit
 * @param cu the code unit to proxy.
 */
public CodeUnitProxy(ListingModel model, Program program, CodeUnit cu) {
	super(model);
	this.program = program;
	this.cu = cu;
	this.addr = cu.getMinAddress();
}
 
Example 12
Source File: PseudoCodeUnit.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Return true if the given CodeUnit follows directly after this code unit.
 * 
 * @throws ConcurrentModificationException
 *             if this object is no longer valid.
 */
@Override
public boolean isSuccessor(CodeUnit codeUnit) {
	Address min = codeUnit.getMinAddress();

	return this.getMaxAddress().isSuccessor(min);
}
 
Example 13
Source File: SpacerFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	Object obj = bf.getProxy().getObject();
	if (!(obj instanceof CodeUnit)) {
		return null;
	}
	CodeUnit cu = (CodeUnit) obj;

	int[] cpath = null;
	if (obj instanceof Data) {
		cpath = ((Data) obj).getComponentPath();
	}

	return new SpacerFieldLocation(cu.getProgram(), cu.getMinAddress(), cpath, col, text);
}
 
Example 14
Source File: CommentsPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void updateComments(CodeUnit cu, String preComment, String postComment, String eolComment,
		String plateComment, String repeatableComment) {
	preComment = (preComment.length() == 0) ? null : preComment;
	postComment = (postComment.length() == 0) ? null : postComment;
	eolComment = (eolComment.length() == 0) ? null : eolComment;
	plateComment = (plateComment.length() == 0) ? null : plateComment;
	repeatableComment = (repeatableComment.length() == 0) ? null : repeatableComment;

	Command cmd = new SetCommentsCmd(cu.getMinAddress(), preComment, postComment, eolComment,
		plateComment, repeatableComment);

	tool.execute(cmd, cu.getProgram());
}
 
Example 15
Source File: MemSearchPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Highlight[] getHighlights(String text, Object obj,
		Class<? extends FieldFactory> fieldFactoryClass, int cursorTextOffset) {
	Program program = navigatable != null ? navigatable.getProgram() : null;
	if (fieldFactoryClass != BytesFieldFactory.class) {
		return NO_HIGHLIGHTS;
	}
	if (checkRemoveHighlights()) {
		return NO_HIGHLIGHTS;
	}
	if (!(obj instanceof CodeUnit)) {
		return NO_HIGHLIGHTS;
	}
	if (!doHighlight) {
		return NO_HIGHLIGHTS;
	}

	if (highlightProgram != program) {
		return NO_HIGHLIGHTS;
	}

	CodeUnit cu = (CodeUnit) obj;
	Address minAddr = cu.getMinAddress();
	Address maxAddr = cu.getMaxAddress();
	List<MemSearchResult> results = getAddressesFoundInRange(minAddr, maxAddr);

	Highlight[] highlights = new Highlight[results.size()];
	for (int i = 0; i < highlights.length; i++) {
		MemSearchResult result = results.get(i);
		int highlightLength = result.getLength();
		Address addr = result.getAddress();
		Color highlightColor = getHighlightColor(addr, highlightLength);
		int startByteOffset = (int) addr.subtract(minAddr);
		int endByteOffset = startByteOffset + highlightLength - 1;
		startByteOffset = Math.max(startByteOffset, 0);
		highlights[i] = getHighlight(text, startByteOffset, endByteOffset, highlightColor);
	}

	return highlights;
}
 
Example 16
Source File: Match.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @param baseLength the minimum number of items which make up a match.
 * There are different values for instruction and byte matches. This
 * value should either be NaiveMatchPlugin.MATCH_LENGTH_FOR_INSTRUCTIONS
 * or NaiveMatchPlugin.MATCH_LENGTH_FOR_BYTES which can be found by
 * calling getMatchLengthForInstructions() or getMatchLengthForBytes().
 * @return The Address at which a continuing byte or code unit would
 * be expected to be found in the other program.
 */
public Address expectedAddressForNextMatch(int baseLength)
{
	Object o  = thisMatch.get( length() - baseLength + 1 );
	if( o instanceof CodeUnit)
	{	
		CodeUnit cu = (CodeUnit)thisMatch.get( length() - baseLength +1 );
		return cu.getMinAddress();
	}
	return this.thisBeginning.add(totalLength() - baseLength + 1);		
}
 
Example 17
Source File: ProgramByteViewerComponentProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void setLocation(ProgramLocation location, boolean fireEvent) {
	currentLocation = location;
	if (location == null) {
		return;
	}

	if (!tool.isVisible(this)) {
		return;
	}

	clipboardProvider.setLocation(location);

	Address address = location.getByteAddress();
	if (!program.getMemory().contains(address)) {
		CodeUnit cu = program.getListing().getCodeUnitAfter(address);
		if (cu != null) {
			address = cu.getMinAddress();
		}
	}

	if (address == null) {
		return;
	}

	ByteBlockInfo byteBlockInfo = blockSet.getByteBlockInfo(address);
	if (byteBlockInfo == null) {
		return;
	}

	ByteBlock block = byteBlockInfo.getBlock();
	BigInteger blockOffset = byteBlockInfo.getOffset();

	int column = 0;
	if (location instanceof ByteViewerProgramLocation) {
		// the character offset only makes sense when coming from the byte viewer; other
		// location character offsets don't match the byte viewer's display
		column = location.getCharOffset();
	}

	panel.setCursorLocation(block, blockOffset, column);
	Address blockSetAddress = blockSet.getAddress(block, blockOffset);
	if (blockSetAddress == null) {
		return; // this can happen during an undo
	}

	currentLocation = getLocation(block, blockOffset, column);

	if (fireEvent && tool.isVisible(this)) {
		updateLocation(block, blockOffset, column, false);
		plugin.fireProgramLocationPluginEvent(this,
			blockSet.getPluginEvent(getName(), block, blockOffset, column));
	}
	else {
		contextChanged();
	}
}