Java Code Examples for ghidra.program.model.listing.Instruction#getAddress()

The following examples show how to use ghidra.program.model.listing.Instruction#getAddress() . 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: Emulate.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Get length of instruction including any delay-slotted instructions.
 * Must be called by emitPcode with lastPseudoInstructionBlock properly set.
 * @param instr
 * @return length of instruction in bytes for use in computing fall-through location
 */
private int getInstructionLength(Instruction instr) throws InstructionDecodeException {
	int length = instr.getLength();
	int delaySlots = instr.getDelaySlotDepth();
	while (delaySlots != 0) {
		try {
			Address nextAddr = instr.getAddress().addNoWrap(instr.getLength());
			Instruction nextInstr = lastPseudoInstructionBlock.getInstructionAt(nextAddr);
			if (nextInstr == null) {
				throw new InstructionDecodeException("Failed to parse delay slot instruction",
					nextAddr);
			}
			instr = nextInstr;
			length += instr.getLength();
			--delaySlots;
		}
		catch (AddressOverflowException e) {
			throw new InstructionDecodeException(
				"Failed to parse delay slot instruction at end of address space",
				instr.getAddress());
		}
	}
	return length;
}
 
Example 2
Source File: InstructionError.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static void dumpInstructionDifference(Instruction newInst, Instruction existingInstr) {
	StringBuilder buf =
		new StringBuilder("Instruction conflict details at " + newInst.getAddress());
	buf.append("\n  New Instruction: ");
	buf.append(getInstructionDetails(newInst));
	buf.append("\n  Existing Instruction: ");
	buf.append(getInstructionDetails(existingInstr));
	Msg.debug(InstructionError.class, buf.toString());
}
 
Example 3
Source File: AssemblyThrasherDevScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected PseudoInstruction disassemble(Instruction orig, byte[] ins) {
	try {
		Address at = orig.getAddress();
		Language language = currentProgram.getLanguage();
		MemBuffer buf = new ByteMemBufferImpl(at, ins, language.isBigEndian());
		InstructionPrototype ip = language.parse(buf, orig, false);
		return new PseudoInstruction(at, ip, buf, orig);
	}
	catch (InsufficientBytesException | UnknownInstructionException
			| AddressOverflowException e) {
		throw new RuntimeException(e);
	}
}
 
Example 4
Source File: InstructionSearchAddressIterator.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Address next() {
	Instruction instruction = instructionIterator.next();
	return instruction.getAddress();
}
 
Example 5
Source File: EmuX86DeobfuscateExampleScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
protected void run() throws Exception {

	String format =
		currentProgram.getOptions(Program.PROGRAM_INFO).getString("Executable Format", null);

	if (currentProgram == null || !currentProgram.getName().startsWith(PROGRAM_NAME) ||
		!"x86:LE:64:default".equals(currentProgram.getLanguageID().toString()) ||
		!ElfLoader.ELF_NAME.equals(format)) {

		printerr(
			"This emulation example script is specifically intended to be executed against the\n" +
				PROGRAM_NAME +
				" program whose source is contained within the GhidraClass exercise files\n" +
				"(see docs/GhidraClass/ExerciseFiles/Emulation/" + PROGRAM_NAME + ".c).\n" +
				"This program should be compiled using gcc for x86 64-bit, imported into your project, \n" +
				"analyzed and open as the active program before running ths script.");
		return;
	}

	// Identify function to be emulated
	mainFunctionEntry = getSymbolAddress("main");

	// Obtain entry instruction in order to establish initial processor context
	Instruction entryInstr = getInstructionAt(mainFunctionEntry);
	if (entryInstr == null) {
		printerr("Instruction not found at main entry point: " + mainFunctionEntry);
		return;
	}

	// Identify important symbol addresses
	// NOTE: If the sample is recompiled the following addresses may need to be adjusted
	Instruction callSite = getCalledFromInstruction("deobfuscate");
	if (callSite == null) {
		printerr("Instruction not found at call site for: deobfuscate");
		return;
	}

	deobfuscateCall = callSite.getAddress();
	deobfuscateReturn = callSite.getFallThrough(); // instruction address immediately after deobfuscate call

	// Remove prior pre-comment
	setPreComment(deobfuscateReturn, null);

	// Establish emulation helper
	emuHelper = new EmulatorHelper(currentProgram);
	try {

		// Initialize stack pointer (not used by this example)
		long stackOffset =
			(entryInstr.getAddress().getAddressSpace().getMaxAddress().getOffset() >>> 1) -
				0x7fff;
		emuHelper.writeRegister(emuHelper.getStackPointerRegister(), stackOffset);

		// Setup breakpoints
		emuHelper.setBreakpoint(deobfuscateCall);
		emuHelper.setBreakpoint(deobfuscateReturn);

		// Set controlled return location so we can identify return from emulated function
		controlledReturnAddr = getAddress(CONTROLLED_RETURN_OFFSET);
		emuHelper.writeStackValue(0, 8, CONTROLLED_RETURN_OFFSET);
		emuHelper.setBreakpoint(controlledReturnAddr);

		Msg.debug(this, "EMU starting at " + mainFunctionEntry);

		// Execution loop until return from function or error occurs
		while (!monitor.isCancelled()) {
			boolean success =
				(emuHelper.getEmulateExecutionState() == EmulateExecutionState.BREAKPOINT)
						? emuHelper.run(monitor)
						: emuHelper.run(mainFunctionEntry, entryInstr, monitor);
			Address executionAddress = emuHelper.getExecutionAddress();
			if (monitor.isCancelled()) {
				println("Emulation cancelled");
				return;
			}
			if (executionAddress.equals(controlledReturnAddr)) {
				println("Returned from function");
				return;
			}
			if (!success) {
				String lastError = emuHelper.getLastError();
				printerr("Emulation Error: " + lastError);
				return;
			}
			processBreakpoint(executionAddress);
		}
	}
	finally {
		// cleanup resources and release hold on currentProgram
		emuHelper.dispose();
	}
}
 
Example 6
Source File: AssemblyThrasherDevScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public void setExpected(Instruction ins) {
	this.orig = ins;
	this.addr = ins.getAddress();
	this.text = ins.toString().trim();
}
 
Example 7
Source File: DisassemblerQueue.java    From ghidra with Apache License 2.0 3 votes vote down vote up
/**
 * Queue priority delay-slot disassembly for current block.
 * Fallthrough must be handled immediately with next InstructionSet
 * to ensure that it remains the start of an InstructionBlock contained 
 * within current InstructionSet.
 * Caller is responsible for adding flow to current block.
 * @param flow instruction flow
 */
void queueDelaySlotFallthrough(Instruction delaySlotInstruction) {
	InstructionBlockFlow dsFallThrough =
		new InstructionBlockFlow(delaySlotInstruction.getMaxAddress().next(),
			delaySlotInstruction.getAddress(), InstructionBlockFlow.Type.PRIORITY);
	priorityQueue.add(dsFallThrough);
}