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

The following examples show how to use ghidra.program.model.listing.Program#getSymbolTable() . 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: FindReferencesToSymbolAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Symbol getSymbol(DecompilerActionContext context) {

		ProgramLocation location = context.getLocation();
		if (location == null) {
			return null;
		}

		Address address = getDecompilerSymbolAddress(location);
		if (address == null) {
			address = location.getAddress();
		}
		Program program = context.getProgram();
		SymbolTable symbolTable = program.getSymbolTable();
		Symbol symbol = symbolTable.getPrimarySymbol(address);
		return symbol;
	}
 
Example 2
Source File: DemangledObject.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Symbol updateExternalSymbol(Program program, Address externalAddr, String symbolName,
		Demangled demangledNamespace) {

	SymbolTable symbolTable = program.getSymbolTable();
	Symbol s = symbolTable.getPrimarySymbol(externalAddr);
	if (s == null) {
		Msg.error(DemangledObject.class,
			"No such external address " + externalAddr + " for " + symbolName);
	}

	try {
		Namespace ns =
			createNamespace(program, demangledNamespace, s.getParentNamespace(), false);
		ExternalLocation extLoc = s.getProgram().getExternalManager().getExternalLocation(s);
		extLoc.setName(ns, symbolName, SourceType.IMPORTED);
	}
	catch (Exception e) {
		Msg.error(DemangledObject.class,
			"Unexpected Exception setting name and namespace for " + symbolName + " in " +
				s.getParentNamespace(),
			e);
	}
	return s;
}
 
Example 3
Source File: PefLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void processInitSymbol(ContainerHeader header, Program program,
		ImportStateCache importState, MessageLog log, TaskMonitor monitor) {
	SymbolTable symbolTable = program.getSymbolTable();

	LoaderInfoHeader loader = header.getLoader();

	int initSectionIndex = loader.getInitSection();
	if (initSectionIndex != -1) {
		SectionHeader initSection = header.getSections().get(initSectionIndex);
		MemoryBlock initBlock = importState.getMemoryBlockForSection(initSection);
		Address address = initBlock.getStart().add(loader.getInitOffset());
		try {
			symbolTable.createLabel(address, PefConstants.INIT, SourceType.IMPORTED);
			CreateDataCmd cmd = new CreateDataCmd(address, new PointerDataType());
			cmd.applyTo(program);
		}
		catch (Exception e) {
			log.appendException(e);
		}
	}
}
 
Example 4
Source File: MapLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException {

	if (!prog.getExecutableFormat().equals(PeLoader.PE_NAME)) {
		throw new IOException("Program must be a " + PeLoader.PE_NAME);
	}

	SymbolTable symtab = prog.getSymbolTable();
	AddressSpace space = prog.getAddressFactory().getDefaultAddressSpace();
	List<MapExport> exports = parseExports(provider, log);
	int successCount = 0;
	for (MapExport exp : exports) {
		try {
			symtab.createLabel(space.getAddress(exp.addr), exp.name,
				SourceType.IMPORTED).setPrimary();
			successCount++;
		}
		catch (InvalidInputException e) {
			log.appendMsg(e.getMessage());
		}
	}
	log.appendMsg("Added " + successCount + " symbols.");
}
 
Example 5
Source File: DefLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void load(ByteProvider provider, LoadSpec loadSpec, List<Option> options, Program prog,
		TaskMonitor monitor, MessageLog log) throws IOException {

	if (!prog.getExecutableFormat().equals(PeLoader.PE_NAME)) {
		throw new IOException("Program must be a " + PeLoader.PE_NAME);
	}

	SymbolTable symtab = prog.getSymbolTable();
	Consumer<String> errorConsumer = err -> log.error("DefLoader", err);
	for (DefExportLine def : parseExports(provider)) {
		Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(prog,
			SymbolUtilities.ORDINAL_PREFIX + def.getOrdinal(), errorConsumer);
		if (symbol == null) {
			continue;
		}
		try {
			Symbol label =
				symtab.createLabel(symbol.getAddress(), def.getName(), SourceType.IMPORTED);
			label.setPrimary();
		}
		catch (InvalidInputException e) {
			log.appendMsg(e.getMessage());
		}
	}
}
 
