Java Code Examples for ghidra.program.model.symbol.Symbol#getName()

The following examples show how to use ghidra.program.model.symbol.Symbol#getName() . 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: EditExternalLocationDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private String getEditName() {
	if (externalLocation != null) {
		Symbol symbol = externalLocation.getSymbol();
		String name = symbol.getName(true);
		Address addr = externalLocation.getAddress();
		if (addr != null) {
			name += " @ " + addr.toString(true);
		}
		return name;
	}
	String editName;
	editName = externalLocation.getSymbol().getParentNamespace().getName(true);
	boolean hasName = locationName != null && locationName.length() > 0;
	if (hasName) {
		editName += "::" + locationName;
	}
	if (address != null) {
		editName += " @ " + address.toString();
	}
	return editName;
}
 
Example 2
Source File: VersionTrackingPluginScreenShots.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private VTMatch getMatch(String sourceLabel, VTAssociationType matchType) {
	List<VTMatchSet> matchSets = session.getMatchSets();
	for (VTMatchSet vtMatchSet : matchSets) {
		Collection<VTMatch> matches = vtMatchSet.getMatches();
		for (VTMatch vtMatch : matches) {
			VTAssociation association = vtMatch.getAssociation();
			VTAssociationType type = association.getType();
			if (type != matchType) {
				continue;
			}
			Address sourceAddr = association.getSourceAddress();
			SymbolTable symbolTable = sourceProgram.getSymbolTable();
			Symbol primarySymbol = symbolTable.getPrimarySymbol(sourceAddr);
			String name = primarySymbol.getName(false);
			if (name.equals(sourceLabel)) {
				return vtMatch;
			}
		}
	}
	return null;
}
 
Example 3
Source File: LabelCodeUnitFormat.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected String getOffcutLabelStringForInstruction(Address offcutAddress,
		Instruction instruction) {
	Program program = instruction.getProgram();
	Symbol offsym = program.getSymbolTable().getPrimarySymbol(offcutAddress);
	Address instructionAddress = instruction.getMinAddress();
	long diff = offcutAddress.subtract(instructionAddress);
	if (!offsym.isDynamic()) {
		return getDefaultOffcutString(offsym, instruction, diff, true);
	}

	Symbol containingSymbol = program.getSymbolTable().getPrimarySymbol(instructionAddress);
	if (containingSymbol != null) {
		return containingSymbol.getName() + PLUS + diff;
	}
	return getDefaultOffcutString(offsym, instruction, diff, true);
}
 
Example 4
Source File: ProgramAnnotatedStringHandler.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private boolean goToSymbol(List<Symbol> symbols, Navigatable navigatable, Program program,
		GoToService goToService) {

	if (symbols.isEmpty()) {
		return false;
	}

	// if there is only one, just go there directly, otherwise have to do a search
	Symbol symbol = symbols.get(0);
	if (symbols.size() == 1) {
		return goToService.goTo(navigatable, symbol.getProgramLocation(), program);
	}

	Address addr = navigatable.getLocation().getAddress();
	QueryData data = new QueryData(symbol.getName(), true);
	return goToService.goToQuery(navigatable, addr, data, null, null);
}
 
Example 5
Source File: CodeUnitDetails.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static String getRefInfo(Program pgm, Reference ref) {
	String typeStr = "Type: " + ref.getReferenceType();
	String fromStr = "  From: " + ref.getFromAddress();
	String operandStr =
		((ref.isMnemonicReference()) ? "  Mnemonic" : ("  Operand: " + ref.getOperandIndex()));
	String toStr = "  To: " + DiffUtility.getUserToAddressString(pgm, ref);
	String sourceStr = "  " + ref.getSource().toString();
	String primaryStr = ((ref.isPrimary()) ? "  Primary" : "");
	String symbolStr = "";
	long symbolID = ref.getSymbolID();
	if (symbolID != -1) {
		Symbol sym = pgm.getSymbolTable().getSymbol(symbolID);
		if (sym != null) {
			symbolStr = "  Symbol: " + sym.getName(true);
		}
	}
	return typeStr + fromStr + operandStr + toStr + sourceStr + primaryStr + symbolStr;
}
 
