Message ID | 20200511191029.8126-1-jason@redhat.com |
---|---|
State | New |
Headers | show |
Series | [pushed] c++: Make references to __cxa_pure_virtual weak. | expand |
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.
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 --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; +}