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

The following examples show how to use ghidra.program.model.lang.Register#isBaseRegister() . 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: InMemoryRangeMapAdapter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.program.util.RangeMapAdapter#setLanguage(ghidra.program.util.LanguageTranslator, ghidra.program.model.lang.Register, ghidra.util.task.TaskMonitor)
 */
@Override
public void setLanguage(LanguageTranslator translator, Register mapReg, TaskMonitor monitor)
		throws CancelledException {

	Register newReg = translator.getNewRegister(mapReg);
	if (newReg == null) {
		// register not translated - clear map
		clearAll();
		return;
	}

	if (newReg.isBaseRegister() && !translator.isValueTranslationRequired(mapReg)) {
		return;
	}

	AddressRangeObjectMap<byte[]> newRangeMap = new AddressRangeObjectMap<byte[]>();
	AddressRangeIterator addressRangeIterator = rangeMap.getAddressRangeIterator();
	while (addressRangeIterator.hasNext()) {
		monitor.checkCanceled();
		AddressRange range = addressRangeIterator.next();
		byte[] oldBytes = rangeMap.getObject(range.getMinAddress());
		RegisterValue regValue = new RegisterValue(mapReg, oldBytes);
		regValue = translator.getNewRegisterValue(regValue);
		if (regValue == null || !regValue.hasAnyValue()) {
			continue; // remove value range
		}
		byte[] newBytes = regValue.toBytes();
		newRangeMap.setObject(range.getMinAddress(), range.getMaxAddress(), newBytes);
	}
	rangeMap = newRangeMap;
}
 
Example 3
Source File: RegisterValueStore.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Clears the address range of any set bits using the mask from the given register value.
 * existing values in the range that have values that are not part of the input mask are 
 * not changed. If register is null, just clear all the values in range
 * @param start the start of the range to clear the register value bits. 
 * @param end the end of the range(inclusive) to clear the register value bits.
 * @param register the register whos mask to use.  If null, clear all values in the given range.
 */
public void clearValue(Address start, Address end, Register register) {

	flushWriteCache();

	// if the mask is all on, then just clear any values that are stored in this range
	if (register == null || register.isBaseRegister()) {
		rangeMap.clearRange(start, end);
		return;
	}

	// Otherwise, mask off bits according to the mask passed in.
	List<AddressRange> list = new ArrayList<AddressRange>();
	AddressRangeIterator rangeIt = rangeMap.getAddressRangeIterator(start, end);
	while (rangeIt.hasNext()) {
		list.add(rangeIt.next());
	}
	Iterator<AddressRange> it = list.iterator();
	while (it.hasNext()) {
		AddressRange indexRange = it.next();
		Address rangeStart = indexRange.getMinAddress();
		Address rangeEnd = indexRange.getMaxAddress();

		byte[] mask = register.getBaseMask();

		RegisterValue currentBaseValue =
			new RegisterValue(register.getBaseRegister(), rangeMap.getValue(rangeStart));
		RegisterValue newBaseValue = currentBaseValue.clearBitValues(mask);

		if (!newBaseValue.hasAnyValue()) {
			rangeMap.clearRange(rangeStart, rangeEnd);
		}
		else {
			rangeMap.set(rangeStart, rangeEnd, newBaseValue.toBytes());
		}
		start = rangeEnd.next();
	}
}
 
Example 4
Source File: DatabaseRangeMapAdapter.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * @see ghidra.program.util.RangeMapAdapter#setLanguage(ghidra.program.util.LanguageTranslator, ghidra.program.model.lang.Register, ghidra.util.task.TaskMonitor)
 */
@Override
public void setLanguage(LanguageTranslator translator, Register mapReg, TaskMonitor monitor)
		throws CancelledException {

	Register newReg = translator.getNewRegister(mapReg);
	if (newReg == null) {
		// register not translated - clear map
		rangeMap.dispose();
		rangeMap = null;
		return;
	}
	Register newBaseReg = newReg.getBaseRegister();

	AddressRangeMapDB tempMap = null;
	if (!newReg.isBaseRegister() || translator.isValueTranslationRequired(mapReg)) {

		// Create temporary map
		String tempName = "TEMP_MAP";
		int retry = 0;
		while (AddressRangeMapDB.exists(dbh, tempName)) {
			tempName = "TEMP_MAP" + (++retry);
		}
		tempMap =
			new AddressRangeMapDB(dbh, addressMap, new Lock("Test"), tempName, errorHandler,
				BinaryField.class, false);

		// Translate range map data into tempMap
		monitor.initialize(rangeMap.getRecordCount());
		monitor.setMessage("Converting " + mapReg.getName() + " values...");
		int cnt = 0;
		AddressRangeIterator rangeIter = rangeMap.getAddressRanges();
		while (rangeIter.hasNext()) {
			if (monitor.isCancelled()) {
				tempMap.dispose();
				throw new CancelledException();
			}
			AddressRange range = rangeIter.next();
			BinaryField value = (BinaryField) rangeMap.getValue(range.getMinAddress());
			byte[] oldBytes = value.getBinaryData();
			RegisterValue regValue = new RegisterValue(mapReg, oldBytes);
			regValue = translator.getNewRegisterValue(regValue);
			if (regValue != null && regValue.hasAnyValue()) {
				byte[] newBytes = regValue.toBytes();
				tempMap.paintRange(range.getMinAddress(), range.getMaxAddress(),
					new BinaryField(newBytes));
			}
			monitor.setProgress(++cnt);
		}
	}

	String newMapName = NAME_PREFIX + newBaseReg.getName();
	if (tempMap == null) {
		if (mapName.equals(newMapName)) {
			// Nothing to change
			return;
		}
	}
	else {
		rangeMap.dispose();
		rangeMap = tempMap;
	}
	if (rangeMap != null) {
		try {
			rangeMap.setName(newMapName);
		}
		catch (DuplicateNameException e) {
			throw new AssertException("Unexpected DuplicateNameException");
		}
	}
	mapName = newMapName;
}