Java Code Examples for ghidra.program.model.listing.Data#getValue()

The following examples show how to use ghidra.program.model.listing.Data#getValue() . 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: PseudoDisassembler.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Interpret the bytes at a location in memory as an address
 * and return the address.  This routine assumes that the bytes
 * needed to create the address are the same size as the bytes
 * needed to represent the toAddr.  So this is somewhat generic.
 * 
 * @param toAddr location of the bytes in memory
 * 
 * @return the address value
 */
public Address getIndirectAddr(Address toAddr) {
	Data data =
		applyDataType(toAddr, PointerDataType.getPointer(null, toAddr.getPointerSize()));

	if (data == null) {
		return null;
	}
	Object objVal = data.getValue();
	if (!(objVal instanceof Address)) {
		return null;
	}
	Address ptrAddr = (Address) objVal;

	return ptrAddr;
}
 
Example 2
Source File: ObjectiveC2_ClassAnalyzer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void processMessageReferences(ObjectiveC2_State state, BinaryReader reader)
		throws Exception {
	state.monitor.setMessage("Objective-C 2.0 Message References...");

	MemoryBlock block =
		state.program.getMemory().getBlock(ObjectiveC2_Constants.OBJC2_MESSAGE_REFS);
	if (block == null) {
		return;
	}
	ObjectiveC1_Utilities.clear(state, block);

	long count = block.getSize() / ObjectiveC2_MessageReference.SIZEOF(state);

	state.monitor.initialize((int) count);

	Address address = block.getStart();

	for (int i = 0; i < count; ++i) {
		if (state.monitor.isCancelled()) {
			break;
		}
		state.monitor.setProgress(i);
		reader.setPointerIndex(address.getOffset());
		ObjectiveC2_MessageReference messageRef =
			new ObjectiveC2_MessageReference(state, reader);
		DataType dt = messageRef.toDataType();
		Data messageRefData = state.program.getListing().createData(address, dt);
		Data selData = messageRefData.getComponent(1);
		Object selAddress = selData.getValue();
		Data selStringData = state.program.getListing().getDataAt((Address) selAddress);
		Object selString = selStringData.getValue();
		ObjectiveC1_Utilities.createSymbol(state.program, null, selString + "_" +
			ObjectiveC2_MessageReference.NAME, address);
		address = address.add(dt.getLength());
	}
}
 
Example 3
Source File: PCodeTestAbstractControlBlock.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Check for a Data pointer at the specified address and return the referenced
 * address.
 * @param addr address of stored pointer
 * @return pointer referenced address or null if no pointer found
 */
protected Address readDefinedDataPointer(Address addr) {
	Data data = program.getListing().getDefinedDataAt(addr);
	if (data == null || !(data.getDataType() instanceof Pointer)) {
		return null;
	}
	return (Address) data.getValue();
}
 
Example 4
Source File: CreateStringScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean createLabelForString(Address addr, int length) throws Exception {
	Listing listing = currentProgram.getListing();
	Memory memory = currentProgram.getMemory();
	Data data = listing.getDataAt(addr);
	String value = (String) data.getValue();
	if (value == null) {
		return false;
	}

	boolean needsUnderscore = true;
	StringBuffer buf = new StringBuffer();
	buf.append("s");
	byte[] bytes = new byte[length];
	try {
		memory.getBytes(addr, bytes);
	}
	catch (MemoryAccessException e) {
	}
	for (int i = 0; i < length; i++) {
		char c = (char) bytes[i];
		if (c > 0x20 && c <= 0x7f) {
			if (needsUnderscore) {
				buf.append('_');
				needsUnderscore = false;
			}
			buf.append(c);
		}
		else if (c != 0) {
			needsUnderscore = true;
		}
	}
	String newLabel = buf.toString();

	createLabel(addr, newLabel, true);
	return true;
}
 
Example 5
Source File: DyldCacheAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean analyze(Program program, AddressSetView set, TaskMonitor monitor, MessageLog log)
		throws Exception {
	Address headerAddress = program.getMinAddress();

	ByteProvider provider = new MemoryByteProvider(program.getMemory(), headerAddress);

	DyldArchitecture architecture = DyldArchitecture.getArchitecture(provider);
	if (architecture == null) {
		log.appendMsg("Invalid DYLD cache file.");
		return false;
	}

	BinaryReader reader =
		new BinaryReader(provider, architecture.getEndianness() == Endian.LITTLE);

	DyldCacheHeader header = new DyldCacheHeader(reader);

	DataType headerDataType = header.toDataType();
	Data headerData = createData(program, headerAddress, headerDataType);
	createFragment(program, headerDataType.getName(), headerData.getMinAddress(),
		headerData.getMaxAddress().add(1));

	reader.setPointerIndex(header.getImagesOffset());
	Address address = toAddr(program, header.getImagesOffset());

	for (int i = 0; i < header.getImagesCount(); ++i) {

		if (monitor.isCancelled()) {
			break;
		}

		DyldCacheImageInfo data = new DyldCacheImageInfo(reader);
		DataType dataDataType = data.toDataType();
		Data dataData = createData(program, address, dataDataType);
		createFragment(program, dataDataType.getName(), dataData.getMinAddress(),
			dataData.getMaxAddress().add(1));

		Address fileOffset = toAddr(program, data.getAddress());
		Data fileData = createData(program, fileOffset, new StringDataType());
		createFragment(program, "LibraryNames", fileData.getMinAddress(),
			fileData.getMaxAddress().add(1));

		String filePath = (String) fileData.getValue();

		Address libraryOffsetAddress =
			toAddr(program, data.getAddress() - header.getBaseAddress());

		MachoBinaryAnalysisCommand command = new MachoBinaryAnalysisCommand(
			libraryOffsetAddress, false, program.getListing().getDefaultRootModule());
		command.applyTo(program, monitor);

		setPlateComment(program, address, filePath);
		setPlateComment(program, libraryOffsetAddress, filePath);

		address = address.add(dataDataType.getLength());
	}

	updateImageBase(program, header);

	return false;
}
 
