Java Code Examples for ghidra.program.model.mem.MemoryBlock#getName()

The following examples show how to use ghidra.program.model.mem.MemoryBlock#getName() . 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: PowerPC64_ElfExtension.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void paintTocAsR2value(long tocBaseOffset, ElfLoadHelper elfLoadHelper,
		TaskMonitor monitor) {

	Program program = elfLoadHelper.getProgram();
	ProgramContext programContext = program.getProgramContext();
	Register r2reg = program.getRegister("r2");
	RegisterValue tocValue = new RegisterValue(r2reg, BigInteger.valueOf(tocBaseOffset));

	for (MemoryBlock block : program.getMemory().getBlocks()) {
		if (block.isExecute()) {
			try {
				programContext.setRegisterValue(block.getStart(), block.getEnd(), tocValue);
			}
			catch (ContextChangeException e) {
				String msg = "Failed to set r2 as TOC_BASE on memory block " + block.getName();
				Msg.error(this, msg + ": " + e.getMessage());
				elfLoadHelper.log(msg);
			}
		}
	}

}
 
Example 2
Source File: GoToPluginTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testMatchingAddressInOverlay_NoMatchInCurrentAddressSpace() throws Exception {
	//
	// Tests that the GoTo service will show a table of address matches when the user
	// queries an address that does not have an entry in the current address space *and* the
	// option to always show all addresses is off.
	//
	loadProgram("x86");
	createOverlay("TestOverlay1", "1002000", 100);
	MemoryBlock overlay2Block = createOverlay("TestOverlay2", "1003000", 100);

	//
	// Put us in an address space that does not have a match for the query address
	// The default space and 'Test Overlay 1' each have an address for 1002000.  The
	// 'Test Overlay 2' does not.  So, put the cursor there.
	String name = overlay2Block.getName();
	assertTrue(cbPlugin.goTo(new ProgramLocation(program, addr(name + "::1003000"))));

	showDialog();
	setText("1002000");
	performOkCallback();
	GhidraProgramTableModel<?> model = waitForModel();
	assertEquals(2, model.getRowCount());
}
 
Example 3
Source File: ObjectiveC2_Utilities.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a symbol with the given name at the specified address.
 * The symbol will be created in a name space with the name of
 * the memory block that contains the address.
 */
public static Symbol createSymbolUsingMemoryBlockAsNamespace(Program program, Address address,
		String name, SourceType sourceType)
		throws DuplicateNameException, InvalidInputException {

	SymbolTable symbolTable = program.getSymbolTable();
	Memory memory = program.getMemory();

	MemoryBlock block = memory.getBlock(address);
	String namespaceName = block.getName();

	Namespace namespace = symbolTable.getNamespace(namespaceName, program.getGlobalNamespace());
	if (namespace == null) {
		namespace = symbolTable.createNameSpace(program.getGlobalNamespace(), namespaceName,
			sourceType);
	}

	return symbolTable.createLabel(address, name, namespace, SourceType.ANALYSIS);
}
 
Example 4
Source File: ImmutableMemoryRangeByteProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String getName() {
	MemoryBlock block = memory.getBlock(start);
	if (block != null) {
		return block.getName();
	}
	return memory.getProgram().getName() + "_" + start.toString();
}
 
Example 5
Source File: GoToPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testWildcardInBlock() throws Exception {

	loadProgram("x86");
	MemoryBlock block = createOverlay("TestOverlay", "1002000", 100);
	String name = block.getName();

	AddLabelCmd cmd = new AddLabelCmd(addr(name + "::1002000"), "Bob", SourceType.USER_DEFINED);
	tool.execute(cmd, program);

	// try a wildcard for an address in the new block (this gets us an extra code path tested)
	setText(name + "::*ob");
	performOkCallback();
	assertEquals(addr("TestOverlay::1002000"), cbPlugin.getCurrentAddress());
}
 
Example 6
Source File: MemorySectionProgramLocationBasedTableColumn.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public String getValue(ProgramLocation rowObject, Settings settings, Program program,
		ServiceProvider serviceProvider) throws IllegalArgumentException {
	Memory memory = program.getMemory();
	MemoryBlock block = memory.getBlock(rowObject.getAddress());
	if (block == null) {
		return "";
	}
	return block.getName();
}
 
Example 7
Source File: CodeUnitFormat.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String addBlockName(Program program, Address toAddress, String name,
		MemoryBlock refBlock, boolean withBlockName) {

	if (withBlockName && refBlock != null) {
		return refBlock.getName() + Address.SEPARATOR_CHAR + name;
	}

	return name;
}
 
