[DebugInfo][dsymutil] Keep locations for function-local globals
authorEllis Hoag <ellishoag@fb.com>
Tue, 14 Dec 2021 06:03:23 +0000 (22:03 -0800)
committerEllis Hoag <ellis.sparky.hoag@gmail.com>
Tue, 14 Dec 2021 17:28:22 +0000 (09:28 -0800)
In debug info, we expect variables to have location info if they are used and we don't want location info for functions that are not used. However, if an unused function is inlined, we could have the scenario where a function is not in the final binary but its static variable is in the final binary. Ensure that variables in the final binary have location debug info even if their scope was inlined.

Also add `--implicit-check-not` to a test for clarity.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D115565

llvm/lib/DWARFLinker/DWARFLinker.cpp
llvm/test/tools/dsymutil/Inputs/inlined-static-variable/4.o [new file with mode: 0644]
llvm/test/tools/dsymutil/X86/dead-stripped.cpp
llvm/test/tools/dsymutil/X86/dummy-debug-map.map
llvm/test/tools/dsymutil/X86/inlined-static-variable.cpp [new file with mode: 0644]

index 022a6a4..ad351eb 100644 (file)
@@ -1423,6 +1423,11 @@ DIE *DWARFLinker::DIECloner::cloneDIE(const DWARFDie &InputDIE,
     Flags |= TF_InFunctionScope;
     if (!Info.InDebugMap && LLVM_LIKELY(!Update))
       Flags |= TF_SkipPC;
+  } else if (Abbrev->getTag() == dwarf::DW_TAG_variable) {
+    // Function-local globals could be in the debug map even when the function
+    // is not, e.g., inlined functions.
+    if ((Flags & TF_InFunctionScope) && Info.InDebugMap)
+      Flags &= ~TF_SkipPC;
   }
 
   for (const auto &AttrSpec : Abbrev->attributes()) {
diff --git a/llvm/test/tools/dsymutil/Inputs/inlined-static-variable/4.o b/llvm/test/tools/dsymutil/Inputs/inlined-static-variable/4.o
new file mode 100644 (file)
index 0000000..cec573c
Binary files /dev/null and b/llvm/test/tools/dsymutil/Inputs/inlined-static-variable/4.o differ
index 8ec1c85..16bbbd6 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: dsymutil -f -y %p/dummy-debug-map.map -oso-prepend-path %p/../Inputs/dead-stripped -o - | llvm-dwarfdump - --debug-info | FileCheck %s
+// RUN: dsymutil -f -y %p/dummy-debug-map.map -oso-prepend-path %p/../Inputs/dead-stripped -o - | llvm-dwarfdump - --debug-info | FileCheck %s --implicit-check-not "{{DW_AT_low_pc|DW_AT_high_pc|DW_AT_location|DW_TAG|NULL}}"
 
 // The test was compiled with:
 // clang++ -O2 -g -c dead-strip.cpp -o 1.o
@@ -8,22 +8,23 @@
 // that are still present in the linked debug info (in this case because
 // they have been DW_TAG_import'd in another namespace).
 
-// Everything in the N namespace bellow doesn't have a debug map entry, and
-// thus is considered dead (::foo() has a debug map entry, otherwse dsymutil
+// Everything in the N namespace below doesn't have a debug map entry, and
+// thus is considered dead (::foo() has a debug map entry, otherwise dsymutil
 // would just drop the CU altogether).
 
+// CHECK: DW_TAG_compile_unit
+// CHECK:   DW_AT_low_pc
+// CHECK:   DW_AT_high_pc
+// CHECK:   DW_TAG_namespace
 namespace N {
 int blah = 42;
 // This is actually a dsymutil-classic bug that we reproduced
 // CHECK: DW_TAG_variable
-// CHECK-NOT: DW_TAG
 // CHECK: DW_AT_location
 
 __attribute__((always_inline)) int foo() { return blah; }
 // CHECK: DW_TAG_subprogram
-// CHECK-NOT: DW_AT_low_pc
-// CHECK-NOT: DW_AT_high_pc
-// CHECK: DW_AT_frame_base
+// CHECK:   DW_AT_frame_base
 
 // CHECK: DW_TAG_subprogram
 
@@ -34,15 +35,21 @@ int bar(unsigned i) {
        return foo();
 }
 // CHECK: DW_TAG_subprogram
-// CHECK-NOT: DW_AT_low_pc
-// CHECK-NOT: DW_AT_high_pc
-// CHECK: DW_AT_frame_base
-// CHECK-NOT: DW_AT_location
-// CHECK-NOT: DW_AT_low_pc
-// CHECK-NOT: DW_AT_high_pc
-
+// CHECK:   DW_AT_frame_base
+// CHECK:   DW_TAG_formal_parameter
+// CHECK:   DW_TAG_variable
+// CHECK:   DW_TAG_inlined_subroutine
+// CHECK:   NULL
+// CHECK: NULL
 }
-// CHECK: TAG_imported_module
+// CHECK: DW_TAG_base_type
+// CHECK: DW_TAG_imported_module
+// CHECK: DW_TAG_subprogram
+// CHECK:   DW_AT_low_pc
+// CHECK:   DW_AT_high_pc
+// CHECK: DW_TAG_base_type
+// CHECK: NULL
+
 using namespace N;
 
 void foo() {}
index aa00018..95b1f72 100644 (file)
@@ -18,5 +18,10 @@ objects:
     symbols:
       - { sym: __Z3foov, objAddr: 0x0, binAddr: 0x30000, size: 0x10 }
       - { sym: __ZN1S3bazIiEEvT_, objAddr: 0x0, binAddr: 0x30010, size: 0x10 }
+  - filename: 4.o
+    symbols:
+      - { sym: __Z3foov, objAddr: 0x0, binAddr: 0x40000, size: 0x10 }
+      - { sym: __ZZ11not_removedvE1b, objAddr: 0x0, binAddr: 0x40010, size: 0x10 }
+      - { sym: __ZZ7removedvE1a, objAddr: 0x0, binAddr: 0x40020, size: 0x10 }
 ...
 
diff --git a/llvm/test/tools/dsymutil/X86/inlined-static-variable.cpp b/llvm/test/tools/dsymutil/X86/inlined-static-variable.cpp
new file mode 100644 (file)
index 0000000..5593b26
--- /dev/null
@@ -0,0 +1,59 @@
+// RUN: dsymutil -f -y %p/dummy-debug-map.map -oso-prepend-path %p/../Inputs/inlined-static-variable -o - | llvm-dwarfdump - | FileCheck %s --implicit-check-not "{{DW_AT_low_pc|DW_AT_high_pc|DW_AT_location|DW_TAG|NULL}}"
+
+// clang -g -c inlined-static-variable.cpp -o 4.o
+
+// The functions removed and not_removed are not in the debug map and are
+// considered dead, but they are also inlined into the function foo which is
+// in the debug map. Those function-local globals are alive and thus should
+// have locations in the debug info even if their functions do not.
+
+inline __attribute__((always_inline)) int removed() {
+  static int a = 0;
+  return ++a;
+}
+
+__attribute__((always_inline)) int not_removed() {
+  static int b = 0;
+  return ++b;
+}
+
+int unused() {
+  static int c = 0;
+  return ++c;
+}
+
+int foo() {
+  return removed() + not_removed();
+}
+
+// CHECK: DW_TAG_compile_unit
+// CHECK:   DW_AT_low_pc
+// CHECK:   DW_AT_high_pc
+
+// CHECK:   DW_TAG_subprogram
+// CHECK:     DW_AT_name       ("removed")
+// CHECK:     DW_TAG_variable
+// CHECK:       DW_AT_name     ("a")
+// CHECK:       DW_AT_location
+// CHECK:     NULL
+
+// CHECK:   DW_TAG_base_type
+// CHECK:   DW_TAG_subprogram
+// CHECK:     DW_AT_name       ("not_removed")
+// CHECK:     DW_TAG_variable
+// CHECK:       DW_AT_name     ("b")
+// CHECK:       DW_AT_location
+// CHECK:     NULL
+
+// CHECK:   DW_TAG_subprogram
+// CHECK:     DW_AT_low_pc
+// CHECK:     DW_AT_high_pc
+// CHECK:     DW_AT_name       ("foo")
+// CHECK:     DW_TAG_inlined_subroutine
+// CHECK:       DW_AT_low_pc
+// CHECK:       DW_AT_high_pc
+// CHECK:     DW_TAG_inlined_subroutine
+// CHECK:       DW_AT_low_pc
+// CHECK:       DW_AT_high_pc
+// CHECK:     NULL
+// CHECK:   NULL