Example 6
Source File: SymbolTableCommand.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void markup(MachHeader header, FlatProgramAPI api, Address baseAddress, boolean isBinary,
		ProgramModule parentModule, TaskMonitor monitor, MessageLog log) {
	updateMonitor(monitor);
	if (isBinary) {
		try {
			createFragment(api, baseAddress, parentModule);
			Address address = baseAddress.getNewAddress(getStartIndex());
			api.createData(address, toDataType());

			if (getStringTableSize() > 0) {
				Address stringTableStart = baseAddress.getNewAddress(getStringTableOffset());
				api.createFragment(parentModule, "string_table", stringTableStart,
					getStringTableSize());
			}

			int symbolIndex = 0;
			Address symbolStartAddr = baseAddress.getNewAddress(getSymbolOffset());
			long offset = 0;
			for (NList symbol : symbols) {
				if (monitor.isCancelled()) {
					return;
				}

				DataType symbolDT = symbol.toDataType();
				Address symbolAddr = symbolStartAddr.add(offset);
				Data symbolData = api.createData(symbolAddr, symbolDT);

				Address stringAddress = baseAddress.getNewAddress(
					getStringTableOffset() + symbol.getStringTableIndex());
				Data stringData = api.createAsciiString(stringAddress);
				String string = (String) stringData.getValue();

				Reference ref =
					api.createMemoryReference(symbolData, stringAddress, RefType.DATA);
				api.setReferencePrimary(ref, false);

				api.setPlateComment(symbolAddr,
					string + "\n" + "Index:           0x" + Integer.toHexString(symbolIndex) +
						"\n" + "Value:           0x" + Long.toHexString(symbol.getValue()) +
						"\n" + "Description:     0x" +
						Long.toHexString(symbol.getDescription() & 0xffff) + "\n" +
						"Library Ordinal: 0x" +
						Long.toHexString(symbol.getLibraryOrdinal() & 0xff));

				offset += symbolDT.getLength();
				++symbolIndex;
			}

			if (getNumberOfSymbols() > 0) {
				api.createFragment(parentModule, "symbols", symbolStartAddr, offset);
			}
		}
		catch (Exception e) {
			log.appendMsg("Unable to create " + getCommandName() + " - " + e.getMessage());
		}
	}
}
 
Example 7
Source File: StringTable_BE_Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearchSelection() throws Exception {
	final AddressSet set = new AddressSet();
	set.addRange(addr(0x100), addr(0x1000));

	// select the address set
	cbPlugin.firePluginEvent(new ProgramSelectionPluginEvent(cbPlugin.getName(),
		new ProgramSelection(set), program));

	SearchStringDialog dialog = getDialog();

	// turn off null Terminate box
	JCheckBox cb = (JCheckBox) findButton(dialog.getComponent(), "Require Null Termination");
	cb.setSelected(false);

	pressButtonByText(dialog.getComponent(), "Search");

	@SuppressWarnings("unchecked")
	StringTableProvider provider =
		((List<StringTableProvider>) getInstanceField("transientProviders", plugin)).get(0);
	StringTableModel model = (StringTableModel) getInstanceField("stringModel", provider);
	GhidraTable table = (GhidraTable) getInstanceField("table", provider);
	waitForTableModel(model);

	setAutoLabelCheckbox(provider, true);

	// test the results size
	assertEquals(4, model.getRowCount());

	// test the first and last ones
	assertEquals(addr(0x100), addressCell(model, 0));
	assertEquals(addr(0x400), addressCell(model, 3));

	// ********************** test Make Unicode String
	// select row for address 400
	Address address = addr(0x400);
	selectRows(table, address);

	// make string
	DockingAction makeStringAction =
		(DockingAction) getInstanceField("makeStringAction", provider);
	performAction(makeStringAction, true);
	waitForTableModel(model);

	// test that the string was actually made correctly
	// test that the string was actually made correctly
	Data d = program.getListing().getDataAt(addr(0x400));
	assertEquals(12, d.getLength());

	String str = (String) d.getValue();
	assertEquals("abcdef", str);

	DataType dt = d.getBaseDataType();
	assertEquals("unicode", dt.getName());

	// test that the label was made correctly
	Symbol sym = program.getSymbolTable().getPrimarySymbol(addr(0x400));
	assertEquals("u_abcdef@00000400", sym.getName());

	selectRows(table, address);
	int selectedRow = table.getSelectedRow();

	// test that the table was updated with the label and preview
	assertEquals("u_abcdef@00000400", labelCell(model, selectedRow));

	CodeUnitTableCellData value = previewCell(model, selectedRow);
	assertEquals("unicode u\"abcdef\"", value.getDisplayString());

}