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

The following examples show how to use ghidra.program.model.lang.Register#equals() . 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: MySwitchAnalyzer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Register findSingleRegister(Varnode value) {
	if (value instanceof VarnodeOperation) {
		Register reg = null;
		for (Varnode input : ((VarnodeOperation)value).getInputValues()) {
			Register inputReg = findSingleRegister(input);
			if (inputReg != null) {
				if (reg != null && !reg.equals(inputReg)) {
					throw new MultipleRegInputsException();
				}
				reg = inputReg;
			}
		}
		return reg;
	}
	if (value.isAddress() || value.isRegister()) {
		return program.getRegister(value.getAddress(), value.getSize());
	}
	return null;
}
 
Example 2
Source File: MipsPreAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Register checkForMove(Instruction start_inst, Instruction curr_inst) {
	// if this is a move, may be copying into another register.
	//  why? wasted code...
	// This is a hack and should be done with real data flow...
	if (curr_inst.getMnemonicString().equals("move")) {
		Register reg = start_inst.getRegister(0);
		Register alt = curr_inst.getRegister(1);
		if (reg != null && reg.equals(alt)) {
			return curr_inst.getRegister(0);
		}
	}
	return alternateReg;
}
 
Example 3
Source File: MipsPreAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if two instructions are a pair.
 * A pair is found if
 *  1) mnemonics are correct
 * 	2) offset difference is correct
 * 	3) destination and base registers match
 *
 * @param offset1
 * @param offset2
 * @param base1
 * @param base2
 * @param destReg1
 * @param destReg2
 * @param str			start inst mnemonic
 * @param curr_inst
 * @return Instruction that is the pair of this one
 */
private Instruction checkPair(Scalar offset1, Scalar offset2, Register base1, Register base2,
		Register destReg1, Register destReg2, Instruction start_inst, Instruction curr_inst) {
	int start_index1 = 0;
	int start_index2 = 0;
	String str = start_inst.getMnemonicString();
	String str2 = curr_inst.getMnemonicString();

	// Check for delay slot
	if (str.charAt(0) == '_') {
		start_index1 = 1;
	}
	if (str2.charAt(0) == '_') {
		start_index2 = 1;
	}

	// Check mnemonics are matching
	if (!str.substring(start_index1, start_index1 + 2).equals(
		str2.substring(start_index2, start_index2 + 2))) {
		return null;
	}

	// Check offset
	long diff = Math.abs(offset2.getSignedValue() - offset1.getSignedValue());
	if ((str.endsWith("wl") || str.endsWith("wr")) && diff != 3) {
		return null;
	}
	else if ((str.endsWith("dl") || str.endsWith("dr")) && diff != 7) {
		return null;
	}

	// Check base and destination registers
	if (base1.equals(base2) && (destReg1.equals(destReg2) | destReg2.equals(alternateReg))) {
		// Match found
		return curr_inst;
	}
	return null;
}
 
Example 4
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 5
Source File: GCAnalyzer.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
protected boolean trySetGQRegister(String gqrName, Program program, TaskMonitor monitor) {
    if (gqrName == null) return false;
    
    var addressSpace = program.getAddressFactory().getDefaultAddressSpace();
    var codeManager = ((ProgramDB)program).getCodeManager();
    var iterator = codeManager.getInstructions(addressSpace.getMinAddress(), true);
    var defaultValue = 0L;
    var upperValueFound = false;
    var lowerValueFound = false;
    
    while (monitor.isCancelled() == false && iterator.hasNext()) {
        if (lowerValueFound && upperValueFound) break;
        
        var instruction = iterator.next();
        var mnemonic = instruction.getMnemonicString();
        
        if (mnemonic.equals("blr") && (upperValueFound || lowerValueFound)) break;
        
        Register register = instruction.getRegister(0);
        
        if (register == null) continue;
        if (register.getName().equals(gqrName) == false) continue;
        
        if (mnemonic.equals("mtspr")) {
            // Back up until we've found where the other register is set.
            Register gqrHolder = instruction.getRegister(1);
            var minAddr = codeManager.getInstructionAfter(addressSpace.getMinAddress()).getAddress().getUnsignedOffset();
            var invalid = false;
            while (monitor.isCancelled() == false && instruction != null &&
                   instruction.getAddress().getUnsignedOffset() > minAddr && invalid == false) {
                if (lowerValueFound && upperValueFound)
                    break;
                instruction = instruction.getPrevious();
                if (instruction == null)
                    break;
                if (instruction.getMnemonicString().contains("bl"))
                    break;
                
                Register rt = instruction.getRegister(0);
                if (rt != null && rt.equals(gqrHolder)) {
                    switch (instruction.getMnemonicString()) {
                    // Lower
                    case "li":
                        defaultValue |= instruction.getScalar(1).getSignedValue();
                        lowerValueFound = true;
                        break;
                    case "ori":
                        defaultValue |= instruction.getScalar(2).getUnsignedValue();
                        lowerValueFound = true;
                        break;
                    case "addi":
                        defaultValue += instruction.getScalar(2).getSignedValue();
                        lowerValueFound = true;
                        break;
                    case "subi":
                        defaultValue -= instruction.getScalar(2).getSignedValue();
                        lowerValueFound = true;
                        break;
                    // Upper
                    case "lis":
                        defaultValue |= (instruction.getScalar(1).getUnsignedValue() & 0xFFFF) << 16;
                        upperValueFound = true;
                        break;
                    case "oris":
                        defaultValue |= (instruction.getScalar(2).getUnsignedValue() & 0xFFFF) << 16;
                        upperValueFound = true;
                        break;
                     // Fail all other cases when we haven't found the value.
                    default:
                        upperValueFound = lowerValueFound = false;
                        invalid = true;
                        break;
                    }
                }
            }
        }
    }
    
    if (upperValueFound | lowerValueFound) {
        return setRegisterValue(gqrName, defaultValue, program, codeManager, addressSpace);
    }
    
    return upperValueFound | lowerValueFound;
}
 
Example 6
Source File: ResolveReferencesRelativeToEbxScript.java    From ghidra with Apache License 2.0 2 votes vote down vote up
private void loopOverInstructionsInFunction(Function function) {
	// find all CALL instructions

	long ebx = -1;

	InstructionIterator instructions = currentProgram.getListing().getInstructions( function.getBody(), true ) ;

	while ( instructions.hasNext() ) {

		Instruction instruction = instructions.next();

		if ( monitor.isCancelled() ) {
			break;
		}

		if ( ebx == -1 ) {
			ebx = getValueForEBX( instruction );
		}

		if ( ebx == -1 ) {
			continue;
		}

		for (int i = 0 ; i < instruction.getNumOperands() ; ++i ) {

			Object [] opObjects = instruction.getOpObjects(i);

			if ( opObjects.length == 2 ) {

				if (opObjects[ 0 ] instanceof Scalar && opObjects[ 1 ] instanceof Register ) {

						Scalar scalar = (Scalar) opObjects[ 0 ];

					Register register = (Register) opObjects[ 1 ];

					if ( register.equals( EBX ) ) {

						Address address = toAddr( (ebx + scalar.getUnsignedValue()) & Conv.INT_MASK );

						if ( isValid( address ) ) {

							removeReferencesFrom(instruction);

							Reference reference = createMemoryReference( instruction, 1, address, RefType.DATA );

							setReferencePrimary( reference );

							println( "Creating reference from " + instruction.getMinAddress() + " to " + address );
						}
					}
				}
			}
		}
	}
}