Java Code Examples for ghidra.program.model.symbol.SymbolTable#createLabel()

The following examples show how to use ghidra.program.model.symbol.SymbolTable#createLabel() . 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: DiffScreenShots.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createDifferences() throws Exception {
	Project project = env.getProject();
	ProjectData projectData = project.getProjectData();
	DomainFile file = projectData.getRootFolder().getFile("WinHelloCpp.exe");
	Program p = (Program) file.getDomainObject(this, false, false, dummyMonitor);
	int id = p.startTransaction("Test");

	Listing listing = p.getListing();
	listing.clearCodeUnits(addr(0x408dcd), addr(0x408dcd), false);
	SymbolTable symbolTable = p.getSymbolTable();
	symbolTable.createLabel(addr(0x408dd9), "BOB", SourceType.USER_DEFINED);
	symbolTable.createLabel(addr(0x408deb), "EXTRA", SourceType.USER_DEFINED);

	p.endTransaction(id, true);
	p.save("some changes", dummyMonitor);
	p.release(this);
}
 
Example 2
Source File: DiffScreenShots.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createDifferencesAt401417() throws Exception {
	Address addr = addr(0x401417);
	Project project = env.getProject();
	ProjectData projectData = project.getProjectData();
	DomainFile file = projectData.getRootFolder().getFile("WinHelloCpp.exe");
	Program p = (Program) file.getDomainObject(this, false, false, dummyMonitor);
	int id = p.startTransaction("Test");

	Function function = p.getFunctionManager().getFunctionContaining(addr);
	SymbolTable symbolTable = p.getSymbolTable();
	symbolTable.createLabel(addr, "MyLabel", function, SourceType.USER_DEFINED);

	p.endTransaction(id, true);
	p.save("some changes", dummyMonitor);
	p.release(this);

	// now make a similar change in the current program, but use the global namespace
	ProgramManager service = tool.getService(ProgramManager.class);
	p = service.getCurrentProgram();
	id = p.startTransaction("Test");
	symbolTable = p.getSymbolTable();
	symbolTable.createLabel(addr, "MyLabel", SourceType.USER_DEFINED);
	p.endTransaction(id, true);
}
 
Example 3
Source File: MicrosoftDemanglerTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testArrayVariable() throws Exception { // NullPointerException
	String mangled = "?Te@NS1@BobsStuff@@0QAY0BAA@$$CBIA";

	MicrosoftDemangler demangler = new MicrosoftDemangler();
	DemangledObject demangledObject = demangler.demangle(mangled);

	int txID = program.startTransaction("Test");

	SymbolTable st = program.getSymbolTable();
	st.createLabel(addr("01001000"), mangled, SourceType.ANALYSIS);

	DemanglerOptions options = new DemanglerOptions();
	demangledObject.applyTo(program, addr("01001000"), options, TaskMonitor.DUMMY);
	program.endTransaction(txID, false);
}
 
Example 4
Source File: ControlFlowGuard.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Performs markup on the ControlFlowGuard check function, if it exists.
 * 
 * @param lcd The PE LoadConfigDirectory.
 * @param is64bit True if the PE is 64-bit; false if it's 32-bit.
 * @param space The program's address space.
 * @param mem The program's memory.
 * @param symbolTable The program's symbol table.
 */
private static void markupCfgCheckFunction(LoadConfigDirectory lcd, boolean is64bit,
		AddressSpace space,
		Memory mem, SymbolTable symbolTable) {

	if (lcd.getCfgCheckFunctionPointer() == 0) {
		return;
	}

	try {
		Address functionPointerAddr = space.getAddress(lcd.getCfgCheckFunctionPointer());
		Address functionAddr = space.getAddress(
			is64bit ? mem.getLong(functionPointerAddr) : mem.getInt(functionPointerAddr));
		symbolTable.createLabel(functionAddr, "_guard_check_icall", SourceType.IMPORTED);
	}
	catch (MemoryAccessException | AddressOutOfBoundsException | InvalidInputException e) {
		Msg.warn(ControlFlowGuard.class, "Unable to label ControlFlowGuard check function.", e);
	}
}
 