Example 6
Source File: ProgramProviderContext.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataTypeComponent getDataTypeComponent(int offset) {
	Data data = getData(offset);
	if (data == null) {
		return null;
	}

	DataType dt = data.getDataType();
	int length = data.getLength();
	String label = null;
	Symbol symbol = data.getPrimarySymbol();
	if (symbol != null && !symbol.isDynamic()) {
		label = symbol.getName();
	}
	String comment = data.getComment(CodeUnit.EOL_COMMENT);
	return new DataTypeComponentImpl(dt, null, length, 0, offset, label, comment);

}
 
Example 7
Source File: FindReferencesToSymbolAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void updateMenuName(Symbol symbol) {

		if (symbol == null) {
			return; // not sure if this can happen
		}

		String symbolName = symbol.getName(false);
		String menuName = MENU_ITEM_TEXT + ' ' + symbolName;

		MenuData data = getPopupMenuData().cloneData();
		data.setMenuPath(new String[] { LocationReferencesService.MENU_GROUP, menuName });
		setPopupMenuData(data);
	}
 
Example 8
Source File: DemangleElfWithOptionScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() throws Exception {

	GnuDemangler demangler = new GnuDemangler();
	if (!demangler.canDemangle(currentProgram)) {
		String executableFormat = currentProgram.getExecutableFormat();
		println("Cannot use the elf demangling options for executable format: " +
			executableFormat);
		return;
	}

	Symbol symbol = null;
	if (currentAddress != null && (currentSelection == null || currentSelection.isEmpty())) {
		symbol = getSymbolAt(currentAddress);
	}
	if (symbol == null) {
		println("No symbol at the current address (selections are not supported)");
		return;
	}

	String mangled = symbol.getName();

	GnuDemanglerOptions options = new GnuDemanglerOptions();
	options.setDoDisassembly(false);
	options.setDemanglerApplicationArguments("-s auto");

	/*
		// for older formats use the deprecated demangler
		options.setDemanglerName(GnuDemanglerOptions.GNU_DEMANGLER_V2_24);
		options.setDemanglerApplicationArguments("-s arm");
	*/

	DemangledObject demangledObject = demangler.demangle(mangled, options);
	if (demangledObject == null) {
		println("Could not demangle: " + mangled);
		return;
	}

	println("Succesfully demangled " + mangled + " to " + demangledObject);
}
 
Example 9
Source File: ProgramDnDTree.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a fragment name; if there is a symbol at the start
 * of the fragment, and if it is user defined, then use the label 
 * as the fragment name; otherwise, just use the address as the name.
 * @param addr first address of fragment
 * 
 * @return String name of fragment
 */
private String generateFragmentName(Address addr) {

	Symbol symbol = program.getSymbolTable().getPrimarySymbol(addr);
	if (symbol == null || symbol.isDynamic()) {
		return addr.toString();
	}
	return symbol.getName();
}
 
Example 10
Source File: AddressLocationDescriptor.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getLabelForAddress(CodeUnitLocation location) {
	Address address = getAddressForLocation(location);
	SymbolTable symbolTable = program.getSymbolTable();
	Symbol symbol = symbolTable.getPrimarySymbol(address);
	if (symbol != null) {
		return symbol.getName();
	}

	if (location instanceof AddressFieldLocation) {
		return ((AddressFieldLocation) location).getAddressRepresentation();
	}

	return location.getAddress().toString();
}
 
