Java Code Examples for ghidra.program.model.listing.Function#getProgram()

The following examples show how to use ghidra.program.model.listing.Function#getProgram() . 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: DeletePrototypeOverrideAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected void decompilerActionPerformed(DecompilerActionContext context) {
	Function func = context.getFunction();
	CodeSymbol sym = getSymbol(func, context.getTokenAtCursor());
	Program program = func.getProgram();
	SymbolTable symtab = program.getSymbolTable();
	int transaction = program.startTransaction("Remove Override Signature");
	boolean commit = true;
	if (!symtab.removeSymbolSpecial(sym)) {
		commit = false;
		Msg.showError(getClass(), context.getDecompilerPanel(),
			"Removing Override Signature Failed", "Error removing override signature");
	}
	program.endTransaction(transaction, commit);

}
 
Example 2
Source File: BasicBlockCounterFunctionAlgorithm.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public int score(Function function, TaskMonitor monitor) throws CancelledException {
	Program program = function.getProgram();
	CodeBlockModel blockModel = new BasicBlockModel(program);

	AddressSetView body = function.getBody();
	long maxIterations = body.getNumAddresses();
	monitor.initialize(maxIterations);

	CodeBlockIterator iterator = blockModel.getCodeBlocksContaining(body, monitor);

	int blockCount = 0;
	while (iterator.hasNext()) {
		monitor.checkCanceled();
		iterator.next();
		blockCount++;
		monitor.incrementProgress(1);

		artificialSleepForDemoPurposes();
	}
	return blockCount;
}
 
Example 3
Source File: AbstractApplyFunctionSignatureAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the comparison panel opposite the one with focus,
 * is read-only
 * <p>
 * eg: if the right-side panel has focus, and the left-side panel is 
 * read-only, this will return true
 *  
 * @param codeComparisonPanel the comparison panel
 * @return true if the non-focused panel is read-only
 */
protected boolean hasReadOnlyNonFocusedSide(
		CodeComparisonPanel<? extends FieldPanelCoordinator> codeComparisonPanel) {
	Function leftFunction = codeComparisonPanel.getLeftFunction();
	Function rightFunction = codeComparisonPanel.getRightFunction();

	if (leftFunction == null || rightFunction == null) {
		return false; // Doesn't have a function on both sides.
	}

	boolean leftHasFocus = codeComparisonPanel.leftPanelHasFocus();
	Program leftProgram = leftFunction.getProgram();
	Program rightProgram = rightFunction.getProgram();
	return (!leftHasFocus && leftProgram.getDomainFile().isReadOnly()) ||
		(leftHasFocus && rightProgram.getDomainFile().isReadOnly());
}
 
Example 4
Source File: AbstractApplyFunctionSignatureAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to change the signature of a function to that of another
 * function
 * 
 * @param provider the parent component provider 
 * @param destinationFunction the function to change
 * @param sourceFunction the function to copy
 * @return true if the operation was successful
 */
protected boolean updateFunction(ComponentProvider provider, Function destinationFunction,
		Function sourceFunction) {

	Program program = destinationFunction.getProgram();
	int txID = program.startTransaction(ACTION_NAME);
	boolean commit = false;

	try {
		FunctionUtility.updateFunction(destinationFunction, sourceFunction);
		commit = true;
	}
	catch (InvalidInputException | DuplicateNameException e) {
		String message = "Couldn't apply the function signature from " +
			sourceFunction.getName() + " to " + destinationFunction.getName() + " @ " +
			destinationFunction.getEntryPoint().toString() + ". " + e.getMessage();
		Msg.showError(this, provider.getComponent(), ACTION_NAME, message, e);
	}
	finally {
		program.endTransaction(txID, commit);
	}

	return commit;
}
 
Example 5
Source File: StackEditorProvider.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public StackEditorProvider(Plugin plugin, Function function) {
	super(plugin);
	this.program = function.getProgram();
	this.function = function;
	program.addListener(this);
	editorModel = new StackEditorModel(this);
	stackModel = (StackEditorModel) editorModel;
	stackModel.load(function);

	initializeActions();
	editorPanel = new StackEditorPanel(program, stackModel, this);
	setTitle(getName() + " - " + getProviderSubTitle(function));
	plugin.getTool().addComponentProvider(this, true);

	addActionsToTool();
	editorPanel.getTable().requestFocus();
}
 
