Java Code Examples for ghidra.program.model.address.Address#isMemoryAddress()

The following examples show how to use ghidra.program.model.address.Address#isMemoryAddress() . 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: EditReferencesModel.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
 */
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
	if (columnIndex == IS_PRIMARY_COL || columnIndex == REF_TYPE_COL) {
		if (rowIndex >= refs.length) {
			return false;
		}
		Address toAddr = refs[rowIndex].getToAddress();
		if (toAddr.isMemoryAddress()) {
			return true;
		}
		if (columnIndex == REF_TYPE_COL) {
			return true;
		}
	}
	return false;
}
 
Example 2
Source File: NextPrevAddressPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private List<Symbol> doBulkGoTo() throws Exception {
	List<Symbol> list = new ArrayList<>();
	Memory memory = program.getMemory();
	int count = 0;
	SymbolIterator iter = program.getSymbolTable().getAllSymbols(true);
	while (iter.hasNext() && count < 11) {
		Symbol symbol = iter.next();
		Address addr = symbol.getAddress();
		if ((addr.isMemoryAddress() && !memory.contains(addr)) || addr.isExternalAddress()) {
			continue;
		}
		list.add(symbol);
		goTo(symbol);
		++count;
	}
	return list;
}
 
Example 3
Source File: VariableImpl.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private VariableStorage computeStorage(Address storageAddr) throws InvalidInputException {
	if (storageAddr == null) {
		return VariableStorage.UNASSIGNED_STORAGE;
	}
	if (!storageAddr.isMemoryAddress() && !storageAddr.isRegisterAddress() &&
		!storageAddr.isStackAddress() && !storageAddr.isHashAddress()) {
		throw new InvalidInputException("Invalid storage address specified: space=" +
			storageAddr.getAddressSpace().getName());
	}
	int dtLength = dataType.getLength();
	if (!storageAddr.isStackAddress()) {
		return new VariableStorage(program, storageAddr, dtLength);
	}

	long stackOffset = storageAddr.getOffset();
	if (stackOffset < 0 && -stackOffset < dtLength) {
		// do not allow stack element to span the 0-offset 
		// i.e., maintain separation of locals and params
		throw new InvalidInputException(
			"Data type does not fit within stack frame constraints (stack offset=" +
				stackOffset + ", size=" + dtLength);
	}
	return new VariableStorage(program, new Varnode(storageAddr, dtLength));
}
 
Example 4
Source File: RenameGlobalAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected void decompilerActionPerformed(DecompilerActionContext context) {
	PluginTool tool = context.getTool();
	final ClangToken tokenAtCursor = context.getTokenAtCursor();
	HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());
	Address addr = null;
	if (highSymbol instanceof HighCodeSymbol) {
		addr = ((HighCodeSymbol) highSymbol).getStorage().getMinAddress();
	}
	if (addr == null || !addr.isMemoryAddress()) {
		Msg.showError(this, tool.getToolFrame(), "Rename Failed",
			"Memory storage not found for global variable");
		return;
	}
	RenameGlobalVariableTask nameTask =
		new RenameGlobalVariableTask(tool, context.getProgram(), context.getDecompilerPanel(),
			tokenAtCursor, addr);
	nameTask.runTask(true);
}
 
Example 5
Source File: FunctionNameStringable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void applyFunctionName(Program program, Function function)
		throws DuplicateNameException, InvalidInputException, CircularDependencyException {

	Address entryPoint = function.getEntryPoint();
	if (entryPoint.isMemoryAddress() && sourceType == SourceType.DEFAULT) {
		// Apply a default name by removing the current one.
		program.getSymbolTable().removeSymbolSpecial(function.getSymbol());
		return;
	}

	SymbolTable symbolTable = program.getSymbolTable();
	Namespace namespace = program.getGlobalNamespace();
	for (NamespaceInfo info : namespaceInfos) {
		Namespace ns = symbolTable.getNamespace(info.name, namespace);
		if (ns != null) {
			if (function.isExternal() != ns.isExternal()) {
				throw new DuplicateNameException("Conflicting namespace: " + info.name);
			}
			if (info.symbolType == SymbolType.CLASS &&
				ns.getSymbol().getSymbolType() == SymbolType.NAMESPACE) {
				// Promote existing namespace to class
				ns = NamespaceUtils.convertNamespaceToClass(ns);
			}
			namespace = ns;
		}
		else {
			namespace = createNamespace(program, info, namespace);
		}
	}

	Symbol s = function.getSymbol();
	s.setNameAndNamespace(symbolName, namespace, sourceType);
}
 
