The DataType interface is the foundation of Ghidra’s type system. All datatypes implement this interface, providing methods for type metadata, encoding/decoding, and type operations.
Overview
Data types in Ghidra represent:
- Primitive Types - Built-in types (int, char, float, etc.)
- Pointer Types - References to other types
- Array Types - Fixed-size sequences of elements
- Structure Types - Composite types with named fields
- Union Types - Overlapping field storage
- Function Signatures - Function prototypes
- Typedef - Type aliases
Core Properties
Type Identity
Returns the name of the datatype.DataType intType = dtm.getDataType("/BuiltInTypes/int");
println(intType.getName()); // "int"
Returns the display name for this datatype.String displayName = dataType.getDisplayName();
Returns the full category path including the datatype name.String pathName = dataType.getPathName();
// e.g., "/MyTypes/MyStruct"
Sets the name of the datatype.dataType.setName("MyCustomType");
Throws InvalidNameException if the name is invalid, or DuplicateNameException if the name conflicts with another type in the same category.
Size and Alignment
Returns the length of this datatype as a number of 8-bit bytes.For primitive types, this is the raw data length (smallest varnode size).
Dynamic and factory datatypes return -1.DataType intType = dtm.getDataType("/BuiltInTypes/int");
int size = intType.getLength(); // Usually 4
No datatype except VoidDataType should ever return 0. Zero-length types should return 1 and have isZeroLength() return true.
Returns the aligned length, equivalent to C/C++ sizeof().For most types this equals getLength(). For some primitives it may differ due to alignment requirements.// x86 32-bit gcc: 80-bit long double
DataType longDouble = dtm.getDataType("/BuiltInTypes/long double");
int rawLen = longDouble.getLength(); // 10 bytes (80 bits)
int alignedLen = longDouble.getAlignedLength(); // 12 bytes (for alignment)
Returns the alignment requirement when this type is used within another type.int alignment = dataType.getAlignment();
Returns true if this datatype is defined with zero length.if (dataType.isZeroLength()) {
println("Zero-length type");
}
Zero-length types still return 1 from getLength().
Returns true if this datatype has not yet been fully defined (e.g., empty structure).if (dataType.isNotYetDefined()) {
println("Type is incomplete");
}
Category and Path
Returns the category path for this datatype.CategoryPath category = dataType.getCategoryPath();
println("Category: " + category.getPath());
setCategoryPath(CategoryPath path)
Sets the category path for this datatype.CategoryPath newCategory = new CategoryPath("/MyTypes");
dataType.setCategoryPath(newCategory);
Returns the complete datatype path.DataTypePath path = dataType.getDataTypePath();
Description
Returns a brief description of this datatype.String desc = dataType.getDescription();
setDescription(String description)
Sets a brief description for this datatype.dataType.setDescription("Custom structure for network packets");
Data Type Manager
Returns the DataTypeManager containing this datatype.DataTypeManager dtm = dataType.getDataTypeManager();
hasLanguageDependantLength()
Returns true if the length is determined by the DataOrganization.if (dataType.hasLanguageDependantLength()) {
println("Length depends on compiler/architecture");
}
Type Operations
Clone and Copy
clone(DataTypeManager dtm)
Returns an instance using the specified DataTypeManager, retaining unique identity.DataType cloned = dataType.clone(programDTM);
Returns the same instance if the DataTypeManager matches.
copy(DataTypeManager dtm)
Returns a new instance (shallow copy) with a new identity.DataType copy = dataType.copy(programDTM);
Equivalence
isEquivalent(DataType dt)
Checks if the given datatype is equivalent to this datatype.The meaning of “equivalent” is datatype-dependent.if (dataType1.isEquivalent(dataType2)) {
println("Types are equivalent");
}
Checks if this datatype depends on the existence of the given datatype.// byte[] depends on byte
ArrayDataType byteArray = new ArrayDataType(byteType, 10, 1);
boolean depends = byteArray.dependsOn(byteType); // true
Replace
replaceWith(DataType dataType)
Replaces the internals of this datatype with the internals of the given datatype.// Update existing type with new definition
existingStruct.replaceWith(newStructDefinition);
Throws UnsupportedOperationException if the datatype does not support change.
Value Operations
Get Value
getValue(MemBuffer buf, Settings settings, int length)
Returns the interpreted data value.For pointers, returns an Address. For integers, returns a Scalar.Data data = listing.getDataAt(addr);
DataType dt = data.getDataType();
Object value = dt.getValue(data, data.getDefaultSettings(), data.getLength());
if (value instanceof Scalar) {
Scalar scalar = (Scalar) value;
println("Value: " + scalar.getValue());
}
getValueClass(Settings settings)
Returns the Class of the value object returned by getValue().Class<?> valueClass = dataType.getValueClass(null);
if (valueClass == Address.class) {
println("This is a pointer type");
}
Representation
getRepresentation(MemBuffer buf, Settings settings, int length)
Returns a string representation of the data.Data data = listing.getDataAt(addr);
String repr = data.getDataType().getRepresentation(
data,
data.getDefaultSettings(),
data.getLength()
);
println("Value: " + repr);
Encoding
Returns true if this type supports encoding (patching).if (dataType.isEncodable()) {
// Can use encodeValue() and encodeRepresentation()
}
encodeValue(Object value, MemBuffer buf, Settings settings, int length)
Encodes a value object to bytes.if (dataType.isEncodable()) {
Scalar value = new Scalar(32, 0x12345678);
byte[] bytes = dataType.encodeValue(value, buf, settings, 4);
}
Throws DataTypeEncodeException if encoding fails.
encodeRepresentation(String repr, MemBuffer buf, Settings settings, int length)
Encodes a string representation to bytes.if (dataType.isEncodable()) {
byte[] bytes = dataType.encodeRepresentation("0x12345678", buf, settings, 4);
}
Mnemonic and Labels
getMnemonic(Settings settings)
Returns the mnemonic for this datatype.String mnemonic = dataType.getMnemonic(null);
Returns the default label prefix for this datatype.String prefix = dataType.getDefaultLabelPrefix();
// e.g., "DAT_" for data, "PTR_" for pointers
getDefaultAbbreviatedLabelPrefix()
Returns an abbreviated label prefix.String abbrev = dataType.getDefaultAbbreviatedLabelPrefix();
getDefaultLabelPrefix(MemBuffer buf, Settings settings, int len, DataTypeDisplayOptions options)
Returns the context-aware default label prefix.String prefix = dataType.getDefaultLabelPrefix(
data,
settings,
data.getLength(),
DataTypeDisplayOptions.DEFAULT
);
Settings
Returns the default settings for this datatype.Settings settings = dataType.getDefaultSettings();
Returns the settings definitions available for this datatype.SettingsDefinition[] defs = dataType.getSettingsDefinitions();
for (SettingsDefinition def : defs) {
println("Setting: " + def.getName());
}
getTypeDefSettingsDefinitions()
TypeDefSettingsDefinition[]
Returns settings definitions for use with TypeDef.
Universal ID
Returns the universal ID for this datatype.This is a unique identifier across all programs and archives.UniversalID uid = dataType.getUniversalID();
Source Archive
Returns the source archive where this type originated.SourceArchive archive = dataType.getSourceArchive();
setSourceArchive(SourceArchive archive)
Sets the source archive.
Timestamps
Returns the timestamp of the last change within the datatype manager.long timestamp = dataType.getLastChangeTime();
setLastChangeTime(long lastChangeTime)
Sets the last change timestamp.
getLastChangeTimeInSourceArchive()
Returns the timestamp of the last sync with the source archive.
setLastChangeTimeInSourceArchive(long timestamp)
Sets the source archive sync timestamp.
Type Relationships
Returns the parent datatypes that reference this datatype.Collection<DataType> parents = dataType.getParents();
for (DataType parent : parents) {
println("Parent: " + parent.getName());
}
This method is intended for DB-managed datatypes only.
Informs this datatype of a parent relationship.Reserved for internal use.
removeParent(DataType dt)
Removes a parent datatype relationship.Reserved for internal use.
State Checks
Returns true if this datatype has been deleted.if (dataType.isDeleted()) {
println("Type has been deleted");
}
Data Organization
Returns the DataOrganization associated with this datatype.DataOrganization dataOrg = dataType.getDataOrganization();
int ptrSize = dataOrg.getPointerSize();
Common Data Types
Built-in Types
// Access built-in types
DataTypeManager dtm = program.getDataTypeManager();
DataType byteType = dtm.getDataType("/BuiltInTypes/byte");
DataType charType = dtm.getDataType("/BuiltInTypes/char");
DataType shortType = dtm.getDataType("/BuiltInTypes/short");
DataType intType = dtm.getDataType("/BuiltInTypes/int");
DataType longType = dtm.getDataType("/BuiltInTypes/long");
DataType floatType = dtm.getDataType("/BuiltInTypes/float");
DataType doubleType = dtm.getDataType("/BuiltInTypes/double");
Creating Arrays
DataTypeManager dtm = program.getDataTypeManager();
DataType intType = dtm.getDataType("/BuiltInTypes/int");
// Create array of 10 ints
ArrayDataType intArray = new ArrayDataType(intType, 10, 4);
Listing listing = program.getListing();
listing.createData(addr, intArray);
Creating Pointers
DataTypeManager dtm = program.getDataTypeManager();
DataType charType = dtm.getDataType("/BuiltInTypes/char");
// Create char pointer
PointerDataType charPtr = new PointerDataType(charType);
listing.createData(addr, charPtr);
Creating Structures
StructureDataType struct = new StructureDataType("MyStruct", 0);
DataTypeManager dtm = program.getDataTypeManager();
DataType intType = dtm.getDataType("/BuiltInTypes/int");
DataType charType = dtm.getDataType("/BuiltInTypes/char");
// Add fields
struct.add(intType, 4, "id", "Unique identifier");
struct.add(charType, 1, "flag", "Status flag");
struct.add(new ArrayDataType(charType, 32, 1), "name", "Name string");
// Add to data type manager
DataType addedStruct = dtm.addDataType(struct, DataTypeConflictHandler.DEFAULT_HANDLER);
// Use the structure
listing.createData(addr, addedStruct);
Creating Unions
UnionDataType union = new UnionDataType("MyUnion");
DataType intType = dtm.getDataType("/BuiltInTypes/int");
DataType floatType = dtm.getDataType("/BuiltInTypes/float");
union.add(intType, "asInt", null);
union.add(floatType, "asFloat", null);
DataType addedUnion = dtm.addDataType(union, DataTypeConflictHandler.DEFAULT_HANDLER);
Example Usage
Working with Data Types
public void analyzeDataTypes(Program program) {
DataTypeManager dtm = program.getDataTypeManager();
Listing listing = program.getListing();
// Get all structures
Iterator<Structure> structs = dtm.getAllStructures();
while (structs.hasNext()) {
Structure struct = structs.next();
println("Structure: " + struct.getName());
println(" Size: " + struct.getLength());
println(" Alignment: " + struct.getAlignment());
// List components
DataTypeComponent[] components = struct.getComponents();
for (DataTypeComponent comp : components) {
println(" " + comp.getFieldName() + ": " + comp.getDataType().getName());
}
}
}
Creating Custom Types
public void createCustomTypes(Program program) throws Exception {
DataTypeManager dtm = program.getDataTypeManager();
// Create a structure for a file header
StructureDataType fileHeader = new StructureDataType("FileHeader", 0);
DataType intType = dtm.getDataType("/BuiltInTypes/int");
DataType shortType = dtm.getDataType("/BuiltInTypes/short");
fileHeader.add(intType, "magic", "Magic number");
fileHeader.add(shortType, "version", "Version number");
fileHeader.add(shortType, "flags", "Flags");
fileHeader.add(intType, "dataOffset", "Offset to data");
fileHeader.add(intType, "dataSize", "Size of data");
// Add to data type manager
DataType added = dtm.addDataType(fileHeader, DataTypeConflictHandler.DEFAULT_HANDLER);
// Use the type
Listing listing = program.getListing();
Data headerData = listing.createData(addr("00400000"), added);
// Access components
Data magicData = headerData.getComponent(0);
Object magicValue = magicData.getValue();
println("Magic: 0x" + Integer.toHexString(((Scalar) magicValue).getIntValue()));
}
Applying Types to Memory
public void applyDataTypes(Program program, Address addr) throws Exception {
DataTypeManager dtm = program.getDataTypeManager();
Listing listing = program.getListing();
// Apply integer
DataType intType = dtm.getDataType("/BuiltInTypes/int");
Data intData = listing.createData(addr, intType);
println("Int value: " + intData.getValue());
// Apply string
DataType stringType = new StringDataType();
Data strData = listing.createData(addr.add(4), stringType, 32);
println("String value: " + strData.getValue());
// Apply array
ArrayDataType byteArray = new ArrayDataType(
dtm.getDataType("/BuiltInTypes/byte"),
16,
1
);
Data arrayData = listing.createData(addr.add(36), byteArray);
println("Array has " + arrayData.getNumComponents() + " elements");
}
Package Location
ghidra.program.model.data.DataType
Constants
Singleton instance of default datatype
Instance of void datatype (deprecated, use VoidDataType.dataType)
Datatype name conflict suffix: ".conflict"