ghidra.program.model.listing.Listing Java Examples

The following examples show how to use ghidra.program.model.listing.Listing. 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: DisplayableEolTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testReferenceToOffcutStringData_UseAbbreviatedCommentOption() throws Exception {
	//
	// When on, the 'Use Abbreviated Automatic Comments' option will show only the offcut
	// portion of offcut string data.
	//
	Address dataStartAddress = addr("0x1001234");
	Address offcutAddress = dataStartAddress.add(4);
	Command cmd =
		new AddMemRefCmd(addr("0x1001000"), offcutAddress, SourceType.USER_DEFINED, 0, true);
	applyCmd(cmd);

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x1001000"));

	// with this at true, only the used part of the string will be rendered
	boolean useAbbreviatedComments = true;

	DisplayableEol displayableEol =
		new DisplayableEol(cu, true, true, true, false, 5, useAbbreviatedComments);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("= \"two\"", comments[0]);// full string is one.two
}
 
Example #2
Source File: ProgramXmlMgr.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createDefaultTree(Program program, XmlProgramOptions options) {

		if (options.isAddToProgram()) {
			return;
		}

		Listing listing = program.getListing();
		if (listing.getTreeNames().length == 0) {
			try {
				listing.createRootModule(TreeManager.DEFAULT_TREE_NAME);
			}
			catch (DuplicateNameException e) {
				// shouldn't happen since we checked the tree names above
				Msg.debug(this, "Unable to create default module", e);
			}
		}

	}
 
Example #3
Source File: TableEntry.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createData(Listing listing, Address entryAddr) {
	DataType primitiveDt;
	switch (size) {
		case 1:
			primitiveDt = new ByteDataType();
			break;
		case 2:
			primitiveDt = new ByteDataType();
			break;
		case 4:
			primitiveDt = new ByteDataType();
			break;
		case 8:
			primitiveDt = new ByteDataType();
			break;
		default:
			return;
	}
	CreateDataCmd cmd = new CreateDataCmd(entryAddr, primitiveDt);
	cmd.applyTo(program);
}
 
Example #4
Source File: UserDefinedPropertyMerger.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Determines all of the property names that are available in the latest and my program.
 * @return the property names.
 */
private String[] getPropertyNames() {
	Listing latestListing = latestPgm.getListing();
	Listing myListing = myPgm.getListing();
	Iterator<String> latestProps = latestListing.getUserDefinedProperties();
	Iterator<String> myProps = myListing.getUserDefinedProperties();
	// Combine the 2 property lists into 1 for use with our comparator.
	ArrayList<String> list = new ArrayList<String>();
	while (latestProps.hasNext()) {
		list.add(latestProps.next());
	}
	while (myProps.hasNext()) {
		String propName = myProps.next();
		// Only add the names we don't have yet.
		if (!list.contains(propName) && !propName.equals("Bookmarks")) {
			list.add(propName);
		}
	}
	return list.toArray(new String[list.size()]);
}
 
Example #5
Source File: FollowFlowProgramBuilder.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private int createPointer(int from, int to) throws Exception {
	int thisPointerSize = 4;

	byte[] bytes = new byte[thisPointerSize];
	dataConverter.getBytes(to, bytes);

	String fromString = "0x" + Integer.toHexString(from);
	String endString = "0x" + Integer.toHexString(from + thisPointerSize - 1);

	clearCodeUnits(fromString, endString, false);
	setBytes(fromString, bytes, false);
	startTransaction();
	Listing listing = getProgram().getListing();
	listing.createData(addr(from), new Pointer32DataType());
	endTransaction();

	return thisPointerSize; // pointer size in bytes.
}
 
