Java Code Examples for ghidra.program.model.address.Address#isRegisterAddress()

The following examples show how to use ghidra.program.model.address.Address#isRegisterAddress() . 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: VariableImpl.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private VariableStorage computeStorage(Address storageAddr) throws InvalidInputException {
	if (storageAddr == null) {
		return VariableStorage.UNASSIGNED_STORAGE;
	}
	if (!storageAddr.isMemoryAddress() && !storageAddr.isRegisterAddress() &&
		!storageAddr.isStackAddress() && !storageAddr.isHashAddress()) {
		throw new InvalidInputException("Invalid storage address specified: space=" +
			storageAddr.getAddressSpace().getName());
	}
	int dtLength = dataType.getLength();
	if (!storageAddr.isStackAddress()) {
		return new VariableStorage(program, storageAddr, dtLength);
	}

	long stackOffset = storageAddr.getOffset();
	if (stackOffset < 0 && -stackOffset < dtLength) {
		// do not allow stack element to span the 0-offset 
		// i.e., maintain separation of locals and params
		throw new InvalidInputException(
			"Data type does not fit within stack frame constraints (stack offset=" +
				stackOffset + ", size=" + dtLength);
	}
	return new VariableStorage(program, new Varnode(storageAddr, dtLength));
}
 
Example 2
Source File: VarnodeInfo.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public void setVarnode(Address address, Integer size) {
	this.address = address;
	this.size = size;
	register = getRegister(program, address, size);
	if (address == null) {
		return;
	}
	if (address.isRegisterAddress() || register != null) {
		type = VarnodeType.Register;
	}
	else if (address.isStackAddress()) {
		type = VarnodeType.Stack;
	}
	else if (address.isMemoryAddress()) {
		type = VarnodeType.Memory;
	}
	else {
		throw new IllegalArgumentException("Illegal varnode address type");
	}
}
 
Example 3
Source File: VarnodeInfo.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public static Register getRegister(Program program, Address address, Integer size) {
	if (address == null) {
		return null;
	}
	if (!address.isRegisterAddress() && !address.getAddressSpace().hasMappedRegisters()) {
		return null;
	}
	if (size == null) {
		return program.getRegister(address);
	}
	Register register = program.getRegister(address, size);
	if (register == null) {
		register = program.getRegister(address);
	}
	return register;

}
 
Example 4
Source File: EditReferencesModel.java    From ghidra with Apache License 2.0 6 votes vote down vote up
static RefType[] getAllowedRefTypes(Program program, Reference ref) {
	Address toAddr = ref.getToAddress();
	if (toAddr.isStackAddress()) {
		return RefTypeFactory.getStackRefTypes();
	}
	if (toAddr.isRegisterAddress()) {
		return RefTypeFactory.getDataRefTypes();
	}
	if (toAddr.isMemoryAddress()) {
		if (program.getAddressFactory().getDefaultAddressSpace() == toAddr.getAddressSpace() ||
			isComputedFlow(program, ref)) {
			return RefTypeFactory.getMemoryRefTypes();
		}
		return RefTypeFactory.getDataRefTypes();
	}
	if (toAddr.isExternalAddress()) {
		return RefTypeFactory.getExternalRefTypes();
	}
	throw new IllegalArgumentException("Unsupported reference");
}
 
Example 5
Source File: RegisterManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the largest register located at the specified address
 * 
 * @param addr
 * @return largest register
 */
public Register getRegister(Address addr) {
	if (!addr.isRegisterAddress() && !addr.getAddressSpace().hasMappedRegisters()) {
		return null;
	}
	return sizeMap.get(new RegisterSizeKey(addr, 0));
}
 
Example 6
Source File: RegisterManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all registers located at the specified address
 * 
 * @param addr
 * @return largest register
 */