Example 6
Source File: GoToQuery.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Namespace getScope(Program program, Namespace parent, String scopeName) {
	int colonIndex = scopeName.lastIndexOf("::");
	if (colonIndex >= 0) {
		String parentScopeName = scopeName.substring(0, colonIndex);
		scopeName = scopeName.substring(colonIndex + 1);
		parent = getScope(program, parent, parentScopeName);
		if (parent == null) {
			return null;
		}
	}
	SymbolTable symTable = program.getSymbolTable();
	Namespace namespace = symTable.getNamespace(scopeName, parent);
	if (namespace != null) {
		return namespace;
	}
	return null;
}
 
Example 7
Source File: MultiTabPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testTabUpdatesOnProgramChange() throws Exception {
	ProgramBuilder builder = new ProgramBuilder("notepad", ProgramBuilder._TOY);
	builder.createMemory("test", "0x0", 100);
	Program p = doOpenProgram(builder.getProgram(), true);
	p.setTemporary(false); // we need to be notified of changes 

	// select notepad
	panel.setSelectedProgram(p);
	int transactionID = p.startTransaction("test");
	try {
		SymbolTable symTable = p.getSymbolTable();
		symTable.createLabel(builder.addr("0x10"), "fred", SourceType.USER_DEFINED);
	}
	finally {
		p.endTransaction(transactionID, true);
	}
	p.flushEvents();
	runSwing(() -> panel.refresh(p));

	JPanel tab = panel.getTab(p);
	JLabel label = (JLabel) findComponentByName(tab, "objectName");
	assertTrue(label.getText().startsWith("*"));
}
 
Example 8
Source File: iOS_FixupArmSymbolsAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public boolean added(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
		throws CancelledException {

	SymbolTable symbolTable = program.getSymbolTable();
	SymbolIterator symbolIterator = symbolTable.getSymbolIterator();
	while (symbolIterator.hasNext()) {
		Symbol symbol = symbolIterator.next();
		if (symbol.isPinned()) {
			symbol.setPinned(false);
		}
	}
	return false;
}
 
Example 9
Source File: RenameGlobalVariableTask.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public RenameGlobalVariableTask(PluginTool tool, Program program, DecompilerPanel panel,
		ClangToken token, Address addr) {
	super(tool, program, panel, token, token.getText());
	address = addr;
	symboltable = program.getSymbolTable();
	symbol = null;
}
 
Example 10
Source File: DexUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static Namespace createNameSpaceFromMangledClassName(Program program,
		Namespace parentNamespace, String className) throws InvalidInputException {
	SymbolTable symbolTable = program.getSymbolTable();
	if (className.startsWith("L") && className.endsWith(";")) {
		String str = className.substring(1, className.length() - 1);
		StringTokenizer tokenizer = new StringTokenizer(str, "/");
		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken();

			Namespace ns = symbolTable.getNamespace(token, parentNamespace);
			if (ns != null) {
				parentNamespace = ns;
				continue;
			}

			try {
				if (tokenizer.hasMoreElements()) { // package name
					parentNamespace = symbolTable.createNameSpace(parentNamespace, token,
						SourceType.ANALYSIS);
				}
				else { // last token should be the class name
					parentNamespace =
						symbolTable.createClass(parentNamespace, token, SourceType.ANALYSIS);
				}
			}
			catch (DuplicateNameException e) {
				// Should never reach here because we already checked for the symbol name
				return null;
			}
		}
	}
	return parentNamespace;
}
 
