Java Code Examples for ghidra.program.model.symbol.RefType#DATA

The following examples show how to use ghidra.program.model.symbol.RefType#DATA . 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: LabelFieldFactoryTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createDataReference(String from, String to) {

		int transaction = program.startTransaction("Test - Add Reference");
		try {
			AddMemRefCmd cmd =
				new AddMemRefCmd(addr(from), addr(to), RefType.DATA, SourceType.USER_DEFINED, 0);
			cmd.applyTo(program);
			program.flushEvents();
			waitForPostedSwingRunnables();
		}
		finally {
			program.endTransaction(transaction, true);
		}
	}
 
Example 2
Source File: SleighInstructionPrototype.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void cacheDefaultOperandRefTypes(InstructionContext context,
		UniqueAddressFactory uniqueFactory) {

	// Resolve handles for each operand
	SleighParserContext protoContext;
	try {
		protoContext = (SleighParserContext) context.getParserContext();
	}
	catch (MemoryAccessException e) {
		throw new RuntimeException(e);
	}
	PcodeOp[] pcode = getPcode(context, null, uniqueFactory);
	if (pcode == null || pcode.length == 0) {
		return;
	}
	FixedHandle[] opHandles = new FixedHandle[opresolve.length];
	for (int index = 0; index < opresolve.length; index++) {
		ConstructState opState = mnemonicState.getSubState(opresolve[index]);
		opHandles[index] = protoContext.getFixedHandle(opState);
	}
	for (int index = 0; index < opHandles.length; index++) {
		if (opHandles[index] == null || opHandles[index].isInvalid() ||
			opRefTypes[index] != null) {
			continue;
		}
		RefType refType = RefType.DATA;
		if (opHandles[index].isDynamic()) {
			refType = getDynamicOperandRefType(opHandles[index], pcode);
		}
		else {
			Varnode var = opHandles[index].getStaticVarnode();
			if (var != null) {
				refType = getStaticOperandRefType(var, pcode);
			}
		}
		opRefTypes[index] = refType;
		for (int n = index + 1; n < opHandles.length; n++) {
			if (opHandles[index].equals(opHandles[n])) {
				opRefTypes[n] = refType;
			}
		}
	}
}
 
Example 3
Source File: SleighInstructionPrototype.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private RefType getStaticOperandRefType(Varnode var, PcodeOp[] pcode) {
	if (var.isConstant()) {
		return RefType.DATA;
	}
	boolean isRead = false;
	boolean isWrite = false;
	for (PcodeOp element : pcode) {
		Varnode[] inputs = element.getInputs();
		switch (element.getOpcode()) {

			case PcodeOp.BRANCHIND:
			case PcodeOp.CALLIND:
			case PcodeOp.RETURN:
				if (inputs[0].equals(var)) {
					return RefType.INDIRECTION;
				}
				break;

			case PcodeOp.BRANCH:
				if (inputs[0].equals(var)) {
					return RefType.UNCONDITIONAL_JUMP;
				}
				break;

			case PcodeOp.CBRANCH:
				if (inputs[0].equals(var)) {
					return RefType.CONDITIONAL_JUMP;
				}
				break;

			case PcodeOp.CALL:
				if (inputs[0].equals(var)) {
					return RefType.UNCONDITIONAL_CALL;
				}
				break;

		}
		if (!var.isUnique()) {
			if (var.equals(element.getOutput())) {
				isWrite = true;
			}
			for (Varnode input : element.getInputs()) {
				if (var.equals(input)) {
					isRead = true;
				}
			}
		}
	}
	if (isRead && isWrite) {
		return RefType.READ_WRITE;
	}
	if (isRead) {
		return RefType.READ;
	}
	if (isWrite) {
		return RefType.WRITE;
	}
	return RefType.DATA;
}
 
Example 4
Source File: SleighInstructionPrototype.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private RefType getDynamicOperandRefType(FixedHandle hand, PcodeOp[] pcode) {
	Varnode offset = hand.getDynamicOffset();
	Varnode staticAddr = hand.getStaticVarnode();
	Varnode temp = hand.getDynamicTemp();
	boolean isRead = false;
	boolean isWrite = false;
	for (PcodeOp element : pcode) {
		Varnode[] inputs = element.getInputs();
		switch (element.getOpcode()) {

			case PcodeOp.LOAD:
				if (temp.equals(element.getOutput())) {
					isRead = true;
				}
				break;

			case PcodeOp.STORE:
				if (offset.equals(inputs[1]) && temp.equals(inputs[2])) {
					isWrite = true;
				}
				break;

			case PcodeOp.BRANCHIND:
			case PcodeOp.CALLIND:
			case PcodeOp.RETURN:
				if (inputs[0].equals(temp) || inputs[0].equals(staticAddr)) {
					return RefType.INDIRECTION;
				}
				break;

			case PcodeOp.BRANCH:
				if (inputs[0].equals(staticAddr)) {
					return RefType.UNCONDITIONAL_JUMP;
				}
				break;

			case PcodeOp.CBRANCH:
				if (inputs[0].equals(staticAddr)) {
					return RefType.CONDITIONAL_JUMP;
				}
				break;

			case PcodeOp.CALL:
				if (inputs[0].equals(staticAddr)) {
					return RefType.UNCONDITIONAL_CALL;
				}
				break;

		}
	}
	if (isRead && isWrite) {
		return RefType.READ_WRITE;
	}
	if (isRead) {
		return RefType.READ;
	}
	if (isWrite) {
		return RefType.WRITE;
	}
	return RefType.DATA;
}