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

The following examples show how to use ghidra.program.model.listing.CodeUnit#getMaxAddress() . 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: NextRangeAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEnabledForContext(NavigatableActionContext context) {
	Address currentAddress = context.getAddress();
	ProgramSelection selection = getSelection(context);
	if (selection == null || selection.isEmpty() || currentAddress == null) {
		return false;
	}

	CodeUnit cu = context.getProgram().getListing().getCodeUnitAt(currentAddress);
	if (cu != null) {
		currentAddress = cu.getMaxAddress();
	}

	AddressRange lastRange = selection.getLastRange();
	Address maxAddress =
		navOptions.isGotoTopAndBottomOfRangeEnabled() ? lastRange.getMaxAddress()
				: lastRange.getMinAddress();
	return currentAddress.compareTo(maxAddress) < 0;
}
 
Example 2
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 3
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 4
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 5
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 6
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 7
Source File: NextRangeAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Address getGoToAddress(ProgramLocationActionContext context) {
	ProgramSelection selection = getSelection(context);
	Address currentAddress = context.getAddress();
	Address maxAddress = currentAddress;

	CodeUnit cu = context.getCodeUnit();
	if (cu != null) {
		maxAddress = cu.getMaxAddress();
	}
	AddressRangeIterator it = selection.getAddressRanges(currentAddress, true);
	if (!it.hasNext()) {
		return currentAddress;   // no next address, just return current address - should never hit this
	}
	AddressRange range = it.next();
	if (range.contains(currentAddress)) {
		if (navOptions.isGotoTopAndBottomOfRangeEnabled()) {
			if (!currentAddress.equals(range.getMaxAddress()) &&
				!maxAddress.equals(range.getMaxAddress())) {
				return range.getMaxAddress();
			}
		}
		if (!it.hasNext()) {
			return currentAddress;
		}
		range = it.next();
	}

	return range.getMinAddress();

}
 
Example 8
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;
}