Example 6
Source File: AbstractVTMatchTableModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(GTableCellRenderingData data) {

	Object value = data.getValue();

	DisplayableListingAddress displayableAddress =
		(DisplayableListingAddress) value;
	String addressString = displayableAddress.getDisplayString();

	GTableCellRenderingData renderData = data.copyWithNewValue(addressString);

	JLabel renderer = (JLabel) super.getTableCellRendererComponent(renderData);

	Program program = displayableAddress.getProgram();
	Address address = displayableAddress.getAddress();
	if (!address.isMemoryAddress() && symbolInspector != null) {
		Symbol s = program.getSymbolTable().getPrimarySymbol(address);
		symbolInspector.setProgram(program);
		Color c = (s != null) ? symbolInspector.getColor(s) : Color.RED;
		setForeground(c);
	}
	else if (!program.getMemory().contains(address)) {
		setForeground(Color.RED);
	}

	renderer.setOpaque(true);

	return renderer;
}
 
Example 7
Source File: AddEditDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public void addLabel(Address address, Program p, Component centeredOverComponent) {
	if (!address.isMemoryAddress()) {
		throw new IllegalArgumentException(
			"AddEditDialog.addLabel only valid for memory address");
	}
	this.addr = address;
	this.program = p;
	SymbolTable symbolTable = program.getSymbolTable();
	symbol = null;
	setTitle("Add Label at " + address);
	initRecentChoices();
	entryPointCheckBox.setEnabled(true);
	entryPointCheckBox.setSelected(symbolTable.isExternalEntryPoint(address));
	pinnedCheckBox.setEnabled(true);
	pinnedCheckBox.setSelected(false);

	Symbol[] symbols = symbolTable.getSymbols(address);

	FunctionSymbol functionSymbol = getFunctionSymbol(address);
	if (functionSymbol == null && symbols.length == 0) {
		primaryCheckBox.setSelected(true);
		primaryCheckBox.setEnabled(false);
	}
	else {
		primaryCheckBox.setSelected(false);
		primaryCheckBox.setEnabled(true);
	}

	namespaceChoices.setEnabled(true);
	initNamespaces();
	clearStatusText();
	tool.showDialog(this, centeredOverComponent);
}
 
Example 8
Source File: EditReferenceDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void configureEditReference(CodeUnit cu, Reference ref) {
	setTitle("Edit Reference");
	setHelpLocation(EDIT_HELP);

	applyButton.setText("Update");

	memRefChoice.setEnabled(false);
	extRefChoice.setEnabled(false);
	stackRefChoice.setEnabled(false);
	regRefChoice.setEnabled(false);

	Address toAddress = ref.getToAddress();
	if (toAddress.isRegisterAddress() || cu.getProgram().getRegister(toAddress) != null) {
		regRefPanel.initialize(cu, ref);
		regRefChoice.setSelected(true);
		regRefChoice.setEnabled(true);
		if (toAddress.isMemoryAddress()) {
			memRefPanel.initialize(cu, ref);
			memRefChoice.setEnabled(true);
		}
	}
	else if (toAddress.isStackAddress()) {
		stackRefPanel.initialize(cu, ref);
		stackRefChoice.setSelected(true);
		stackRefChoice.setEnabled(true);
	}
	else if (toAddress.isMemoryAddress()) {
		memRefPanel.initialize(cu, ref);
		memRefChoice.setSelected(true);
		memRefChoice.setEnabled(true);
	}
	else if (toAddress.isExternalAddress()) {
		extRefPanel.initialize(cu, ref);
		extRefChoice.setSelected(true);
		extRefChoice.setEnabled(true);
	}
	else {
		throw new AssertException("Unknown address type");
	}
}
 
Example 9
Source File: GoToPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testSaveRestoreState() throws Exception {
	int maxEntries = plugin.getMaximumGotoEntries();

	loadProgram("x86.exe");
	Memory memory = program.getMemory();

	int count = 0;
	SymbolIterator iter = program.getSymbolTable().getAllSymbols(true);
	while (iter.hasNext() && count < 30) {
		Symbol symbol = iter.next();
		Address addr = symbol.getAddress();
		if ((addr.isMemoryAddress() && !memory.contains(addr)) || addr.isExternalAddress()) {
			continue;
		}
		setText(symbol.getName());
		performOkCallback();
		++count;
	}

	SaveState saveState = new SaveState("test");
	plugin.writeDataState(saveState);

	plugin.readDataState(saveState);

	GhidraComboBox<?> combo = findComponent(dialog, GhidraComboBox.class);
	assertNotNull(combo);
	assertEquals(maxEntries, combo.getModel().getSize());
}
 
