Java Code Examples for ghidra.program.model.listing.Program#getAddressFactory()

The following examples show how to use ghidra.program.model.listing.Program#getAddressFactory() . 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: AddRemoveAddressRangeDialog.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected AddRemoveAddressRangeDialog(String type, String programIndicator, Program program,
		AddressRangeListener listener) {
	super(programIndicator + " Address Range", true, true, true, false);
	this.program = program;
	addressFactory = program.getAddressFactory();
	this.listener = listener;
	setHelpLocation(new HelpLocation(HelpTopics.LABEL, "AddEditDialog"));
	addWorkPanel(createAddressRangePanel());

	setFocusComponent(minAddressField);

	addOKButton();
	addCancelButton();

	setOkButtonText(type);
	setDefaultButton(okButton);
}
 
Example 2
Source File: JavaClassUtil.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public final static boolean isClassFile(Program program) {

		AddressFactory factory = program.getAddressFactory();
		byte[] bytes = new byte[4];
		try {
			AddressSpace space = factory.getAddressSpace(JavaLoader.CONSTANT_POOL);
			if (space != null) {
				Address address = space.getMinAddress();
				program.getMemory().getBytes(address, bytes);
			}
		}
		catch (Exception e) {
			Msg.error(JavaClassUtil.class, "Exception reading program bytes: " + e.getMessage(), e);
			return false;
		}
		return Arrays.equals(bytes, JavaClassConstants.MAGIC_BYTES);
	}
 
Example 3
Source File: JavaLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void doLoad(ByteProvider provider, Program program, TaskMonitor monitor)
		throws LockException, MemoryConflictException, AddressOverflowException,
		CancelledException, DuplicateNameException, IOException {
	AddressFactory af = program.getAddressFactory();
	AddressSpace space = af.getAddressSpace(CONSTANT_POOL);
	Memory memory = program.getMemory();
	alignmentReg = program.getRegister("alignmentPad");

	BinaryReader reader = new BinaryReader(provider, false);
	ClassFileJava classFile = new ClassFileJava(reader);

	Address address = space.getAddress(0);

	// Create a block of memory with just the right size
	memory.createInitializedBlock("_" + provider.getName() + "_", address,
		provider.getInputStream(0), provider.length(), monitor, false);

	createMethodLookupMemoryBlock(program, monitor);
	createMethodMemoryBlocks(program, provider, classFile, monitor);

}
 
Example 4
Source File: RelativeJumpTableSwitch.java    From ghidra with Apache License 2.0 6 votes vote down vote up
static RelativeJumpTableSwitch getRelativeJumpTableSwitch(Program program, VarnodeOperation op) {
	if (op.getPCodeOp().getOpcode() != PcodeOp.INT_ADD) {
		return null;
	}
	AddressFactory addrFactory = program.getAddressFactory();
	Address jumpBase = null;
	TableEntry offset = null;
	Address opAddr = op.getPCodeOp().getSeqnum().getTarget();
	Varnode[] inputValues = op.getInputValues();
	if (inputValues[0].isConstant()) {
		jumpBase = getAddress(addrFactory, inputValues[0].getOffset(), opAddr);
		offset = TableEntry.getTableEntry(program, inputValues[1]);
	}
	else if (inputValues[1].isConstant()) {
		jumpBase = getAddress(addrFactory, inputValues[1].getOffset(), opAddr);
		offset = TableEntry.getTableEntry(program, inputValues[0]);
	}
	if (jumpBase == null || offset == null) {
		return null; // does not qualify
	}
	return new RelativeJumpTableSwitch(jumpBase, offset);
}
 
Example 5
Source File: VertexInfo.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private FGVertex getVertex(FGController controller, Map<AddressHasher, FGVertex> vertexMap,
		AddressInfo addressInfo) {

	Program program = controller.getProgram();
	AddressFactory addressFactory = program.getAddressFactory();
	Address minAddress = addressFactory.getAddress(addressInfo.addressRangeStart);
	Address maxAddress = addressFactory.getAddress(addressInfo.addressRangeEnd);
	AddressHasher addressHasher = new AddressHasher(minAddress, maxAddress);
	FGVertex vertex = vertexMap.get(addressHasher);
	if (vertex == null) {
		return null;
	}

	vertex.setLocation(getVertexLocation());

	return vertex;
}
 