Example 5
Source File: ControlFlowGuard.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Performs markup on the ControlFlowGuard dispatch function, if it exists.
 * 
 * @param lcd The PE LoadConfigDirectory.
 * @param is64bit True if the PE is 64-bit; false if it's 32-bit.
 * @param space The program's address space.
 * @param mem The program's memory.
 * @param symbolTable The program's symbol table.
 */
private static void markupCfgDispatchFunction(LoadConfigDirectory lcd, boolean is64bit,
		AddressSpace space, Memory mem, SymbolTable symbolTable) {

	if (lcd.getCfgDispatchFunctionPointer() == 0) {
		return;
	}

	try {
		Address functionPointerAddr = space.getAddress(lcd.getCfgDispatchFunctionPointer());
		Address functionAddr = space.getAddress(
			is64bit ? mem.getLong(functionPointerAddr) : mem.getInt(functionPointerAddr));
		symbolTable.createLabel(functionAddr, "_guard_dispatch_icall", SourceType.IMPORTED);
	}
	catch (MemoryAccessException | AddressOutOfBoundsException | InvalidInputException e) {
		Msg.warn(ControlFlowGuard.class, "Unable to label ControlFlowGuard dispatch function.",
			e);
	}
}
 
Example 6
Source File: ControlFlowGuard.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Performs markup on the ReturnFlowGuard failure routine, if it exists.
 * 
 * @param lcd The PE LoadConfigDirectory.
 * @param space The program's address space.
 * @param symbolTable The program's symbol table.
 */
private static void markupRfgFailureRoutine(LoadConfigDirectory lcd, AddressSpace space,
		SymbolTable symbolTable) {

	if (lcd.getRfgFailureRoutine() == 0) {
		return;
	}

	try {
		Address routineAddr = space.getAddress(lcd.getRfgFailureRoutine());
		symbolTable.createLabel(routineAddr, "_guard_ss_verify_failure", SourceType.IMPORTED);
	}
	catch (AddressOutOfBoundsException | InvalidInputException e) {
		Msg.warn(ControlFlowGuard.class, "Unable to label ReturnFlowGuard failure routine.", e);
	}
}
 
Example 7
Source File: ControlFlowGuard.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Performs markup on the ReturnFlowGuard "default" failure routine function, if it exists.
 * 
 * @param lcd The PE LoadConfigDirectory.
 * @param is64bit True if the PE is 64-bit; false if it's 32-bit.
 * @param space The program's address space.
 * @param mem The program's memory.
 * @param symbolTable The program's symbol table.
 */
private static void markupRfgDefaultFailureRoutine(LoadConfigDirectory lcd, boolean is64bit,
		AddressSpace space, Memory mem, SymbolTable symbolTable) {

	if (lcd.getRfgFailureRoutineFunctionPointer() == 0) {
		return;
	}

	try {
		Address functionPointerAddr =
			space.getAddress(lcd.getRfgFailureRoutineFunctionPointer());
		Address functionAddr = space.getAddress(
			is64bit ? mem.getLong(functionPointerAddr) : mem.getInt(functionPointerAddr));
		symbolTable.createLabel(functionAddr, "_guard_ss_verify_failure_default",
			SourceType.IMPORTED);
	}
	catch (MemoryAccessException | AddressOutOfBoundsException | InvalidInputException e) {
		Msg.warn(ControlFlowGuard.class,
			"Unable to label ReturnFlowGuard default failure routine.", e);
	}
}
 
Example 8
Source File: ControlFlowGuard.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Performs markup on the ReturnFlowGuard verify stack pointer function, if it exists.
 * 
 * @param lcd The PE LoadConfigDirectory.
 * @param is64bit True if the PE is 64-bit; false if it's 32-bit.
 * @param space The program's address space.
 * @param mem The program's memory.
 * @param symbolTable The program's symbol table.
 */
