From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by sourceware.org (Postfix) with ESMTPS id 0668B385800F for ; Tue, 8 Dec 2020 12:51:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0668B385800F Received: by mail-pg1-x543.google.com with SMTP id t3so12084744pgi.11 for ; Tue, 08 Dec 2020 04:51:33 -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=2QJp7NhKwUURtbXrnIGZXTO4y32O+6jeJARjI7/hhzQ=; b=XmtnOTZgyA3ncjfz0N5/uAakuYoVFH1qVOyD5y6LqGaa1glxryozpJtzRxddiZ46TB +eP4j+7Ccc0DrMWVnEolIAnV2dzts5Sy5tEgbvzbQYsHLwQfd1HW6UKKliDcrJBFvLaC 5DcUXBMhMWaJy+n2U0y7qkbToljLSE1WT+OAAXXzonhCYxZ+kCSJn/g3S5EVZ2VORLP8 1wguVzBF4zGir9KedGctqLz9I/RAusqksYujoc51hQri5JMnmmEtn/FuaQ/S804y3gEw Xp3oRzGmyUuM8EVq6DKVP4S825VkMyKVHF9vQJlcOeDM2p7n7H9sHjEtP+ir2MXw2+D2 pd+Q== X-Gm-Message-State: AOAM530i8zwxoxKLDilAWv0zOz6ylcLb+rJT6FkHKBbBkizR8OXiYA8f m4YZEAtSWCkQeo3Na3YjCqwEdnNnKoE= X-Google-Smtp-Source: ABdhPJyh+tq4DTPos9vLjU9zEbzJnDU5R3EHZFnxBFOarKNh1uzmDdms/227w9O0CaVWW96QzHudhg== X-Received: by 2002:a63:4648:: with SMTP id v8mr23142719pgk.248.1607431892798; Tue, 08 Dec 2020 04:51:32 -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 gz2sm3234846pjb.2.2020.12.08.04.51.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Dec 2020 04:51:31 -0800 (PST) Received: from gnu-cfl-2.localdomain (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id A0E321A017E; Tue, 8 Dec 2020 04:51:30 -0800 (PST) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Subject: V3 [PATCH 2/2] Warn used and not used symbols in section with the same name Date: Tue, 8 Dec 2020 04:51:28 -0800 Message-Id: <20201208125128.775313-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20201208125128.775313-1-hjl.tools@gmail.com> References: <20201208125128.775313-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3040.4 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-List-Received-Date: Tue, 08 Dec 2020 12:51:35 -0000 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 c5ea29c4e4c..346b3bea890 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -7732,11 +7732,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; -- 2.28.0