Example 6
Source File: ProjectInfoFilesystemTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void verifyProgram(String path) throws Exception {

		DomainFile df = project.getProjectData().getFile(path);
		assertNotNull("Domain file not found: " + path);

		assertTrue(df.isVersioned());
		assertTrue(df.isCheckedOut());
		assertTrue(df.isCheckedOutExclusive());// non-shared version repo

		ItemCheckoutStatus[] checkouts = df.getCheckouts();
		assertEquals(1, checkouts.length);

		Version[] versionHistory = df.getVersionHistory();
		assertEquals(1, versionHistory.length);
		assertEquals("Initial", versionHistory[0].getComment());

		Program p =
			(Program) df.getDomainObject(this, false, false, TaskMonitorAdapter.DUMMY_MONITOR);
		try {
			AddressFactory addressFactory = p.getAddressFactory();
			ProgramUserData programUserData = p.getProgramUserData();
			StringPropertyMap propertyMap =
				programUserData.getStringProperty("TEST", "userProp1", false);
			AddressIterator propertyIterator = propertyMap.getPropertyIterator();
			Address addr = addressFactory.getAddress("0100");
			assertEquals(addr, propertyIterator.next());
			assertEquals("userValue100", propertyMap.getString(addr));
			addr = addressFactory.getAddress("0200");
			assertEquals(addr, propertyIterator.next());
			assertEquals("userValue200", propertyMap.getString(addr));
		}
		finally {
			p.release(this);
		}

	}
 
Example 7
Source File: MotorolaHexLoader.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public List<Option> getDefaultOptions(ByteProvider provider, LoadSpec loadSpec,
		DomainObject domainObject, boolean loadIntoProgram) {
	String blockName = "";
	boolean isOverlay = false;
	Address baseAddr = null;
	if (domainObject instanceof Program) {
		Program program = (Program) domainObject;
		AddressFactory addressFactory = program.getAddressFactory();
		if (addressFactory != null) {
			AddressSpace defaultAddressSpace = addressFactory.getDefaultAddressSpace();
			if (defaultAddressSpace != null) {
				baseAddr = defaultAddressSpace.getAddress(0);
			}
		}
	}

	ArrayList<Option> list = new ArrayList<Option>();

	if (loadIntoProgram) {
		list.add(new Option(OPTION_NAME_IS_OVERLAY, isOverlay));
		list.add(new Option(OPTION_NAME_BLOCK_NAME, blockName));
	}
	else {
		isOverlay = false;
	}
	if (baseAddr == null) {
		list.add(new Option(OPTION_NAME_BASE_ADDRESS, Address.class));
	}
	else {
		list.add(new Option(OPTION_NAME_BASE_ADDRESS, baseAddr));
	}
	return list;
}
 
Example 8
Source File: ContextState.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
	 * Constructs an empty state.
	 * @param entryPt the entry point for the context state
	 * @param programCtx initial program context or null
	 * @param program the program
	 */
	public ContextState(Address entryPt, ProgramContext programCtx, Program program) {
		this.program = program;
		this.previousState = null;
		this.memory = program.getMemory();
		this.pcodeEntry = new SequenceNumber(entryPt, 0);
		this.language = program.getLanguage();
		this.addrFactory = program.getAddressFactory();
//		this.stackPointerIsValid = true;
		if (programCtx != null) {
			copyEntryContext(pcodeEntry.getTarget(), programCtx);
		}
	}
 
Example 9
Source File: ToyProgramBuilder.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Construct toy program builder using toy language "builder" variant.
 * This builder will be the program consumer and must be disposed
 * @param name program name
 * @param bigEndian language endianess
 * @param consumer program consumer (if null this builder will be used as consumer and must be disposed to release program)
 * @throws Exception
 */
public ToyProgramBuilder(String name, boolean bigEndian, boolean wordAligned, Object consumer)
		throws Exception {
	super(name, getToyLanguageId(bigEndian, wordAligned), consumer);
	Program program = getProgram();
	addrFactory = program.getAddressFactory();
	defaultSpace = addrFactory.getDefaultAddressSpace();
	definedInstrAddresses = new ArrayList<Address>();
}
 
Example 10
Source File: AbstractScalarOperandHover.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void formatAsAddressVal(Program program, Address addr, Scalar scalar,
		StringBuilder htmlText) {

	// maybe the scalar is an address..
	long scalarLong = scalar.getValue();
	AddressFactory factory = program.getAddressFactory();
	AddressSpace space = factory.getDefaultAddressSpace();
	Address asAddress;
	try {
		asAddress = factory.getAddress(space.getBaseSpaceID(), scalarLong);
	}
	catch (AddressOutOfBoundsException ex) {
		asAddress = null;	// Constant doesn't make sense as an address
	}

	Memory memory = program.getMemory();
	if (asAddress != null && memory.contains(asAddress)) {
		htmlText.append("<hr>");
		htmlText.append("<table>");

		addReprRow(htmlText, "Address", asAddress.toString());

		// .. and maybe it points to some data...
		Data data = program.getListing().getDataContaining(asAddress);
		if (data != null) {
			Symbol primary = data.getPrimarySymbol();
			if (primary != null) {
				addReprRow(htmlText, "Symbol",
					HTMLUtilities.italic(HTMLUtilities.friendlyEncodeHTML(primary.getName())));
			}
		}

		htmlText.append("</table>");
	}
}
 
Example 11
Source File: AbstractDwarfEHDecoder.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Get the DWARF-encoded address value as stored by the context
 * @param context Stores program location and decode parameters
 * @return the address
 * @throws MemoryAccessException if the data can't be read
 */
