From patchwork Tue Dec 8 12:51:27 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: 1412672 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=Reb9MSYl; 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 4Cr0S70PKjz9sWQ for ; Tue, 8 Dec 2020 23:51:51 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D568C3939C1D; Tue, 8 Dec 2020 12:51:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D568C3939C1D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1607431897; bh=8aKERvPSL0P0r4BK94B4ATRDvxiqyufIXHT0JabQ4bs=; 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=Reb9MSYlTv2aYRjpIc2p9tQSpHF4BuDK3J/ueIifHcnfl8mzY4+vFpVRLXIdOZ+Dr SMGzpAFSPpovKOln3sZ9/FiVH7N6HIBGntLqsWJwy7DAe2IIwrD+olVo7kzQaXwR9g +6Svz3Cl2N2+CSN0VKEd80hegf8BxjvF2NmL6rTU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by sourceware.org (Postfix) with ESMTPS id 33B193857819 for ; Tue, 8 Dec 2020 12:51:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 33B193857819 Received: by mail-pf1-x441.google.com with SMTP id i3so10237377pfd.6 for ; Tue, 08 Dec 2020 04:51:35 -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=8aKERvPSL0P0r4BK94B4ATRDvxiqyufIXHT0JabQ4bs=; b=O4KEsHMPxT2FXkjjbdNs7Vz6G5TJxZ+puYX0KS6fxQmTF6e8YHaKIOzON1piejYfJ0 MJZbboJsVepgzxS4LM/NCBDrO58I0SRmZ1Jucw1JRLVyYlpSQr7g+Nv2Fl8XFyaunKmm XwiKByaNJ67esZOaLePbEKbtq/1hTu5Q8UD7ARxpMWsJfyfoloh2KKCEEn56HfgmDT5d /6B8IsHbyIg5qi3GC3NKnWGujK3/hdBrwu5QIzXfCII0KuvxUEacvxGgZZLkkYq7fgrh YDa6zSj7V/G9Tw1eiLCQQrGk7JIlKtGONdvq1rL4DrowSbOq01YAkGp+t0HdIG+ifhwj 7HPw== X-Gm-Message-State: AOAM533vJ/5pF2ke+JzkTBEC9W0m/ht5jvc5wDA2PYx4ZCwt4QqTJqdE xVxRFqCdUd4p22mLlAqUAkVHJpd8Lrk= X-Google-Smtp-Source: ABdhPJxhugAjBMeMYv0K78vzlX8vDo3fXXGGinFlI0kKLKKNLR6IKBE26e/wjfxZe5kNLAE+pAMJwA== X-Received: by 2002:a17:90a:bb83:: with SMTP id v3mr4180530pjr.28.1607431893924; Tue, 08 Dec 2020 04:51:33 -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 m3sm9640066pfa.134.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 94C0A1A0178; Tue, 8 Dec 2020 04:51:30 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: V3 [PATCH 1/2] Switch to a new section if the SECTION_RETAIN bit doesn't match Date: Tue, 8 Dec 2020 04:51:27 -0800 Message-Id: <20201208125128.775313-2-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 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-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 definitions marked with used attribute and unmarked definitions are placed in the section with the same name, switch to a new section if the SECTION_RETAIN bit doesn't match. gcc/ PR target/98146 * output.h (switch_to_section): Add a tree argument, default to nullptr. * varasm.c (get_section): If the SECTION_RETAIN bit doesn't match, return and switch to a new section later. (assemble_start_function): Pass decl to switch_to_section. (assemble_variable): Likewise. (switch_to_section): If the SECTION_RETAIN bit doesn't match, switch to a new section. gcc/testsuite/ PR target/98146 * c-c++-common/attr-used-5.c: New test. * c-c++-common/attr-used-6.c: Likewise. * c-c++-common/attr-used-7.c: Likewise. * c-c++-common/attr-used-8.c: Likewise. * c-c++-common/attr-used-9.c: Likewise. --- gcc/output.h | 2 +- gcc/testsuite/c-c++-common/attr-used-5.c | 26 ++++++++++++++++++++ gcc/testsuite/c-c++-common/attr-used-6.c | 26 ++++++++++++++++++++ gcc/testsuite/c-c++-common/attr-used-7.c | 8 +++++++ gcc/testsuite/c-c++-common/attr-used-8.c | 8 +++++++ gcc/testsuite/c-c++-common/attr-used-9.c | 28 ++++++++++++++++++++++ gcc/varasm.c | 30 ++++++++++++++++++++---- 7 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/attr-used-5.c create mode 100644 gcc/testsuite/c-c++-common/attr-used-6.c create mode 100644 gcc/testsuite/c-c++-common/attr-used-7.c create mode 100644 gcc/testsuite/c-c++-common/attr-used-8.c create mode 100644 gcc/testsuite/c-c++-common/attr-used-9.c diff --git a/gcc/output.h b/gcc/output.h index fa8ace1f394..1f9af46da1d 100644 --- a/gcc/output.h +++ b/gcc/output.h @@ -548,7 +548,7 @@ extern void switch_to_other_text_partition (void); extern section *get_cdtor_priority_section (int, bool); extern bool unlikely_text_section_p (section *); -extern void switch_to_section (section *); +extern void switch_to_section (section *, tree = nullptr); extern void output_section_asm_op (const void *); extern void record_tm_clone_pair (tree, tree); diff --git a/gcc/testsuite/c-c++-common/attr-used-5.c b/gcc/testsuite/c-c++-common/attr-used-5.c new file mode 100644 index 00000000000..9fc0d3834e9 --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-used-5.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -O2" } */ + +struct dtv_slotinfo_list +{ + struct dtv_slotinfo_list *next; +}; + +extern struct dtv_slotinfo_list *list; + +static int __attribute__ ((section ("__libc_freeres_fn"))) +free_slotinfo (struct dtv_slotinfo_list **elemp) +{ + if (!free_slotinfo (&(*elemp)->next)) + return 0; + return 1; +} + +__attribute__ ((used, section ("__libc_freeres_fn"))) +static void free_mem (void) +{ + free_slotinfo (&list); +} + +/* { dg-final { scan-assembler "__libc_freeres_fn,\"ax\"" { target R_flag_in_section } } } */ +/* { dg-final { scan-assembler "__libc_freeres_fn,\"axR\"" { target R_flag_in_section } } } */ diff --git a/gcc/testsuite/c-c++-common/attr-used-6.c b/gcc/testsuite/c-c++-common/attr-used-6.c new file mode 100644 index 00000000000..0cb82ade5a9 --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-used-6.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -O2" } */ + +struct dtv_slotinfo_list +{ + struct dtv_slotinfo_list *next; +}; + +extern struct dtv_slotinfo_list *list; + +static int __attribute__ ((used, section ("__libc_freeres_fn"))) +free_slotinfo (struct dtv_slotinfo_list **elemp) +{ + if (!free_slotinfo (&(*elemp)->next)) + return 0; + return 1; +} + +__attribute__ ((section ("__libc_freeres_fn"))) +void free_mem (void) +{ + free_slotinfo (&list); +} + +/* { dg-final { scan-assembler "__libc_freeres_fn,\"ax\"" { target R_flag_in_section } } } */ +/* { dg-final { scan-assembler "__libc_freeres_fn,\"axR\"" { target R_flag_in_section } } } */ diff --git a/gcc/testsuite/c-c++-common/attr-used-7.c b/gcc/testsuite/c-c++-common/attr-used-7.c new file mode 100644 index 00000000000..fba2706ffc1 --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-used-7.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -O2" } */ + +int __attribute__((used,section(".data.foo"))) foo2 = 2; +int __attribute__((section(".data.foo"))) foo1 = 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 new file mode 100644 index 00000000000..4da4aabe573 --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-used-8.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -O2" } */ + +int __attribute__((section(".data.foo"))) foo1 = 1; +int __attribute__((used,section(".data.foo"))) foo2 = 2; + +/* { 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-9.c b/gcc/testsuite/c-c++-common/attr-used-9.c new file mode 100644 index 00000000000..cf3bde67622 --- /dev/null +++ b/gcc/testsuite/c-c++-common/attr-used-9.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-Wall -O2" } */ + +struct dtv_slotinfo_list +{ + struct dtv_slotinfo_list *next; +}; + +extern struct dtv_slotinfo_list *list; + +static int __attribute__ ((used, section ("__libc_freeres_fn"))) +free_slotinfo (struct dtv_slotinfo_list **elemp) +{ + if (!free_slotinfo (&(*elemp)->next)) + return 0; + return 1; +} + +__attribute__ ((section ("__libc_freeres_fn"))) +static void free_mem (void) +/* { dg-warning "defined but not used" "" { target *-*-* } .-1 } */ +{ + free_slotinfo (&list); +} + +/* { dg-final { scan-assembler-not "__libc_freeres_fn\n" } } */ +/* { dg-final { scan-assembler-not "__libc_freeres_fn,\"ax\"" { target R_flag_in_section } } } */ +/* { dg-final { scan-assembler "__libc_freeres_fn,\"axR\"" { target R_flag_in_section } } } */ diff --git a/gcc/varasm.c b/gcc/varasm.c index 0fac3688828..c5ea29c4e4c 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -342,6 +342,11 @@ get_section (const char *name, unsigned int flags, tree decl, sect->common.flags |= (SECTION_WRITE | SECTION_RELRO); return sect; } + /* If the SECTION_RETAIN bit doesn't match, return and switch + to a new section later. */ + if ((sect->common.flags & SECTION_RETAIN) + != (flags & SECTION_RETAIN)) + return sect; /* Sanity check user variables for flag changes. */ if (sect->named.decl != NULL && DECL_P (sect->named.decl) @@ -1852,7 +1857,7 @@ assemble_start_function (tree decl, const char *fnname) /* Switch to the correct text section for the start of the function. */ - switch_to_section (function_section (decl)); + switch_to_section (function_section (decl), decl); if (crtl->has_bb_partition && !hot_label_written) ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label); @@ -2348,7 +2353,7 @@ assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED, && (strcmp (sect->named.name, ".vtable_map_vars") == 0)) handle_vtv_comdat_section (sect, decl); else - switch_to_section (sect); + switch_to_section (sect, decl); if (align > BITS_PER_UNIT) ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT)); assemble_variable_contents (decl, name, dont_output_data, @@ -7715,10 +7720,27 @@ output_section_asm_op (const void *directive) the current section is NEW_SECTION. */ void -switch_to_section (section *new_section) +switch_to_section (section *new_section, tree decl) { if (in_section == new_section) - return; + { + if (HAVE_GAS_SHF_GNU_RETAIN + && (new_section->common.flags & SECTION_NAMED) + && decl != nullptr + && (!!DECL_PRESERVE_P (decl) + != !!(new_section->common.flags & SECTION_RETAIN))) + { + /* If the SECTION_RETAIN bit doesn't match, switch to a new + section. */ + if (DECL_PRESERVE_P (decl)) + new_section->common.flags |= SECTION_RETAIN; + else + new_section->common.flags &= ~(SECTION_RETAIN + | SECTION_DECLARED); + } + else + return; + } if (new_section->common.flags & SECTION_FORGET) in_section = NULL;