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.

Tested x86_64-pc-linux-gnu, applying to trunk.

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

        * decl.c (cxx_init_decl_processing): Call declare_weak for
        __cxa_pure_virtual.
---
 gcc/cp/decl.c                            |  3 +++
 gcc/testsuite/g++.dg/abi/pure-virtual1.C | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/abi/pure-virtual1.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index dea1ba07c0e..1b6a5672334 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -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
index 00000000000..823328ea951
--- /dev/null
+++ b/gcc/testsuite/g++.dg/abi/pure-virtual1.C
@@ -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;
+}

base-commit: 2b2d298ff845ab7a07ffbd51da79473736da3324
-- 
2.18.1

Reply via email to