Java Code Examples for ghidra.program.model.listing.Function#getSymbol()

The following examples show how to use ghidra.program.model.listing.Function#getSymbol() . 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: DecompilerProvider.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void goToFunction(Function function, boolean newWindow) {

	GoToService service = tool.getService(GoToService.class);
	if (service == null) {
		return;
	}

	Navigatable navigatable = this;
	if (newWindow) {
		DecompilerProvider newProvider = plugin.createNewDisconnectedProvider();
		navigatable = newProvider;
	}

	Symbol symbol = function.getSymbol();
	ExternalManager externalManager = program.getExternalManager();
	ExternalLocation externalLocation = externalManager.getExternalLocation(symbol);
	if (externalLocation != null) {
		service.goToExternalLocation(navigatable, externalLocation, true);
	}
	else {
		Address address = function.getEntryPoint();
		service.goTo(navigatable, new ProgramLocation(program, address), program);
	}
}
 
Example 2
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteDefaultFunctionLabel() throws Exception {
	// ensure that default function label can not be deleted

	Address addr = addr("0x1");

	String defaultName = SymbolUtilities.getDefaultFunctionName(addr);
	Function function = createFunction(addr);
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertTrue(functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals(1, symbols.length);
	assertEquals(defaultName, symbols[0].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(false, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals(primarySymbol, functionSymbol);
}
 
Example 3
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteOnlyFunctionLabel() throws Exception {

	Address addr = addr("0x1");

	String defaultName = SymbolUtilities.getDefaultFunctionName(addr);
	Function function = createFunction(addr, "MyFunction");
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertEquals(false, functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals("MyFunction", functionSymbol.getName());
	assertEquals(1, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(true, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals(defaultName, primarySymbol.getName());
	function = getFunction(addr);
	assertNotNull(function);
	assertEquals(defaultName, function.getSymbol().getName());
}
 
Example 4
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteOtherLabelWhereFunction() throws Exception {

	Address addr = addr("0x1");

	Function function = createFunction(addr, "MyFunction");
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertEquals(false, functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals("MyFunction", functionSymbol.getName());
	assertEquals(1, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());

	createSymbol(addr, "OtherSymbol");
	symbols = getSymbols(addr);
	assertEquals(2, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());
	assertEquals(true, symbols[0].isPrimary());
	assertEquals("OtherSymbol", symbols[1].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, "OtherSymbol");
	assertEquals(true, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals("MyFunction", primarySymbol.getName());
	function = getFunction(addr);
	assertNotNull(function);
	assertEquals("MyFunction", function.getSymbol().getName());
}
 
Example 5
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteFunctionLabelWhereMultiple() throws Exception {

	Address addr = addr("0x1");

	Function function = createFunction(addr, "MyFunction");
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertEquals(false, functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals("MyFunction", functionSymbol.getName());
	assertEquals(1, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());

	createSymbol(addr, "OtherSymbol");
	symbols = getSymbols(addr);
	assertEquals(2, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());
	assertEquals(true, symbols[0].isPrimary());
	assertEquals("OtherSymbol", symbols[1].getName());

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(true, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals("OtherSymbol", primarySymbol.getName());
	function = getFunction(addr);
	assertNotNull(function);
	assertEquals("OtherSymbol", function.getSymbol().getName());
}
 
Example 6
Source File: PinnedSymbolTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void createPinnedFunctionSymbol()
		throws DuplicateNameException, InvalidInputException, OverlappingFunctionException {
	Address addr = addr(0xc);
	AddressSet set = new AddressSet(addr);
	Function fun = program.getFunctionManager().createFunction("MyFunction", addr, set,
		SourceType.USER_DEFINED);
	Symbol symbol = fun.getSymbol();
	symbol.setPinned(true);
}
 
Example 7
Source File: NXProgramBuilder.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
private void evaluateElfSymbol(ElfSymbol elfSymbol, Address address, boolean isFakeExternal)
{
    try
    {
        if (elfSymbol.isSection()) {
            // Do not add section symbols to program symbol table
            return;
        }

        String name = elfSymbol.getNameAsString();
        if (name == null || name.isEmpty()) {
            return;
        }

        boolean isPrimary = (elfSymbol.getType() == ElfSymbol.STT_FUNC) ||
            (elfSymbol.getType() == ElfSymbol.STT_OBJECT) || (elfSymbol.getSize() != 0);
        // don't displace existing primary unless symbol is a function or object symbol
        if (name.contains("@")) {
            isPrimary = false; // do not make version symbol primary
        }
        else if (!isPrimary && (elfSymbol.isGlobal() || elfSymbol.isWeak())) {
            Symbol existingSym = program.getSymbolTable().getPrimarySymbol(address);
            isPrimary = (existingSym == null);
        }

        this.createSymbol(address, name, isPrimary, elfSymbol.isAbsolute(), null);

        // NOTE: treat weak symbols as global so that other programs may link to them.
        // In the future, we may want additional symbol flags to denote the distinction
        if ((elfSymbol.isGlobal() || elfSymbol.isWeak()) && !isFakeExternal) 
        {
            program.getSymbolTable().addExternalEntryPoint(address);
        }
        
        if (elfSymbol.getType() == ElfSymbol.STT_FUNC) 
        {
            Function existingFunction = program.getFunctionManager().getFunctionAt(address);
            if (existingFunction == null) {
                Function f = createOneByteFunction(null, address, false);
                if (f != null) {
                    if (isFakeExternal && !f.isThunk()) {
                        ExternalLocation extLoc = program.getExternalManager().addExtFunction(Library.UNKNOWN, name, null, SourceType.IMPORTED);
                        f.setThunkedFunction(extLoc.getFunction());
                        // revert thunk function symbol to default source
                        Symbol s = f.getSymbol();
                        if (s.getSource() != SourceType.DEFAULT) {
                            program.getSymbolTable().removeSymbolSpecial(f.getSymbol());
                        }
                    }
                }
            }
        }
    }
    catch (DuplicateNameException | InvalidInputException e)
    {
        e.printStackTrace();
    }
}
 
Example 8
Source File: ExternalManagerDB.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private ExternalLocation addExtLocation(Namespace extNamespace, String extLabel,
		Address extAddr, boolean isFunction, SourceType sourceType, boolean reuseExisting)
		throws InvalidInputException, DuplicateNameException {
	if (extNamespace == null) {
		extNamespace = getLibraryScope(Library.UNKNOWN);
		if (extNamespace == null) {
			extNamespace = addExternalLibraryName(Library.UNKNOWN, SourceType.ANALYSIS);
		}
	}
	else if (!extNamespace.isExternal()) {
		throw new InvalidInputException("The namespace must be an external namespace.");
	}
	sourceType = checkExternalLabel(extLabel, extAddr, sourceType);
	if (extAddr != null && !extAddr.isLoadedMemoryAddress()) {
		throw new InvalidInputException("Invalid memory address");
	}
	lock.acquire();
	try {
		if (sourceType == SourceType.DEFAULT) {
			extLabel = null;
		}
		ExternalLocationDB extLoc =
			(ExternalLocationDB) getExtLocation(extNamespace, extLabel, extAddr, reuseExisting);

		if (extLoc != null) {
			// if there is already a location with the address, then we must use it
			if (extAddr != null || reuseExisting) {
				if (extLabel != null && !extLabel.equals(extLoc.getLabel())) {
					extLoc.setLabel(extLabel, sourceType);
				}
				if (isFunction) {
					// transform to a function if needed
					extLoc = (ExternalLocationDB) createFunction(extLoc).getExternalLocation();
				}
				return extLoc;
			}
		}
		// ok can't or don't want to reuse an existing one, so make a new one.
		SymbolDB s;
		Address externalSpaceAddress = symbolMgr.getNextExternalSymbolAddress();
		String extMemAddrString = (extAddr != null) ? extAddr.toString() : null;
		if (isFunction) {
			Function function = functionMgr.createExternalFunction(externalSpaceAddress,
				extLabel, extNamespace, extMemAddrString, sourceType);
			s = (SymbolDB) function.getSymbol();
		}
		else {
			s = (SymbolDB) symbolMgr.createCodeSymbol(externalSpaceAddress, extLabel,
				extNamespace, sourceType, extMemAddrString);
		}
		return new ExternalLocationDB(this, s);
	}
	finally {
		lock.release();
	}
}
 
Example 9
Source File: DeleteLabelCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFunctionLabelWhereOtherHasRefs() throws Exception {

	Address addr = addr("0x1");

	Function function = createFunction(addr, "MyFunction");
	assertNotNull(function);
	Symbol[] symbols = getSymbols(addr);
	Symbol functionSymbol = function.getSymbol();
	assertEquals(false, functionSymbol.getSource() == SourceType.DEFAULT);
	assertEquals("MyFunction", functionSymbol.getName());
	assertEquals(1, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());

	createSymbol(addr, "ThirdSymbol");
	createSymbol(addr, "OtherSymbol");

	symbols = getSymbols(addr);
	assertEquals(3, symbols.length);
	assertEquals("MyFunction", symbols[0].getName());
	assertEquals(true, symbols[0].isPrimary());
	assertEquals("ThirdSymbol", symbols[1].getName());
	assertEquals("OtherSymbol", symbols[2].getName());

	// Add a reference from 01002257 Operand1 to 01002239
	ReferenceManager refMgr = notepad.getReferenceManager();
	Reference ref;
	int txId = notepad.startTransaction("Add Ref and Set Label");
	try {
		ref = refMgr.addMemoryReference(addr("0x01002257"), addr, RefType.DATA,
			SourceType.USER_DEFINED, 1);
		// Set Label to OtherSymbol for above reference.
		refMgr.setAssociation(symbols[2], ref);
	}
	finally {
		notepad.endTransaction(txId, true);
	}
	Reference[] symRefs = getAssociatedReferences(symbols[2]);
	assertEquals(1, symRefs.length);

	DeleteLabelCmd deleteLabelCmd = new DeleteLabelCmd(addr, function.getName());
	assertEquals(true, applyCmd(notepad, deleteLabelCmd));

	Symbol primarySymbol = symtab.getPrimarySymbol(addr);
	assertNotNull(primarySymbol);
	assertEquals("OtherSymbol", primarySymbol.getName());
	function = getFunction(addr);
	assertNotNull(function);
	assertEquals("OtherSymbol", function.getSymbol().getName());
	symRefs = getAssociatedReferences(primarySymbol);
	assertEquals(0, symRefs.length);
}