Java Code Examples for ghidra.program.model.lang.Register#getBitLength()

The following examples show how to use ghidra.program.model.lang.Register#getBitLength() . 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: RegisterValueStore.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
	 * Preserve register values and handle register name/size change.
	 * @param translator
	 * @param monitor
	 * @return true if translated successfully, false if register not mapped 
	 * value storage should be discarded.
	 * @throws CancelledException
	 */
	public boolean setLanguage(LanguageTranslator translator, TaskMonitor monitor)
			throws CancelledException {
		Register newReg = translator.getNewRegister(baseRegister);
		if (newReg == null) {
			return false;
		}
		flushWriteCache();
// TODO: What should we do if new register is not a base-register ? - The code below will not work!
		if (newReg.isProcessorContext() || !newReg.isBaseRegister() ||
			!newReg.getName().equals(baseRegister.getName()) ||
			newReg.getBitLength() != baseRegister.getBitLength()) {
			rangeMap.setLanguage(translator, baseRegister, monitor);
			baseRegister = newReg.getBaseRegister();
		}
		return true;
	}
 
Example 2
Source File: CreateThunkFunctionCmd.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Check if the setRegisters contains the varnode or any of its parents.
 * If a parent register has been set, then this varnode is set
 */
private static boolean containsRegister(Program program, HashSet<Varnode> setRegisters,
		Varnode regVarnode) {
	if (setRegisters.contains(regVarnode)) {
		return true;
	}
	// check the parent varnode
	Register register = program.getRegister(regVarnode);
	Register parentRegister = register.getParentRegister();
	if (parentRegister == null) {
		return false;
	}
	Varnode parentVarnode =
		new Varnode(parentRegister.getAddress(), parentRegister.getBitLength() / 8);
	return setRegisters.contains(parentVarnode);
}
 
Example 3
Source File: StorageAddressModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean isValidAddress(VarnodeInfo varnode, int row) {
	Address address = varnode.getAddress();
	if (address == null) {
		statusText = "Row " + row + ": Invalid Varnode Address";
		return false;
	}
	try {
		new AddressRangeImpl(address, varnode.getSize());
	}
	catch (AddressOverflowException e) {
		statusText = "Row " + row + ": Varnode wraps within " +
			address.getAddressSpace().getName() + " space.";
		return false;
	}
	if (address.isStackAddress()) {
		long stackOffset = address.getOffset();
		if (stackOffset < 0 && -stackOffset < varnode.getSize()) {
			// do not allow stack varnode to span the 0-offset 
			// i.e., maintain separation of locals and params
			statusText = "Row " + row + ": Stack varnode spans 0 offset";
			return false;
		}
		return true;
	}
	Register register = program.getRegister(address, varnode.getSize());
	if (register == null) {
		Register register2 = program.getRegister(address);
		if (register2 != null) {
			statusText = "Row " + row + ": Register (size=" + (register2.getBitLength() / 8) +
				") too small for specified size(" + varnode.getSize() + ")";
		}
		else if (address.isRegisterAddress()) {
			statusText = "Row " + row + ": Invalid Register";
			return false;
		}
		return address.isMemoryAddress();
	}
	return true; // is register
}
 
Example 4
Source File: StorageAddressModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void setVarnode(VarnodeInfo info, Register reg) {
	Address addr = reg.getAddress();
	int size;
	int currentSize = getCurrentSize();
	int regSize = reg.getBitLength() / 8;
	if (unconstrained) {
		setVarnode(info, addr, regSize);
		return;
	}
	Integer curVarnodeSize = info.getSize();
	if (reg.hasChildren() || curVarnodeSize == null || regSize < curVarnodeSize ||
		currentSize < requiredSize) {
		size = reg.getBitLength() / 8;
		if (curVarnodeSize != null) {
			currentSize -= curVarnodeSize;
		}
		if (currentSize < requiredSize) {
			size = Math.min(size, requiredSize - currentSize);
		}
	}
	else {
		size = curVarnodeSize;
	}
	if (reg.isBigEndian()) {
		// adjust address for big endian register
		int s = Math.min(reg.getMinimumByteSize(), size);
		addr = addr.add(reg.getMinimumByteSize() - s);
	}
	setVarnode(info, addr, size);

}
 
Example 5
Source File: EditRegisterValueDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private JComponent buildWorkPanel(Register register, Address start, Address end,
		BigInteger value, AddressFactory factory) {

	JTextField registerField =
		new JTextField(register.getName() + " (" + register.getBitLength() + ")");
	registerField.setEditable(false);

	startAddrField = new AddressInput();
	endAddrField = new AddressInput();
	ChangeListener changeListener = new ChangeListener() {
		@Override
		public void stateChanged(ChangeEvent e) {
			updateOk();
		}
	};
	startAddrField.setAddressFactory(factory);
	endAddrField.setAddressFactory(factory);
	startAddrField.addChangeListener(changeListener);
	endAddrField.addChangeListener(changeListener);

	registerValueField = new FixedBitSizeValueField(register.getBitLength(), true, false);
	startAddrField.setAddress(start);
	endAddrField.setAddress(end);
	registerValueField.setValue(value);

	JPanel panel = new JPanel(new PairLayout(5, 1));

	panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	panel.add(new GLabel("Register:"));
	panel.add(registerField);
	panel.add(new GLabel("Start Address:"));
	panel.add(startAddrField);
	panel.add(new GLabel("End Address:"));
	panel.add(endAddrField);
	panel.add(new GLabel("Value:"));
	panel.add(registerValueField);

	return panel;
}
 
Example 6
Source File: ListingDiff.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Determine if the first and second instructions objects for a particular operand differ.
 * The opObjects are checked using the currently specified ignore flags for determining 
 * instruction operand differences.
 * @param opObjects1 the operand objects that compose an operand for the first instruction
 * @param opObjects2 the operand objects that compose an operand for the second instruction
 * @return true if the opObjects differ based on the current diff ignore flags.
 */
private boolean opObjectsDiffer(Object[] opObjects1, Object[] opObjects2) {
	if (opObjects1.length != opObjects2.length) {
		return true;
	}
	for (int i = 0; i < opObjects1.length; i++) {
		Object obj1 = opObjects1[i];
		Object obj2 = opObjects2[i];
		if (obj1.equals(obj2)) {
			continue;
		}
		if (obj1 instanceof Scalar && obj2 instanceof Scalar) {
			if (ignoreConstants) {
				continue;
			}
		}
		else if (obj1 instanceof Address && obj2 instanceof Address) {
			if (ignoreConstants) {
				continue;
			}
		}
		else if (obj1 instanceof Register && obj2 instanceof Register) {
			Register reg1 = (Register) obj1;
			Register reg2 = (Register) obj2;
			int len1 = reg1.getBitLength();
			int len2 = reg2.getBitLength();
			if (len1 != len2) {
				return true;
			}
			if (!ignoreRegisters && !reg1.equals(reg2)) {
				return true;
			}
			continue;
		}
		return true;
	}
	return false;
}
 
Example 7
Source File: SetRegisterValueDialog.java    From ghidra with Apache License 2.0 4 votes vote down vote up
RegisterWrapper(Register register) {
	this.register = register;
	displayName = register.getName() + " (" + register.getBitLength() + getAliases() + ")";
}