@Override
public Address decodeAddress(DwarfDecodeContext context) throws MemoryAccessException {
	Program prog = context.getProgram();
	AddressFactory addrFactory = prog.getAddressFactory();
	AddressSpace ram = addrFactory.getDefaultAddressSpace();

	long offset = decode(context);

	return addrFactory.getAddress(ram.getBaseSpaceID(), offset);
}
 
Example 12
Source File: AddressSet.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a range of addresses to this set.
 * @param program program whose AddressFactory is used to resolve address ranges that span
 * multiple address spaces.
 * @param start the start address of the range to add
 * @param end the end address of the range to add
 */
public void addRange(Program program, Address start, Address end) {
	if (start.getAddressSpace().equals(end.getAddressSpace())) {
		addRange(start, end);
		return;
	}
	AddressFactory addressFactory = program.getAddressFactory();
	add(addressFactory.getAddressSet(start, end));
}
 
Example 13
Source File: ChooseAddressSetEditorPanel.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public ChooseAddressSetEditorPanel(final PluginTool tool, final String name,
		final Program program, final AddressSetView selectionAddressSet,
		final AddressSetView myInitialAddressSet,
		final AddressSetChoice initialAddressSetChoice) {

	super(new BorderLayout());
	// Establish the initial state from the parameters.
	this.tool = tool;
	this.name = name;
	this.program = program;
	this.addressFactory = program.getAddressFactory();
	this.selectionAddressSet = new AddressSet(selectionAddressSet);
	if (selectionAddressSet != null && !selectionAddressSet.isEmpty()) {
		hasSelection = true;
	}
	this.myInitialAddressSet = new AddressSet(myInitialAddressSet);
	this.initialAddressSetChoice = (initialAddressSetChoice != null) ? initialAddressSetChoice
			: (hasSelection ? AddressSetChoice.SELECTION : AddressSetChoice.ENTIRE_PROGRAM);

	if (myInitialAddressSet != null && !myInitialAddressSet.isEmpty()) {
		myCurrentAddressSet = new AddressSet(myInitialAddressSet);
	}
	else {
		if (hasSelection) {
			myCurrentAddressSet = new AddressSet(selectionAddressSet);
		}
		else {
			myCurrentAddressSet = new AddressSet(program.getMemory());
		}
	}

	setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), name));

	add(createChooseSourcePanel(), BorderLayout.NORTH);
	add(createRangeListPanel(), BorderLayout.CENTER);
	switch (initialAddressSetChoice) {
		case SELECTION:
			toolSelectionButton.setSelected(true);
			choseToolSelection();
			break;
		case MANUALLY_DEFINED:
			myRangesButton.setSelected(true);
			choseMyRanges();
			break;
		case ENTIRE_PROGRAM:
		default:
			entireProgramButton.setSelected(true);
			choseEntireProgram();
			break;
	}
}
 
Example 14
Source File: RestoreSelectionPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private ProgramSelection selectAgainInPrimary(Program program) {
	AddressFactory addressFactory = program.getAddressFactory();
	return selectRange(addressFactory.getAddress("1001024"),
		addressFactory.getAddress("1001028"));
}
 
Example 15
Source File: MemoryMapXmlMgr.java    From ghidra with Apache License 2.0 4 votes vote down vote up
MemoryMapXmlMgr(Program program, MessageLog log) {
	this.program = program;
	this.memory = program.getMemory();
	this.factory = program.getAddressFactory();
	this.log = log;
}
 
Example 16
Source File: DexUtil.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static Address toLookupAddress(Program program, int methodIndex) {
	AddressFactory addressFactory = program.getAddressFactory();
	AddressSpace defaultAddressSpace = addressFactory.getDefaultAddressSpace();
	return defaultAddressSpace.getAddress(DexUtil.LOOKUP_ADDRESS + (methodIndex * 4));
}
 
Example 17
Source File: RestoreSelectionPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private ProgramSelection selectInPrimary(Program program) {
	AddressFactory addressFactory = program.getAddressFactory();
	return selectRange(addressFactory.getAddress("1001010"),
		addressFactory.getAddress("1001020"));
}
 
Example 18
Source File: ProgramOverlaysTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
Address addr(Program p, String addrString) {
	AddressFactory af = p.getAddressFactory();
	return af.getAddress(addrString);
}
 
Example 19
Source File: JavaClassUtil.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static Address toLookupAddress(Program program, int methodIndex) {
	AddressFactory addressFactory = program.getAddressFactory();
	AddressSpace defaultAddressSpace = addressFactory.getDefaultAddressSpace();
	return defaultAddressSpace.getAddress(JavaClassUtil.LOOKUP_ADDRESS + (methodIndex * 4));
}
 
Example 20
Source File: RestoreSelectionPluginTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private ProgramSelection selectInSecondary(Program program) {
	AddressFactory addressFactory = program.getAddressFactory();
	return selectRange(addressFactory.getAddress("1000:0b12"),
		addressFactory.getAddress("1000:0b21"));
}