Ghidra is a software reverse engineering (SRE) framework
GP-1682 DWARF - fix bad ordering of params w/abstract & concrete inst
dev747368adf9113f3f32
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.javaindex 35893f8ff8..df0c9c0c54 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DIEAggregate.java@@ -15,6 +15,8 @@*/package ghidra.app.util.bin.format.dwarf4;+import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag.DW_TAG_formal_parameter;+import java.util.*;import java.io.IOException;@@ -430,10 +432,43 @@ public class DIEAggregate {return getRef(DWARFAttribute.DW_AT_type);}+ /**+ * Return a list of children that are of a specific DWARF type.+ * <p>+ * @param childTag see {@link DWARFTag DWARFTag DW_TAG_* values}+ * @return List of children DIEs that match the specified tag+ */+ public List<DebugInfoEntry> getChildren(int childTag) {+ return getHeadFragment().getChildren(childTag);+ }+public boolean hasAttribute(int attribute) {return findAttribute(attribute) != null;}+ /**+ * Return a {@link DIEAggregate} that only contains the information present in the+ * "abstract instance" (and lower) DIEs.+ *+ * @return a new {@link DIEAggregate}, or null if this DIEA was not split into a concrete and+ * abstract portion+ */+ public DIEAggregate getAbstractInstance() {+ AttrInfo aoAttr = findAttribute(DWARFAttribute.DW_AT_abstract_origin);+ if (aoAttr == null) {+ return null;+ }+ int aoIndex = 0;+ for (; aoIndex < fragments.length; aoIndex++) {+ if (fragments[aoIndex] == aoAttr.die) {+ DebugInfoEntry[] partialFrags = new DebugInfoEntry[fragments.length - aoIndex - 1];+ System.arraycopy(fragments, aoIndex + 1, partialFrags, 0, partialFrags.length);+ return new DIEAggregate(partialFrags);+ }+ }+ throw new IllegalArgumentException("Should not get here");+ }+/*** Returns the signed integer value of the requested attribute after resolving* any DWARF expression opcodes.@@ -909,6 +944,58 @@ public class DIEAggregate {return false;}+ /**+ * Returns a function's parameter list, taking care to ensure that the params+ * are well ordered (to avoid issues with concrete instance param ordering)+ *+ * @return list of params for this function+ */+ public List<DIEAggregate> getFunctionParamList() {++ // build list of params, as seen by the function's DIEA+ List<DIEAggregate> params = new ArrayList<>();+ for (DebugInfoEntry paramDIE : getChildren(DW_TAG_formal_parameter)) {+ DIEAggregate paramDIEA = getProgram().getAggregate(paramDIE);+ params.add(paramDIEA);+ }++ // since the function might be defined using an abstract and concrete parts,+ // and the param ordering of the concrete part can be inconsistent, re-order the+ // params according to the abstract instance's params.+ // Extra concrete params will be discarded.+ DIEAggregate abstractDIEA = getAbstractInstance();+ if (abstractDIEA != null) {+ List<DIEAggregate> newParams = new ArrayList<>();+ for (DebugInfoEntry paramDIE : abstractDIEA.getChildren(DW_TAG_formal_parameter)) {+ int index = findDIEInList(params, paramDIE);+ if (index >= 0) {+ newParams.add(params.get(index));+ params.remove(index);+ }+ else {+ // add generic (abstract) definition of the param to the list+ newParams.add(getProgram().getAggregate(paramDIE));+ }+ }+ if ( !params.isEmpty() ) {+ Msg.warn(this, "Extra params in concrete DIE instance: " + params);+ Msg.warn(this, this.toString());+ }+ params = newParams;+ }++ return params;+ }++ private static int findDIEInList(List<DIEAggregate> dieas, DebugInfoEntry die) {+ for (int i = 0; i < dieas.size(); i++) {+ if (dieas.get(i).hasOffset(die.getOffset())) {+ return i;+ }+ }+ return -1;+ }+/*** A simple class used by findAttribute() to return the found attribute, along with* the DIE it was found in, and the DWARFForm type of the raw attribute.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 7b02e8fd21..7e617924df 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@@ -15,6 +15,10 @@*/package ghidra.app.util.bin.format.dwarf4.next;+import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.DW_AT_count;+import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.DW_AT_upper_bound;+import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag.DW_TAG_subrange_type;+import java.util.*;import java.util.stream.Collectors;@@ -277,12 +281,10 @@ public class DWARFDataTypeImporter {boolean foundThisParam = false;List<ParameterDefinition> params = new ArrayList<>();- for (DebugInfoEntry childEntry : diea.getHeadFragment().getChildren(- DWARFTag.DW_TAG_formal_parameter)) {- DIEAggregate childDIEA = prog.getAggregate(childEntry);+ for (DIEAggregate paramDIEA : diea.getFunctionParamList()) {- String paramName = childDIEA.getName();- DWARFDataType paramDT = getDataType(childDIEA.getTypeRef(), null);+ String paramName = paramDIEA.getName();+ DWARFDataType paramDT = getDataType(paramDIEA.getTypeRef(), null);if (paramDT == null || paramDT.dataType.getLength() <= 0) {Msg.error(this, "Bad function parameter type for " + dni.asCategoryPath());return null;@@ -291,7 +293,7 @@ public class DWARFDataTypeImporter {ParameterDefinition pd = new ParameterDefinitionImpl(paramName, paramDT.dataType, null);params.add(pd);- foundThisParam |= DWARFUtil.isThisParam(childDIEA);+ foundThisParam |= DWARFUtil.isThisParam(paramDIEA);}FunctionDefinitionDataType funcDef =@@ -299,7 +301,7 @@ public class DWARFDataTypeImporter {funcDef.setReturnType(returnType.dataType);funcDef.setArguments(params.toArray(new ParameterDefinition[params.size()]));- if (!diea.getHeadFragment().getChildren(DWARFTag.DW_TAG_unspecified_parameters).isEmpty()) {+ if (!diea.getChildren(DWARFTag.DW_TAG_unspecified_parameters).isEmpty()) {funcDef.setVarArgs(true);}@@ -416,8 +418,7 @@ public class DWARFDataTypeImporter {}private void populateStubEnum(Enum enumDT, DIEAggregate diea) {- for (DebugInfoEntry childEntry : diea.getHeadFragment().getChildren(- DWARFTag.DW_TAG_enumerator)) {+ for (DebugInfoEntry childEntry : diea.getChildren(DWARFTag.DW_TAG_enumerator)) {DIEAggregate childDIEA = prog.getAggregate(childEntry);String childName = childDIEA.getName();@@ -599,8 +600,7 @@ public class DWARFDataTypeImporter {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)) {+ for (DebugInfoEntry childEntry : diea.getChildren(DWARFTag.DW_TAG_member)) {DIEAggregate childDIEA = prog.getAggregate(childEntry);// skip static member vars as they do not have storage in the structure@@ -790,7 +790,7 @@ public class DWARFDataTypeImporter {private void populateStubStruct_worker(DWARFDataType ddt, StructureDataType structure,DIEAggregate diea, int childTagType) throws IOException, DWARFExpressionException {- for (DebugInfoEntry childEntry : diea.getHeadFragment().getChildren(childTagType)) {+ for (DebugInfoEntry childEntry : diea.getChildren(childTagType)) {DIEAggregate childDIEA = prog.getAggregate(childEntry);// skip static member vars as they do not have storage in the structure@@ -1035,20 +1035,19 @@ public class DWARFDataTypeImporter {// The first element in the DWARF dimension list would be where a wild-card (-1 length)// dimension would be defined.List<Integer> dimensions = new ArrayList<>();- List<DebugInfoEntry> subrangeDIEs =- diea.getHeadFragment().getChildren(DWARFTag.DW_TAG_subrange_type);+ List<DebugInfoEntry> subrangeDIEs = diea.getChildren(DW_TAG_subrange_type);for (int subRangeDIEIndex = 0; subRangeDIEIndex < subrangeDIEs.size(); subRangeDIEIndex++) {DIEAggregate subrangeAggr = prog.getAggregate(subrangeDIEs.get(subRangeDIEIndex));long numElements = -1;try {- if (subrangeAggr.hasAttribute(DWARFAttribute.DW_AT_count)) {+ if (subrangeAggr.hasAttribute(DW_AT_count)) {numElements =- subrangeAggr.parseUnsignedLong(DWARFAttribute.DW_AT_count, 0xbadbeef);+ subrangeAggr.parseUnsignedLong(DW_AT_count, 0xbadbeef);}// Otherwise check for an upper bound- else if (subrangeAggr.hasAttribute(DWARFAttribute.DW_AT_upper_bound)) {+ else if (subrangeAggr.hasAttribute(DW_AT_upper_bound)) {long upperBound =- subrangeAggr.parseUnsignedLong(DWARFAttribute.DW_AT_upper_bound, 0xbadbeef);+ subrangeAggr.parseUnsignedLong(DW_AT_upper_bound, 0xbadbeef);// fix special flag values used by DWARF to indicate that the array dimension// is unknown. 64bit 0xffffff...s and 32bit 0xffff..s willdiff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeManager.javaindex a420e6f3ba..e307f82509 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeManager.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/next/DWARFDataTypeManager.java@@ -15,9 +15,10 @@*/package ghidra.app.util.bin.format.dwarf4.next;-import java.io.IOException;import java.util.*;+import java.io.IOException;+import ghidra.app.util.bin.format.dwarf4.*;import ghidra.app.util.bin.format.dwarf4.encoding.DWARFEncoding;import ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag;@@ -720,26 +721,23 @@ public class DWARFDataTypeManager {DataType returnDataType = getDataType(diea.getTypeRef(), baseDataTypeVoid);boolean foundThisParam = false;List<ParameterDefinition> params = new ArrayList<>();- for (DebugInfoEntry childEntry : diea.getHeadFragment()- .getChildren(- DWARFTag.DW_TAG_formal_parameter)) {- DIEAggregate childDIEA = prog.getAggregate(childEntry);+ for (DIEAggregate paramDIEA : diea.getFunctionParamList()) {- String paramName = childDIEA.getName();- DataType paramDT = getDataType(childDIEA.getTypeRef(), null);+ String paramName = paramDIEA.getName();+ DataType paramDT = getDataType(paramDIEA.getTypeRef(), null);if (paramDT == null || paramDT.getLength() <= 0) {Msg.error(this,"Bad function parameter type for function " + dni.asCategoryPath() +", param " + params.size() + " : " + paramDT + ", func die " +diea.getHexOffset() + ", param type die: " +- childDIEA.getTypeRef().getHexOffset());+ paramDIEA.getTypeRef().getHexOffset());return null;}ParameterDefinition pd = new ParameterDefinitionImpl(paramName, paramDT, null);params.add(pd);- foundThisParam |= DWARFUtil.isThisParam(childDIEA);+ foundThisParam |= DWARFUtil.isThisParam(paramDIEA);}FunctionDefinitionDataType funcDef =new FunctionDefinitionDataType(dni.getParentCP(), dni.getName(), dataTypeManager);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 d05ea769c0..55a1c5b315 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@@ -15,15 +15,20 @@*/package ghidra.app.util.bin.format.dwarf4.next;-import java.io.IOException;+import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.*;+import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag.*;+import java.util.*;import java.util.stream.Collectors;+import java.io.IOException;+import ghidra.app.cmd.comments.AppendCommentCmd;import ghidra.app.cmd.label.SetLabelPrimaryCmd;import ghidra.app.util.bin.format.dwarf4.*;import ghidra.app.util.bin.format.dwarf4.attribs.DWARFNumericAttribute;-import ghidra.app.util.bin.format.dwarf4.encoding.*;+import ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute;+import ghidra.app.util.bin.format.dwarf4.encoding.DWARFSourceLanguage;import ghidra.app.util.bin.format.dwarf4.expression.*;import ghidra.program.database.data.DataTypeUtilities;import ghidra.program.database.function.OverlappingFunctionException;@@ -111,7 +116,7 @@ public class DWARFFunctionImporter {try {switch (diea.getTag()) {- case DWARFTag.DW_TAG_subprogram:+ case DW_TAG_subprogram:try {processSubprogram(diea);}@@ -119,7 +124,7 @@ public class DWARFFunctionImporter {Msg.error(this, "Failed to process subprog " + diea.getHexOffset(), e);}break;- case DWARFTag.DW_TAG_variable:+ case DW_TAG_variable:// only process variable definitions that are static variables// (ie. they are children of the compunit root, ie. depth == 1)// local variables should be children of dw_tag_subprograms@@ -133,12 +138,12 @@ public class DWARFFunctionImporter {}}break;- case DWARFTag.DW_TAG_label:+ case DW_TAG_label:processLabel(diea);break;- case DWARFTag.DW_TAG_gnu_call_site:- case DWARFTag.DW_TAG_call_site:+ case DW_TAG_gnu_call_site:+ case DW_TAG_call_site:DIEAggregate partDIEA = DIEAggregate.createSkipHead(diea);if (partDIEA != null && !isBadSubprogramDef(partDIEA)) {processSubprogram(partDIEA);@@ -260,16 +265,13 @@ public class DWARFFunctionImporter {boolean skipFuncSignature = false;List<Parameter> formalParams = new ArrayList<>();- for (DebugInfoEntry childEntry : diea.getHeadFragment().getChildren(- DWARFTag.DW_TAG_formal_parameter)) {- DIEAggregate childDIEA = prog.getAggregate(childEntry);+ for (DIEAggregate paramDIEA : diea.getFunctionParamList()) {- DataType childDT = dwarfDTM.getDataType(childDIEA.getTypeRef(), null);- if (childDT == null || DataTypeComponent.usesZeroLengthComponent(childDT)) {- String paramName =- childDIEA.getString(DWARFAttribute.DW_AT_name, "param" + formalParams.size());+ DataType paramDT = dwarfDTM.getDataType(paramDIEA.getTypeRef(), null);+ if (paramDT == null || DataTypeComponent.usesZeroLengthComponent(paramDT)) {+ String paramName = paramDIEA.getString(DW_AT_name, "param" + formalParams.size());Msg.warn(this, "DWARF: zero-length function parameter " + paramName +- ":" + childDT.getName() + ", omitting from definition of " ++ ":" + paramDT.getName() + ", omitting from definition of " +dfunc.dni.getName() + "@" + dfunc.address);// skip this parameter because its data type is a zero-width type that typically does// not generate code. If this varies compiler-to-compiler, setting@@ -277,7 +279,7 @@ public class DWARFFunctionImporter {continue;}- Parameter formalParam = createFormalParameter(childDIEA);+ Parameter formalParam = createFormalParameter(paramDIEA);if (formalParam == null) {skipFuncSignature = true;break;@@ -285,7 +287,7 @@ public class DWARFFunctionImporter {formalParams.add(formalParam);if (!formalParamsOnly) {- DWARFVariable var = processVariable(childDIEA, dfunc, null, -1);+ DWARFVariable var = processVariable(paramDIEA, dfunc, null, -1);if (var == null) {// we had an error, can't rely on detailed param data, fallback to// formal params@@ -297,15 +299,14 @@ public class DWARFFunctionImporter {}}}- dfunc.varArg =- !diea.getHeadFragment().getChildren(DWARFTag.DW_TAG_unspecified_parameters).isEmpty();+ dfunc.varArg = !diea.getChildren(DW_TAG_unspecified_parameters).isEmpty();processFuncChildren(diea, dfunc);Function gfunc = createFunction(dfunc, diea);if (gfunc != null) {- if (diea.getBool(DWARFAttribute.DW_AT_noreturn, false)) {+ if (diea.getBool(DW_AT_noreturn, false)) {gfunc.setNoReturn(true);}if (formalParams.isEmpty() && dfunc.localVarErrors) {@@ -341,7 +342,7 @@ public class DWARFFunctionImporter {}}-+private void updateFunctionSignatureWithFormalParams(Function gfunc, List<Parameter> params,DataType returnType, boolean varArgs, DIEAggregate diea) {try {@@ -411,7 +412,7 @@ public class DWARFFunctionImporter {DIEAggregate childDIEA = prog.getAggregate(childEntry);switch (childDIEA.getTag()) {- case DWARFTag.DW_TAG_variable: {+ case DW_TAG_variable: {DWARFVariable var =processVariable(childDIEA, dfunc, null, dfunc.address.getOffset());@@ -420,18 +421,18 @@ public class DWARFFunctionImporter {}break;}- case DWARFTag.DW_TAG_lexical_block:+ case DW_TAG_lexical_block:processLexicalBlock(childDIEA, dfunc);break;- case DWARFTag.DW_TAG_label:+ case DW_TAG_label:processLabel(childDIEA);break;- case DWARFTag.DW_TAG_inlined_subroutine:+ case DW_TAG_inlined_subroutine:processInlinedSubroutine(childDIEA, dfunc);break;- case DWARFTag.DW_TAG_gnu_call_site:- case DWARFTag.DW_TAG_call_site:+ case DW_TAG_gnu_call_site:+ case DW_TAG_call_site:DIEAggregate partDIEA = DIEAggregate.createSkipHead(diea);if (partDIEA != null && !isBadSubprogramDef(partDIEA)) {processSubprogram(partDIEA);@@ -443,7 +444,7 @@ public class DWARFFunctionImporter {}private Parameter createFormalParameter(DIEAggregate diea) {- String name = diea.getString(DWARFAttribute.DW_AT_name, null);+ String name = diea.getString(DW_AT_name, null);DataType dt = dwarfDTM.getDataType(diea.getTypeRef(), dwarfDTM.getVoidType());try {@@ -575,7 +576,7 @@ public class DWARFFunctionImporter {String contextStr = (dfunc != null)? " for function " + dfunc.dni.getName() + "@" + dfunc.address: "";- if (diea.getTag() != DWARFTag.DW_TAG_formal_parameter) {+ if (diea.getTag() != DW_TAG_formal_parameter) {Msg.warn(this,"Variable " + dvar.dni.getName() + "[" + dvar.type.getName() +", size=" + dvar.type.getLength() + "]" + contextStr +@@ -630,7 +631,7 @@ public class DWARFFunctionImporter {return;}- boolean external = diea.getBool(DWARFAttribute.DW_AT_external, false);+ boolean external = diea.getBool(DW_AT_external, false);outputGlobal(staticVariableAddress, dvar.type, external,DWARFSourceInfo.create(diea), dvar.dni);@@ -702,12 +703,11 @@ public class DWARFFunctionImporter {// location of this lexical block?// Process low and high pc if it exists- if (diea.hasAttribute(DWARFAttribute.DW_AT_low_pc) &&- diea.hasAttribute(DWARFAttribute.DW_AT_high_pc)) {+ if (diea.hasAttribute(DW_AT_low_pc) && diea.hasAttribute(DW_AT_high_pc)) {lowPC = diea.getLowPC(0);}// Otherwise process a range list- else if (diea.hasAttribute(DWARFAttribute.DW_AT_ranges)) {+ else if (diea.hasAttribute(DW_AT_ranges)) {List<DWARFRange> ranges = diea.parseDebugRange(DWARFAttribute.DW_AT_ranges);// No range found@@ -742,14 +742,13 @@ public class DWARFFunctionImporter {Number highPC = null;// Process low and high pc if it exists- if (diea.hasAttribute(DWARFAttribute.DW_AT_low_pc) &&- diea.hasAttribute(DWARFAttribute.DW_AT_high_pc)) {+ if (diea.hasAttribute(DW_AT_low_pc) && diea.hasAttribute(DW_AT_high_pc)) {lowPC = diea.getLowPC(0);highPC = diea.getHighPC();}// Otherwise process a range list- else if (diea.hasAttribute(DWARFAttribute.DW_AT_ranges)) {- List<DWARFRange> ranges = diea.parseDebugRange(DWARFAttribute.DW_AT_ranges);+ else if (diea.hasAttribute(DW_AT_ranges)) {+ List<DWARFRange> ranges = diea.parseDebugRange(DW_AT_ranges);// No range foundif (ranges.isEmpty()) {@@ -1475,7 +1474,7 @@ public class DWARFFunctionImporter {}String name = prog.getEntryName(diea);- if (name != null && diea.hasAttribute(DWARFAttribute.DW_AT_low_pc)) {+ if (name != null && diea.hasAttribute(DW_AT_low_pc)) {Address address = toAddr(diea.getLowPC(0));if (address.getOffset() != 0) {try {