Example #6
Source File: DisplayableEolTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testReferenceToOffcutStringData() throws Exception {

	Address dataStartAddress = addr("0x1001234");
	Address offcutAddress = dataStartAddress.add(2);
	Command cmd =
		new AddMemRefCmd(addr("0x1001000"), offcutAddress, SourceType.USER_DEFINED, 0, true);
	applyCmd(cmd);

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x1001000"));

	// with this at false, all of the string will be rendered
	boolean useAbbreviatedComments = false;

	DisplayableEol displayableEol =
		new DisplayableEol(cu, true, true, true, false, 5, useAbbreviatedComments);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("= \"one.two\"", comments[0]);
}
 
Example #7
Source File: RemoveAllOffcutReferencesScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	ReferenceManager referenceManager = currentProgram.getReferenceManager();
	AddressIterator iterator = referenceManager.getReferenceDestinationIterator(currentProgram.getMinAddress(), true);
	while (iterator.hasNext()) {
		if (monitor.isCancelled()) {
			break;
		}
 		Address address = iterator.next();
			CodeUnit codeUnit = listing.getCodeUnitContaining(address);
			if (codeUnit != null) {
				if (!codeUnit.getMinAddress().equals(address)) {
					monitor.setMessage("Removing offcut reference at "+address);
			 		ReferenceIterator referencesTo = referenceManager.getReferencesTo(address);
			 		while (referencesTo.hasNext()) {
			 			if (monitor.isCancelled()) {
							break;
						}
			 			Reference reference = referencesTo.next();
			 			referenceManager.delete(reference);
			 		}
			}
		}
	}
}
 
Example #8
Source File: DeleteExitCommentsScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
   public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	AddressSetView set = currentProgram.getMemory();
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	int updateCount=0;
	AddressIterator iter = listing.getCommentAddressIterator(CodeUnit.POST_COMMENT, set, true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		CodeUnit cu = listing.getCodeUnitAt(addr);
		String[] comment = cu.getCommentAsArray(CodeUnit.POST_COMMENT);
		if (comment.length == 1 && comment[0].endsWith(EXIT_COMMENT)) {
			cu.setComment(CodeUnit.POST_COMMENT, null);
			++updateCount;
		}
	}
	if (updateCount > 0) {
		String cmt = updateCount > 1? "comments" : "comment";
		println("Removed " + updateCount + " exit post " + cmt + ".");
	}
	else {
		println("Did not find any exit post comments.");
	}
}
 
Example #9
Source File: DemangledAddressTableTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void assertPointersAt(int totalNonPointerData, int totalInstructions, String... addrs) {
	Listing listing = program.getListing();
	int index = 0;
	for (Data d : listing.getDefinedData(true)) {
		if (d.isPointer()) {
			assertTrue("too many pointers found, expected only " + addrs.length,
				index < addrs.length);
			assertEquals("unexpected pointer at " + d.getAddress(), addr(addrs[index++]),
				d.getAddress());
		}
	}
	assertEquals("insufficient pointers created", addrs.length, index);
	assertEquals("missing expected non-pointer data", totalNonPointerData,
		listing.getNumDefinedData() - index);
	assertEquals("missing expected instructions", totalInstructions,
		listing.getNumInstructions());
}
 
Example #10
Source File: AddSingleReferenceInSwitchTable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
  public void run() throws Exception {
  	
  	Program program = currentProgram;
  	Listing listing = program.getListing(); 
  	
  	// Ask for base address 
  	//  (equals the pc when program hits the switch table, 
  	//   which equals the address of the "add pc, .." instruction + 4)
  	Address pc = askAddress("Address", "Enter switch base address (hex, don't use 0x)");

  	// Get current data value
  	Data data = listing.getDefinedDataAt(currentAddress);
  	long currVal = NumericUtilities.parseHexLong(data.getValue().toString().substring(2));
  	
// Calculate referenced addr
Address refAddr = pc.add(2 * currVal);
	
// Add reference
println("Adding ref " + refAddr.toString() + " to address " + data.getAddressString(false, true));
data.addValueReference(refAddr, RefType.DATA);

  }
 
