Java Code Examples for ghidra.program.model.listing.Program#getGlobalNamespace()

The following examples show how to use ghidra.program.model.listing.Program#getGlobalNamespace() . 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: CreateLibraryAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createExternalLibrary(SymbolTreeActionContext context) {

		TreePath[] selectionPaths = context.getSelectedSymbolTreePaths();

		Program program = context.getProgram();
		Namespace parent = program.getGlobalNamespace();
		GTreeNode node = (GTreeNode) selectionPaths[0].getLastPathComponent();

		if (node instanceof SymbolTreeRootNode) {
			node = node.getChild("Imports");
		}

		String newExternalLibraryName = createExternalLibrary(program, parent);
		if (newExternalLibraryName == null) {
			return;
		}

		program.flushEvents();
		context.getSymbolTree().startEditing(node, newExternalLibraryName);
	}
 
Example 2
Source File: CreateClassAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createNewClass(SymbolTreeActionContext context) {

		TreePath[] selectionPaths = context.getSelectedSymbolTreePaths();
		Program program = context.getProgram();
		Namespace parent = program.getGlobalNamespace();
		GTreeNode node = (GTreeNode) selectionPaths[0].getLastPathComponent();

		if (node instanceof SymbolNode) {
			Symbol symbol = ((SymbolNode) node).getSymbol();
			parent = (Namespace) symbol.getObject();
			if (parent == null) {
				return; // assume selected node has been deleted
			}
		}

		final String newClassName = createClass(program, parent);
		if (newClassName == null) {
			// error occurred
			return;
		}
		program.flushEvents();
		context.getSymbolTree().startEditing(node, newClassName);
	}
 
Example 3
Source File: CreateNamespaceAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createNamespace(SymbolTreeActionContext context) {

		TreePath[] selectionPaths = context.getSelectedSymbolTreePaths();

		Program program = context.getProgram();
		Namespace parent = program.getGlobalNamespace();
		GTreeNode node = (GTreeNode) selectionPaths[0].getLastPathComponent();

		if (node instanceof SymbolNode) {
			Symbol symbol = ((SymbolNode) node).getSymbol();
			parent = (Namespace) symbol.getObject();
			if (parent == null) {
				return; // assume selected symbol has been deleted
			}
		}

		String newNamespaceName = createNamespace(program, parent);
		if (newNamespaceName == null) {
			// error occurred
			return;
		}

		program.flushEvents();
		context.getSymbolTree().startEditing(node, newNamespaceName);
	}
 
Example 4
Source File: MultipleSymbolStringable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Namespace getNamespaceForNewLabel(Program program, SymbolInfo symbolInfo,
		Address address) throws DuplicateNameException, InvalidInputException {

	if (symbolInfo.isNamespaceFunctionBased) {
		Function f = program.getFunctionManager().getFunctionContaining(address);
		if (f != null) {
			return f;
		}
	}

	// otherwise create or get the path of namespaces.
	Namespace namespace = program.getGlobalNamespace();
	for (NamespaceInfo info : symbolInfo.namespaceInfos) {
		namespace = getOrCreateNamespace(program, info, namespace);
	}
	return namespace;
}
 
Example 5
Source File: AnalysisRecipeTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Namespace getCorrespondingNamespace(Program source, Namespace ns, Program p) {
	Namespace parent = ns.getParentNamespace();
	if (parent == source.getGlobalNamespace()) {
		return p.getGlobalNamespace();
	}
	Namespace other = getCorrespondingNamespace(source, parent, p);
	Symbol symbol = getUniqueSymbol(p, ns.getName(), other);
	return (Namespace) symbol.getObject();
}
 
Example 6
Source File: DexUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static Namespace getOrCreateNameSpace(Program program, String name) {
	SymbolTable symbolTable = program.getSymbolTable();
	Namespace parent = program.getGlobalNamespace();

	Namespace namespace = symbolTable.getNamespace(name, parent);
	if (namespace != null) {
		return namespace;
	}
	try {
		return symbolTable.createNameSpace(parent, name, SourceType.ANALYSIS);
	}
	catch (Exception e) {
		return program.getGlobalNamespace();
	}
}
 
