diff mbox series

V4 [PATCH 2/3] Warn used and not used symbols in section with the same name

Message ID 20201215173031.3242097-3-hjl.tools@gmail.com
State New
Headers show
Series V4 [PATCH 2/3] Warn used and not used symbols in section with the same name | expand

Commit Message

H.J. Lu Dec. 15, 2020, 5:30 p.m. UTC
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(-)

Comments

Jeff Law Dec. 16, 2020, 12:44 a.m. UTC | #1
On 12/15/20 10:30 AM, H.J. Lu wrote:
> 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.
OK
jeff
diff mbox series

Patch

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 %<used%> attribute and %qD with "
+		   "%<used%> 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;