Example #11
Source File: AddCommentToProgramScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
    Address minAddress = currentProgram.getMinAddress();
    Listing listing = currentProgram.getListing();
    CodeUnit codeUnit = listing.getCodeUnitAt( minAddress );
    codeUnit.setComment( CodeUnit.PLATE_COMMENT, "AddCommentToProgramScript - This is an added comment!" );
}
 
Example #12
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickVarious() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_BOTH, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_MY, false);
		chooseComment("Post", addr("0x100230d"), KEEP_LATEST, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.
		chooseComment("Repeatable", addr("0x10028b1"), KEEP_BOTH, false);
//		chooseComment("Post", addr("0x10028b1"), KEEP_BOTH, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
			cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment" + "\n" + "My Repeatable Comment",
			cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment" + "\n" + "My EOL Comment",
			cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("My Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));
	}
 
Example #13
Source File: ReferenceMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
   public void testFallthroughConflictDontUseForAllPickLatest() throws Exception {

	setupFallthroughConflictUseForAll();

	executeMerge(ASK_USER);

	chooseCodeUnit("0x01002f49", "0x01002f4a", KEEP_LATEST); // Fallthrough is now a code unit diff.
	chooseCodeUnit("0x01003059", "0x0100305a", KEEP_LATEST); // Fallthrough is now a code unit diff.
	chooseReference("0x01002f5d", 1, KEEP_LATEST, false);
	waitForMergeCompletion();

	ReferenceManager refMgr = resultProgram.getReferenceManager();
	Reference[] refs;
	refs = refMgr.getReferencesFrom(addr("0x01002f49"), -1);
	assertEquals(1, refs.length);
	assertEquals(addr("0x01002f55"), refs[0].getToAddress());
	assertEquals(RefType.FALL_THROUGH, refs[0].getReferenceType());
	assertTrue(refs[0].isPrimary());

	refs = refMgr.getReferencesFrom(addr("0x01002f5d"), 1);
	Arrays.sort(refs);
	assertEquals(2, refs.length);
	assertEquals(addr("0x10024ce"), refs[0].getToAddress());
	assertEquals(addr("0x1002517"), refs[1].getToAddress());
	assertEquals(RefType.DATA, refs[0].getReferenceType());
	assertTrue(refs[0].isPrimary());

	Listing listing = resultProgram.getListing();
	assertEquals(addr("0x01003060"),
		listing.getInstructionAt(addr("0x01003059")).getFallThrough());
}
 
Example #14
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickMine() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_MY, true);
		chooseComment("Post", addr("0x100230d"), KEEP_MY, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Repeatable", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.
//		chooseComment("Post", addr("0x10028b1"), KEEP_MY, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("My Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("My EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("My Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("My Post Comment", cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("My Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("My Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("My EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("My Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("My Post Comment", cu.getComment(CodeUnit.POST_COMMENT));
	}
 
