c++: Make references to __cxa_pure_virtual weak.
authorJason Merrill <jason@redhat.com>
Mon, 11 May 2020 18:05:46 +0000 (14:05 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 11 May 2020 19:10:05 +0000 (15:10 -0400)
If a program has no other dependencies on libstdc++, we shouldn't require it
just for __cxa_pure_virtual, which is only there to give a prettier
diagnostic before crashing the program; resolving the reference to NULL will
also crash, just without the diagnostic.

gcc/cp/ChangeLog
2020-05-11  Jason Merrill  <jason@redhat.com>

* decl.c (cxx_init_decl_processing): Call declare_weak for
__cxa_pure_virtual.

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/g++.dg/abi/pure-virtual1.C [new file with mode: 0644]

index 1fdc388..2cc9b8d 100644 (file)
@@ -1,5 +1,10 @@
 2020-05-11  Jason Merrill  <jason@redhat.com>
 
+       * decl.c (cxx_init_decl_processing): Call declare_weak for
+       __cxa_pure_virtual.
+
+2020-05-11  Jason Merrill  <jason@redhat.com>
+
        * pt.c (instantiate_class_template_1): Call tsubst_expr for
        STATIC_ASSERT member.
        * ptree.c (cxx_print_xnode): Handle STATIC_ASSERT.
index dea1ba0..1b6a567 100644 (file)
@@ -4544,6 +4544,9 @@ cxx_init_decl_processing (void)
   abort_fndecl
     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
                            ECF_NORETURN | ECF_NOTHROW | ECF_COLD);
+  if (flag_weak)
+    /* If no definition is available, resolve references to NULL.  */
+    declare_weak (abort_fndecl);
 
   /* Perform other language dependent initializations.  */
   init_class_processing ();
diff --git a/gcc/testsuite/g++.dg/abi/pure-virtual1.C b/gcc/testsuite/g++.dg/abi/pure-virtual1.C
new file mode 100644 (file)
index 0000000..823328e
--- /dev/null
@@ -0,0 +1,21 @@
+// Test that we don't need libsupc++ just for __cxa_pure_virtual.
+// { dg-do link }
+// { dg-require-weak }
+// { dg-additional-options "-fno-rtti -nodefaultlibs -lc" }
+
+struct A
+{
+  int i;
+  virtual void f() = 0;
+  A(): i(0) {}
+};
+
+struct B: A
+{
+  virtual void f() {}
+};
+
+int main()
+{
+  B b;
+}