Example 6
Source File: EditFunctionPurgeAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void showDialog(Function function) {
	int currentFunctionPurgeSize = function.getStackPurgeSize();

	if (currentFunctionPurgeSize == Function.INVALID_STACK_DEPTH_CHANGE ||
		currentFunctionPurgeSize == Function.UNKNOWN_STACK_DEPTH_CHANGE) {
		currentFunctionPurgeSize = 0;
	}
	NumberInputDialog numberInputDialog = new NumberInputDialog("Please Enter Function Purge",
		"Enter Function Purge", currentFunctionPurgeSize, -1048576, 1048576, false);
	numberInputDialog.setHelpLocation(new HelpLocation("FunctionPlugin", "Function_Purge"));

	if (!numberInputDialog.show()) {
		functionPlugin.getTool().setStatusInfo("User cancelled function purge");
		return;
	}
	int newFunctionPurgeSize = numberInputDialog.getValue();

	if (newFunctionPurgeSize != currentFunctionPurgeSize) {
		Command command = new SetFunctionPurgeCommand(function, newFunctionPurgeSize);

		PluginTool tool = functionPlugin.getTool();
		Program program = function.getProgram();

		if (!tool.execute(command, program)) {
			tool.setStatusInfo("Unable to set function purge on " + "function: " +
					function.getName());
		}
	}
}
 
Example 7
Source File: FunctionCallFixupFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	ProxyObj<?> proxy = bf.getProxy();
	if (proxy instanceof FunctionProxy) {
		FunctionProxy functionProxy = (FunctionProxy) proxy;
		Function function = functionProxy.getObject();
		return new FunctionCallFixupFieldLocation(function.getProgram(),
			functionProxy.getLocationAddress(), functionProxy.getFunctionAddress(),
			function.getCallFixup(), col);
	}

	return null;
}
 
Example 8
Source File: FunctionRepeatableCommentFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	ProxyObj<?> proxy = bf.getProxy();
	if (proxy instanceof FunctionProxy) {
		FunctionProxy functionProxy = (FunctionProxy) proxy;
		Function function = functionProxy.getObject();
		return new FunctionRepeatableCommentFieldLocation(function.getProgram(),
			functionProxy.getLocationAddress(), functionProxy.getFunctionAddress(),
			function.getRepeatableCommentAsArray(), row, col);
	}
	return null;
}
 
Example 9
Source File: VTFunctionAssociationProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void functionRemoved(ProgramChangeRecord record) {
	Function function = (Function) record.getObject();
	Program program = function.getProgram();
	if (program == controller.getSourceProgram()) {
		sourceFunctionsModel.functionRemoved(function);
	}
	else {
		destinationFunctionsModel.functionRemoved(function);
	}
}
 
Example 10
Source File: ThunkedFunctionFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	ProxyObj<?> proxy = bf.getProxy();
	if (proxy instanceof FunctionProxy) {
		FunctionProxy functionProxy = (FunctionProxy) proxy;
		Function function = functionProxy.getObject();
		Function thunkedFunction = function.getThunkedFunction(false);
		return new ThunkedFunctionFieldLocation(function.getProgram(),
			functionProxy.getLocationAddress(), functionProxy.getFunctionAddress(),
			thunkedFunction != null ? thunkedFunction.getEntryPoint() : null, col);
	}

	return null;
}
 
