Changes in v2: 1. Check decl non-null before dereferencing it. 2. Update PR rtl-optimization/113617 from
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113617#c14 --- For a private COMDAT function symbol reference in readonly data section, instead of putting it in .data.rel.ro or .rodata.cst section, call function_rodata_section to get the read-only or relocated read-only data section associated with the function DECL so that the COMDAT section will be used for the private COMDAT function symbol. gcc/ PR rtl-optimization/113617 * varasm.cc (default_elf_select_rtx_section): Call function_rodata_section to get the read-only or relocated read-only data section for private COMDAT function symbol reference. gcc/testsuite/ PR rtl-optimization/113617 * g++.dg/pr113617-1a.C: New test. * g++.dg/pr113617-1b.C: Likewise. --- gcc/testsuite/g++.dg/pr113617-1a.C | 145 +++++++++++++++++++++++++++++ gcc/testsuite/g++.dg/pr113617-1b.C | 8 ++ gcc/varasm.cc | 30 ++++++ 3 files changed, 183 insertions(+) create mode 100644 gcc/testsuite/g++.dg/pr113617-1a.C create mode 100644 gcc/testsuite/g++.dg/pr113617-1b.C diff --git a/gcc/testsuite/g++.dg/pr113617-1a.C b/gcc/testsuite/g++.dg/pr113617-1a.C new file mode 100644 index 00000000000..c93f08b5068 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr113617-1a.C @@ -0,0 +1,145 @@ +// { dg-do compile { target fpic } } +// { dg-require-visibility "" } +// { dg-options "-O2 -std=c++11 -fPIC -fvisibility=hidden -fvisibility-inlines-hidden" } + +namespace { +template <int V> struct J { static constexpr int value = V; }; +template <bool V> using K = J<V>; +using M = K<true>; +template <int> struct L { template <typename _Tp, typename> using type = _Tp; }; +template <bool _Cond, typename _If, typename _Else> using N = typename L<_Cond>::type<_If, _Else>; +M k; +template <typename _Tp> struct O { using type = _Tp; }; +template <typename _Up> +struct P : N<M ::value, O<_Up>, _Up> {}; +template <typename _Tp> struct Q { using type = typename P<_Tp>::type; }; +} +namespace R { +struct H; +enum G {}; +template <typename> class S; +struct T { using U = bool (*) (H &, const H &, G); U F; }; +template <typename, typename> class B; +template <typename _R, typename _F, typename... _A> +struct B<_R(_A...), _F> { + static bool F(H &, const H &, G) { return false; } + __attribute__((noipa)) static _R bar(const H &) {} +}; +template <typename _R, typename... _A> +struct S<_R(_A...)> : T { + template <typename _F> using AH = B<_R(), _F>; + template <typename _F> S(_F) { + using AG = AH<_F>; + barr = AG::bar; + F = AG::F; + } + using AF = _R (*)(const H &); + AF barr; +}; +template <typename> class I; +template <typename _F, typename... _B> +struct I<_F(_B...)> {}; +template <typename> using W = decltype(k); +template <int, typename _F, typename... _B> struct V { + typedef I<typename Q<_F>::type(typename Q<_B>::type...)> type; +}; +template <typename _F, typename... _B> +__attribute__((noipa)) typename V<W<_F>::value, _F, _B...>::type +baz(_F, _B...) { return typename V<W<_F>::value, _F, _B...>::type (); } +template <typename _Tp> struct AJ { + template <typename _Up> struct _Ptr { using type = _Up *; }; + using AI = typename _Ptr<_Tp>::type; +}; +template <typename _Tp> struct Y { + using AI = typename AJ<_Tp>::AI; + AI operator->(); +}; +} +extern int z; +namespace N1 { +namespace N2 { +namespace N3 { +enum Z { Z1, Z2 }; +template <int> struct X { + template <typename _F> + __attribute__((noipa)) void boo(long long, long long, long long, _F &) {} +}; +struct AC { + AC(int); + void m1(R::S<void()>); +}; +template <typename> +__attribute__((noipa)) void garply(void *, long long, long long, long long) {} +template <> +template <typename _F> +void X<Z2>::boo(long long, long long x, long long y, _F &fi) { + AC pool(z); + for (;;) { + auto job = R::baz(garply<_F>, &fi, y, y, x); + pool.m1(job); + } +} +struct AB { + static AB &bleh(); + template <typename _F> + void boo(long first, long x, long y, _F fi) { + switch (ab1) { + case Z1: + ab2->boo(first, x, y, fi); + case Z2: + ab3->boo(first, x, y, fi); + } + } + Z ab1; + R::Y<X<Z1>> ab2; + R::Y<X<Z2>> ab3; +}; +template <typename, bool> struct C; +template <typename _F> struct C<_F, false> { + __attribute__((noipa)) C(_F) {} + void boo(long first, long x, long y) { + auto u = AB::bleh(); + u.boo(first, x, y, *this); + } +}; +template <typename _F> struct AA { typedef C<_F, 0> type; }; +} +} +} +struct AD { + template <typename _F> + static void boo(long first, long x, long y, _F f) { + typename N1::N2::N3::AA<_F>::type fi(f); + fi.boo(first, x, y); + } + template <typename _F> + static void boo(long first, long x, _F f) { + boo(first, x, 0, f); + } +}; +template <typename> struct A { + void foo(long long, long long); + int *c; +}; +namespace { +template <typename> struct D { __attribute__((noipa)) D(int *) {} }; +} +template <typename T> +void A<T>::foo(long long x, long long y) +{ + int e; + D<T> d(&e); + AD::boo(0, y, d); + long p; + for (p = 0; p < x; p++) + c[p] = c[p - 1]; +} +int z; +long xx1; +void corge() { + A<long long> a; + a.foo(xx1, 0); +} + +// { dg-final { scan-assembler-not ".section\t\.data\.rel\.ro\.local,\"aw\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } +// { dg-final { scan-assembler ".section\t.data\.rel\.ro\.local\..*,\"awG\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } diff --git a/gcc/testsuite/g++.dg/pr113617-1b.C b/gcc/testsuite/g++.dg/pr113617-1b.C new file mode 100644 index 00000000000..4a02bf44e72 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr113617-1b.C @@ -0,0 +1,8 @@ +// { dg-do compile { target fpic } } +// { dg-require-visibility "" } +// { dg-options "-O2 -std=c++11 -fno-pic -fvisibility=hidden -fvisibility-inlines-hidden" } + +#include "pr113617-1a.C" + +// { dg-final { scan-assembler-not ".section\t\.rodata\.cst(4|8),\"aM\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } +// { dg-final { scan-assembler ".section\t\.rodata\..*,\"aG\"" { target { { i?86-*-linux* x86_64-*-linux* } && { ! ia32 } } } } } diff --git a/gcc/varasm.cc b/gcc/varasm.cc index fa17eff551e..c26d95b7178 100644 --- a/gcc/varasm.cc +++ b/gcc/varasm.cc @@ -7459,16 +7459,46 @@ default_elf_select_rtx_section (machine_mode mode, rtx x, { int reloc = compute_reloc_for_rtx (x); + tree decl = nullptr; + + /* If it is a private COMDAT function symbol reference, call + function_rodata_section for the read-only or relocated read-only + data section associated with function DECL so that the COMDAT + section will be used for the private COMDAT function symbol. */ + if (HAVE_COMDAT_GROUP) + { + if (GET_CODE (x) == CONST + && GET_CODE (XEXP (x, 0)) == PLUS + && CONST_INT_P (XEXP (XEXP (x, 0), 1))) + x = XEXP (XEXP (x, 0), 0); + + if (GET_CODE (x) == SYMBOL_REF) + { + decl = SYMBOL_REF_DECL (x); + if (decl + && (TREE_CODE (decl) != FUNCTION_DECL + || !DECL_COMDAT_GROUP (decl) + || TREE_PUBLIC (decl))) + decl = nullptr; + } + } + /* ??? Handle small data here somehow. */ if (reloc & targetm.asm_out.reloc_rw_mask ()) { + if (decl) + return targetm.asm_out.function_rodata_section (decl, true); + if (reloc == 1) return get_named_section (NULL, ".data.rel.ro.local", 1); else return get_named_section (NULL, ".data.rel.ro", 3); } + if (decl) + return targetm.asm_out.function_rodata_section (decl, false); + return mergeable_constant_section (mode, align, 0); } -- 2.43.0