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

The following examples show how to use ghidra.program.model.listing.Function#isExternal() . 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: FunctionEditorDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static String createTitle(Function function) {
	StringBuilder strBuilder = new StringBuilder();
	if (function.isExternal()) {
		strBuilder.append("Edit External Function");
		ExternalLocation extLoc = function.getExternalLocation();
		Address addr = extLoc.getAddress();
		if (addr != null) {
			strBuilder.append(" at ");
			strBuilder.append(addr.toString());
		}
	}
	else {
		Function thunkedFunction = function.getThunkedFunction(false);
		if (thunkedFunction != null) {
			strBuilder.append("Edit Thunk Function at ");
			strBuilder.append(function.getEntryPoint().toString());
		}
		else {
			strBuilder.append("Edit Function at ");
			strBuilder.append(function.getEntryPoint().toString());
		}
	}
	return strBuilder.toString();
}
 
Example 2
Source File: GoToExternalLocationAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ProgramSymbolActionContext context) {
	Symbol symbol = context.getFirstSymbol();
	if (symbol == null) {
		return; // assume symbol removed
	}
	Object obj = symbol.getObject();
	ExternalLocation extLoc = null;
	if (obj instanceof Function) {
		Function f = (Function) obj;
		if (f.isExternal()) {
			extLoc = f.getExternalLocation();
		}
	}
	if (obj instanceof ExternalLocation) {
		extLoc = (ExternalLocation) obj;
	}
	if (extLoc != null) {
		plugin.goTo(extLoc);
	}
}
 
Example 3
Source File: ThunkedFunctionFieldFactory.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Color getThunkedFunctionNameColor(Function thunkedFunction) {
	if (!thunkedFunction.isExternal()) {
		return literalColor;
	}
	ExternalLocation externalLocation = thunkedFunction.getExternalLocation();
	String libName = externalLocation.getLibraryName();
	if (Library.UNKNOWN.equals(libName)) {
		return unresolvedThunkRefColor;
	}
	ExternalManager externalManager = thunkedFunction.getProgram().getExternalManager();
	String path = externalManager.getExternalLibraryPath(libName);
	if (path == null || path.length() == 0) {
		return unresolvedThunkRefColor;
	}
	return resolvedThunkRefColor;
}
 
Example 4
Source File: CDisplayPanel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void showFunction(Function function) {
	if (function == null) {
		clearAndShowMessage("No Function");
		return;
	}
	if (function.isExternal()) {
		clearAndShowMessage("\"" + function.getName(true) + "\" is an external function.");
		return;
	}
	Program program = function.getProgram();
	Address entry = function.getEntryPoint();
	ProgramLocation location = new ProgramLocation(program, entry);
	controller.display(program, location, new ViewerPosition(0, 0, 0));
}
 
Example 5
Source File: AnalyzeStackRefsAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isEnabledForContext(ListingActionContext context) {
	if (context.hasSelection()) {
		return true;
	}
	Function func = funcPlugin.getFunction(context);
	if (func != null) {
		return !func.isExternal();
	}
	return false;
}
 
Example 6
Source File: EditStackAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isEnabledForContext(ListingActionContext context) {
	if (context.hasSelection()) {
		return true;
	}
	Function func = getFunction(context);
	if (func != null) {
		return !func.isExternal();
	}
	return false;
}
 
Example 7
Source File: DumpFunctionPatternInfoScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
protected void run() throws Exception {
	if (!isRunningHeadless()) {
		totalFuncs = 0;
		programsAnalyzed = 0;
	}

	int numFirstBytes = askInt("Number of first bytes", "bytes");
	int numFirstInstructions = askInt("Number of first instructions", "instructions");
	int numPreBytes = askInt("Number of pre bytes", "bytes");
	int numPreInstructions = askInt("Number of pre instructions", "instructions");
	int numReturnBytes = askInt("Number of return bytes", "bytes");
	int numReturnInstructions = askInt("Number of return instructions", "instructions");
	String saveDirName = askString("Directory to save results", "directory");
	String contextRegsCSV = askString("Context register csv", "csv");

	File saveDir = new File(saveDirName);
	if (!saveDir.isDirectory()) {
		Msg.info(this, "Invalid save directory: " + saveDirName);
		return;
	}

	List<String> contextRegisters = DataGatheringParams.getContextRegisterList(contextRegsCSV);

	programsAnalyzed++;
	if (currentProgram == null) {
		Msg.info(this, "null current program: try again with the -process option");
		return;
	}

	if (currentProgram.getFunctionManager().getFunctionCount() == 0) {
		Msg.info(this, "No functions found in " + currentProgram.getName() + ", skipping.");
		return;
	}

	FunctionIterator fIter = currentProgram.getFunctionManager().getFunctions(true);
	DataGatheringParams params = new DataGatheringParams();
	params.setNumPreBytes(numPreBytes);
	params.setNumFirstBytes(numFirstBytes);
	params.setNumReturnBytes(numReturnBytes);
	params.setNumPreInstructions(numPreInstructions);
	params.setNumFirstInstructions(numFirstInstructions);
	params.setNumReturnInstructions(numReturnInstructions);
	params.setContextRegisters(contextRegisters);

	FileBitPatternInfo funcPatternList = new FileBitPatternInfo();
	funcPatternList.setLanguageID(currentProgram.getLanguageID().getIdAsString());
	funcPatternList.setGhidraURL("TODO: url");
	funcPatternList.setNumPreBytes(numPreBytes);
	funcPatternList.setNumPreInstructions(numPreInstructions);
	funcPatternList.setNumFirstBytes(numFirstBytes);
	funcPatternList.setNumFirstInstructions(numFirstInstructions);
	funcPatternList.setNumReturnBytes(numReturnBytes);
	funcPatternList.setNumReturnInstructions(numReturnInstructions);

	AddressSetView initialized = currentProgram.getMemory().getLoadedAndInitializedAddressSet();
	while (fIter.hasNext()) {
		monitor.checkCanceled();
		Function func = fIter.next();
		if (func.isThunk()) {
			continue;
		}
		if (func.isExternal()) {
			continue;
		}
		if (!initialized.contains(func.getEntryPoint())) {
			continue;
		}
		if (currentProgram.getListing().getInstructionAt(func.getEntryPoint()) == null) {
			continue;
		}

		FunctionBitPatternInfo fStart =
			new FunctionBitPatternInfo(currentProgram, func, params);
		if (fStart.getFirstBytes() != null) {
			funcPatternList.getFuncBitPatternInfo().add(fStart);
			totalFuncs++;
		}
	}

	File savedFile = new File(saveDir.getAbsolutePath() + File.separator +
		currentProgram.getDomainFile().getPathname().replaceAll("/", "_") + "_" +
		currentProgram.getExecutableMD5() + "_funcInfo.xml");
	funcPatternList.toXmlFile(savedFile);
	Msg.info(this,
		"Programs analyzed: " + programsAnalyzed + "; total functions: " + totalFuncs);
}