Example 8
Source File: BlockPanel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/** Creates labels for the block names */
private void buildLabels() {
	removeAll();
	setLayout(null);

	Container parent = getParent();

	MemoryBlock[] blocks = map.getBlocks();
	if (blocks == null) {
		return;
	}

	for (MemoryBlock block : blocks) {
		JLabel label = new GDLabel(block.getName());
		label.setFont(FONT);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setToolTipText(block.getName());

		Rectangle rect = map.getBlockPosition(block);
		int height = getHeight();
		int width = metrics.stringWidth(block.getName());
		if (rect.width < width) {
			label.setText("...");
		}
		int labelWidth = Math.min(rect.width, width);
		labelWidth = Math.max(labelWidth, 3);
		int labelHeight = height - 1;
		int x = rect.x + (rect.width - 1) / 2 - labelWidth / 2;
		int y = 0;

		label.setBounds(x, y, labelWidth, labelHeight);
		add(label);
	}
	invalidate();
	if (parent != null) {
		parent.validate();
	}
}
 
Example 9
Source File: AddressFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getAddressString(CodeUnit cu) {
	Address addr = cu.getMinAddress();
	AddressSpace space = addr.getAddressSpace();
	if (displayBlockName) {
		String text = addr.toString(false, padZeros ? 16 : minHexDigits);
		MemoryBlock block = cu.getProgram().getMemory().getBlock(addr);
		if (block != null) {
			return block.getName() + ":" + text;
		}
	}
	return addr.toString(space.showSpaceName(), padZeros ? 16 : minHexDigits);
}
 
Example 10
Source File: XRefFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
protected String getBlockName(Program pgm, Address addr) {
	Memory mem = pgm.getMemory();
	MemoryBlock block = mem.getBlock(addr);
	if (block != null) {
		return block.getName();
	}
	return "";
}
 
Example 11
Source File: ElfSectionHeader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
ElfSectionHeader(ElfHeader header, MemoryBlock block, int sh_name, long imageBase)
		throws MemoryAccessException {

	this.header = header;
	this.sh_name = sh_name;

	if (block.isInitialized()) {
		sh_type = ElfSectionHeaderConstants.SHT_PROGBITS;
	}
	else {
		sh_type = ElfSectionHeaderConstants.SHT_NOBITS;
	}
	sh_flags = ElfSectionHeaderConstants.SHF_ALLOC | ElfSectionHeaderConstants.SHF_WRITE |
		ElfSectionHeaderConstants.SHF_EXECINSTR;
	sh_addr = block.getStart().getOffset();
	sh_offset = block.getStart().getAddressableWordOffset() - imageBase;
	sh_size = block.getSize();
	sh_link = 0;
	sh_info = 0;
	sh_addralign = 0;
	sh_entsize = 0;
	name = block.getName();

	data = new byte[(int) sh_size];
	if (block.isInitialized()) {
		block.getBytes(block.getStart(), data);
	}

	modified = true;
}
 
Example 12
Source File: EntropyOverviewColorService.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getBlockName(Address address) {
	MemoryBlock block = program.getMemory().getBlock(address);
	if (block != null) {
		return block.getName();
	}
	return "";
}
 
Example 13
Source File: AddressTypeOverviewColorService.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getBlockName(Address address) {
	MemoryBlock block = program.getMemory().getBlock(address);
	if (block != null) {
		return block.getName();
	}
	return "";
}
 
Example 14
Source File: PointerPullerScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Namespace getNameSpaceForData(Data data)
		throws DuplicateNameException, InvalidInputException {
	String nameSpaceName = "PulledPointers";
	MemoryBlock memoryBlock = getMemoryBlock(data.getMinAddress());
	if (memoryBlock != null) {
		nameSpaceName = memoryBlock.getName();
	}
	Namespace nameSpace = getNamespace(null, nameSpaceName);
	if (nameSpace == null) {
		nameSpace = currentProgram.getSymbolTable().createNameSpace(null, nameSpaceName,
			SourceType.ANALYSIS);
	}
	return nameSpace;
}
 
Example 15
Source File: MemoryBlockStartFieldFactory.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a comment to be show at the beginning of each block that shows the following:
 *   - block name
 *   - block type
 *   - block start/end address (size)
 *   - block comment
 * 
 * @param cu
 */