Example 10
Source File: InstructionPanel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the color to use to render the specified operand
 */
private Color getOperandColor(int opIndex) {

	Program program = currentCodeUnit.getProgram();

	// rely on primary reference if available as this should be the basis of 
	// the formatted operand representation.
	Reference ref = currentCodeUnit.getPrimaryReference(opIndex);
	Address refAddr = ref != null ? ref.getToAddress() : currentCodeUnit.getAddress(opIndex);

	if (refAddr == null) {
		return DEFAULT_FG_COLOR;
	}

	if (refAddr.isMemoryAddress() && !program.getMemory().contains(refAddr)) {
		return NOT_IN_MEMORY_COLOR;
	}

	SymbolTable st = program.getSymbolTable();
	Symbol sym = st.getSymbol(ref);
	if (sym != null) {
		symbolInspector.setProgram(program);
		return symbolInspector.getColor(sym);
	}

	return DEFAULT_FG_COLOR;
}
 
Example 11
Source File: MatchSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static SymbolPath getCleanSymbolPath(SymbolPath path, Address address) {
	if (!address.isMemoryAddress()) {
		return path;
	}
	String symbolName = path.getName();
	String cleanName = SymbolUtilities.getCleanSymbolName(symbolName, address);
	if (!cleanName.equals(symbolName)) {
		return new SymbolPath(path.getParent(), cleanName);
	}
	return path;
}
 
Example 12
Source File: SymbolInspector.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if symbol is at a non-existent address
 * @param s the symbol to check
 * @return boolean true if symbol is bad
 */
public boolean isBadReferenceSymbol(Symbol s) {
	if (memory == null) {
		return true;
	}
	Address a = s.getAddress();
	if (a.isMemoryAddress()) {
		return !memory.contains(s.getAddress());
	}
	return false;
}
 
Example 13
Source File: OperandFieldHelper.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the font and color to use to render an operand when that operand
 * is a reference.
 */
private ColorStyleAttributes getAddressAttributes(CodeUnit cu, Address destAddr, int opIndex,
		Program program) {

	if (destAddr == null) {
		return separatorAttributes;
	}

	if (destAddr.isMemoryAddress() && !program.getMemory().contains(destAddr)) {
		return badRefAttributes;
	}

	SymbolTable st = program.getSymbolTable();
	ReferenceManager refMgr = program.getReferenceManager();

	Reference ref = refMgr.getReference(cu.getMinAddress(), destAddr, opIndex);
	Symbol sym = st.getSymbol(ref);
	if (sym != null) {
		inspector.setProgram(program);
		ColorStyleAttributes newAttributes = new ColorStyleAttributes();
		ColorAndStyle c = inspector.getColorAndStyle(sym);
		newAttributes.colorAttribute = c.getColor();
		newAttributes.styleAttribute = c.getStyle();
		return newAttributes;
	}
	return addressAttributes;
}
 
Example 14
Source File: AbstractVTMatchTableModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(GTableCellRenderingData data) {

	Object value = data.getValue();

	DisplayableListingAddress displayableAddress =
		(DisplayableListingAddress) value;
	String addressString = displayableAddress.getDisplayString();

	GTableCellRenderingData renderData = data.copyWithNewValue(addressString);

	JLabel renderer = (JLabel) super.getTableCellRendererComponent(renderData);

	Program program = displayableAddress.getProgram();
	Address address = displayableAddress.getAddress();
	if (!address.isMemoryAddress() && symbolInspector != null) {
		Symbol s = program.getSymbolTable().getPrimarySymbol(address);
		symbolInspector.setProgram(program);
		Color c = (s != null) ? symbolInspector.getColor(s) : Color.RED;
		setForeground(c);
	}
	else if (!program.getMemory().contains(address)) {
		setForeground(Color.RED);
	}

	renderer.setOpaque(true);

	return renderer;
}
 
Example 15
Source File: MemoryBlock.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Determine if the specified address is contained within the reserved EXTERNAL block.
 * @param address address of interest
 * @param program
 * @return true if address is contained within the reserved EXTERNAL block, else false.
 */
public static boolean isExternalBlockAddress(Address address, Program program) {
	Memory memory = program.getMemory();
	if (!address.isMemoryAddress()) {
		return false;
	}
	MemoryBlock block = memory.getBlock(address);
	return block != null && MemoryBlock.EXTERNAL_BLOCK_NAME.equals(block.getName());
}
 
