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

The following examples show how to use ghidra.program.model.symbol.Symbol#getAddress() . 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: VariableLocation.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Address getVariableAddress(Variable var) {
	Symbol sym = var.getSymbol();
	if (sym == null) {
		return Address.NO_ADDRESS; // auto-params have no symbol
	}
	if (sym.getProgram() != program) {
		// Attempt to locate corresponding variable symbol within the current program
		// to allow for use in Diff operations
		Symbol otherSym = SimpleDiffUtility.getVariableSymbol(sym, program);
		if (otherSym != null) {
			return otherSym.getAddress();
		}
		return Address.NO_ADDRESS;
	}
	return sym.getAddress();
}
 
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: 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 4
Source File: RISCVAddressAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Check for a global GP register symbol or discovered symbol
 * @param program
 * @param set
 * @param monitor
 */
private void checkForGlobalGP(Program program, AddressSetView set, TaskMonitor monitor) {
	Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program,
			RISCV___GLOBAL_POINTER,
			err -> Msg.error(this, err));
	if (symbol != null) {
		gp_assumption_value = symbol.getAddress();
		return;
	}
}
 
Example 5
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 6
Source File: SymbolRowObjectToAddressTableRowMapper.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public Address map(Symbol rowObject, Program data, ServiceProvider serviceProvider) {
	if (rowObject == null) {
		return null;
	}
	return rowObject.getAddress();
}
 
Example 7
Source File: ShowSymbolReferencesAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private ProgramLocation getProgramLocation(SymbolNode symbolNode) {
	Symbol symbol = symbolNode.getSymbol();
	if (symbol instanceof FunctionSymbol) {
		return new FunctionSignatureFieldLocation(symbol.getProgram(), symbol.getAddress());
	}
	return symbol.getProgramLocation();
}
 
Example 8
Source File: ProgramManagerPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean gotoProgramRef(Program program, String ref) {
	if (ref == null) {
		return false;
	}

	String trimmedRef = ref.trim();
	if (trimmedRef.length() == 0) {
		return false;
	}
	List<Symbol> symbols = NamespaceUtils.getSymbols(trimmedRef, program);
	Symbol sym = symbols.isEmpty() ? null : symbols.get(0);

	ProgramLocation loc = null;
	if (sym != null) {
		SymbolType type = sym.getSymbolType();
		if (type == SymbolType.FUNCTION) {
			loc = new FunctionSignatureFieldLocation(sym.getProgram(), sym.getAddress());
		}
		else if (type == SymbolType.LABEL) {
			loc = new LabelFieldLocation(sym);
		}
	}
	else {
		Address addr = program.getAddressFactory().getAddress(trimmedRef);
		if (addr != null && addr.isMemoryAddress()) {
			loc = new CodeUnitLocation(program, addr, 0, 0, 0);
		}
	}
	if (loc == null) {
		Msg.showError(this, null, "Navigation Failed",
			"Referenced label/function not found: " + trimmedRef);
		return false;
	}

	firePluginEvent(new ProgramLocationPluginEvent(getName(), loc, program));

	return true;
}
 
Example 9
Source File: AddressEvaluatorTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
   public void testEval() throws Exception {
	Program p = createDefaultProgram("Test", ProgramBuilder._TOY_LE, this);
	addrFactory = p.getAddressFactory();
	int txId = p.startTransaction("Test");
	try {
		assertEquals(addr("0x19"), AddressEvaluator.evaluate(p, "(2+3)*5"));
		assertEquals(addr("0x11"), AddressEvaluator.evaluate(p, "2+3*5"));
		assertEquals(addr("0x11"), AddressEvaluator.evaluate(p, "2+(3*5)"));
		assertEquals(addr("0x11"), AddressEvaluator.evaluate(p, "(2+3*5)"));
		assertEquals(addr("0x16"), AddressEvaluator.evaluate(p, "0x11+5"));
		assertEquals(addr("0x5"), AddressEvaluator.evaluate(p, "5"));
		assertEquals(addr("0x3"), AddressEvaluator.evaluate(p, "0-5+8"));
		assertEquals(addr("0x3"), AddressEvaluator.evaluate(p, "-5+8"));
		assertEquals(addr("0xfffffffB"), AddressEvaluator.evaluate(p, "-5"));
		assertEquals(addr("0x11"), AddressEvaluator.evaluate(p, "3+(5+(3*2)+(3))"));
		assertEquals(addr("0xff00"), AddressEvaluator.evaluate(p, "0xffff ^ 0xff"));
		assertEquals(addr("0x123f"), AddressEvaluator.evaluate(p, "0xffff & 0x123f"));
		assertEquals(addr("0x1234"), AddressEvaluator.evaluate(p, "0x1200 | 0x0034"));
		assertEquals(addr("0xffffffff"), AddressEvaluator.evaluate(p, "~ 0x0"));
		assertEquals(addr("0x1201"), AddressEvaluator.evaluate(p, "0x1200 | ~(0xfffffffe)"));
		Symbol s = p.getSymbolTable().createLabel(addr("0x100"), "entry", SourceType.IMPORTED);
		Address a = s.getAddress();
		a = a.add(10);
		assertEquals(a, AddressEvaluator.evaluate(p, "entry+5*2"));
	}
	finally {
		p.endTransaction(txId, true);
		p.release(this);
	}
}
 
