From patchwork Wed Jan 31 18:58:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1893635 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OeSyy4J+; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TQBCx1R65z1yQ0 for ; Thu, 1 Feb 2024 05:59:25 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 350C03858426 for ; Wed, 31 Jan 2024 18:59:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 366ED3858C78 for ; Wed, 31 Jan 2024 18:59:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 366ED3858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 366ED3858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706727545; cv=none; b=TMvFTcln0CTaZWC5wWH1Jq5B6wq4htL6+mQbP4ZPdv0ThgO14iUh6MkL/b0bQb8gwKjwg1EP3dc91asQq6EZaLIoJe6tTrFthA1PDdiUJAtcpnFHhzC/vQT3sgWID2oXVhOfG+4Gv2nRnqYzP/0T5OvDnHXVHt/S3HPIS7Mg5ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706727545; c=relaxed/simple; bh=puVuVLQc2sjH0N3Mgk/If77726hbv0Z0fkoOqTu124Y=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=jxB0nEIymS7rJ/m1zBRySwJtx3ir/pUVKLlxuLGRU+5aWppzoAB6YVkQEisnwLDE2PmxTKU1E84kzCkZIrEqUa3ytZr9ozmG2RfZNcrjiENmrlJZoMSSuECMlNFQvTE8hgO1q9xMS7PO8BicDQWZy6AaB9bG1z2sX3JNxkXdE2M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706727542; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references; bh=jnr5EkWb+poJNiOkmN5hg1IbpaWCfTI5pQP3633QWeE=; b=OeSyy4J+1sjE4cI6xA4u9bWUTngW0VTA2DPZT4bbgW5QyvjhM+ZHeaPAxOW1NYQxa9gBH3 5is3a65yEyseP/yfiS8m5zB+0aS2wn9gIFmnFvNzxZ+PyJ9oudhjzym0ZAv0eoLvoU0w/m nsSGYhxuiGGEEEnSFdsIgrr1JHy3K/Y= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-108-_MEgUPIUPBKh5T6DpkfnFQ-1; Wed, 31 Jan 2024 13:58:59 -0500 X-MC-Unique: _MEgUPIUPBKh5T6DpkfnFQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0D59B3C00085; Wed, 31 Jan 2024 18:58:59 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.70]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C4068492BC6; Wed, 31 Jan 2024 18:58:58 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 40VIwps7295820 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 31 Jan 2024 19:58:51 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 40VIwo7R295819; Wed, 31 Jan 2024 19:58:50 +0100 Date: Wed, 31 Jan 2024 19:58:50 +0100 From: Jakub Jelinek To: Richard Biener , Jeff Law , "H.J. Lu" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] varasm, v3: Handle private COMDAT function symbol reference in readonly data section [PR113617] Message-ID: References: <20240131022136.572689-1-hjl.tools@gmail.com> MIME-Version: 1.0 In-Reply-To: X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org On Wed, Jan 31, 2024 at 07:11:20PM +0100, Jakub Jelinek wrote: > On Wed, Jan 31, 2024 at 09:39:12AM -0800, H.J. Lu wrote: > > GNU binutils has no issues with it: > > I know, I meant gcc. > So, it seems get_section handles section purely by name lookup > and isn't prepared to deal with multiple different sections > of the same name, but different comdat group. > > Thus, maybe at least temporarily we need to use unique > section names here, say > .data.rel.ro.local.pool. > .data.rel.ro.pool. > .rodata.pool. > where would be the name of the comdat group, i.e. > IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)) Here is an untested patch which implements that: 2024-01-31 Jakub Jelinek H.J. Lu PR rtl-optimization/113617 * varasm.cc (default_elf_select_rtx_section): For references to private symbols in comdat sections use .data.relro.local.pool., .data.relro.pool. or .rodata. comdat sections. * g++.dg/other/pr113617.C: New test. * g++.dg/other/pr113617.h: New test. * g++.dg/other/pr113617-aux.cc: New test. Jakub --- gcc/varasm.cc.jj 2024-01-30 08:44:43.304175273 +0100 +++ gcc/varasm.cc 2024-01-31 19:49:47.499880994 +0100 @@ -7458,17 +7458,63 @@ default_elf_select_rtx_section (machine_ unsigned HOST_WIDE_INT align) { int reloc = compute_reloc_for_rtx (x); + tree decl = nullptr; + const char *prefix = nullptr; + int flags = 0; + + /* 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 (reloc == 1) + if (decl) + { + prefix = reloc == 1 ? ".data.rel.ro.local" : ".data.rel.ro"; + flags = SECTION_WRITE | SECTION_RELRO; + } + else 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) + { + const char *comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)); + if (!prefix) + prefix = ".rodata"; + size_t prefix_len = strlen (prefix); + size_t comdat_len = strlen (comdat); + size_t len = prefix_len + sizeof (".pool.") + comdat_len; + char *name = XALLOCAVEC (char, len); + memcpy (name, prefix, prefix_len); + memcpy (name + prefix_len, ".pool.", sizeof (".pool.") - 1); + memcpy (name + prefix_len + sizeof (".pool.") - 1, comdat, + comdat_len + 1); + return get_section (name, flags | SECTION_LINKONCE, decl); + } + return mergeable_constant_section (mode, align, 0); } --- gcc/testsuite/g++.dg/other/pr113617.C.jj 2024-01-31 19:46:30.678626083 +0100 +++ gcc/testsuite/g++.dg/other/pr113617.C 2024-01-31 19:46:26.598682981 +0100 @@ -0,0 +1,27 @@ +// PR rtl-optimization/113617 +// { dg-do link { target c++11 } } +// { dg-options "-O2" } +// { dg-additional-options "-fPIC" { target fpic } } */ +// { dg-additional-options "-shared" { target shared } } */ +// { dg-additional-sources pr113617-aux.cc } + +#include "pr113617.h" + +int z; +long xx1; +void corge() { + A a; + a.foo(xx1, 0); +} + +typedef unsigned long int VV __attribute__((vector_size (2 * sizeof (long)))); +VV vv; +__attribute__((noipa)) static void fn1 (void) {} +__attribute__((noipa)) static void fn2 (void) {} + +void +fn3 () +{ + VV a = { (unsigned long) &fn1, (unsigned long) &fn2 }; + vv = a; +} --- gcc/testsuite/g++.dg/other/pr113617.h.jj 2024-01-31 19:46:33.415587908 +0100 +++ gcc/testsuite/g++.dg/other/pr113617.h 2024-01-30 13:34:07.922368904 +0100 @@ -0,0 +1,132 @@ +namespace { +template struct J { static constexpr int value = V; }; +template using K = J; +using M = K; +template struct L { template using type = _Tp; }; +template using N = typename L<_Cond>::type<_If, _Else>; +M k; +template struct O { using type = _Tp; }; +template +struct P : N, _Up> {}; +template struct Q { using type = typename P<_Tp>::type; }; +} +namespace R { +struct H; +enum G {}; +template class S; +struct T { using U = bool (*) (H &, const H &, G); U F; }; +template class B; +template +struct B<_R(_A...), _F> { + static bool F(H &, const H &, G) { return false; } + __attribute__((noipa)) static _R bar(const H &) {} +}; +template +struct S<_R(_A...)> : T { + template using AH = B<_R(), _F>; + template S(_F) { + using AG = AH<_F>; + barr = AG::bar; + F = AG::F; + } + using AF = _R (*)(const H &); + AF barr; +}; +template class I; +template +struct I<_F(_B...)> {}; +template using W = decltype(k); +template struct V { + typedef I::type(typename Q<_B>::type...)> type; +}; +template +__attribute__((noipa)) typename V::value, _F, _B...>::type +baz(_F, _B...) { return typename V::value, _F, _B...>::type (); } +template struct AJ { + template struct _Ptr { using type = _Up *; }; + using AI = typename _Ptr<_Tp>::type; +}; +template struct Y { + using AI = typename AJ<_Tp>::AI; + AI operator->(); +}; +} +extern int z; +namespace N1 { +namespace N2 { +namespace N3 { +enum Z { Z1, Z2 }; +template struct X { + template + __attribute__((noipa)) void boo(long long, long long, long long, _F &) {} +}; +struct AC { + AC(int); + void m1(R::S); +}; +template +__attribute__((noipa)) void garply(void *, long long, long long, long long) {} +template <> +template +void X::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 + 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> ab2; + R::Y> ab3; +}; +template struct C; +template 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 struct AA { typedef C<_F, 0> type; }; +} +} +} +struct AD { + template + 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 + static void boo(long first, long x, _F f) { + boo(first, x, 0, f); + } +}; +template struct A { + void foo(long long, long long); + int *c; +}; +namespace { +template struct D { __attribute__((noipa)) D(int *) {} }; +} +template +void A::foo(long long x, long long y) +{ + int e; + D d(&e); + AD::boo(0, y, d); + long p; + for (p = 0; p < x; p++) + c[p] = c[p - 1]; +} --- gcc/testsuite/g++.dg/other/pr113617-aux.cc.jj 2024-01-31 19:46:40.573488077 +0100 +++ gcc/testsuite/g++.dg/other/pr113617-aux.cc 2024-01-31 19:47:03.579167215 +0100 @@ -0,0 +1,9 @@ +// PR rtl-optimization/113617 +// { dg-do link { target { c++17 && c++14_down } } } + +#include "pr113617.h" + +void qux() { + A a; + a.foo(0, 0); +}