Example 11
Source File: LabelMarkupItemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Symbol addDefaultLabel(Address address, Program program) {

		SymbolTable symbolTable = program.getSymbolTable();
		int transaction = -1;
		try {
			transaction = program.startTransaction("Test - Add Label");
			ReferenceManager referenceManager = program.getReferenceManager();
			referenceManager.addMemoryReference(address, address, RefType.READ,
				SourceType.USER_DEFINED, 0);
			return symbolTable.getPrimarySymbol(address);
		}
		finally {
			program.endTransaction(transaction, true);
		}
	}
 
Example 12
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 13
Source File: FunctionMatchSet.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @param thisProgramName Name of this program (i.e. the program from
 * which the matching was initiated.
 * @param otherProgramName Name of the program being matched.
 */
public FunctionMatchSet(Program aProgram, Program bProgram) {
	super();
	this.aProgram = aProgram;
	this.bProgram = bProgram;
	this.aSymbolTable = aProgram.getSymbolTable();
	this.bSymbolTable = bProgram.getSymbolTable();

}
 
Example 14
Source File: LabelTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Symbol getSymbol(ProgramLocation rowObject, Program program)
		throws IllegalArgumentException {
	ProgramLocation location = rowObject;
	if (rowObject instanceof VariableLocation) {
		Variable var = ((VariableLocation) rowObject).getVariable();
		if (var != null) {
			return var.getSymbol();
		}
	}
	Address address = location.getAddress();
	SymbolTable symbolTable = program.getSymbolTable();

	return symbolTable.getPrimarySymbol(address);
}
 
Example 15
Source File: MultipleSymbolStringable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Namespace createNamespace(Program program, NamespaceInfo info, Namespace namespace)
		throws DuplicateNameException, InvalidInputException {
	SymbolTable symbolTable = program.getSymbolTable();
	String name = info.name;
	SymbolType type = info.symbolType;
	SourceType sourceType = info.sourceType;

	if (type == SymbolType.LIBRARY) {
		return symbolTable.createExternalLibrary(name, sourceType);
	}
	else if (type == SymbolType.CLASS) {
		return symbolTable.createClass(namespace, name, sourceType);
	}
	return symbolTable.createNameSpace(namespace, name, sourceType);
}
 
Example 16
Source File: ImportStateCache.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ImportStateCache(Program program, ContainerHeader header) {
	this.program = program;
	this.symbolTable = program.getSymbolTable();
	this.importNamespace = createNamespace(null, PefConstants.IMPORT);
	this.tVectNamespace = createNamespace(null, PefConstants.TVECT);

	LoaderInfoHeader loader = header.getLoader();
	List<ImportedLibrary> libraries = loader.getImportedLibraries();
	for (ImportedLibrary library : libraries) {
		String libraryName = SymbolUtilities.replaceInvalidChars(library.getName(), true);
		Namespace libraryNamespace = createNamespace(importNamespace, libraryName);
		libraryNamespaceMap.put(library, libraryNamespace);
		librarySymbolsMap.put(library, new HashMap<String, Symbol>());
	}
}
 
Example 17
Source File: DataLabelMarkupItemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Symbol addLabel(String name, Address address, Program program)
		throws InvalidInputException {

	SymbolTable symbolTable = program.getSymbolTable();
	int transaction = -1;
	try {
		transaction = program.startTransaction("Test - Add Label");
		return symbolTable.createLabel(address, name, SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transaction, true);
	}
}
 
Example 18
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 19
Source File: ReferenceFromLabelTableColumn.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private Symbol getSymbol( ReferenceAddressPair rowObject, Program program ) {
    Address fromAddress = rowObject.getSource();
       SymbolTable symbolTable = program.getSymbolTable();
       return symbolTable.getPrimarySymbol(fromAddress);
}
 
Example 20
Source File: AddressToSymbolTableRowMapper.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Symbol map(Address rowObject, Program program, ServiceProvider serviceProvider) {
	SymbolTable symbolTable = program.getSymbolTable();
	return symbolTable.getPrimarySymbol(rowObject);
}