Example 10
Source File: MipsR5900AddressAnalyzer.java    From ghidra-emotionengine with Apache License 2.0 4 votes vote down vote up
/**
 * Check for a global GP register symbol or discovered symbol
 * @param set
 */
private void checkForGlobalGP(Program program, AddressSetView set, TaskMonitor monitor) {
	// don't want to check for it
	if (!discoverGlobalGPSetting) {
		return;
	}

	// TODO: Use gp_value provided by MIPS .reginfo or dynamic attributes - check for Elf loader symbol
	// see MIPS_ElfExtension.MIPS_GP_VALUE_SYMBOL
	Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, "_mips_gp_value",
		err -> Msg.error(this, err));
	if (symbol != null) {
		gp_assumption_value = symbol.getAddress();
		return;
	}

	if (set != null && !set.isEmpty()) {
		// if GP is already Set, don't go looking for a value.
		AddressRangeIterator registerValueAddressRanges =
			program.getProgramContext().getRegisterValueAddressRanges(gp);
		while (registerValueAddressRanges.hasNext()) {
			// but set it so we know if the value we are assuming actually changes
			AddressRange next = registerValueAddressRanges.next();
			if (set.contains(next.getMinAddress(), next.getMaxAddress())) {
				RegisterValue registerValue =
					program.getProgramContext().getRegisterValue(gp, next.getMinAddress());
				gp_assumption_value = next.getMinAddress().getNewAddress(
					registerValue.getUnsignedValue().longValue());
				return;
			}
		}
	}

	// look for the global _gp variable set by ELF binaries

	symbol =
		SymbolUtilities.getLabelOrFunctionSymbol(program, "_gp", err -> Msg.error(this, err));
	if (symbol == null) {
		symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, "_GP",
			err -> Msg.error(this, err));
	}

	if (symbol != null) {
		gp_assumption_value = symbol.getAddress();
	}

	// look for any setting of _gp_# variables
	Symbol s1 =
		SymbolUtilities.getLabelOrFunctionSymbol(program, "_gp_1", err -> Msg.error(this, err));
	if (s1 == null) {
		return;
	}
	// if we found a _gp symbol we set, and there is a global symbol, something is amiss
	if (gp_assumption_value != null && s1.getAddress().equals(gp_assumption_value)) {
		gp_assumption_value = null;
		return;
	}
	Symbol s2 =
		SymbolUtilities.getLabelOrFunctionSymbol(program, "_gp_2", err -> Msg.error(this, err));
	if (s2 == null) {
		// if there is only 1, assume can use the value for now
		gp_assumption_value = s1.getAddress();
	}
	return;
}
 
Example 11
Source File: LabelSearchAddressIterator.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Address next() {
	Symbol symbol = labelIterator.next();
	return symbol.getAddress();
}
 
Example 12
Source File: SymbolToAddressTableRowMapper.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public Address map( Symbol rowObject, Program program, ServiceProvider serviceProvider ) {
    return rowObject.getAddress();
}
 