public Register[] getRegisters(Address addr) {
	if (addr.isRegisterAddress() || addr.getAddressSpace().hasMappedRegisters()) {
		List<Register> list = registerAddressMap.get(getGlobalAddress(addr));
		if (list != null) {
			Register[] regs = new Register[list.size()];
			list.toArray(regs);
			return regs;
		}
	}
	return new Register[0];
}
 
Example 7
Source File: ASTGraphTask.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String translateVarnode(Varnode node, boolean useVarName) {
	if (node == null) {
		return "null";
	}
	Program p = hfunction.getFunction().getProgram();
	Address addr = node.getAddress();
	if (node.isConstant()) {
		return "#" + NumericUtilities.toHexString(addr.getOffset(), node.getSize());
	}
	else if (node.isUnique()) {
		return "u_" + Long.toHexString(addr.getOffset());
	}
	else if (addr.isRegisterAddress()) {
		Register r = p.getRegister(addr, node.getSize());
		if (r == null) {
			r = p.getRegister(addr);
		}
		if (r != null) {
			return r.getName();
		}
	}
	else if (addr.isStackAddress()) {
		if (useVarName) {
			HighVariable var = node.getHigh();
			if (var != null) {
				return var.getName();
			}
		}
		return "Stack[" + NumericUtilities.toSignedHexString(addr.getOffset()) + "]";
	}
	else if (addr.isMemoryAddress()) {
		return addr.toString(true);
	}
	return node.toString();
}
 
Example 8
Source File: SymbolRenderer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getAddressString(Address address) {
	if (address.isStackAddress()) {
		return getStackAddressString(address);
	}
	else if (address.isRegisterAddress()) {
		return getRegisterAddressString(address);
	}
	else if (address.isExternalAddress() || address == Address.NO_ADDRESS) {
		return "";
	}
	return address.toString();
}
 
Example 9
Source File: EditReferenceDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
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 10
Source File: GoToHelper.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ProgramLocation getLocation(Program program, Address currentAddress,
		Address gotoAddress) {
	ProgramLocation loc = getProgramLocationForAddress(gotoAddress, program);
	if (loc != null) {
		return loc;
	}
	if (gotoAddress.isStackAddress() || gotoAddress.isRegisterAddress()) {
		// Convert stack/register address into variable address
		Function func = program.getFunctionManager().getFunctionContaining(currentAddress);
		if (func != null) {
			for (Variable v : func.getAllVariables()) {
				VariableStorage storage = v.getVariableStorage();
				if (storage.contains(gotoAddress)) {
					return new VariableNameFieldLocation(program, v, 0);
				}
			}
		}
	}

	SymbolTable symTable = program.getSymbolTable();
	ReferenceManager refMgr = program.getReferenceManager();
	Reference ref = refMgr.getReference(currentAddress, gotoAddress, 0);
	Symbol symbol = symTable.getSymbol(ref);

	if (symbol != null) {
		return symbol.getProgramLocation();
	}
	return null;
}
 
Example 11
Source File: EditReferenceDialog.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void setAddOpIndex(int opIndex, int subIndex) {

		CodeUnit cu = instrPanel.getCurrentCodeUnit();
		Program p = cu.getProgram();
		boolean inFunction =
			(p.getFunctionManager().getFunctionContaining(cu.getMinAddress()) != null);
		Reference[] refs = p.getReferenceManager().getReferencesFrom(cu.getMinAddress(), opIndex);
		Address existingRefAddr = refs.length != 0 ? refs[0].getToAddress() : null;

		if (!memRefPanel.initialize(cu, opIndex, subIndex)) {
			throw new AssertException("Memory reference must always be permitted");
		}

		memRefChoice.setEnabled(true);
		extRefChoice.setEnabled(extRefPanel.initialize(cu, opIndex, subIndex));
		stackRefChoice.setEnabled(inFunction && stackRefPanel.initialize(cu, opIndex, subIndex));
		regRefChoice.setEnabled(inFunction && regRefPanel.initialize(cu, opIndex, subIndex));

		memRefChoice.setSelected(true);
		if (existingRefAddr != null) {
			if (existingRefAddr.isStackAddress()) {
				if (stackRefChoice.isEnabled()) {
					stackRefChoice.setSelected(true);
				}
			}
			else if (existingRefAddr.isRegisterAddress()) {
				if (regRefChoice.isEnabled()) {
					regRefChoice.setSelected(true);
				}
			}
			else if (existingRefAddr.isExternalAddress()) {
				if (extRefChoice.isEnabled()) {
					extRefChoice.setSelected(true);
				}
			}
		}
		else {
			if (stackRefChoice.isEnabled() && stackRefPanel.isValidStackRef()) {
				stackRefChoice.setSelected(true);
			}
			else if (regRefChoice.isEnabled()) {
				regRefChoice.setSelected(true);
			}
		}
	}
 
