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

The following examples show how to use ghidra.program.model.lang.Register#isProcessorContext() . 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: ClearCmd.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void removeRegisters(ProgramContext pc, AddressRange range, TaskMonitor monitor)
		throws CancelledException {
	for (Register reg : pc.getRegistersWithValues()) {
		monitor.checkCanceled();

		if (reg.isProcessorContext()) {
			continue; // skip context register
		}
		try {
			pc.remove(range.getMinAddress(), range.getMaxAddress(), reg);
		}
		catch (ContextChangeException e) {
			Msg.error(this, e.getMessage() + " in range " + range, e); // unexpected
		}
	}
}
 
Example 3
Source File: CreateThunkFunctionCmd.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static void addSetRegisters(Program program, Address entry,
		HashSet<Varnode> setRegisters) {
	Register[] regWithVals = program.getProgramContext().getRegistersWithValues();
	for (Register register : regWithVals) {
		if (register.isProcessorContext()) {
			continue;
		}

		RegisterValue regVal = program.getProgramContext().getRegisterValue(register, entry);
		if (regVal == null) {
			continue;
		}
		if (!regVal.hasValue()) {
			continue;
		}
		Register reg = regVal.getRegister();
		setRegisters.add(new Varnode(reg.getAddress(), reg.getMinimumByteSize()));
	}
}
 
Example 4
Source File: AbstractStoredProgramContext.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected final RegisterValueStore createRegisterValueStore(Register baseRegister,
		RangeMapAdapter adapter) {
	RegisterValueStore store =
		new RegisterValueStore(baseRegister, adapter, baseRegister.isProcessorContext());
	registerValueMap.put(baseRegister, store);
	return store;
}
 
Example 5
Source File: AbstractProgramContext.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public final Register[] getProcessorStateRegisters() {
	List<Register> list = new ArrayList<Register>();
	for (Register register : registers) {
		if (register.isProcessorContext()) {
			list.add(register);
		}
	}
	return list.toArray(new Register[list.size()]);
}
 
Example 6
Source File: RegisterContextBuilder.java    From ghidra with Apache License 2.0 5 votes vote down vote up
RegisterContextBuilder(Program program, Register reg, long mask) {
	if (reg.isProcessorContext()) {
		throw new IllegalArgumentException("reg may not be processor context register");
	}
	this.program = program;
	this.reg = reg;
	this.isBitRegister = false;
	this.mask = mask != 0 ? BigInteger.valueOf(mask) : null;
}
 
Example 7
Source File: ProgramDiff.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/** Gets the addresses where the Program Context (register bits) differ
 * between two programs.
 *
 * @param addressSet the addresses to check for differences.
 * The addresses in this address set should be derived from program1.
 * @param monitor the task monitor for indicating the progress of
 * determining differences. This monitor reports the progress to the user.
 *
 * @return the addresses of code units where the program context (register
 * bits values) differs.
 * The addresses in this address set are derived from program1.
 *
 * @throws ProgramConflictException if the two programs are not comparable since registers differ.
 * @throws CancelledException if the user cancelled the Diff.
 */
private AddressSet getProgramContextDifferences(AddressSetView addressSet, TaskMonitor monitor)
		throws ProgramConflictException, CancelledException {
	AddressSet differences = new AddressSet();
	ProgramContext pc1 = program1.getProgramContext();
	ProgramContext pc2 = program2.getProgramContext();
	String[] names1 = pc1.getRegisterNames();
	String[] names2 = pc2.getRegisterNames();
	Arrays.sort(names1);
	Arrays.sort(names2);
	if (!Arrays.equals(names1, names2)) {
		throw new ProgramConflictException(
			"Program Context Registers don't match between the programs.");
	}

	// Check each address range from the address set for differences.
	AddressSet inCommon = pgmMemComp.getAddressesInCommon();
	addressSet = (addressSet != null) ? inCommon.intersect(addressSet) : inCommon;

	for (String element : names1) {
		monitor.checkCanceled();
		Register rb1 = pc1.getRegister(element);
		Register rb2 = pc2.getRegister(element);
		if (rb1.isProcessorContext() || rb2.isProcessorContext()) {
			continue; // context handled with CodeUnit differencing
		}
		Register p1 = rb1.getParentRegister();
		Register p2 = rb2.getParentRegister();
		if (p1 != null && p2 != null && p1.getName().equals(p2.getName())) {
			continue;
		}

		getProgramContextDifferences(pc1, rb1, pc2, rb2, addressSet, differences, monitor);
	}
	return differences;
}
 