Example #15
Source File: CommentMergeManager1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
	public void testAddDiffUseForAllPickLatest() throws Exception {
		setupAddDiffCommentUseForAll();

		executeMerge(ASK_USER);

		chooseComment("Plate", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Pre", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Eol", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Repeatable", addr("0x100230d"), KEEP_LATEST, true);
		chooseComment("Post", addr("0x100230d"), KEEP_LATEST, true);

//		chooseComment("Plate", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Pre", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Eol", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Repeatable", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.
//		chooseComment("Post", addr("0x10028b1"), KEEP_LATEST, false); // UseForAll will do this.

		waitForMergeCompletion();

		Listing listing = resultProgram.getListing();

		CodeUnit cu = listing.getCodeUnitAt(addr("0x10028b1"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("Latest Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));

		cu = listing.getCodeUnitAt(addr("0x100230d"));
		assertEquals("Latest Plate Comment", cu.getComment(CodeUnit.PLATE_COMMENT));
		assertEquals("Latest Pre Comment", cu.getComment(CodeUnit.PRE_COMMENT));
		assertEquals("Latest EOL Comment", cu.getComment(CodeUnit.EOL_COMMENT));
		assertEquals("Latest Repeatable Comment", cu.getComment(CodeUnit.REPEATABLE_COMMENT));
		assertEquals("Latest Post Comment", cu.getComment(CodeUnit.POST_COMMENT));
	}
 
Example #16
Source File: ReferenceMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
    public void testFallthroughConflictUseForAllPickLatest() throws Exception {

		setupFallthroughConflictUseForAll();

		executeMerge(ASK_USER);

		chooseCodeUnit("0x01002f49", "0x01002f4a", KEEP_LATEST, true); // Fallthrough is now a code unit diff.
//		chooseCodeUnit("0x01003059", "0x0100305a", KEEP_LATEST); // 0x01003059 Handled by Use For All.
		chooseReference("0x01002f5d", 1, KEEP_LATEST, false); // 01002f5d
		waitForMergeCompletion();

		ReferenceManager refMgr = resultProgram.getReferenceManager();
		Reference[] refs;
		refs = refMgr.getReferencesFrom(addr("0x01002f49"), -1);
		assertEquals(1, refs.length);
		assertEquals(addr("0x01002f55"), refs[0].getToAddress());
		assertEquals(RefType.FALL_THROUGH, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());

		refs = refMgr.getReferencesFrom(addr("0x01002f5d"), 1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x10024ce"), refs[0].getToAddress());
		assertEquals(addr("0x1002517"), refs[1].getToAddress());
		assertEquals(RefType.DATA, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());

		Listing listing = resultProgram.getListing();
		assertEquals(addr("0x01003060"),
			listing.getInstructionAt(addr("0x01003059")).getFallThrough());
	}
 
Example #17
Source File: QualifiedSelectionPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @param instructionSet
 */
private void checkForInstructions(ProgramSelection instructionSet) {
	Listing listing = program.getListing();
	AddressIterator iter = instructionSet.getAddresses(true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		assertNotNull("Expected instruction at " + addr + ".",
			listing.getInstructionContaining(addr));
	}
}
 
Example #18
Source File: ReferenceMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
    public void testFallthroughConflictUseForAllPickMy() throws Exception {

		setupFallthroughConflictUseForAll();

		executeMerge(ASK_USER);

		chooseCodeUnit("0x01002f49", "0x01002f4a", KEEP_MY, true); // Fallthrough is now a code unit diff.
//		chooseCodeUnit("0x01003059", "0x0100305a", KEEP_MY); // 0x01003059 Handled by Use For All.
		chooseReference("0x01002f5d", 1, KEEP_LATEST, false); // 01002f5d
		waitForMergeCompletion();

		ReferenceManager refMgr = resultProgram.getReferenceManager();
		Reference[] refs;
		refs = refMgr.getReferencesFrom(addr("0x01002f49"), -1);
		assertEquals(1, refs.length);
		assertEquals(addr("0x01002f5d"), refs[0].getToAddress());
		assertEquals(RefType.FALL_THROUGH, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());

		refs = refMgr.getReferencesFrom(addr("0x01002f5d"), 1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x10024ce"), refs[0].getToAddress());
		assertEquals(addr("0x1002517"), refs[1].getToAddress());
		assertEquals(RefType.DATA, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());

		Listing listing = resultProgram.getListing();
		assertNull(listing.getInstructionAt(addr("0x01003059")).getFallThrough());
	}
 
Example #19
Source File: ReferenceMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
    public void testPrimaryConflictDontUseForAllPickLatest() throws Exception {

		setupPrimaryConflictUseForAll();

		executeMerge(ASK_USER);

		chooseCodeUnit("0x01003059", "0x0100305a", KEEP_LATEST, false);
		chooseReference("0x01002f49", -1, KEEP_LATEST, true); // 0x01002f49
//		chooseReference("0x01002f5d", 1, KEEP_LATEST, false); // 0x01002f5d Handled by Use For All.
		waitForMergeCompletion();

		ReferenceManager refMgr = resultProgram.getReferenceManager();
		Reference[] refs;
		refs = refMgr.getReferencesFrom(addr("0x01002f49"), -1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x01002f55"), refs[0].getToAddress());
		assertEquals(RefType.READ_WRITE, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());
		assertEquals(addr("0x01002f5d"), refs[1].getToAddress());
		assertEquals(RefType.READ, refs[1].getReferenceType());
		assertFalse(refs[1].isPrimary());

		refs = refMgr.getReferencesFrom(addr("0x01002f5d"), 1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x10024ce"), refs[0].getToAddress());
		assertEquals(addr("0x1002517"), refs[1].getToAddress());
		assertEquals(RefType.DATA, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());

		Listing listing = resultProgram.getListing();
		assertEquals(addr("0x01003060"),
			listing.getInstructionAt(addr("0x01003059")).getFallThrough());
	}
 
Example #20
Source File: ReferenceMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
    public void testPrimaryConflictUseForAllPickLatest() throws Exception {

		setupPrimaryConflictUseForAll();

		executeMerge(ASK_USER);

		chooseCodeUnit("0x01003059", "0x0100305a", KEEP_LATEST, false);
		chooseReference("0x01002f49", -1, KEEP_LATEST, true); // 0x01002f49
//		chooseReference("0x01002f5d", 1, KEEP_LATEST, false); // 0x01002f5d Handled by Use For All.
		waitForMergeCompletion();

		ReferenceManager refMgr = resultProgram.getReferenceManager();
		Reference[] refs;
		refs = refMgr.getReferencesFrom(addr("0x01002f49"), -1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x01002f55"), refs[0].getToAddress());
		assertEquals(RefType.READ_WRITE, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());
		assertEquals(addr("0x01002f5d"), refs[1].getToAddress());
		assertEquals(RefType.READ, refs[1].getReferenceType());
		assertFalse(refs[1].isPrimary());

		refs = refMgr.getReferencesFrom(addr("0x01002f5d"), 1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x10024ce"), refs[0].getToAddress());
		assertEquals(addr("0x1002517"), refs[1].getToAddress());
		assertEquals(RefType.DATA, refs[0].getReferenceType());
		assertTrue(refs[0].isPrimary());

		Listing listing = resultProgram.getListing();
		assertEquals(addr("0x01003060"),
			listing.getInstructionAt(addr("0x01003059")).getFallThrough());
	}
 
Example #21
Source File: ReferenceMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
    public void testPrimaryConflictUseForAllPickMy() throws Exception {

		setupPrimaryConflictUseForAll();

		executeMerge(ASK_USER);

		chooseCodeUnit("0x01003059", "0x0100305a", KEEP_LATEST, false);
		chooseReference("0x01002f49", -1, KEEP_MY, true); // 0x01002f49
//		chooseReference("0x01002f5d", 1, KEEP_MY, false); // 0x01002f5d Handled by Use For All.
		waitForMergeCompletion();

		ReferenceManager refMgr = resultProgram.getReferenceManager();
		Reference[] refs;
		refs = refMgr.getReferencesFrom(addr("0x01002f49"), -1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x01002f55"), refs[0].getToAddress());
		assertEquals(RefType.READ_WRITE, refs[0].getReferenceType());
		assertFalse(refs[0].isPrimary());
		assertEquals(addr("0x01002f5d"), refs[1].getToAddress());
		assertEquals(RefType.READ, refs[1].getReferenceType());
		assertTrue(refs[1].isPrimary());

		refs = refMgr.getReferencesFrom(addr("0x01002f5d"), 1);
		assertEquals(2, refs.length);
		assertEquals(addr("0x10024ce"), refs[0].getToAddress());
		assertEquals(RefType.DATA, refs[0].getReferenceType());
		assertFalse(refs[0].isPrimary());
		assertEquals(addr("0x1002517"), refs[1].getToAddress());
		assertEquals(RefType.READ, refs[1].getReferenceType());
		assertTrue(refs[1].isPrimary());

		Listing listing = resultProgram.getListing();
		assertEquals(addr("0x01003060"),
			listing.getInstructionAt(addr("0x01003059")).getFallThrough());
	}
 
Example #22
Source File: DisplayableEolTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoPossiblePointerEOL() throws Exception {

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x110"));
	DisplayableEol displayableEol = new DisplayableEol(cu, true, true, true, false, 5, true);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("?  ->  00000120", comments[0]);
}
 
Example #23
Source File: DisplayableEolTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testReferenceToStringData() throws Exception {

	Command cmd = new AddMemRefCmd(addr("0x1001000"), addr("0x1001234"),
		SourceType.USER_DEFINED, 0, true);
	applyCmd(cmd);

	Listing listing = program.getListing();
	CodeUnit cu = listing.getCodeUnitAt(addr("0x1001000"));
	DisplayableEol displayableEol = new DisplayableEol(cu, true, true, true, false, 5, true);

	String[] comments = displayableEol.getAutomaticComment();
	assertEquals(1, comments.length);
	assertEquals("= \"one.two\"", comments[0]);
}
 
Example #24
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 #25
Source File: DeleteEmptyPlateCommentsScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
   public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	AddressSetView set = currentProgram.getMemory();
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	int updateCount=0;
	AddressIterator iter = listing.getCommentAddressIterator(CodeUnit.PLATE_COMMENT, set, true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		CodeUnit cu = listing.getCodeUnitAt(addr);
		if (cu != null) {
			String[] comment = cu.getCommentAsArray(CodeUnit.PLATE_COMMENT);
			if (comment.length == 1 && comment[0].equals(EMPTY_PLATE)) {
				cu.setComment(CodeUnit.PLATE_COMMENT, null);
				++updateCount;
			}
		}
	}
	if (updateCount > 0) {
		String cmt = updateCount > 1? "comments" : "comment";
		println("Removed " + updateCount + " emtpy plate " + cmt + ".");
	}
	else {
		println("Did not find any empty plate comments.");
	}
}
 
Example #26
Source File: DeleteDeadDefaultPlatesScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
   public void run() throws Exception {
	Listing listing = currentProgram.getListing();
	AddressSetView set = currentProgram.getMemory();
	if (currentSelection != null && !currentSelection.isEmpty()) {
		set = currentSelection;
	}
	int updateCount=0;
	AddressIterator iter = listing.getCommentAddressIterator(CodeUnit.PLATE_COMMENT, set, true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		CodeUnit cu = listing.getCodeUnitAt(addr);
		if (cu != null) {
			String[] comment = cu.getCommentAsArray(CodeUnit.PLATE_COMMENT);
			if (comment.length == 1 && comment[0].equals(DEAD_PLATE)) {
				cu.setComment(CodeUnit.PLATE_COMMENT, null);
				++updateCount;
			}
		}
	}
	if (updateCount > 0) {
		String cmt = updateCount > 1? "comments" : "comment";
		println("Removed " + updateCount + " default plate " + cmt + ".");
	}
	else {
		println("Did not find any dead plate comments.");
	}
}
 
Example #27
Source File: SubsToFuncsScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
	BlockModelService blockModelService = state.getTool().getService(BlockModelService.class);
	Listing listing = currentProgram.getListing();
	StringBuffer errorBuf = new StringBuffer();
	CodeBlockModel cbm = blockModelService.getActiveSubroutineModel();
	AddressSetView addrset =
		currentSelection == null ? (AddressSetView) currentProgram.getMemory()
				: currentSelection;
	CodeBlockIterator cbIter = cbm.getCodeBlocksContaining(addrset, monitor);
	while (cbIter.hasNext()) {
		CodeBlock block = cbIter.next();
		FunctionIterator fIter = listing.getFunctions(block, true);
		if (!fIter.hasNext()) {
			try {
				String name = "DEAD_" + block.getFirstStartAddress();
				Symbol symbol = getSymbolAt(block.getFirstStartAddress());
				if (symbol != null && !symbol.isDynamic()) {
					name = symbol.getName();
				}
				listing.createFunction(name, block.getFirstStartAddress(), block,
					SourceType.USER_DEFINED);
			}
			catch (Exception e) {
				errorBuf.append(e.toString() + "\n");
			}
		}
	}
	if (errorBuf.length() > 0) {
		println(errorBuf.toString());
	}
}
 
Example #28
Source File: QualifiedSelectionPluginTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * @param undefinedSet
 */
private void checkForUndefined(ProgramSelection undefinedSet) {
	Listing listing = program.getListing();
	AddressIterator iter = undefinedSet.getAddresses(true);
	while (iter.hasNext()) {
		Address addr = iter.next();
		assertNotNull("Expected undefined at " + addr + ".", listing.getUndefinedDataAt(addr));
	}
}
 
Example #29
Source File: X64DbgExport.java    From GhidraX64Dbg with MIT License 5 votes vote down vote up
/**
 * Adds comments labels to the X64 database. Does not retrieve plate comments
 * and pre/post comments as X64 does not really support them.
 */
public void populateComments() {

	Listing listing = currentProgram.getListing();

	for (Address commentedAddress : listing.getCommentAddressIterator(currentProgram.getMemory(), true)) {

		CodeUnit cu = listing.getCodeUnitAt(commentedAddress);

		// Skip empty code units or empty comment
		if (cu == null) {
			continue;
		}

		String comment;
		String start = "0x" + Long.toHexString(commentedAddress.subtract(imageBase));

		// Process EOL comments
		comment = cu.getComment(CodeUnit.EOL_COMMENT);
		if (comment != null && comment.length() != 0) {
			this.comments.add(new X64Label(start, comment));
		}

		// Process repeatable comments
		comment = cu.getComment(CodeUnit.REPEATABLE_COMMENT);
		if (cu.getComment(CodeUnit.REPEATABLE_COMMENT) != null) {
			this.comments.add(new X64Label(start, comment));
		}
	}
}
 
Example #30
Source File: DemangledAddressTableTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the DemangledAddressTable will not create a sequence of data 
 * pointers due to a data collision.  This test deals with the case where primitive types have been 
 * previously created. End of block considered end of address table.
 */
@Test
public void testApply_NoNextSymbol_DataCollision() throws Exception {

	// this is: vtable for UnqiueSpace
	String mangled = "_ZTV11UniqueSpace";
	Address addr = addr("0x0110");

	SymbolTable symbolTable = program.getSymbolTable();
	symbolTable.createLabel(addr, mangled, SourceType.IMPORTED);

	Listing listing = program.getListing();
	listing.createData(addr("0x114"), Undefined4DataType.dataType);
	listing.createData(addr("0x118"), Undefined4DataType.dataType);
	listing.createData(addr("0x120"), DWordDataType.dataType);

	DemangledObject demangled = DemanglerUtil.demangle(mangled);
	assertTrue(demangled instanceof DemangledAddressTable);

	assertTrue(demangled.applyTo(program, addr, new DemanglerOptions(), TaskMonitor.DUMMY));

	// expected: UniqueSpace::vtable
	Symbol[] symbols = symbolTable.getSymbols(addr);
	assertEquals(2, symbols.length);
	assertEquals("vtable", symbols[0].getName());
	assertEquals(mangled, symbols[1].getName());

	Namespace ns = symbols[0].getParentNamespace();
	assertEquals("UniqueSpace", ns.getName(false));

	// should fail to create pointers since table region (to end of block) contains
	// data other than pointer or undefined
	assertPointersAt(3, 0);
}