From patchwork Tue Dec 15 17:30:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1416611 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=bL++n/DQ; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CwQJy52C0z9sPB for ; Wed, 16 Dec 2020 04:30:58 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AD4F2387086C; Tue, 15 Dec 2020 17:30:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD4F2387086C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1608053441; bh=6Oz+bV+Gv0X/g3Uhpw1VQemg8/XSDR6nocAzxI2gpi8=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=bL++n/DQnUaPq2zaDDFJbWXieJ1jtQm9cu5UbXg79acHvugfLdCdVOQEXXSozlJCM WbbsJak/bLyU9xo1s2S3okvYiBG8k9xQEVO8JGC3V1nAgcV47bD8/YA2nU35i5w4vv xrpJP1I7MI3JHoP4tbTWKR+9M5a+uDx1Tgy1NXic= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by sourceware.org (Postfix) with ESMTPS id 7BD2B3861823 for ; Tue, 15 Dec 2020 17:30:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7BD2B3861823 Received: by mail-pl1-x643.google.com with SMTP id t6so11408495plq.1 for ; Tue, 15 Dec 2020 09:30:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6Oz+bV+Gv0X/g3Uhpw1VQemg8/XSDR6nocAzxI2gpi8=; b=PYB/SHVtMmlvz4ZPb0Lc3rnSPUD96XJF8BGg4tZwOlfbE6d95yrRanxAmHQb0gYo59 JNUnB6oX2f/EHaAUE+72fcSXO9db+Lr3W2ovongULHUwHJgYF0FE0KbQujlwBr+BfWea 6aC+VC85unG4ubDdghhcwW/ZIdL1kN67gDcg1kjdyTK7hKUUrv71vlbVez5zgtyyK39P IdA+OJtyqEb/5KW5pU/9kMP9OelPnZNQyH+PMiwAXKztgbSDiNeTLocKWgjdDhmJ+FT2 woAams7CPy9rNEuigKXQgUFiDt5VAL64EgJleQPnMxdiKMCG1GwnN9Snslz+Q6A2y1w2 mfWg== X-Gm-Message-State: AOAM531kjz9ZQUmkbkXbI5fWY5eCbmTA3FdqgGDYcvYO3DApQV6I3jDk rz/KpslDcSfTS4hegZI4rJIq3aOBDyc= X-Google-Smtp-Source: ABdhPJyGGMC9oXXnLf0La2Uy+sUZHQu5GdggwdnKIUFmyNg67g/A+/ZoLeVTfiXqfPpSo37mlQto4Q== X-Received: by 2002:a17:902:7689:b029:da:52:4586 with SMTP id m9-20020a1709027689b02900da00524586mr28150553pll.47.1608053437168; Tue, 15 Dec 2020 09:30:37 -0800 (PST) Received: from gnu-cfl-2.localdomain (c-69-181-90-243.hsd1.ca.comcast.net. [69.181.90.243]) by smtp.gmail.com with ESMTPSA id ds24sm22228834pjb.30.2020.12.15.09.30.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Dec 2020 09:30:34 -0800 (PST) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 7070C1A03F0; Tue, 15 Dec 2020 09:30:33 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: V4 [PATCH 2/3] Warn used and not used symbols in section with the same name Date: Tue, 15 Dec 2020 09:30:30 -0800 Message-Id: <20201215173031.3242097-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201215173031.3242097-1-hjl.tools@gmail.com> References: <20201215173031.3242097-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3040.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" When SECTION_RETAIN is used, issue a warning when a symbol without used attribute and a symbol with used attribute are placed in the section with the same name, like int __attribute__((used,section(".data.foo"))) foo2 = 2; int __attribute__((section(".data.foo"))) foo1 = 1; since assembler will put them in different sections with the same section name. gcc/ PR target/98146 * varasm.c (switch_to_section): Warn when a symbol without used attribute and a symbol with used attribute are placed in the section with the same name. gcc/testsuite/ PR target/98146 * c-c++-common/attr-used-5.c: Updated. * c-c++-common/attr-used-6.c: Likewise. * c-c++-common/attr-used-7.c: Likewise. * c-c++-common/attr-used-8.c: Likewise. --- gcc/testsuite/c-c++-common/attr-used-5.c | 1 + gcc/testsuite/c-c++-common/attr-used-6.c | 1 + gcc/testsuite/c-c++-common/attr-used-7.c | 1 + gcc/testsuite/c-c++-common/attr-used-8.c | 1 + gcc/varasm.c | 22 +++++++++++++++++++--- 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/gcc/testsuite/c-c++-common/attr-used-5.c b/gcc/testsuite/c-c++-common/attr-used-5.c index 9fc0d3834e9..ba59326e452 100644 --- a/gcc/testsuite/c-c++-common/attr-used-5.c +++ b/gcc/testsuite/c-c++-common/attr-used-5.c @@ -10,6 +10,7 @@ extern struct dtv_slotinfo_list *list; static int __attribute__ ((section ("__libc_freeres_fn"))) free_slotinfo (struct dtv_slotinfo_list **elemp) +/* { dg-warning "'.*' without 'used' attribute and '.*' with 'used' attribute are placed in a section with the same name" "" { target R_flag_in_section } .-1 } */ { if (!free_slotinfo (&(*elemp)->next)) return 0; diff --git a/gcc/testsuite/c-c++-common/attr-used-6.c b/gcc/testsuite/c-c++-common/attr-used-6.c index 0cb82ade5a9..5d20f875bf0 100644 --- a/gcc/testsuite/c-c++-common/attr-used-6.c +++ b/gcc/testsuite/c-c++-common/attr-used-6.c @@ -18,6 +18,7 @@ free_slotinfo (struct dtv_slotinfo_list **elemp) __attribute__ ((section ("__libc_freeres_fn"))) void free_mem (void) +/* { dg-warning "'.*' without 'used' attribute and '.*' with 'used' attribute are placed in a section with the same name" "" { target R_flag_in_section } .-1 } */ { free_slotinfo (&list); } diff --git a/gcc/testsuite/c-c++-common/attr-used-7.c b/gcc/testsuite/c-c++-common/attr-used-7.c index fba2706ffc1..75576bcabe5 100644 --- a/gcc/testsuite/c-c++-common/attr-used-7.c +++ b/gcc/testsuite/c-c++-common/attr-used-7.c @@ -3,6 +3,7 @@ int __attribute__((used,section(".data.foo"))) foo2 = 2; int __attribute__((section(".data.foo"))) foo1 = 1; +/* { dg-warning "'.*' without 'used' attribute and '.*' with 'used' attribute are placed in a section with the same name" "" { target R_flag_in_section } .-1 } */ /* { dg-final { scan-assembler ".data.foo,\"aw\"" { target R_flag_in_section } } } */ /* { dg-final { scan-assembler ".data.foo,\"awR\"" { target R_flag_in_section } } } */ diff --git a/gcc/testsuite/c-c++-common/attr-used-8.c b/gcc/testsuite/c-c++-common/attr-used-8.c index 4da4aabe573..e4982db1044 100644 --- a/gcc/testsuite/c-c++-common/attr-used-8.c +++ b/gcc/testsuite/c-c++-common/attr-used-8.c @@ -2,6 +2,7 @@ /* { dg-options "-Wall -O2" } */ int __attribute__((section(".data.foo"))) foo1 = 1; +/* { dg-warning "'.*' without 'used' attribute and '.*' with 'used' attribute are placed in a section with the same name" "" { target R_flag_in_section } .-1 } */ int __attribute__((used,section(".data.foo"))) foo2 = 2; /* { dg-final { scan-assembler ".data.foo,\"aw\"" { target R_flag_in_section } } } */ diff --git a/gcc/varasm.c b/gcc/varasm.c index e3e49d53e5d..72d5379303a 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -7761,11 +7761,27 @@ switch_to_section (section *new_section, tree decl) { /* If the SECTION_RETAIN bit doesn't match, switch to a new section. */ + tree used_decl, no_used_decl; + if (DECL_PRESERVE_P (decl)) - new_section->common.flags |= SECTION_RETAIN; + { + new_section->common.flags |= SECTION_RETAIN; + used_decl = decl; + no_used_decl = new_section->named.decl; + } else - new_section->common.flags &= ~(SECTION_RETAIN - | SECTION_DECLARED); + { + new_section->common.flags &= ~(SECTION_RETAIN + | SECTION_DECLARED); + used_decl = new_section->named.decl; + no_used_decl = decl; + } + warning (OPT_Wattributes, + "%+qD without % attribute and %qD with " + "% attribute are placed in a section with " + "the same name", no_used_decl, used_decl); + inform (DECL_SOURCE_LOCATION (used_decl), + "%qD was declared here", used_decl); } else return;