Example 16
Source File: AddressBasedTableModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramSelection getProgramSelection(int[] rows) {
	AddressSet addressSet = new AddressSet();
	for (int element : rows) {
		Address addr = getAddress(element);
		if (addr.isMemoryAddress()) {
			addressSet.addRange(addr, addr);
		}
	}
	return new ProgramSelection(addressSet);
}
 
Example 17
Source File: ExternalLocationDB.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void setLocation(String label, Address addr, SourceType source)
		throws InvalidInputException {
	if (label != null && label.length() == 0) {
		label = null;
	}
	if (label == null && addr == null) {
		throw new InvalidInputException("Either an external label or address is required");
	}
	if (addr != null && !addr.isMemoryAddress()) {
		throw new InvalidInputException("Invalid memory address");
	}
	setLabel(label, source);
	setAddress(addr);
}
 
Example 18
Source File: OperandFieldMouseHandler.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private boolean checkMemRefs(Navigatable navigatable, CodeUnit codeUnit,
		OperandFieldLocation loc, ServiceProvider serviceProvider) {

	Address refAddr = loc.getRefAddress();
	if (refAddr == null || !refAddr.isMemoryAddress()) {
		return false;
	}

	Reference[] refs = codeUnit.getOperandReferences(loc.getOperandIndex());
	Address[] addrs = getAddressesForReferences(refs, codeUnit, serviceProvider);
	if (addrs.length == 0) {
		return false;
	}

	if (addrs.length > 1) {
		List<OutgoingReferenceEndpoint> outgoingReferences = new ArrayList<>();
		for (int i = 0; i < refs.length; i++) {
			Reference ref = refs[i];
			boolean offcut = ReferenceUtils.isOffcut(codeUnit.getProgram(), ref.getToAddress());
			outgoingReferences.add(new OutgoingReferenceEndpoint(ref, addrs[i], offcut));
		}

		IncomingReferencesTableModel model = new IncomingReferencesTableModel("Operand",
			serviceProvider, codeUnit.getProgram(), outgoingReferences, null);
		TableService service = serviceProvider.getService(TableService.class);

		Navigatable nav = NavigationUtils.getActiveNavigatable();
		String addressString = codeUnit.getMinAddress().toString();
		service.showTable("Operand References for " + addressString, "Operands", model,
			"References", nav);
		return true;
	}

	// 1 address found
	Address gotoAddr = addrs[0];
	if (gotoAddr == null) {
		return false;
	}

	GoToService goToService = serviceProvider.getService(GoToService.class);
	return goToService.goTo(navigatable, codeUnit.getProgram(), gotoAddr,
		codeUnit.getAddress());
}
 
Example 19
Source File: DeleteReferencesAction.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnabledForContext(ListingActionContext context) {

	boolean actionOK = false;

	ProgramLocation loc = context.getLocation();
	if (!(loc instanceof CodeUnitLocation)) {
		return false;
	}

	getPopupMenuData().setMenuItemName(DEFAULT_MENU_ITEM_NAME);

	int opIndex;
	if (loc instanceof MnemonicFieldLocation) {
		opIndex = ReferenceManager.MNEMONIC;
	}
	else if (loc instanceof OperandFieldLocation) {
		opIndex = ((OperandFieldLocation) loc).getOperandIndex();
	}
	else {
		setEnabled(false);
		return false;
	}

	Reference[] refs = context.getProgram().getReferenceManager().getReferencesFrom(
		context.getAddress(), opIndex);
	if (refs.length != 0) {
		actionOK = true;
		Address toAddr = refs[0].getToAddress();
		if (toAddr.isMemoryAddress()) {
			getPopupMenuData().setMenuItemName(MEMORY_MENU_ITEM_NAME);
		}
		else if (toAddr.isExternalAddress()) {
			getPopupMenuData().setMenuItemName(EXTERNAL_MENU_ITEM_NAME);
		}
		else if (refs[0].isStackReference()) {
			getPopupMenuData().setMenuItemName(STACK_MENU_ITEM_NAME);
		}
		else if (refs[0].getToAddress().isRegisterAddress()) {
			getPopupMenuData().setMenuItemName(REGISTER_MENU_ITEM_NAME);
		}
		else {
			actionOK = false;
		}
	}
	return actionOK;
}
 
Example 20
Source File: SymbolType.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isValidAddress(Program program, Address symbolAddress) {
	return symbolAddress.isMemoryAddress() || symbolAddress.isExternalAddress();
}