Example 13
Source File: CopyPasteFunctionInfoTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testPasteFunctionName() throws Exception {

	// in notepad (Browser(1)) copy ghidra function to
	// taskman (Browser(2) at address 1004700
	Symbol symbol = getUniqueSymbol(programOne, "ghidra");
	Address addr = symbol.getAddress();
	goToAddr(toolOne, addr);
	click1();
	toolOne.firePluginEvent(
		new ProgramSelectionPluginEvent("test", new ProgramSelection(addr, addr), programOne));

	ClipboardPlugin plugin = getPlugin(toolOne, ClipboardPlugin.class);
	ClipboardContentProviderService service =
		getCodeBrowserClipboardContentProviderService(plugin);
	plugin.copySpecial(service, CodeBrowserClipboardProvider.LABELS_COMMENTS_TYPE);

	// go to address 01004700 in Browser(2)
	goToAddr(toolTwo, 0x1004700);
	click2();

	// paste
	plugin = getPlugin(toolTwo, ClipboardPlugin.class);
	DockingActionIf pasteAction = getAction(plugin, "Paste");
	assertEnabled(pasteAction);
	performAction(pasteAction, true);
	waitForSwing();

	// function FUN_01004700 should be renamed to "ghidra"
	CodeBrowserPlugin cb = getPlugin(toolTwo, CodeBrowserPlugin.class);
	cb.goToField(getAddr(programTwo, 0x01004700), LabelFieldFactory.FIELD_NAME, 0, 0);
	ListingTextField f = (ListingTextField) cb.getCurrentField();
	assertEquals("ghidra", f.getText());

	undo(programTwo);
	cb.goToField(getAddr(programTwo, 0x01004700), LabelFieldFactory.FIELD_NAME, 0, 0);
	f = (ListingTextField) cb.getCurrentField();
	assertEquals("FUN_01004700", f.getText());

	redo(programTwo);
	cb.goToField(getAddr(programTwo, 0x01004700), LabelFieldFactory.FIELD_NAME, 0, 0);
	f = (ListingTextField) cb.getCurrentField();
	assertEquals("ghidra", f.getText());
}
 
Example 14
Source File: CopyPasteFunctionInfoTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testPasteFunctionComment() throws Exception {

	// in Browser(1) select the entry address
	Address addr = getAddr(programOne, 0x01006420);
	goToAddr(toolOne, addr);
	click1();
	toolOne.firePluginEvent(
		new ProgramSelectionPluginEvent("test", new ProgramSelection(addr, addr), programOne));

	ClipboardPlugin plugin = getPlugin(toolOne, ClipboardPlugin.class);
	ClipboardContentProviderService service =
		getCodeBrowserClipboardContentProviderService(plugin);
	plugin.copySpecial(service, CodeBrowserClipboardProvider.LABELS_COMMENTS_TYPE);

	// in Browser(2) go to entry in taskman
	Symbol symbol = getUniqueSymbol(programTwo, "entry");
	Address entryAddr = symbol.getAddress();
	goToAddr(toolTwo, entryAddr);
	click2();

	// paste
	plugin = getPlugin(toolTwo, ClipboardPlugin.class);
	DockingActionIf pasteAction = getAction(plugin, "Paste");
	assertEnabled(pasteAction);
	performAction(pasteAction, true);
	waitForSwing();

	CodeBrowserPlugin cb = getPlugin(toolTwo, CodeBrowserPlugin.class);
	cb.goToField(entryAddr, PlateFieldFactory.FIELD_NAME, 0, 0);
	ListingTextField f = (ListingTextField) cb.getCurrentField();
	assertEquals(4, f.getNumRows());
	assertTrue(f.getText().indexOf("FUNCTION") > 0);
	assertTrue(f.getText().indexOf("My function comments for entry") > 0);

	undo(programTwo);
	cb.goToField(entryAddr, PlateFieldFactory.FIELD_NAME, 0, 0);
	f = (ListingTextField) cb.getCurrentField();
	assertEquals(3, f.getNumRows());
	assertTrue(f.getText().indexOf("FUNCTION") > 0);

	redo(programTwo);
	cb.goToField(entryAddr, PlateFieldFactory.FIELD_NAME, 0, 0);
	f = (ListingTextField) cb.getCurrentField();
	assertEquals(4, f.getNumRows());
	assertTrue(f.getText().indexOf("FUNCTION") > 0);
	assertTrue(f.getText().indexOf("My function comments for entry") > 0);

}