diff mbox series

[pushed] c++: Make references to __cxa_pure_virtual weak.

Message ID 20200511191029.8126-1-jason@redhat.com
State New
Headers show
Series [pushed] c++: Make references to __cxa_pure_virtual weak. | expand

Commit Message

Jason Merrill May 11, 2020, 7:10 p.m. UTC
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  <jason@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


base-commit: 2b2d298ff845ab7a07ffbd51da79473736da3324

Comments

Andreas Schwab May 12, 2020, 8:27 a.m. UTC | #1
On Mai 11 2020, Jason Merrill via Gcc-patches wrote:

> 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.

That doesn't work on ia64:

pure-virtual1.o:(.data.rel.ro._ZTV1A[_ZTV1A]+0x10): undefined reference to `__cxa_pure_virtual'
collect2: error: ld returned 1 exit status
$ nm -u pure-virtual1.o 
                 U __cxa_pure_virtual

Andreas.
Christophe Lyon May 12, 2020, 1:59 p.m. UTC | #2
On Tue, 12 May 2020 at 10:28, Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Mai 11 2020, Jason Merrill via Gcc-patches wrote:
>
> > 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.
>
> That doesn't work on ia64:
>
> pure-virtual1.o:(.data.rel.ro._ZTV1A[_ZTV1A]+0x10): undefined reference to `__cxa_pure_virtual'
> collect2: error: ld returned 1 exit status
> $ nm -u pure-virtual1.o
>                  U __cxa_pure_virtual
>

This doesn't work on aarch64-elf either: I'm using newlib/libgloss'
-specs=aem-ve.specs, which has:
*lib:
cpu-init/rdimon-aem-el3.o%s --start-group %(libc) %(libgloss) --end-group

Since the testcase uses -nodefaultlibs, this also removes libgloss
from the link, leading to:
/aarch64-none-elf/lib/rdimon-crt0.o: in function `_mainCRTStartup':
/newlib/libgloss/aarch64/crt0.S:113: undefined reference to `__heap_limit'
/newlib/libgloss/aarch64/crt0.S:114: undefined reference to `__heap_limit'
/newlib/libgloss/aarch64/crt0.S:165: undefined reference to
`initialise_monitor_handles'
[...]

Christophe


> Andreas.
>
> --
> Andreas Schwab, schwab@linux-m68k.org
> GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
> "And now for something completely different."
diff mbox series

Patch

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;
+}