Example 11
Source File: NXProgramBuilder.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
private Symbol checkPrimary(Symbol sym) 
{
    if (sym == null || sym.isPrimary()) 
    {
        return sym;
    }

    String name = sym.getName();
    Address addr = sym.getAddress();

    if (name.indexOf("@") > 0) { // <sym>@<version> or <sym>@@<version>
        return sym; // do not make versioned symbols primary
    }

    // if starts with a $, probably a markup symbol, like $t,$a,$d
    if (name.startsWith("$")) {
        return sym;
    }

    // if sym starts with a non-letter give preference to an existing symbol which does
    if (!Character.isAlphabetic(name.codePointAt(0))) {
        Symbol primarySymbol = program.getSymbolTable().getPrimarySymbol(addr);
        if (primarySymbol != null && primarySymbol.getSource() != SourceType.DEFAULT &&
            Character.isAlphabetic(primarySymbol.getName().codePointAt(0))) {
            return sym;
        }
    }

    SetLabelPrimaryCmd cmd = new SetLabelPrimaryCmd(addr, name, sym.getParentNamespace());
    if (cmd.applyTo(program)) {
        return program.getSymbolTable().getSymbol(name, addr, sym.getParentNamespace());
    }

    Msg.error(this, cmd.getStatusMsg());

    return sym;
}
 
Example 12
Source File: ListingGraphComponentPanel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String createTitle() {
	Address minAddress = addressSet.getMinAddress();
	String newTitle = minAddress.toString();
	SymbolTable symbolTable = program.getSymbolTable();
	Symbol primarySymbol = symbolTable.getPrimarySymbol(minAddress);
	if (primarySymbol != null) {
		newTitle += " - " + primarySymbol.getName(false);
	}
	return newTitle;
}
 
Example 13
Source File: BlockModelGraphDisplayListener.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected List<String> getVertices(AddressSetView addrSet) {
	if (addrSet.isEmpty()) {
		return Collections.emptyList();
	}

	// Identify all blocks which have an entry point within the selection address set
	ArrayList<String> blockList = new ArrayList<String>();
	try {
		SymbolTable symTable = program.getSymbolTable();
		CodeBlockIterator cbIter =
			blockModel.getCodeBlocksContaining(addrSet, TaskMonitor.DUMMY);
		while (cbIter.hasNext()) {
			CodeBlock block = cbIter.next();
			String addrString;
			Address addr = block.getFirstStartAddress();
			if (addr.isExternalAddress()) {
				Symbol s = symTable.getPrimarySymbol(addr);
				addrString = s.getName(true);
			}
			else {
				addrString = addr.toString();
			}
			blockList.add(addrString);
		}
	}
	catch (CancelledException e) {
		// Will not happen with dummyMonitor
		// Model has already done the work when the graph was created
	}

	return blockList;
}
 
Example 14
Source File: GlobalSymbolMap.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a HighSymbol based on the id of the underlying Ghidra Symbol. The Symbol
 * is looked up in the SymbolTable and then a HighSymbol is created with the name and
 * dataType associated with the Symbol. If a Symbol cannot be found, null is returned.
 * @param id is the database id of the CodeSymbol
 * @param dataType is the recovered data-type of the symbol
 * @param sz is the size in bytes of the desired symbol
 * @return the CodeSymbol wrapped as a HighSymbol or null
 */
public HighSymbol populateSymbol(long id, DataType dataType, int sz) {
	if ((id >> 56) == (HighSymbol.ID_BASE >> 56)) {
		return null;		// This is an internal id, not a database key
	}
	Symbol symbol = symbolTable.getSymbol(id);
	if (symbol == null) {
		return null;
	}
	HighSymbol highSym = null;
	if (symbol instanceof CodeSymbol) {
		if (dataType == null) {
			Object dataObj = symbol.getObject();
			if (dataObj instanceof Data) {
				dataType = ((Data) dataObj).getDataType();
				sz = dataType.getLength();
			}
			else {
				dataType = DataType.DEFAULT;
				sz = 1;
			}
		}
		highSym = new HighCodeSymbol((CodeSymbol) symbol, dataType, sz, func);
	}
	else if (symbol instanceof FunctionSymbol) {
		highSym = new HighFunctionShellSymbol(id, symbol.getName(), symbol.getAddress(),
			func.getDataTypeManager());
	}
	else {
		return null;
	}
	insertSymbol(highSym, symbol.getAddress());
	return highSym;
}
 