protected List<AttributedString> createBlockStartText(CodeUnit cu) {

	List<AttributedString> lines = new ArrayList<>();

	// Need to get the memory block that this code unit is a part of, so we can check
	// its start address.
	MemoryBlock block = cu.getMemory().getBlock(cu.getAddress());

	if (block == null) {
		return null;
	}

	// If the code unit is not at the start of the block, just return.
	if (!(cu.getMinAddress().equals(block.getStart()))) {
		return null;
	}

	MemoryBlockType blockType = block.getType();

	String type = "";
	if (blockType != MemoryBlockType.DEFAULT) {
		if (block.isMapped()) {
			type = "(" + block.getSourceInfos().get(0).getDescription() + ")";
		}
		else {
			type = "(" + blockType + ")";
		}
	}
	String line1 = block.getName() + " " + type;
	String line2 = block.getComment();
	String line3 = block.getStart().toString(true) + "-" + block.getEnd().toString(true);

	AttributedString borderAS = new AttributedString("//", color, getMetrics());
	lines.add(borderAS);
	lines.add(new AttributedString("// " + line1, color, getMetrics()));
	if (line2 != null && !line2.isEmpty()) {
		lines.add(new AttributedString("// " + line2, color, getMetrics()));
	}
	lines.add(new AttributedString("// " + line3, color, getMetrics()));
	lines.add(borderAS);

	return lines;
}
 
Example 16
Source File: AddressBasedLocation.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static String buildStringRepresentation(Program program, Address address,
		Reference reference, ShowBlockName showBlockName) {
	if (address == null) {
		return "<NULL>";
	}
	if (address.getAddressSpace().getType() == AddressSpace.TYPE_NONE) {
		return ""; // NO_ADDRESS or EXT_FROM_ADDRESS not rendered
	}
	if (address.isExternalAddress()) {
		return getExternalAddressRepresentation(program, address);
	}
	if (address.isVariableAddress()) {
		return getVariableAddressRepresentation();
	}
	if (address.isStackAddress()) {
		return getStackAddressRepresentation(address);
	}
	if (address.isConstantAddress()) {
		return getConstantAddressRepresentation(address);
	}
	if (address.isRegisterAddress()) {
		return getRegisterAddressRepresentation(program, address);
	}

	// Handle all other spaces (e.g., memory, other, overlays, hash, etc.)
	String addrStr;
	if (reference != null && reference.isOffsetReference()) {
		OffsetReference offsetRef = (OffsetReference) reference;
		long offset = offsetRef.getOffset();
		boolean neg = (offset < 0);
		Address baseAddr = offsetRef.getBaseAddress();
		addrStr = baseAddr.toString() + (neg ? "-" : "+") + "0x" +
			Long.toHexString(neg ? -offset : offset);
	}
	else if (reference != null && reference.isShiftedReference()) {
		// TODO: unsure of rendering which has never really been addressed
		// TODO: shifted references have never addressed concerns related to
		// addressable unit size
		ShiftedReference shiftedRef = (ShiftedReference) reference;
		StringBuilder buf = new StringBuilder();
		buf.append(address.toString());
		buf.append("(0x");
		buf.append(Long.toHexString(shiftedRef.getValue()));
		buf.append("<<");
		buf.append(Long.toString(shiftedRef.getShift()));
		buf.append(")");
		addrStr = buf.toString();
	}
	else {
		addrStr = address.toString();
	}

	if (showBlockName != ShowBlockName.NEVER) {
		Memory mem = program.getMemory();
		MemoryBlock toBlock = mem.getBlock(address);
		if (toBlock != null && showBlockName == ShowBlockName.NON_LOCAL && reference != null &&
			toBlock.equals(mem.getBlock(reference.getFromAddress()))) {
			toBlock = null;
		}
		if (toBlock != null) {
			addrStr = toBlock.getName() + "::" + addrStr;
		}
	}

	return addrStr;
}
 
Example 17
Source File: SampleProgramTreePlugin.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public boolean applyTo(DomainObject obj, TaskMonitor monitor) {
	Program program = (Program) obj;

	listing = program.getListing();

	createDefaultTreeView(program, programTreeName);

	Memory mem = program.getMemory();

	ProgramModule root_module = listing.getRootModule(programTreeName);

	AddressSet set = new AddressSet(mem);

	try {
		root_module.createModule("Fragments");
	}
	catch (DuplicateNameException e) {
		// don't care???
	}
	ProgramModule frags = listing.getModule(programTreeName, "Fragments");

	long startCount = set.getNumAddresses();
	monitor.initialize(startCount);
	while (!monitor.isCancelled() && !set.isEmpty()) {
		MemoryBlock block = mem.getBlock(set.getMinAddress());
		Address start = block.getStart();
		Address end = block.getEnd();

		set.deleteRange(block.getStart(), block.getEnd());

		long numLeft = set.getNumAddresses();
		monitor.setProgress(startCount - numLeft);

		String mod_name = block.getName();

		monitor.setMessage("Module " + start + " : " + mod_name);

		ProgramModule mod = make_module(mod_name, frags);
		makeFragment(start, end, "frag_" + fragment_count, mod);
		fragment_count++;
	}

	return true;
}