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

Logic used to decide if variable was a function-scoped variable was too
simplistic and couldn't handle static global variables created by clang.
This commit is contained in:
dev747368
2026-02-25 19:52:43 +00:00
parent 4d9441abc8
commit d035d31f7e
2 changed files with 21 additions and 5 deletions
@@ -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
@@ -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;