Example 15
Source File: ReferenceFromLabelTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
   public String getValue(ReferenceAddressPair rowObject, Settings settings, 
        Program program, ServiceProvider serviceProvider) throws IllegalArgumentException {

	Symbol s = getSymbol( rowObject, program );
	if (s != null) {
		return s.getName(true);
	}
	return null;
}
 
Example 16
Source File: LabelTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String getValue(ProgramLocation rowObject, Settings settings, Program program,
		ServiceProvider serviceProvider) throws IllegalArgumentException {
	if (rowObject instanceof LabelFieldLocation) {
		LabelFieldLocation labelFieldLocation = (LabelFieldLocation) rowObject;
		return labelFieldLocation.getSymbolPath().getName();
	}
	Symbol symbol = getSymbol(rowObject, program);
	if (symbol != null) {
		return symbol.getName();
	}
	return null;
}
 
Example 17
Source File: SubsToFuncsScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
	BlockModelService blockModelService = state.getTool().getService(BlockModelService.class);
	Listing listing = currentProgram.getListing();
	StringBuffer errorBuf = new StringBuffer();
	CodeBlockModel cbm = blockModelService.getActiveSubroutineModel();
	AddressSetView addrset =
		currentSelection == null ? (AddressSetView) currentProgram.getMemory()
				: currentSelection;
	CodeBlockIterator cbIter = cbm.getCodeBlocksContaining(addrset, monitor);
	while (cbIter.hasNext()) {
		CodeBlock block = cbIter.next();
		FunctionIterator fIter = listing.getFunctions(block, true);
		if (!fIter.hasNext()) {
			try {
				String name = "DEAD_" + block.getFirstStartAddress();
				Symbol symbol = getSymbolAt(block.getFirstStartAddress());
				if (symbol != null && !symbol.isDynamic()) {
					name = symbol.getName();
				}
				listing.createFunction(name, block.getFirstStartAddress(), block,
					SourceType.USER_DEFINED);
			}
			catch (Exception e) {
				errorBuf.append(e.toString() + "\n");
			}
		}
	}
	if (errorBuf.length() > 0) {
		println(errorBuf.toString());
	}
}
 
Example 18
Source File: AbstractTextFilter.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected String getSymbolText(Address address) {
	VTSession session = controller.getSession();
	Program sourceProgram = session.getSourceProgram();
	SymbolTable symbolTable = sourceProgram.getSymbolTable();
	Symbol symbol = symbolTable.getPrimarySymbol(address);
	if (symbol == null) {
		return "<No Symbol>";
	}
	return symbol.getName();
}
 
Example 19
Source File: SymbolColumnTypeMapper.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public String convert(Symbol symbol) {
	return symbol.getName();
}
 
Example 20
Source File: VTMatchOneToManyTableProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public void loadLocalInfo(Address infoAddress) {
	if (infoAddress == null) {
		labelValue.setText("");
		labelTypeValue.setText("");
		addressValue.setText("");
		return;
	}

	VTSession session = controller.getSession();
	Program program = (isSource ? session.getSourceProgram() : session.getDestinationProgram());

	// LABEL,
	SymbolTable symbolTable = program.getSymbolTable();
	Symbol symbol = symbolTable.getPrimarySymbol(infoAddress);
	String labelValueText;
	if (symbol == null) {
		labelValueText = "<No Symbol>";
	}
	else {
		labelValueText = symbol.getName();
	}
	labelValue.setText(labelValueText);

	// LABEL_SOURCE,
	String labelTypeValueText;
	if (symbol == null) {
		labelTypeValueText = "<none>";
	}
	else {
		labelTypeValueText = symbol.getSource().getDisplayString();
	}
	labelTypeValue.setText(labelTypeValueText);

	// ADDRESS
	DisplayableListingAddress displayableAddress =
		new DisplayableListingAddress(program, infoAddress);
	addressValue.setText(displayableAddress.getDisplayString());

	localPanel.validate();
	localPanel.invalidate();
}