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

The following examples show how to use ghidra.program.model.listing.Data#getDataType() . 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: SettingsTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetDefaultSettings() throws Exception {
	listing.createData(addr(10), new ByteDataType(), 1);
	Data data = listing.getDataAt(addr(10));
	ByteDataType dt = (ByteDataType) data.getDataType();
	Settings defaultSettings = dt.getDefaultSettings();
	defaultSettings.setString("color", "red");
	defaultSettings.setLong("someLongValue", 10);
	defaultSettings.setByteArray("bytes", new byte[] { 0, 1, 2 });

	assertEquals("red", defaultSettings.getString("color"));
	Long lv = defaultSettings.getLong("someLongValue");
	assertNotNull(lv);
	assertEquals(10, lv.longValue());
	byte[] b = defaultSettings.getByteArray("bytes");
	assertNotNull(b);
	assertEquals(3, b.length);

	defaultSettings.setValue("long", new Long(10));
	Object obj = defaultSettings.getValue("long");
	assertNotNull(obj);
	assertEquals(10, ((Long) obj).longValue());
}
 
Example 2
Source File: UnionLocationDescriptor.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
protected DataType getSourceDataType() {
	Data data = getData(getLocation());
	Data parentData = getParent(data);
	DataType dataType = parentData.getDataType();
	if (!(dataType instanceof Union)) {
		throw new AssertException("A Union is required for this LocationDescriptor");
	}
	union = (Union) dataType;
	return dataType;
}
 
Example 3
Source File: SettingsTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testInstanceSettings() throws Exception {

	listing.createData(addr(10), new ByteDataType(), 1);
	Data data = listing.getDataAt(addr(10));
	ByteDataType dt = (ByteDataType) data.getDataType();
	Settings defaultSettings = dt.getDefaultSettings();
	defaultSettings.setLong("format", FormatSettingsDefinition.CHAR);
	defaultSettings.setLong("signed", 0);
	defaultSettings.setLong("padded", 1);

	SettingsDefinition[] defs = dt.getSettingsDefinitions();
	for (int i = 0; i < defs.length; i++) {

		if (defs[i] instanceof EnumSettingsDefinition) {
			EnumSettingsDefinition enumDef = (EnumSettingsDefinition) defs[i];
			int value = enumDef.getChoice(data);
			enumDef.setChoice(data, value);
			if (i == 0) {
				assertEquals(FormatSettingsDefinition.CHAR, data.getLong("format").longValue());
			}
			else if (i == 1) {
				assertEquals(0, data.getLong("signed").longValue());
			}
			else if (i == 2) {
				assertEquals(1, data.getLong("padded").longValue());
			}

		}
	}
}
 
Example 4
Source File: FieldNameFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getFieldName(Data data) {
	Data parent = data.getParent();
	if (parent != null && (parent.getDataType() instanceof Array)) {
		String indexStr = format.prefix +
			Integer.toString(data.getComponentIndex(), format.radix) + format.postfix;
		return "[" + indexStr + "]";
	}
	return data.getFieldName();
}
 