private static void markupRfgDefaultStackPointerFunction(LoadConfigDirectory lcd,
		boolean is64bit, AddressSpace space, Memory mem, SymbolTable symbolTable) {

	if (lcd.getRfgVerifyStackPointerFunctionPointer() == 0) {
		return;
	}

	try {
		Address functionPointerAddr =
			space.getAddress(lcd.getRfgVerifyStackPointerFunctionPointer());
		Address functionAddr = space.getAddress(
			is64bit ? mem.getLong(functionPointerAddr) : mem.getInt(functionPointerAddr));
		symbolTable.createLabel(functionAddr, "_guard_ss_verify_sp_default",
			SourceType.IMPORTED);
	}
	catch (MemoryAccessException | AddressOutOfBoundsException | InvalidInputException e) {
		Msg.warn(ControlFlowGuard.class,
			"Unable to label ReturnFlowGuard verify stack pointer function.", e);
	}
}
 
Example 9
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 10
Source File: NXProgramBuilder.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
public Symbol createSymbol(Address addr, String name, boolean isPrimary, boolean pinAbsolute, Namespace namespace) throws InvalidInputException 
{
    // TODO: At this point, we should be marking as data or code
    SymbolTable symbolTable = program.getSymbolTable();
    Symbol sym = symbolTable.createLabel(addr, name, namespace, SourceType.IMPORTED);
    if (isPrimary) {
        checkPrimary(sym);
    }
    if (pinAbsolute && !sym.isPinned()) {
        sym.setPinned(true);
    }
    return sym;
}
 
Example 11
Source File: AbstractVersionControlActionTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void createHistoryEntry(Program program, String symbolName)
		throws InvalidInputException, IOException, CancelledException {
	int transactionID = program.startTransaction("test");
	try {
		SymbolTable symTable = program.getSymbolTable();
		symTable.createLabel(program.getMinAddress().getNewAddress(0x010001000), symbolName,
			SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transactionID, true);
		program.save(null, TaskMonitor.DUMMY);
	}
}
 
Example 12
Source File: VersionControlSlowScreenShots.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected void createHistoryEntry(Program p, String symbolName)
		throws InvalidInputException, IOException, CancelledException {
	int transactionID = p.startTransaction("test");
	try {
		SymbolTable symTable = p.getSymbolTable();
		symTable.createLabel(p.getMinAddress().getNewAddress(0x010001000), symbolName,
			SourceType.USER_DEFINED);
	}
	finally {
		p.endTransaction(transactionID, true);
		p.save(null, TaskMonitor.DUMMY);
	}
}
 
Example 13
Source File: ProgramTreePlugin1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateFragFromCUwithLabel() throws Exception {
	SymbolTable symbolTable = program.getSymbolTable();

	Address start = getAddr(0x0100101c);
	Address end = getAddr(0x0100101f);

	AddressSet set = new AddressSet();
	set.addRange(start, end);

	int transactionID = program.startTransaction("Test");
	symbolTable.createLabel(start, "MyLabel", SourceType.USER_DEFINED);
	program.endTransaction(transactionID, true);

	set.addRange(getAddr(0x01001190), getAddr(0x01001193));

	int childCount = root.getChildCount();

	addCodeUnits(root, set);

	program.flushEvents();

	assertEquals(childCount + 1, root.getChildCount());
	ProgramNode node = (ProgramNode) root.getChildAt(childCount);
	assertEquals("MyLabel", node.getName());

	ProgramFragment f = node.getFragment();
	assertTrue(f.hasSameAddresses(set));

	undo();
	assertEquals(childCount, root.getChildCount());
	redo();
	assertEquals(childCount + 1, root.getChildCount());
	node = (ProgramNode) root.getChildAt(childCount);
	assertEquals("MyLabel", node.getName());
}
 