Example 7
Source File: AbstractProgramLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createSymbol(Program program, AddressLabelInfo info, boolean anchorSymbols) {
	SymbolTable symTable = program.getSymbolTable();
	Address addr = info.getAddress();
	Symbol s = symTable.getPrimarySymbol(addr);
	try {
		if (s == null || s.getSource() == SourceType.IMPORTED) {
			Namespace namespace = program.getGlobalNamespace();
			if (info.getScope() != null) {
				namespace = info.getScope();
			}
			s = symTable.createLabel(addr, info.getLabel(), namespace,
				info.getSource());
			if (info.isEntry()) {
				symTable.addExternalEntryPoint(addr);
			}
			if (info.isPrimary()) {
				s.setPrimary();
			}
			if (anchorSymbols) {
				s.setPinned(true);
			}
		}
		else if (s.getSource() == SourceType.DEFAULT) {
			String labelName = info.getLabel();
			if (s.getSymbolType() == SymbolType.FUNCTION) {
				Function f = (Function) s.getObject();
				f.setName(labelName, SourceType.IMPORTED);
			}
			else {
				s.setName(labelName, SourceType.IMPORTED);
			}
			if (anchorSymbols) {
				s.setPinned(true);
			}
		}
	}
	catch (DuplicateNameException | InvalidInputException e) {
		// Nothing to do
	}
}
 
Example 8
Source File: DexUtil.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static Namespace createNameSpaceFromMangledClassName(Program program, String className)
		throws InvalidInputException {
	Namespace namespace = program.getGlobalNamespace();
	return createNameSpaceFromMangledClassName(program, namespace, className);
}
 
Example 9
Source File: SymbolCategoryNode.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public SymbolCategoryNode(SymbolCategory symbolCategory, Program program) {
	this.symbolCategory = symbolCategory;
	this.program = program;
	this.symbolTable = program.getSymbolTable();
	this.globalNamespace = (GlobalNamespace) program.getGlobalNamespace();
}
 
Example 10
Source File: NamespacePath.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Converts this NamespacePath into a Ghidra {@link Namespace} in the specified {@link Program},
 * creating missing elements on the path as necessary.
 *
 * @param program Ghidra {@link Program} where the namespace should be retrieved from or created in.
 * @return {@link Namespace} or fallback to the progam's Global root namespace if problem.
 */
public Namespace getNamespace(Program program) {
	if (isRoot()) {
		return program.getGlobalNamespace();
	}
	try {
		Namespace result = parent.getNamespace(program);
		Namespace existingNamespace =
			NamespaceUtils.getFirstNonFunctionNamespace(result, name, program);
		SymbolType targetSymbolType = flattenSymbolTypeForDNI(type);
		SymbolType existingSymbolType =
			(existingNamespace != null) ? existingNamespace.getSymbol().getSymbolType() : null;

		if (existingNamespace == null) {
			result = (targetSymbolType == SymbolType.NAMESPACE)
					? program.getSymbolTable().createNameSpace(result, name,
						SourceType.IMPORTED)
					: program.getSymbolTable().createClass(result, name, SourceType.IMPORTED);

		}
		else if (existingSymbolType == targetSymbolType) {
			result = existingNamespace;
		}
		else {
			// conflict type
			if (existingSymbolType == SymbolType.NAMESPACE &&
				targetSymbolType == SymbolType.CLASS) {
				result = NamespaceUtils.convertNamespaceToClass(existingNamespace);
			}
			else if (existingSymbolType == SymbolType.CLASS &&
				targetSymbolType == SymbolType.NAMESPACE) {
				// silently allow this
				result = existingNamespace;
			}
			else {
				Msg.error(this, "Error getting Ghidra namespace for " + asNamespaceString());
				result = program.getGlobalNamespace();
			}
		}
		return result;
	}
	catch (DuplicateNameException | InvalidInputException e) {
		Msg.error(this, "Failed to create Ghidra namespace for " + asNamespaceString());
		return program.getGlobalNamespace();
	}
}