Example 8
Source File: ResultsState.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Set an assumed register value immediately following construction and prior to flow.
 * @param register (context register not permitted)
 * @param value
 */
public void assume(Register register, long value) {
	if (register.isProcessorContext()) {
		throw new IllegalArgumentException("Context register not permitted");
	}
	entryState.store(new Varnode(register.getAddress(), register.getMinimumByteSize()),
		new Varnode(addrFactory.getConstantAddress(value), register.getMinimumByteSize()));
}
 
Example 9
Source File: ResultsState.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
	 * Check for register corresponding to the specified varnode and add to inputRegs.
	 * @param addressVarnode register or address varnode which has not been written
	 */
	private void checkInput(Varnode addressVarnode) {
// TODO: Should we limit to registers defined in input parameter block
		Address addr = addressVarnode.getAddress();
		Register reg = program.getRegister(addr, addressVarnode.getSize());
		if (reg == null || reg.isProcessorContext() || reg.isProgramCounter() ||
			containsRegister(reg, registersModified)) {
			return;
		}
		addRegister(reg, inputRegs);
	}
 
Example 10
Source File: ResultsState.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Check value assignments for items of interest:
 * <ul>
 * <li>Frame pointer identification</li>
 * <li>Tracking of modified registers</li>
 * </ul>
 * @param output output varnode
 * @param value assigned value
 * @param op pcode operation
 * @throws CancelledException 
 */
private void checkAssignment(Varnode output, Varnode value, PcodeOp op, TaskMonitor monitor)
		throws CancelledException {

	Varnode[] inputs = op.getInputs();
	int opcode = op.getOpcode();
	if (output != null && !output.isUnique() && opcode != PcodeOp.LOAD &&
		opcode != PcodeOp.STORE && opcode != PcodeOp.COPY) {
		checkStackOffsetAssignment(op, value, monitor);
	}

	Register reg = program.getRegister(output.getAddress(), output.getSize());
	if (reg == null || reg.isProgramCounter() || reg.isProcessorContext() ||
		framePointerCandidatesDismissed.contains(reg.getBaseRegister())) {
		Msg.debug(this, "SET: " + output + " = " + value);
		return;
	}
	if (addRegister(reg, registersModified)) {
		if (DEBUG) {
			Msg.debug(this, "MODIFIED: " + reg + " = " + value);
		}
	}
	else {
		Msg.debug(this, "SET: " + output + " = " + value);
	}
	if (framePointerCandidates.containsKey(reg)) {
		if (value.getAddress().equals(reg.getAddress())) {
			return; // Ignore register restore
		}
		framePointerCandidatesDismissed.add(reg);
		framePointerCandidates.remove(reg);
		return;
	}

	if (opcode != PcodeOp.LOAD) {
		if (value.equals(getStackPointerVarnode()) ||
			(inputs.length == 1 && inputs[0].equals(getStackPointerVarnode())) ||
			(inputs.length == 2 && (inputs[0].equals(getStackPointerVarnode()) || inputs[1].equals(getStackPointerVarnode())))) {
			framePointerCandidates.put(reg, new FramePointerCandidate(reg, op.getSeqnum(),
				value));
			return;
		}
	}
	framePointerCandidatesDismissed.add(reg);
}