Example 5
Source File: CreateDataInStructureCmdTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
   public void testCreateNoFitData() throws Exception {

	long startOffset = UNDEFINED_AREA;

	int structLen = 1;
	Command cmd = new CreateStructureCmd(addr(startOffset), structLen);
	cmd.applyTo(program);

	cmd = new CreateDataInStructureCmd(addr(startOffset), new int[] { 0 }, new ByteDataType());
	assertTrue(cmd.applyTo(program));

	cmd = new CreateDataInStructureCmd(addr(startOffset), new int[] { 0 }, new WordDataType());
	assertTrue(cmd.applyTo(program));

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(2, d.getLength());

	Structure struct = (Structure) d.getDataType();
	struct.setName("TestStructA");
	assertEquals(2, struct.getLength());
	assertEquals(1, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(2, comp.getLength());
	assertTrue(comp.getDataType() instanceof WordDataType);

	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	assertEquals(struct, structA);

}
 
Example 6
Source File: SettingsTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNames() throws Exception {
	listing.createData(addr(10), new ByteDataType(), 1);
	Data data = listing.getDataAt(addr(10));
	ByteDataType dt = (ByteDataType) data.getDataType();
	Settings defaultSettings = dt.getDefaultSettings();
	defaultSettings.setString("color", "red");
	defaultSettings.setLong("someLongValue", 10);
	defaultSettings.setByteArray("bytes", new byte[] { 0, 1, 2 });
	defaultSettings.setString("endian", "big Endian");

	String[] names = defaultSettings.getNames();
	assertEquals(4, names.length);
}
 
Example 7
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 8
Source File: ProgramStructureProviderContext.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ProgramStructureProviderContext(Program program, ProgramLocation loc) {
	this.program = program;

	int dataPath[] = loc.getComponentPath();
	Data data = program.getListing().getDefinedDataContaining(loc.getAddress());
	data = data.getComponent(dataPath);
	this.addr = data.getMinAddress();
	myoffset = data.getParentOffset();
	data = data.getParent();
	struct = (Structure) data.getDataType();
}
 
Example 9
Source File: SettingsTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsEmpty() throws Exception {
	listing.createData(addr(10), new ByteDataType(), 1);
	Data data = listing.getDataAt(addr(10));
	ByteDataType dt = (ByteDataType) data.getDataType();
	Settings defaultSettings = dt.getDefaultSettings();
	defaultSettings.setString("color", "red");
	defaultSettings.setLong("someLongValue", 10);
	defaultSettings.setByteArray("bytes", new byte[] { 0, 1, 2 });

	assertTrue(!defaultSettings.isEmpty());

	defaultSettings.clearAllSettings();
	assertTrue(defaultSettings.isEmpty());
}
 
Example 10
Source File: CreateDataInStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
   public void testCreateDataInCompoundStructure() throws Exception {

	// Create structure data type: TestStructA
	testCreateDataInStructure();
	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	DataType structAPtr = program.getDataTypeManager().getPointer(structA);

	long startOffset = UNDEFINED_AREA + structA.getLength();
	int defaultPtrLen = program.getAddressFactory().getDefaultAddressSpace().getPointerSize();

	int structLen = defaultPtrLen + structA.getLength();

	Command cmd = new CreateStructureCmd(addr(startOffset), structLen);
	cmd.applyTo(program);

	cmd = new CreateDataInStructureCmd(addr(startOffset), new int[] { 0 }, structA);
	cmd.applyTo(program);

	cmd = new CreateDataInStructureCmd(addr(startOffset), new int[] { 1 }, structAPtr);
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	struct.setName("TestStructB");
	assertEquals(structLen, struct.getLength());
	assertEquals(2, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(structA.getLength(), comp.getLength());
	assertTrue(comp.getDataType().isEquivalent(structA));

	comp = struct.getComponent(1);
	assertEquals(defaultPtrLen, comp.getLength());
	assertTrue(comp.getDataType().isEquivalent(structAPtr));

	DataType structB =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructB");
	assertEquals(struct, structB);
}
 
Example 11
Source File: CreateArrayInStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
   public void testCreateArrayInNestedStructureCmd() throws Exception {

	long startOffset = UNDEFINED_AREA;

	Structure struct1 = new StructureDataType("IntStruct", 0);
	struct1.add(new ByteDataType());
	struct1.add(new WordDataType());
	struct1.add(new DWordDataType());
	struct1.add(new QWordDataType());

	Command cmd = new CreateDataCmd(addr(startOffset + 1), struct1);
	cmd.applyTo(program);
	Data dataAt = program.getListing().getDataAt(addr(startOffset + 1));
	struct1 = (Structure) dataAt.getDataType();

	int structLen = struct1.getLength() + 10;
	cmd = new CreateStructureCmd(addr(startOffset), structLen);
	cmd.applyTo(program);

	DataType dt = new Pointer16DataType(new ByteDataType());
	cmd = new CreateArrayInStructureCmd(addr(startOffset), 3, dt, new int[] { 1, 1 });
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	struct.setName("TestStructA");
	assertEquals(structLen, struct.getLength());
	assertEquals(11, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(1, comp.getLength());
	assertEquals(DataType.DEFAULT, comp.getDataType());

	comp = struct.getComponent(1);
	assertEquals(struct1.getLength(), comp.getLength());
	assertTrue(comp.getDataType() instanceof Structure);
	Structure s = (Structure) comp.getDataType();
	assertEquals(3, s.getNumComponents());
	assertEquals(s, struct1);

	comp = struct1.getComponent(0);
	assertEquals(1, comp.getLength());
	assertTrue(comp.getDataType() instanceof ByteDataType);

	comp = struct1.getComponent(1);
	assertEquals(6, comp.getLength());
	assertTrue(comp.getDataType() instanceof Array);
	Array a = (Array) comp.getDataType();
	assertEquals(2, a.getElementLength());
	assertEquals(3, a.getNumElements());
	assertTrue(a.getDataType().isEquivalent(dt));

	comp = struct1.getComponent(2);
	assertEquals(8, comp.getLength());
	assertTrue(comp.getDataType() instanceof QWordDataType);

	for (int i = 2; i < 11; i++) {
		comp = struct.getComponent(i);
		assertEquals(1, comp.getLength());
		assertEquals(DataType.DEFAULT, comp.getDataType());
	}

	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	assertEquals(struct, structA);
}
 
Example 12
Source File: CreateArrayInStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
   public void testCreateArrayInStructureCmd() throws Exception {

	Address addr = addr(UNDEFINED_AREA);
	int structLen = 30;
	Command cmd = new CreateStructureCmd(addr, structLen);
	cmd.applyTo(program);

	DataType dt = new Pointer16DataType(new ByteDataType());
	cmd = new CreateArrayInStructureCmd(addr, 10, dt, new int[] { 0 });
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr);
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	struct.setName("TestStructA");
	assertEquals(structLen, struct.getLength());
	assertEquals(11, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(20, comp.getLength());
	assertTrue(comp.getDataType() instanceof Array);
	Array a = (Array) comp.getDataType();
	assertEquals(2, a.getElementLength());
	assertEquals(10, a.getNumElements());
	assertTrue(a.getDataType().isEquivalent(dt));

	for (int i = 1; i < 11; i++) {
		comp = struct.getComponent(i);
		assertEquals(1, comp.getLength());
		assertEquals(DataType.DEFAULT, comp.getDataType());
	}

	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	assertEquals(struct, structA);
}
 
Example 13
Source File: CreateDataInStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
   public void testCreateDataInStructure() throws Exception {

	long startOffset = UNDEFINED_AREA;
	DataType floatPtr = program.getDataTypeManager().getPointer(new FloatDataType());
	int defaultPtrLen = program.getAddressFactory().getDefaultAddressSpace().getPointerSize();

	int structLen = defaultPtrLen + 1;

	Command cmd = new CreateStructureCmd(addr(startOffset), structLen);
	cmd.applyTo(program);

	cmd = new CreateDataInStructureCmd(addr(startOffset), new int[] { 0 }, new ByteDataType());
	cmd.applyTo(program);

	cmd = new CreateDataInStructureCmd(addr(startOffset), new int[] { 1 }, floatPtr);
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	struct.setName("TestStructA");
	assertEquals(structLen, struct.getLength());
	assertEquals(2, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(1, comp.getLength());
	assertTrue(comp.getDataType() instanceof ByteDataType);

	comp = struct.getComponent(1);
	assertEquals(defaultPtrLen, comp.getLength());
	assertTrue(floatPtr.isEquivalent(comp.getDataType()));

	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	assertEquals(struct, structA);
}
 
Example 14
Source File: CreateStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
   public void testCreateStructure() {

	long startOffset = UNDEFINED_AREA;
	long offset = startOffset;
	DataType floatPtr = program.getDataTypeManager().getPointer(new FloatDataType());
	DataType stringPtr = program.getDataTypeManager().getPointer(new StringDataType());
	int defaultPtrLen = program.getAddressFactory().getDefaultAddressSpace().getPointerSize();

	offset = createData(offset, new ByteDataType());
	offset = createData(offset, floatPtr);
	offset = createData(offset, 10, new StringDataType());
	offset = createArray(offset, 8, 4, stringPtr);

	int structLen = (int) (offset - startOffset);
	CreateStructureCmd cmd =
		new CreateStructureCmd("TestStructA", addr(startOffset), structLen);
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());

	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	assertEquals(structLen, struct.getLength());
	assertEquals(4, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(1, comp.getLength());
	assertTrue(comp.getDataType() instanceof ByteDataType);

	comp = struct.getComponent(1);
	assertEquals(defaultPtrLen, comp.getLength());
	assertTrue(floatPtr.isEquivalent(comp.getDataType()));

	comp = struct.getComponent(2);
	assertEquals(10, comp.getLength());
	assertTrue(comp.getDataType() instanceof StringDataType);

	comp = struct.getComponent(3);
	assertEquals(8 * defaultPtrLen, comp.getLength());
	assertTrue(comp.getDataType() instanceof Array);
	Array a = (Array) comp.getDataType();
	assertEquals(defaultPtrLen, a.getElementLength());
	assertEquals(8, a.getNumElements());
	assertTrue(a.getDataType().isEquivalent(stringPtr));

	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	assertEquals(struct, structA);
}
 
Example 15
Source File: DataAction4Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testStructureModifyInsideArray() throws Exception {

	// Create structure (length = 0x20)

	makeSelection(0x01006a00, 0x01006a1f);
	//assertNull("No data expected", getContextData());

	doCreateStructureAction();
	clearSelection();

	Data structData = getContextData();
	assertNotNull(structData);
	assertTrue(structData.isStructure());
	DataType structDt = structData.getDataType();

	doAction(CREATE_ARRAY, false);

	final NumberInputDialog dlg1 = waitForDialogComponent(NumberInputDialog.class);
	assertNotNull("Expected element count input dialog", dlg1);

	Runnable r = () -> dlg1.setInput(5);
	runSwing(r);
	waitForPostedSwingRunnables();

	pressButtonByText(dlg1, "OK");

	waitForPostedSwingRunnables();

	Set<DockingActionIf> actions = getActionsByOwner(tool, plugin.getName());
	checkOnArray(actions, structDt, 5);

	// Expand structure
	cb.toggleOpen(getContextData());

	gotoLocation(0x01006a00, new int[] { 0 });

	// Expand structure
	cb.toggleOpen(getContextData());

	gotoLocation(0x01006a00, new int[] { 0, 0 });

	doAction(DEFINE_BYTE, true);

	actions = getActionsByOwner(tool, plugin.getName());
	checkOnDefined(actions, ByteDataType.class);

	gotoLocation(0x01006a01, new int[] { 0, 1 });

	doAction(DEFINE_FLOAT, true);

	actions = getActionsByOwner(tool, plugin.getName());
	checkOnDefined(actions, FloatDataType.class);

	Data pdata = getContextData().getParent();
	assertNotNull(pdata);
	assertTrue(pdata.isStructure());
	Structure struct = (Structure) pdata.getDataType();
	assertEquals(0x20, struct.getLength());
	DataTypeComponent[] structComps = struct.getComponents();
	assertTrue(structComps[0].getDataType() instanceof ByteDataType);
	assertTrue(structComps[1].getDataType() instanceof FloatDataType);

	pdata = pdata.getParent();
	assertNotNull(pdata);
	assertTrue(pdata.isArray());
	assertEquals(5 * 0x20, pdata.getLength());

	assertNull(pdata.getParent());

}
 
Example 16
Source File: CreateDataInStructureCmd.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
	 * @see ghidra.framework.cmd.Command#applyTo(ghidra.framework.model.DomainObject)
	 */
	@Override
	public boolean applyTo(DomainObject obj) {
		Program program = (Program) obj;
		Data data = program.getListing().getDefinedDataContaining(addr);
		Data dataComp = data.getComponent(componentPath);
		if (dataComp == null) {
			msg = "Component data not found";
			return false;
		}

		DataType parentDataType = dataComp.getParent().getBaseDataType();

		if (!(parentDataType instanceof Composite)) {
			msg = "Invalid command usage";
			return false;
		}

		if (newDataType instanceof FactoryDataType) {
			msg = "Factory data-type not allowed in structure or union: " + newDataType.getName();
			return false;
		}

		DataType existingDT = dataComp.getDataType();
		int index = dataComp.getComponentIndex();

		newDataType =
			DataUtilities.reconcileAppliedDataType(existingDT, newDataType, stackPointers);

		if (newDataType instanceof Dynamic) {
			msg = "Dynamically sized data-type not allowed: " + newDataType.getName();
			return false;
		}

		// Ensure that dynamically sized types adjust to the data type manager
		newDataType = program.getDataTypeManager().resolve(newDataType, null);

		try {
			if (parentDataType instanceof Structure) {
				Structure struct = (Structure) parentDataType;
				if (newDataType == DataType.DEFAULT) {
					struct.clearComponent(index);
				}
				else {
//			        MemBuffer memBuf = new ProgramStructureProviderContext(program,addr, 
//	    	        					struct, dataComp.getParentOffset());
					DataTypeInstance dti = DataTypeInstance.getDataTypeInstance(newDataType, -1);
					struct.replace(index, dti.getDataType(), dti.getLength());
				}
			}
			else if (parentDataType instanceof Union) {
				Union union = (Union) parentDataType;
				DataTypeComponent comp = union.getComponent(index);
				String comment = comp.getComment();
				String fieldName = comp.getFieldName();
				union.insert(index, newDataType);
				union.delete(index + 1);
				comp = union.getComponent(index);
				comp.setComment(comment);
				comp.setFieldName(fieldName);
			}

		}
		catch (Exception e) {
			msg = e.getMessage();
			return false;
		}
		return true;
	}
 
Example 17
Source File: CreateDataInStructureBackgroundCmd.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
	 * 
	 * @see ghidra.framework.cmd.BackgroundCommand#applyTo(ghidra.framework.model.DomainObject, ghidra.util.task.TaskMonitor)
	 */
	@Override
	public boolean applyTo(DomainObject obj, TaskMonitor monitor) {

		Program program = (Program) obj;
		Data data = program.getListing().getDefinedDataContaining(addr);
		Data startData = data.getComponent(startPath);
		if (startData == null) {
			setStatusMsg("Component data not found");
			return false;
		}

		Data parent = startData.getParent();
		DataType parentDataType = parent.getBaseDataType();

		if (!(parentDataType instanceof Structure)) {
			setStatusMsg("Range based operation only supported within Structures");
			return false;
		}

		DataType existingDT = startData.getDataType();
		int startIndex = startData.getComponentIndex();
		Data lastComp = parent.getComponentAt(
			(int) (startData.getMinAddress().subtract(parent.getMinAddress()) + length - 1));
		int endIndex = lastComp.getComponentIndex();

		Structure struct = (Structure) parentDataType;

		// TODO: Is looking at the first component data-type (existingDT) the right thing to do for a selection ??

		if (newDataType instanceof FactoryDataType) {
			setStatusMsg("Factory data-type not allowed in structure: " + newDataType.getName());
			return false;
		}

		newDataType = newDataType.clone(program.getDataTypeManager());
		newDataType =
			DataUtilities.reconcileAppliedDataType(existingDT, newDataType, stackPointers);

		if (newDataType instanceof Dynamic && !((Dynamic) newDataType).canSpecifyLength()) {
			setStatusMsg(
				"Non-sizable Dynamic data-type not allowed in structure: " + newDataType.getName());
			return false;
		}

		for (int i = endIndex; i >= startIndex; i--) {
			struct.clearComponent(i);
		}

		if (newDataType != DataType.DEFAULT) {
			int index = startIndex;
			int numCreated = 0;
			while (length > 0) {
				try {
//			        MemBuffer memBuf = new ProgramStructureProviderContext(program,addr, 
//    	    	    					struct, struct.getComponent(index).getOffset());
					DataTypeInstance dti =
						DataTypeInstance.getDataTypeInstance(newDataType, length);
					if (dti == null || dti.getLength() > length) {
						break;
					}
					DataTypeComponent dtc =
						struct.replace(index, dti.getDataType(), dti.getLength());
					length -= dtc.getLength();
					numCreated++;
					index++;
				}

				catch (Exception e) {
					setStatusMsg(e.getMessage());
					return false;
				}
			}
			if (numCreated == 0) {
				setStatusMsg("Not enough space");
				return false;
			}
		}
		return true;
	}
 
Example 18
Source File: CreateStructureInStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * This method is the same as {@link #testCreateStructureInStructure()} 
 * with the exception that this method creates a structure before creating
 * the Command object.
 * 
 * @throws Exception If there is a problem setting the names of the
 *         structures.
 */
@Test
   public void testCreateStructureInStructureFromStructure() {

	long startOffset = UNDEFINED_AREA;
	long offset = startOffset;
	DataType floatPtr = program.getDataTypeManager().getPointer(new FloatDataType());
	DataType stringPtr = program.getDataTypeManager().getPointer(new StringDataType());
	int defaultPtrLen = program.getAddressFactory().getDefaultAddressSpace().getPointerSize();

	offset = createData(offset, new ByteDataType());
	offset = createData(offset, floatPtr);
	offset = createData(offset, 10, new StringDataType());
	offset = createArray(offset, 8, 4, stringPtr);

	int structLen = (int) (offset - startOffset);
	Command cmd = new CreateStructureCmd("TestStructA", addr(startOffset), structLen);
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	assertEquals(structLen, struct.getLength());
	assertEquals(4, struct.getNumComponents());

	Address address = addr(startOffset);
	int[] fromPath = new int[] { 1 };
	int[] toPath = new int[] { 2 };
	Structure childStructure = StructureFactory.createStructureDataTypeInStrucuture(program,
		address, fromPath, toPath, "TestStructB", true);
	cmd = new CreateStructureInStructureCmd(childStructure, address, fromPath, toPath);
	cmd.applyTo(program);

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(1, comp.getLength());
	assertTrue(comp.getDataType() instanceof ByteDataType);

	comp = struct.getComponent(1);
	assertEquals(defaultPtrLen + 10, comp.getLength());
	assertTrue(comp.getDataType() instanceof Structure);
	Structure s = (Structure) comp.getDataType();

	comp = struct.getComponent(2);
	assertEquals(8 * defaultPtrLen, comp.getLength());
	assertTrue(comp.getDataType() instanceof Array);
	Array a = (Array) comp.getDataType();
	assertEquals(defaultPtrLen, a.getElementLength());
	assertEquals(8, a.getNumElements());
	assertTrue(a.getDataType().isEquivalent(stringPtr));

	assertEquals(2, s.getNumComponents());
	assertEquals(defaultPtrLen + 10, s.getLength());

	comp = s.getComponent(0);
	assertEquals(defaultPtrLen, comp.getLength());
	assertTrue(comp.getDataType().isEquivalent(floatPtr));

	comp = s.getComponent(1);
	assertEquals(10, comp.getLength());
	assertTrue(comp.getDataType() instanceof StringDataType);

	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	assertEquals(struct, structA);

	DataType structB =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructB");
	assertEquals(s, structB);
}
 
Example 19
Source File: CreateStructureCmdTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
   public void testCreateCompoundStructure() {

	// Create structure data type: TestStructA
	testCreateStructure();
	DataType structA =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructA");
	DataType structAPtr = program.getDataTypeManager().getPointer(structA);

	long startOffset = UNDEFINED_AREA + structA.getLength();
	long offset = startOffset;

	int defaultPtrLen = program.getAddressFactory().getDefaultAddressSpace().getPointerSize();

	offset = createData(offset, structA);
	offset = createData(offset, structAPtr);

	int structLen = (int) (offset - startOffset);
	CreateStructureCmd cmd =
		new CreateStructureCmd("TestStructB", addr(startOffset), structLen);
	cmd.applyTo(program);

	Data d = program.getListing().getDataAt(addr(startOffset));
	assertNotNull(d);
	assertTrue(d.isDefined());
	assertTrue(d.getDataType() instanceof Structure);
	assertEquals(structLen, d.getLength());

	Structure struct = (Structure) d.getDataType();
	assertEquals(structLen, struct.getLength());
	assertEquals(2, struct.getNumComponents());

	DataTypeComponent comp = struct.getComponent(0);
	assertEquals(structA.getLength(), comp.getLength());
	assertTrue(comp.getDataType().isEquivalent(structA));

	comp = struct.getComponent(1);
	assertEquals(defaultPtrLen, comp.getLength());
	assertTrue(comp.getDataType().isEquivalent(structAPtr));

	DataType structB =
		program.getDataTypeManager().getDataType(CategoryPath.ROOT, "TestStructB");
	assertEquals(struct, structB);
}
 
Example 20
Source File: FdeTable.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Creates an FDE Table at the specified Address.
 * 
 * @param addr Address at which the FDE Table should be created.
 * @param decoder the decoder for DWARF encoded exception handling information
 * @param fdeTableCnt the number of exception handler FDEs.
 * @throws MemoryAccessException if the needed memory can't be read.
 * @throws ExceptionHandlerFrameException if the FDE table can't be decoded.
 */
public void create(Address addr, DwarfEHDecoder decoder, long fdeTableCnt)
		throws MemoryAccessException, ExceptionHandlerFrameException {
	CreateStructureCmd dataCmd = null;
	long curFdeTableCnt = 0;
	
	if (addr == null || decoder == null || monitor.isCancelled()) {
		return;
	}
	
	initFdeTableDataType(decoder);
	
	monitor.setMessage("Creating Frame Description Table Entries");
	monitor.setShowProgressValue(true);
	monitor.setIndeterminate(false);
	monitor.initialize(fdeTableCnt);

	/* Create a new FDE structures beginning at startAddress */
	MemoryBlock curMemBlock = prog.getMemory().getBlock(".eh_frame_hdr");
	while( curMemBlock != null &&
		  (addr.compareTo( curMemBlock.getEnd()) < 0) && 
		  (curFdeTableCnt < fdeTableCnt) )
	{
		/* Create a new FDE structure */
		dataCmd = new CreateStructureCmd( fdeTableEntry, addr);
		dataCmd.applyTo(prog);
		
		/*
		 * -- Create references to the 'initial location' and 'data
		 * location' --
		 */
		Data fdeTableData = prog.getListing().getDataAt(addr);
		Structure fdeStruct = (Structure) fdeTableData.getDataType();

		DataTypeComponent locComponent = fdeStruct.getComponent(0);
		Address locComponentAddr = addr.add(locComponent.getOffset());
		DwarfDecodeContext locDecodeContext =
			new DwarfDecodeContext(prog, locComponentAddr, curMemBlock);
		Address locAddr = decoder.decodeAddress(locDecodeContext);

		// this is an indirect reference to code from the table,
		//  so tag reference as an indirect code flow
		prog.getReferenceManager().addMemoryReference(locComponentAddr, locAddr,
			RefType.INDIRECTION,
			SourceType.ANALYSIS, 0);

		DataTypeComponent dataComponent = fdeStruct.getComponent(1);
		Address dataComponentAddr = addr.add(dataComponent.getOffset());
		DwarfDecodeContext dataDecodeContext =
			new DwarfDecodeContext(prog, dataComponentAddr, curMemBlock);
		Address dataAddr = decoder.decodeAddress(dataDecodeContext);

		prog.getReferenceManager().addMemoryReference(dataComponentAddr, dataAddr, RefType.DATA,
				SourceType.ANALYSIS, 0);

		/* Increment curAddress by number of bytes in a FDE Table entry */
		curFdeTableCnt++;
		addr = addr.add(fdeTableEntry.getLength());

		monitor.incrementProgress(1);
	}
}