Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3000 tweak handling of flex arrays in DWARF import.

dev7473687cf6f14d91d3
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporter.java
index 918a25ee8e..53a605aa2d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporter.java
@@ -16,51 +16,18 @@
package ghidra.app.util.bin.format.dwarf4.next;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.IdentityHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Set;
+import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import ghidra.app.plugin.core.datamgr.util.DataTypeUtils;
-import ghidra.app.util.bin.format.dwarf4.DIEAggregate;
-import ghidra.app.util.bin.format.dwarf4.DWARFUtil;
-import ghidra.app.util.bin.format.dwarf4.DebugInfoEntry;
-import ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute;
-import ghidra.app.util.bin.format.dwarf4.encoding.DWARFEndianity;
-import ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag;
+import ghidra.app.util.bin.format.dwarf4.*;
+import ghidra.app.util.bin.format.dwarf4.encoding.*;
import ghidra.app.util.bin.format.dwarf4.expression.DWARFExpressionException;
import ghidra.program.database.data.DataTypeUtilities;
-import ghidra.program.model.data.Array;
-import ghidra.program.model.data.ArrayDataType;
-import ghidra.program.model.data.BitFieldDataType;
-import ghidra.program.model.data.CategoryPath;
-import ghidra.program.model.data.DataType;
-import ghidra.program.model.data.DataTypeComponent;
-import ghidra.program.model.data.DataTypeConflictHandler;
-import ghidra.program.model.data.DataTypeImpl;
-import ghidra.program.model.data.DataTypeManager;
-import ghidra.program.model.data.DefaultDataType;
+import ghidra.program.model.data.*;
import ghidra.program.model.data.Enum;
-import ghidra.program.model.data.EnumDataType;
-import ghidra.program.model.data.FunctionDefinitionDataType;
-import ghidra.program.model.data.GenericCallingConvention;
-import ghidra.program.model.data.InvalidDataTypeException;
-import ghidra.program.model.data.ParameterDefinition;
-import ghidra.program.model.data.ParameterDefinitionImpl;
-import ghidra.program.model.data.Pointer;
-import ghidra.program.model.data.PointerDataType;
-import ghidra.program.model.data.Structure;
-import ghidra.program.model.data.StructureDataType;
-import ghidra.program.model.data.TypeDef;
-import ghidra.program.model.data.TypedefDataType;
-import ghidra.program.model.data.UnionDataType;
import ghidra.util.InvalidNameException;
import ghidra.util.Msg;
import ghidra.util.exception.DuplicateNameException;
@@ -621,6 +588,8 @@ public class DWARFDataTypeImporter {
*/
private void populateStubUnion(DWARFDataType ddt, DIEAggregate diea)
throws IOException, DWARFExpressionException {
+ long unionSize = diea.getUnsignedLong(DWARFAttribute.DW_AT_byte_size, -1);
+
UnionDataType union = (UnionDataType) ddt.dataType;
for (DebugInfoEntry childEntry : diea.getHeadFragment().getChildren(
DWARFTag.DW_TAG_member)) {
@@ -647,10 +616,16 @@ public class DWARFDataTypeImporter {
continue;
}
+ if (childDT.dataType instanceof Dynamic ||
+ childDT.dataType instanceof FactoryDataType) {
+ DWARFUtil.appendDescription(union, memberDesc("Missing member",
+ "dynamic length type", memberName, childDT, -1, bitSize, -1), "\n");
+ continue;
+ }
int dtLen = childDT.dataType.getLength();
- if (dtLen == 0) {
- DWARFUtil.appendDescription(union, memberDesc("Missing member", "zero length type",
- memberName, childDT, -1, bitSize, -1), "\n");
+ if (unionSize != -1 && dtLen > unionSize) {
+ DWARFUtil.appendDescription(union, memberDesc("Missing member",
+ "data type larger than union", memberName, childDT, -1, bitSize, -1), "\n");
continue;
}
@@ -695,6 +670,23 @@ public class DWARFDataTypeImporter {
}
}
}
+
+ if (union.getLength() < unionSize) {
+ // if the Ghidra union data type is smaller than the DWARF union, pad it out
+ DataType padding = Undefined.getUndefinedDataType((int) unionSize);
+ try {
+ union.add(padding, null,
+ "Automatically generated padding to match DWARF declared size");
+ }
+ catch (IllegalArgumentException exc) {
+ DWARFUtil.appendDescription(union,
+ "Failed to add padding to union, size should be " + unionSize, "\n");
+ }
+ }
+ if (unionSize > 0 && union.getLength() > unionSize) {
+ DWARFUtil.appendDescription(union, "Imported union size (" + union.getLength() +
+ ") is larger than DWARF value (" + unionSize + ")", "\n");
+ }
}
/**
@@ -829,10 +821,10 @@ public class DWARFDataTypeImporter {
}
}
- int dtLen = childDT.dataType.getLength();
- if (dtLen == 0) {
+ if (childDT.dataType instanceof Dynamic ||
+ childDT.dataType instanceof FactoryDataType) {
DWARFUtil.appendDescription(structure, memberDesc("Missing member",
- "zero length type", memberName, childDT, memberOffset, bitSize, -1), "\n");
+ "dynamic length type", memberName, childDT, memberOffset, bitSize, -1), "\n");
continue;
}
@@ -845,12 +837,21 @@ public class DWARFDataTypeImporter {
// TODO: rename parent struct here. use .conflict or _basetype?
//}
- if (childDT.isEmptyArrayType && childDT.dataType instanceof Array &&
- memberOffset == structure.getLength() &&
- structure.getFlexibleArrayComponent() == null) {
+ if (childDT.isEmptyArrayType && childDT.dataType instanceof Array) {
- DataType arrayElementType = ((Array) childDT.dataType).getDataType();
- structure.setFlexibleArrayComponent(arrayElementType, memberName, null);
+ if (memberOffset == structure.getLength() &&
+ structure.getFlexibleArrayComponent() == null) {
+ DataType arrayElementType = ((Array) childDT.dataType).getDataType();
+ structure.setFlexibleArrayComponent(arrayElementType, memberName, null);
+ }
+ else {
+ DWARFUtil.appendDescription(structure,
+ memberDesc("Missing member",
+ "Unsupported interior flex array: " + childDT.dataType.getName(),
+ memberName, childDT, memberOffset, -1, -1),
+ "\n");
+
+ }
// skip the rest of this loop as it deals with adding component children members.
continue;
@@ -869,7 +870,7 @@ public class DWARFDataTypeImporter {
int containerLen;
if (hasMemberOffset) {
int byteSize = childDIEA.parseInt(DWARFAttribute.DW_AT_byte_size, -1);
- containerLen = byteSize <= 0 ? dtLen : byteSize;
+ containerLen = byteSize <= 0 ? childDT.dataType.getLength() : byteSize;
}
else {
containerLen = structure.getLength();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporterTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporterTest.java
index dc0f9f21fa..dbf0203095 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporterTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeImporterTest.java
@@ -19,8 +19,7 @@ import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.*;
import static org.junit.Assert.*;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
import org.junit.Test;
@@ -630,6 +629,86 @@ public class DWARFDataTypeImporterTest extends DWARFTestBase {
assertEquals("anon_struct_for_f3_f4", anonDT.getName());
}
+ @Test
+ public void testStructFlexarray() throws CancelledException, IOException, DWARFException {
+
+ DebugInfoEntry intDIE = addInt(cu);
+ DebugInfoEntry arrayDIE = newArray(cu, intDIE, false, -1);
+
+ DebugInfoEntry structDIE = newStruct("mystruct", 100).create(cu);
+ newMember(structDIE, "f1", intDIE, 0).create(cu);
+ newMember(structDIE, "flexarray", arrayDIE, 100).create(cu);
+
+ importAllDataTypes();
+
+ Structure structdt = (Structure) dataMgr.getDataType(rootCP, "mystruct");
+ assertNotNull(structdt.getFlexibleArrayComponent());
+
+ }
+
+ @Test
+ public void testStructInteriorFlexarray()
+ throws CancelledException, IOException, DWARFException {
+
+ DebugInfoEntry intDIE = addInt(cu);
+ DebugInfoEntry arrayDIE = newArray(cu, intDIE, false, -1);
+
+ DebugInfoEntry structDIE = newStruct("mystruct", 100).create(cu);
+ newMember(structDIE, "f1", intDIE, 0).create(cu);
+ newMember(structDIE, "flexarray", arrayDIE, 99).create(cu);
+
+ importAllDataTypes();
+
+ Structure structdt = (Structure) dataMgr.getDataType(rootCP, "mystruct");
+ assertTrue(structdt.getDescription().contains("Missing member flexarray"));
+ assertNull(structdt.getFlexibleArrayComponent());
+
+ }
+
+ @Test
+ public void testStructBitfields() throws CancelledException, IOException, DWARFException {
+
+ DebugInfoEntry intDIE = addInt(cu);
+
+ DebugInfoEntry structDIE = newStruct("mystruct", 100).create(cu);
+ newMember(structDIE, "f1", intDIE, 0).create(cu);
+ newMember(structDIE, "bitfield1_3", intDIE, 4) //
+ .addInt(DW_AT_bit_size, 3) //
+ .addInt(DW_AT_bit_offset, 29) //
+ .create(cu);
+ newMember(structDIE, "bitfield2_2", intDIE, 4) //
+ .addInt(DW_AT_bit_size, 2) //
+ .addInt(DW_AT_bit_offset, 27) //
+ .create(cu);
+ newMember(structDIE, "bitfield3_9", intDIE, 4) //
+ .addInt(DW_AT_bit_size, 9) //
+ .addInt(DW_AT_bit_offset, 18) //
+ .create(cu);
+
+ importAllDataTypes();
+
+ Structure structdt = (Structure) dataMgr.getDataType(rootCP, "mystruct");
+ List<DataTypeComponent> bitfields = getBitFieldComponents(structdt);
+ Set<Integer> expectedBitfieldSizes = new HashSet<Integer>(Set.of(2, 3, 9));
+ for (DataTypeComponent dtc : bitfields) {
+ BitFieldDataType bfdt = (BitFieldDataType) dtc.getDataType();
+ expectedBitfieldSizes.remove(bfdt.getBitSize());
+ }
+ assertTrue(expectedBitfieldSizes.size() == 0);
+ }
+
+ List<DataTypeComponent> getBitFieldComponents(Structure struct) {
+ List<DataTypeComponent> results = new ArrayList<>();
+ for (DataTypeComponent dtc : struct.getComponents()) {
+ if (dtc.getDataType() instanceof BitFieldDataType) {
+ results.add(dtc);
+ }
+ }
+ return results;
+ }
+
+ //----------------------------------------------------------------------------------------------------
+
@Test
public void testUnion() throws CancelledException, IOException, DWARFException {
@@ -660,14 +739,38 @@ public class DWARFDataTypeImporterTest extends DWARFTestBase {
assertEquals("f1", uniondt.getComponent(0).getFieldName());
// "f2_self" field should not have been added as it was a recursive reference back to ourself
assertEquals("f3", uniondt.getComponent(1).getFieldName());
+ assertEquals(UNION_STATIC_SIZE, uniondt.getLength());
+ }
+
+ @Test
+ public void testUnionFlexArray() throws CancelledException, IOException, DWARFException {
+ // flex array in a union is converted to an 1 element array (if it can fit)
+
+ DebugInfoEntry intDIE = addInt(cu);
+ DebugInfoEntry arrayDIE = newArray(cu, intDIE, false, -1);
- // This test is not valid as Ghidra does not allow unions with statically specified size
- // Needs research to see what DWARF does and if this could ever be different than
- // what the contents of the union are
- //assertEquals(UNION_STATIC_SIZE, uniondt.getLength());
+ int UNION_STATIC_SIZE = 10;
+ DebugInfoEntry unionDIE = new DIECreator(DWARFTag.DW_TAG_union_type) //
+ .addString(DW_AT_name, "myunion") //
+ .addInt(DW_AT_byte_size, UNION_STATIC_SIZE) //
+ .create(cu);
+
+ newMember(unionDIE, "f1", intDIE, -1).create(cu);
+ newMember(unionDIE, "flexarray", arrayDIE, -1).create(cu);
+
+ //----------------------
+
+ importAllDataTypes();
+
+ Union uniondt = (Union) dataMgr.getDataType(rootCP, "myunion");
+ assertEquals("f1", uniondt.getComponent(0).getFieldName());
+ DataTypeComponent flexDTC = uniondt.getComponent(1);
+ assertEquals("flexarray", flexDTC.getFieldName());
+ assertTrue(flexDTC.getDataType() instanceof Array);
}
+ //----------------------------------------------------------------------------------------------
/**
* Test skipping const, volatile data types.
* @throws CancelledException
@@ -719,9 +822,15 @@ public class DWARFDataTypeImporterTest extends DWARFTestBase {
// anon struct
}
- // not implemented yet
- public void testArray() {
- // multi dim, emtpy [],
+ @Test
+ public void testArray() throws CancelledException, IOException, DWARFException {
+ DebugInfoEntry intDIE = addInt(cu);
+ DebugInfoEntry arrayDIE = newArray(cu, intDIE, false, 10);
+
+ importAllDataTypes();
+
+ Array arr = (Array) dwarfDTM.getDataType(arrayDIE.getOffset(), null);
+ assertNotNull(arr);
}
// not implemented yet
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/DWARFTestBase.java b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/DWARFTestBase.java
index 4e8236cabd..a27508aace 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/DWARFTestBase.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/DWARFTestBase.java
@@ -261,4 +261,21 @@ public class DWARFTestBase extends AbstractGhidraHeadedIntegrationTest {
DW_AT_data_member_location, offset).setParent(parentStruct);
return field;
}
+
+ protected DebugInfoEntry newArray(MockDWARFCompilationUnit dcu, DebugInfoEntry baseTypeDIE,
+ boolean elideEmptyDimRangeValue, int... dimensions) {
+ DebugInfoEntry arrayType = new DIECreator(DWARFTag.DW_TAG_array_type) //
+ .addRef(DW_AT_type, baseTypeDIE).create(dcu);
+ for (int dimIndex = 0; dimIndex < dimensions.length; dimIndex++) {
+ int dim = dimensions[dimIndex];
+ DIECreator dimDIE = new DIECreator(DWARFTag.DW_TAG_subrange_type) //
+ .setParent(arrayType);
+ if (dim != -1 || !elideEmptyDimRangeValue) {
+ dimDIE.addInt(DW_AT_upper_bound, dimensions[dimIndex]);
+ }
+ dimDIE.create(dcu);
+ }
+ return arrayType;
+ }
+
}