PR88006 and 87462 turn out to be the same problem, but started by different commits.

The problem is that debug wants to emit a constant (for a non-type template parameter of a template instantiation) that refers to the address of some local function. That local might not be emitted though.

gen_remaining_tmpl_value_param_die_attribute is aware of the problem:
    /* We do this in two phases - first get the cases we can
         handle during early-finish, preserving those we cannot
         (containing symbolic constants where we don't yet know
         whether we are going to output the referenced symbols).
         For those we try again at late-finish.  */

But the second phase unconditionally emits the constant:
              if (! early_dwarf
                  && (dwarf_version >= 5 || !dwarf_strict))
                loc = loc_descriptor_from_tree (e->arg, 2, NULL);

and loc_descriptor_from_tree eventually ends up in const_ok_for_output_1. That function assumes pretty much any non SYMBOL_REF_EXTERNAL_P SYMBOL_REF is accessible. That's not true.

I'm not sure if there's a more canonical way of checking for a local-decl's emission, but not having a symtab entry seems significant?

booted & tested on x86_64-linux, ok?

nathan

--
Nathan Sidwell
2018-11-14  Nathan Sidwell  <nat...@acm.org>

	PR debug/88006
	PR debug/87462
	* dwarf2out.c (const_ok_for_output_1): Check symtab for internal
	decls.

	* g++.dg/debug/dwarf2/pr87462.C: New.
	* g++.dg/debug/dwarf2/pr88006.C: New.

Index: dwarf2out.c
===================================================================
--- dwarf2out.c	(revision 266082)
+++ dwarf2out.c	(working copy)
@@ -14480,13 +14472,15 @@ const_ok_for_output_1 (rtx rtl)
   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
     return false;
 
-  /* Avoid references to external symbols in debug info, on several targets
-     the linker might even refuse to link when linking a shared library,
-     and in many other cases the relocations for .debug_info/.debug_loc are
-     dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
-     to be defined within the same shared library or executable are fine.  */
   if (SYMBOL_REF_EXTERNAL_P (rtl))
     {
+      /* Avoid references to external symbols in debug info, on
+	 several targets the linker might even refuse to link when
+	 linking a shared library, and in many other cases the
+	 relocations for .debug_info/.debug_loc are dropped, so the
+	 address becomes zero anyway.  Hidden symbols, guaranteed to
+	 be defined within the same shared library or executable are
+	 fine.  */
       tree decl = SYMBOL_REF_DECL (rtl);
 
       if (decl == NULL || !targetm.binds_local_p (decl))
@@ -14496,6 +14490,18 @@ const_ok_for_output_1 (rtx rtl)
 	  return false;
 	}
     }
+  else
+    {
+      /* Internal decls are not necessarily emitted -- they could be
+         inlined or whatever.  */
+      if (tree decl = SYMBOL_REF_DECL (rtl))
+	if (!symtab_node::get (decl))
+	  {
+	    expansion_failed (NULL_TREE, rtl,
+			      "Symbol not emitted in current TU.\n");
+	    return false;
+	  }
+    }
 
   return true;
 }
Index: testsuite/g++.dg/debug/dwarf2/pr87462.C
===================================================================
--- testsuite/g++.dg/debug/dwarf2/pr87462.C	(revision 0)
+++ testsuite/g++.dg/debug/dwarf2/pr87462.C	(working copy)
@@ -0,0 +1,20 @@
+// { dg-additional-options "-dA -std=gnu++17 -gdwarf-4 -O1 -fdebug-types-section" }
+// reject .pseudo label, but "label" is ok.
+// { dg-final { scan-assembler-not "\[^L\"\]_ZN5Test18testFuncEv" } }
+// undefined ref to _ZN5Test18testFuncEv
+
+class Test1 {
+public:
+  static int testFunc() { return 1; }
+};
+
+template <typename T,
+          T (*funcImpl)()>
+class TestWrapper {
+public:
+  static T func() __attribute((noinline)) { return (*funcImpl)(); } 
+};
+
+int main() {
+  return TestWrapper<int, &Test1::testFunc>::func();
+}
Index: testsuite/g++.dg/debug/dwarf2/pr88006.C
===================================================================
--- testsuite/g++.dg/debug/dwarf2/pr88006.C	(revision 0)
+++ testsuite/g++.dg/debug/dwarf2/pr88006.C	(working copy)
@@ -0,0 +1,39 @@
+// { dg-additional-options "-dA -std=gnu++17 -gdwarf-4 -O1 -fdebug-types-section" }
+// reject .pseudo label, but "label" is ok.
+// { dg-final { scan-assembler-not "\[^\"\]_ZN3Foo4mfunEv" } }
+// undefined ref to _ZN3Foo4mfunEv
+
+struct Foo {
+  void mfun () {}
+};
+
+struct A { static constexpr bool Value = false; };
+
+template <bool> struct B { typedef int Type; };
+
+class Arg
+{
+  template <typename Unused> struct Local : A {};
+
+public:
+  template <typename Init, typename = typename B<Local<Init>::Value>::Type>
+  Arg (Init) {}
+};
+
+class Lambda {
+  static constexpr int Unused = 0;
+    
+public:
+  Lambda (Arg);
+};
+
+// Generated ref to Foo::mfun in the type die of an instantiation of this
+template <void (Foo::*unused)()> struct Callable {};
+
+class I {
+  I() : lamb ([this] {}) {}
+
+  Lambda lamb;
+
+  Callable<&Foo::mfun> bm;
+};

Reply via email to