Example 11
Source File: FunctionTagFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden to ensure that we return` a {@link FunctionTagFieldLocation} instance. 
 */
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	ProxyObj<?> proxy = bf.getProxy();
	if (proxy instanceof FunctionProxy) {
		FunctionProxy functionProxy = (FunctionProxy) proxy;
		Function function = functionProxy.getObject();
		return new FunctionTagFieldLocation(function.getProgram(),
			functionProxy.getLocationAddress(), functionProxy.getFunctionAddress(),
			function.getCallFixup(), col);
	}
	return null;
}
 
Example 12
Source File: FunctionSignatureSourceFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	ProxyObj<?> proxy = bf.getProxy();
	if (proxy instanceof FunctionProxy) {
		FunctionProxy functionProxy = (FunctionProxy) proxy;
		Function function = functionProxy.getObject();
		return new FunctionSignatureSourceFieldLocation(function.getProgram(),
			functionProxy.getLocationAddress(), functionProxy.getFunctionAddress(),
			function.getSignatureSource().toString(), col);
	}

	return null;
}
 
Example 13
Source File: FunctionPurgeFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @see ghidra.app.util.viewer.field.FieldFactory#getProgramLocation(int, int, ghidra.app.util.viewer.field.ListingField)
 */
@Override
public ProgramLocation getProgramLocation(int row, int col, ListingField bf) {
	Object obj = bf.getProxy().getObject();
	if (!(obj instanceof Function) || row < 0 || col < 0) {
		return null;
	}

	Function func = (Function) obj;

	return new FunctionPurgeFieldLocation(func.getProgram(), func.getEntryPoint(), col);
}
 
Example 14
Source File: HashStore.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public HashStore(Function a,TaskMonitor mon) throws CancelledException {
	function = a;
	program = a.getProgram();
	monitor = mon;
	blockList = new TreeMap<Address,Block>();
	hashSort = new TreeMap<Hash,HashEntry>();
	matchSort = new TreeSet<HashEntry>(new HashOrderComparator());
	matchedBlockCount = 0;			// No matches yet
	matchedInstructionCount = 0;
	totalInstructions = 0;			// basic-blocks and instructions not modeled yet
	initializeStructures();			// Model basic-blocks and instructions
}
 
Example 15
Source File: FidProgramSeeker.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static ArrayList<Function> getParents(Function function,boolean followThunks) {
	Program program = function.getProgram();
	FunctionManager functionManager = program.getFunctionManager();
	ReferenceManager referenceManager = program.getReferenceManager();
	HashSet<Address> alreadyDone = new HashSet<Address>();
	ArrayList<Function> funcList = new ArrayList<Function>();
	int size = 0;
	Address curAddr = function.getEntryPoint();
	Address[] thunkAddresses = null;
	if (followThunks) {
		thunkAddresses = function.getFunctionThunkAddresses();
		if (thunkAddresses != null)
			size = thunkAddresses.length;
	}
	int pos = -1;
	for(;;) {
		ReferenceIterator referenceIterator = referenceManager.getReferencesTo(curAddr);
		for (Reference reference : referenceIterator) {
			// monitor.checkCanceled();
			Address fromAddress = reference.getFromAddress();
			if (reference.getReferenceType().isCall()) {
				Function par = functionManager.getFunctionContaining(fromAddress);
				if (par != null) {
					Address entryPoint = par.getEntryPoint();
					if (!alreadyDone.contains(entryPoint)) {
						funcList.add(par);
						alreadyDone.add(entryPoint);
					}
				}
			}
		}
		pos += 1;
		if (pos >= size) break;
		curAddr = thunkAddresses[pos];
	}

	return funcList;
}
 
Example 16
Source File: FidProgramSeeker.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static ArrayList<Function> getChildren(Function function,boolean followThunks) {
		Program program = function.getProgram();
		FunctionManager functionManager = program.getFunctionManager();
		ReferenceManager referenceManager = program.getReferenceManager();
		HashSet<Address> alreadyDone = new HashSet<Address>();
		ArrayList<Function> funcList = new ArrayList<Function>();
		AddressIterator referenceIterator =
				referenceManager.getReferenceSourceIterator(function.getBody(), true);
		for (Address address : referenceIterator) {
//			monitor.checkCanceled();
			Reference[] referencesFrom = referenceManager.getReferencesFrom(address);
			for (Reference reference : referencesFrom) {
				Address toAddress = reference.getToAddress();
				if (reference.getReferenceType().isCall() && !alreadyDone.contains(toAddress)) {
					Function child = functionManager.getFunctionContaining(toAddress);
					if (child != null) {
						if (followThunks && child.isThunk()) {
							child = child.getThunkedFunction(true);
						}
						funcList.add(child);
						alreadyDone.add(toAddress);
					}
				}
			}
		}
		return funcList;
	}
 
Example 17
Source File: FunctionBodyFunctionExtentGenerator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public List<CodeUnit> calculateExtent(Function func) {
	ArrayList<CodeUnit> units = new ArrayList<CodeUnit>();

	final AddressSetView body = func.getBody();
	final Program program = func.getProgram();
	final Listing listing = program.getListing();

	InstructionIterator codeUnitIterator = listing.getInstructions(body, true);
	for (Instruction codeUnit : codeUnitIterator) {
		units.add(codeUnit);
	}

	return units;
}
 
Example 18
Source File: VTFunctionAssociationProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void functionAdded(ProgramChangeRecord record) {
	Function function = (Function) record.getObject();
	Program program = function.getProgram();
	if (program == controller.getSourceProgram()) {
		sourceFunctionsModel.functionAdded(function);
	}
	else {
		destinationFunctionsModel.functionAdded(function);
	}
}
 
Example 19
Source File: JumpTable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void writeOverride(Function func) throws InvalidInputException {
	if (override == null)
		throw new InvalidInputException("Jumptable is not an override");
	Address[] destlist = override.getDestinations();
	if (destlist.length == 0)
		throw new InvalidInputException("Jumptable has no destinations");
	if (!func.getBody().contains(opAddress))
		throw new InvalidInputException("Switch is not in function body");
	Program program = func.getProgram();
	SymbolTable symtab = program.getSymbolTable();

	Namespace space = HighFunction.findCreateOverrideSpace(func);
	if (space == null)
		throw new InvalidInputException("Could not create \"override\" namespace");
	space = HighFunction.findCreateNamespace(symtab, space, "jmp_" + opAddress.toString());

	if (!HighFunction.clearNamespace(symtab, space))
		throw new InvalidInputException(
			"Jumptable override namespace contains non-label symbols.");

	HighFunction.createLabelSymbol(symtab, opAddress, "switch", space, SourceType.USER_DEFINED,
		false);
	for (int i = 0; i < destlist.length; ++i) {
		String nm = "case_" + Integer.toString(i);
		HighFunction.createLabelSymbol(symtab, destlist[i], nm, space, SourceType.USER_DEFINED,
			false);
	}
}
 
Example 20
Source File: StackEditorProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
static String getProviderSubTitle(Function function) {
	Program pgm = function.getProgram();
	return function.getName() + " (" + pgm.getDomainFile().getName() + ")";
}