Example 14
Source File: MarkupWallaceSrcScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void createNewLabel(Address address, String name, Namespace namespace, SourceType sourceType) {
	SymbolTable symbolTable = currentProgram.getSymbolTable();
	if(getSymbolAt(address).getSource().equals(SourceType.DEFAULT)){
		try {
			symbolTable.createLabel(address, name, namespace, sourceType);
		} catch (InvalidInputException e) {
			println("Invalid input to create label.");
		}
	}
}
 
Example 15
Source File: VersionControlAction2Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testCheckIn() throws Exception {
	final GTreeNode node = getNode(PROGRAM_A);
	addToVersionControl(node, false);

	selectNode(node);
	final DockingActionIf action = getAction("CheckOut");
	runSwing(() -> action.actionPerformed(getDomainFileActionContext(node)), false);
	waitForSwing();
	waitForTasks();

	Program program = (Program) ((DomainFileNode) node).getDomainFile()
			.getDomainObject(this,
				true, false, TaskMonitor.DUMMY);
	int transactionID = program.startTransaction("test");
	try {
		SymbolTable symTable = program.getSymbolTable();
		symTable.createLabel(program.getMinAddress().getNewAddress(0x010001000), "fred",
			SourceType.USER_DEFINED);
	}
	finally {
		program.endTransaction(transactionID, true);
		program.save(null, TaskMonitor.DUMMY);
	}
	program.release(this);
	final DockingActionIf checkInAction = getAction("CheckIn");
	runSwing(() -> checkInAction.actionPerformed(getDomainFileActionContext(node)), false);
	waitForSwing();
	VersionControlDialog dialog = waitForDialogComponent(VersionControlDialog.class);
	assertNotNull(dialog);
	final JTextArea textArea = findComponent(dialog, JTextArea.class);
	assertNotNull(textArea);
	final JCheckBox cb = findComponent(dialog, JCheckBox.class);
	assertNotNull(cb);
	runSwing(() -> {
		textArea.setText("This is a test");
		cb.setSelected(false);
	});
	pressButtonByText(dialog, "OK");
	waitForTasks();
	DomainFile df = ((DomainFileNode) node).getDomainFile();
	assertTrue(!df.isCheckedOut());

}
 
Example 16
Source File: LinuxSystemMapImportScript.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void readSystemMap(BufferedReader reader) throws IOException {

		SymbolTable st = currentProgram.getSymbolTable();
		AddressSpace addrspace = currentProgram.getAddressFactory().getDefaultAddressSpace();
		if (currentSelection != null && !currentSelection.isEmpty()) {
			addrspace = currentProgram.getAddressFactory().getDefaultAddressSpace();
		}

		int lineno = 0;
		String line;
		while (!monitor.isCancelled() && (line = reader.readLine()) != null) {
			lineno++;
			line = line.trim();
			String address, type, name;

			String[] tokens = line.split("\\s");
			if (tokens.length == 3) {
				address = tokens[0];
				type = tokens[1];
				name = tokens[2];
				// account for /proc/ksyms output of the type: c0100000 modulesym [module]
				if (name.matches("^\\[.*]$")) {
					name = tokens[1] + "__" + tokens[2];
					type = "";
				}
			}
			else if (tokens.length == 2) {
				address = tokens[0];
				type = "";
				name = tokens[1];
			}
			else {
				println("Error parsing line " + lineno + ": \"" + line + "\".");
				continue;
			}

			Address addr = getAddress(addrspace, address);
			if (addr == null) {
				println("Error parsing line " + lineno + ": \"" + line +
					"\": could not parse address.");
				continue;
			}

			try {
				st.createLabel(addr, name, SourceType.USER_DEFINED);
			}
			catch (InvalidInputException e) {
				println("Error adding label for line " + lineno + ": \"" + line + "\"");
				continue;
			}

			if (type.equalsIgnoreCase("t")) {
				// This is a function
				disassemble(addr);
				createFunction(addr, name);
			}
		}
	}