https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101452

            Bug ID: 101452
           Summary: [debug, dwarf-5] undefined static member removed by
                    -feliminate-unused-debug-symbols
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: debug
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

Test-case:
...
$ cat test.c
struct a {
  int a;
  static int sa;
};

int
main (void)
{
  struct a a;

  a.a = 1;

  return 0;
}
...

Compile with dwarf v4:
...
$ ./install/bin/g++ test.c -c -g -gdwarf-4
$ readelf -wi test.o
  ...
 <1><2d>: Abbrev Number: 2 (DW_TAG_structure_type)
    <2e>   DW_AT_name        : a
  ...
 <2><38>: Abbrev Number: 3 (DW_TAG_member)
    <39>   DW_AT_name        : a
  ...
 <2><43>: Abbrev Number: 4 (DW_TAG_member)
    <44>   DW_AT_name        : sa  
  ...
 <2><4e>: Abbrev Number: 0
...

Compile with dwarf v5:
...
$ ./install/bin/g++ test.c -c -g -gdwarf-5
$ readelf -wi test.o
  ...
 <1><2e>: Abbrev Number: 2 (DW_TAG_structure_type)
    <2f>   DW_AT_name        : a
  ...
 <2><39>: Abbrev Number: 3 (DW_TAG_member)
    <3a>   DW_AT_name        : a
  ...
 <2><44>: Abbrev Number: 0
...

The sa member is dropped by -feliminate-unused-debug-symbols.

Fixed by:
...
$ git diff
diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c
index 82783c4968b..fa9a97f3ebb 100644
--- a/gcc/dwarf2out.c
+++ b/gcc/dwarf2out.c
@@ -29952,7 +29952,8 @@ prune_unused_types_walk (dw_die_ref die)
          if (get_AT (die, DW_AT_external))
            {
              for (c = die->die_parent; c; c = c->die_parent)
-               if (c->die_tag == DW_TAG_subprogram)
+               if (c->die_tag == DW_TAG_subprogram
+                   || c->die_tag == DW_TAG_structure_type)
                  break;
              if (!c)
                return;
...

Note that actually defining the var using this:
...
int a::sa;
...
also brings back the missing DIE.

Reply via email to