Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3170 Added bool bitfield support and transitioned BooleanDataType to extend AbstractIntegerDataType

ghidra19d2ab478c0d6
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm b/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm
index b42b96b11c..54d41a63ce 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/DataTypeEditors/StructureEditor.htm
@@ -1313,7 +1313,7 @@
entry can be directly entered or via the mouse wheel while the cursor is over this entry field.</P>
<P><B>Base Datatype</B> - (required) specifies the numeric datatype associated with the bitfield.
- Valid datatypes include primitive integer types (e.g., char, int, etc.), enum types, and
+ Valid datatypes include primitive integer types (e.g., char, bool, int, etc.), enum types, and
typedef types of integer or enum types. This input allows direct text input with auto-complete
assistance or via full access to a datatype tree chooser by clicking the '...' button.</P>
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java
index a608123d2f..1e70e4ca16 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java
@@ -323,6 +323,9 @@ class DataDB extends CodeUnitDB implements Data {
if (obj instanceof Scalar) {
return (Scalar) obj;
}
+ else if (obj instanceof Boolean) {
+ return new Scalar(getLength() * 8, ((Boolean) obj).booleanValue() ? 1 : 0);
+ }
else if (obj instanceof Address) {
Address addrObj = (Address) obj;
long offset = addrObj.getAddressableWordOffset();
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java
index 81a4dbea68..5026d857d9 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java
@@ -33,8 +33,6 @@ import ghidra.util.StringFormat;
*/
public abstract class AbstractIntegerDataType extends BuiltIn implements ArrayStringable {
- private static final long serialVersionUID = 1L;
-
static final String C_SIGNED_CHAR = "signed char";
static final String C_UNSIGNED_CHAR = "unsigned char";
static final String C_SIGNED_SHORT = "short";
@@ -59,7 +57,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
/**
* Constructor
* @param name a unique signed/unsigned data-type name (also used as the mnemonic)
- * @param signed
+ * @param signed true if signed, false if unsigned
* @param dtm data-type manager whose data organization should be used
*/
public AbstractIntegerDataType(String name, boolean signed, DataTypeManager dtm) {
@@ -92,7 +90,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
}
/**
- * Returns true if this is a signed integer data-type
+ * @return true if this is a signed integer data-type
*/
public boolean isSigned() {
return signed;
@@ -116,7 +114,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
}
/**
- * Returns the Assembly style data-type declaration
+ * @return the Assembly style data-type declaration
* for this data-type.
*/
public String getAssemblyMnemonic() {
@@ -124,7 +122,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
}
/**
- * Returns the C style data-type mnemonic
+ * @return the C style data-type mnemonic
* for this data-type.
* NOTE: currently the same as getCDeclaration().
*/
@@ -134,7 +132,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
}
/**
- * Returns the C style data-type declaration
+ * @return the C style data-type declaration
* for this data-type. Null is returned if
* no appropriate declaration exists.
*/
@@ -162,28 +160,6 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
return null;
}
- /**
- * Get the value of integer data as a BigInteger
- * @param buf the data buffer.
- * @param settings the settings to use.
- * @return BigInteger data value
- */
- public BigInteger getBigIntegerValue(MemBuffer buf, Settings settings) {
- Object value = getValue(buf, settings, getLength());
- if (value instanceof Scalar) {
- Scalar s = (Scalar) value;
- return s.getBigInteger();
- }
- if (value instanceof BigInteger) {
- return (BigInteger) value;
- }
- if (value instanceof Character) {
- // FIXME: consider flipping around getValue and getBigIntegerValue
- return BigInteger.valueOf((Character) value);
- }
- return null;
- }
-
@Override
public Object getValue(MemBuffer buf, Settings settings, int length) {
@@ -233,9 +209,6 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
return Scalar.class;
}
- /**
- * @see ghidra.program.model.data.DataType#getRepresentation(ghidra.program.model.mem.MemBuffer, ghidra.docking.settings.Settings, int)
- */
@Override
public String getRepresentation(MemBuffer buf, Settings settings, int length) {
@@ -269,6 +242,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
* Get integer representation of the big-endian value.
* @param bigInt BigInteger value with the appropriate sign
* @param settings integer format settings (PADDING, FORMAT, etc.)
+ * @param bitLength number of value bits to be used from bigInt
* @return formatted integer string
*/
public String getRepresentation(BigInteger bigInt, Settings settings, int bitLength) {
@@ -382,7 +356,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
* the element at index <code>i</code> points to the datatype of size <code>i+1</code>,
* with additional types with no size restriction appended after the first 8.
*
- * @return
+ * @return array of all signed integer types (char and bool types excluded)
*/
private static AbstractIntegerDataType[] getSignedTypes() {
if (signedTypes == null) {
@@ -400,7 +374,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
* the element at index <code>i</code> points to the datatype of size <code>i+1</code>,
* with additional types with no size restriction appended after the first 8.
*
- * @return
+ * @return array of all unsigned integer types (char and bool types excluded)
*/
private static AbstractIntegerDataType[] getUnsignedTypes() {
if (unsignedTypes == null) {
@@ -455,6 +429,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
* Returns all built-in signed integer data-types.
* @param dtm optional program data-type manager, if specified
* generic data-types will be returned in place of fixed-sized data-types.
+ * @return array of all signed integer types (char and bool types excluded)
*/
public static AbstractIntegerDataType[] getSignedDataTypes(DataTypeManager dtm) {
AbstractIntegerDataType[] dataTypes = getSignedTypes().clone();
@@ -524,6 +499,7 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
* Returns all built-in unsigned integer data-types
* @param dtm optional program data-type manager, if specified
* generic data-types will be returned in place of fixed-sized data-types.
+ * @return array of all unsigned integer types (char and bool types excluded)
*/
public static AbstractIntegerDataType[] getUnsignedDataTypes(DataTypeManager dtm) {
AbstractIntegerDataType[] dataTypes = getUnsignedTypes().clone();
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BooleanDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BooleanDataType.java
index 0df2516b16..7604765125 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BooleanDataType.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/BooleanDataType.java
@@ -15,6 +15,8 @@
*/
package ghidra.program.model.data;
+import java.math.BigInteger;
+
import ghidra.docking.settings.Settings;
import ghidra.docking.settings.SettingsDefinition;
import ghidra.program.model.lang.DecompilerLanguage;
@@ -24,7 +26,7 @@ import ghidra.program.model.mem.MemoryAccessException;
/**
* Provides a definition of an Ascii byte in a program.
*/
-public class BooleanDataType extends BuiltIn {
+public class BooleanDataType extends AbstractIntegerDataType {
private final static long serialVersionUID = 1;
@@ -40,7 +42,7 @@ public class BooleanDataType extends BuiltIn {
}
public BooleanDataType(DataTypeManager dtm) {
- super(null, "bool", dtm);
+ super("bool", false, dtm);
}
@Override
@@ -55,6 +57,11 @@ public class BooleanDataType extends BuiltIn {
return name;
}
+ @Override
+ public String getCDeclaration() {
+ return name;
+ }
+
@Override
public int getLength() {
return 1; // TODO: Size should probably be based upon data organization
@@ -94,9 +101,12 @@ public class BooleanDataType extends BuiltIn {
return b.booleanValue() ? "TRUE" : "FALSE";
}
- /**
- * @see ghidra.program.model.data.BuiltIn#getBuiltInSettingsDefinitions()
- */
+ @Override
+ public String getRepresentation(BigInteger bigInt, Settings settings, int bitLength) {
+ return bigInt.testBit(0) ? "TRUE" : "FALSE";
+
+ }
+
@Override
protected SettingsDefinition[] getBuiltInSettingsDefinitions() {
return SETTINGS_DEFS;
@@ -112,4 +122,10 @@ public class BooleanDataType extends BuiltIn {
return "BOOL";
}
+ @Override
+ public AbstractIntegerDataType getOppositeSignednessDataType() {
+ // TODO: only unsigned supported
+ return this;
+ }
+
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java
index fffaed47c1..09b05e6453 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java
@@ -493,7 +493,12 @@ public class PcodeDataTypeManager {
FunctionPrototype fproto = new FunctionPrototype(fdef, cspec, voidInputIsVarargs);
fproto.buildPrototypeXML(resBuf, this);
}
- else if (type instanceof AbstractIntegerDataType) {
+ else if (type instanceof BooleanDataType) {
+ SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "bool");
+ SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());
+ resBuf.append('>');
+ }
+ else if (type instanceof AbstractIntegerDataType) { // must handle char and bool above
boolean signed = ((AbstractIntegerDataType) type).isSigned();
int sz = type.getLength();
if (sz <= 0) {
@@ -503,11 +508,6 @@ public class PcodeDataTypeManager {
SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", sz);
resBuf.append('>');
}
- else if (type instanceof BooleanDataType) {
- SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "bool");
- SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());
- resBuf.append('>');
- }
else if (type instanceof AbstractFloatDataType) {
SpecXmlUtils.encodeStringAttribute(resBuf, "metatype", "float");
SpecXmlUtils.encodeSignedIntegerAttribute(resBuf, "size", type.getLength());