Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6494 fix DWARF handling of static global variables in a namespace

dev747368d035d31f7eda
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java
index 5021c57dd0..419c125d0e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DIEAggregate.java
@@ -267,6 +267,21 @@ public class DIEAggregate {
return getDIEContainer().getAggregate(parent);
}
+ /**
+ * Returns the first ancestor (parent, grandparent, etc) that matches the specified DIE
+ * tag type.
+ *
+ * @param ancestorType {@link DWARFTag} DIE type
+ * @return {@link DIEAggregate} containing requested ancestor, or null if not found
+ */
+ public DIEAggregate findAncestor(DWARFTag ancestorType) {
+ DIEAggregate parent = getParent();
+ while (parent != null && parent.getTag() != ancestorType) {
+ parent = parent.getParent();
+ }
+ return parent;
+ }
+
/**
* Returns the depth of the head fragment, where depth is defined as
* the distance between the DIE and the root DIE of the owning compilation
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFFunctionImporter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFFunctionImporter.java
index ce07df2d68..dd86a951eb 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFFunctionImporter.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/DWARFFunctionImporter.java
@@ -15,6 +15,7 @@
*/
package ghidra.app.util.bin.format.dwarf;
+import static ghidra.app.util.bin.format.dwarf.DWARFTag.*;
import static ghidra.app.util.bin.format.dwarf.attribs.DWARFAttributeId.*;
import java.io.IOException;
@@ -115,11 +116,11 @@ public class DWARFFunctionImporter {
}
break;
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
- // and will be handled in processFuncChildren()
- if (diea.getDepth() == 1) {
+ // Only process variable definitions that are global static variables
+ // (not nested under a subprogram DIE)
+ // Static variables scoped inside a function should be children of
+ // dw_tag_subprograms and will be handled in processFuncChildren()
+ if (diea.findAncestor(DW_TAG_subprogram) == null) {
outputGlobal(DWARFVariable.readGlobalVariable(diea));
}
break;