Example 12
Source File: AddressBasedLocation.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static String buildStringRepresentation(Program program, Address address,
		Reference reference, ShowBlockName showBlockName) {
	if (address == null) {
		return "<NULL>";
	}
	if (address.getAddressSpace().getType() == AddressSpace.TYPE_NONE) {
		return ""; // NO_ADDRESS or EXT_FROM_ADDRESS not rendered
	}
	if (address.isExternalAddress()) {
		return getExternalAddressRepresentation(program, address);
	}
	if (address.isVariableAddress()) {
		return getVariableAddressRepresentation();
	}
	if (address.isStackAddress()) {
		return getStackAddressRepresentation(address);
	}
	if (address.isConstantAddress()) {
		return getConstantAddressRepresentation(address);
	}
	if (address.isRegisterAddress()) {
		return getRegisterAddressRepresentation(program, address);
	}

	// Handle all other spaces (e.g., memory, other, overlays, hash, etc.)
	String addrStr;
	if (reference != null && reference.isOffsetReference()) {
		OffsetReference offsetRef = (OffsetReference) reference;
		long offset = offsetRef.getOffset();
		boolean neg = (offset < 0);
		Address baseAddr = offsetRef.getBaseAddress();
		addrStr = baseAddr.toString() + (neg ? "-" : "+") + "0x" +
			Long.toHexString(neg ? -offset : offset);
	}
	else if (reference != null && reference.isShiftedReference()) {
		// TODO: unsure of rendering which has never really been addressed
		// TODO: shifted references have never addressed concerns related to
		// addressable unit size
		ShiftedReference shiftedRef = (ShiftedReference) reference;
		StringBuilder buf = new StringBuilder();
		buf.append(address.toString());
		buf.append("(0x");
		buf.append(Long.toHexString(shiftedRef.getValue()));
		buf.append("<<");
		buf.append(Long.toString(shiftedRef.getShift()));
		buf.append(")");
		addrStr = buf.toString();
	}
	else {
		addrStr = address.toString();
	}

	if (showBlockName != ShowBlockName.NEVER) {
		Memory mem = program.getMemory();
		MemoryBlock toBlock = mem.getBlock(address);
		if (toBlock != null && showBlockName == ShowBlockName.NON_LOCAL && reference != null &&
			toBlock.equals(mem.getBlock(reference.getFromAddress()))) {
			toBlock = null;
		}
		if (toBlock != null) {
			addrStr = toBlock.getName() + "::" + addrStr;
		}
	}

	return addrStr;
}
 
Example 13
Source File: RegisterManager.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the smallest register at the specified address whose size is 
 * greater than or equal the specified size.
 * @param addr register address
 * @param size the size of the register (in bytes).  A value of 0 will return the 
 * largest register at the specified addr
 * @return register
 */
public Register getRegister(Address addr, int size) {
	if (!addr.isRegisterAddress() && !addr.getAddressSpace().hasMappedRegisters()) {
		return null;
	}
	return sizeMap.get(new RegisterSizeKey(addr, size));
}