Ghidra is a software reverse engineering (SRE) framework
GT-3000 tweak DWARF error reporting and dynamic size handling
dev747368c334f02eed0d
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.javaindex 53a605aa2d..e026b2c52b 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@@ -616,17 +616,26 @@ public class DWARFDataTypeImporter {continue;}+ String memberComment = null;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;+ memberComment = "Unsupported dynamic size data type: " + childDT.dataType;+ childDT.dataType = Undefined.getUndefinedDataType(1);}int dtLen = childDT.dataType.getLength();if (unionSize != -1 && dtLen > unionSize) {- DWARFUtil.appendDescription(union, memberDesc("Missing member",- "data type larger than union", memberName, childDT, -1, bitSize, -1), "\n");- continue;+ if (dtLen > 1) {+ // replace problematic datatype with 1 byte undefined placeholder+ memberComment =+ "Data type larger than union's declared size: " + childDT.dataType;+ childDT.dataType = Undefined.getUndefinedDataType(1);+ }+ else {+ // can't do any fancy replacement, just add warning to union's description+ DWARFUtil.appendDescription(union, memberDesc("Missing member",+ "data type larger than union", memberName, childDT, -1, bitSize, -1), "\n");+ continue;+ }}if (isBitField) {@@ -642,7 +651,7 @@ public class DWARFDataTypeImporter {// DWARF has attributes (DWARFAttribute.DW_AT_data_bit_offset, DWARFAttribute.DW_AT_bit_offset)// that specify the bit_offset of the field in the union. We don't use them.try {- union.addBitField(childDT.dataType, bitSize, memberName, null);+ union.addBitField(childDT.dataType, bitSize, memberName, memberComment);}catch (InvalidDataTypeException e) {Msg.error(this,@@ -657,7 +666,7 @@ public class DWARFDataTypeImporter {// just a normal fieldtry {DataTypeComponent dataTypeComponent =- union.add(childDT.dataType, memberName, null);+ union.add(childDT.dataType, memberName, memberComment);// adding a member to a composite can cause a clone() of the datatype instance, so// update the instance mapping to keep track of the new instance.updateMapping(childDT.dataType, dataTypeComponent.getDataType());@@ -821,12 +830,8 @@ public class DWARFDataTypeImporter {}}- if (childDT.dataType instanceof Dynamic ||- childDT.dataType instanceof FactoryDataType) {- DWARFUtil.appendDescription(structure, memberDesc("Missing member",- "dynamic length type", memberName, childDT, memberOffset, bitSize, -1), "\n");- continue;- }+ boolean isDynamicSizedType = (childDT.dataType instanceof Dynamic ||+ childDT.dataType instanceof FactoryDataType);//if (childDT.getPathName().equals(structure.getPathName()) && childDT != structure) {// The child we are adding has the exact same fullpath as us.@@ -858,6 +863,12 @@ public class DWARFDataTypeImporter {}if (isBitField) {+ if (isDynamicSizedType) {+ DWARFUtil.appendDescription(structure, memberDesc("Missing member",+ "dynamic length type", memberName, childDT, memberOffset, bitSize, -1),+ "\n");+ continue;+ }if (!BitFieldDataType.isValidBaseDataType(childDT.dataType)) {DWARFUtil.appendDescription(structure,memberDesc("Missing member",@@ -920,6 +931,12 @@ public class DWARFDataTypeImporter {}}else {+ String memberComment = null;+ if (isDynamicSizedType) {+ memberComment = "Unsupported dynamic size data type: " + childDT.dataType;+ childDT.dataType = Undefined.getUndefinedDataType(1);+ }+int childLength = getUnpaddedDataTypeLength(childDT.dataType);if (memberOffset + childLength > structure.getLength()) {DWARFUtil.appendDescription(structure, memberDesc("Missing member",@@ -941,7 +958,7 @@ public class DWARFDataTypeImporter {try {DataTypeComponent dtc = structure.replaceAtOffset(memberOffset,- childDT.dataType, childLength, memberName, null);+ childDT.dataType, childLength, memberName, memberComment);// struct.replaceAtOffset() clones the childDT, which will mess up our// identity based mapping in currentImplDataTypeToDDT.diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.javaindex b77d59cedc..0d18785e70 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFFunctionImporter.java@@ -34,7 +34,6 @@ import ghidra.program.model.data.Enum;import ghidra.program.model.lang.*;import ghidra.program.model.listing.*;import ghidra.program.model.listing.Function.FunctionUpdateType;-import ghidra.program.model.mem.MemoryBlock;import ghidra.program.model.pcode.Varnode;import ghidra.program.model.symbol.*;import ghidra.program.model.util.CodeUnitInsertionException;@@ -751,30 +750,26 @@ public class DWARFFunctionImporter {private Data createVariable(Address address, DataType dataType, DWARFNameInfo dni) {try {- MemoryBlock block = currentProgram.getMemory().getBlock(address);+ String eolComment = null;if (dataType instanceof Dynamic || dataType instanceof FactoryDataType) {- if (!block.isInitialized()) {- Msg.warn(this, "Dynamically sized data type in un-initialized memory: " +- dataType + " at " + address);- }- Data result = DataUtilities.createData(currentProgram, address, dataType, -1, false,- ClearDataMode.CLEAR_ALL_UNDEFINED_CONFLICT_DATA);- variablesProcesesed.add(address);- return result;- }- if (isDataTypeCompatibleWithExistingData(dataType, address)) {- Data result = DataUtilities.createData(currentProgram, address, dataType, -1, false,- ClearDataMode.CLEAR_ALL_CONFLICT_DATA);- variablesProcesesed.add(address);- return result;+ eolComment = "Unsupported dynamic data type: " + dataType;+ dataType = Undefined.getUndefinedDataType(1);}- else {- Msg.warn(this,- "Could not place static variable " +- dni.getNamespacePath().asFormattedString() + " : " + dataType + " at " +- address + " because existing data type conflicts.");+ if (!isDataTypeCompatibleWithExistingData(dataType, address)) {+ appendComment(address, CodeUnit.EOL_COMMENT,+ "Could not place DWARF static variable " ++ dni.getNamespacePath().asFormattedString() + " : " + dataType ++ " because existing data type conflicts.",+ "\n");return null;}+ Data result = DataUtilities.createData(currentProgram, address, dataType, -1, false,+ ClearDataMode.CLEAR_ALL_CONFLICT_DATA);+ variablesProcesesed.add(address);+ if (eolComment != null) {+ appendComment(address, CodeUnit.EOL_COMMENT, eolComment, "\n");+ }+ return result;}catch (CodeUnitInsertionException | DataTypeConflictException e) {Msg.error(this, "Error creating data object at " + address, e);@@ -805,8 +800,7 @@ public class DWARFFunctionImporter {importSummary.globalVarsAdded++;if (sourceInfo != null) {- currentProgram.getListing().setComment(address, CodeUnit.EOL_COMMENT,- sourceInfo.getDescriptionStr());+ appendComment(address, CodeUnit.EOL_COMMENT, sourceInfo.getDescriptionStr(), "\n");if (varData != null) {moveIntoFragment(dni.getName(), varData.getMinAddress(), varData.getMaxAddress(),