When SECTION_RETAIN is used, issue a warning of symbol without used attribute is placed in the same section with symbol with used attribute, 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 other/98121 * varasm.c (switch_to_section): Warn if symbol without used attribute is placed in a section with symbol with used attribute. gcc/testsuite/ PR other/98121 * 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 | 19 +++++++++++++++++-- 5 files changed, 21 insertions(+), 2 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..f9199587c51 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 is placed in a section with" "" { target *-*-* } .-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 4526a692ee4..8f60c550be1 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 is placed in a section with" "" { target *-*-* } .-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..cca1f2b8a33 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 is placed in a section with" "" { target *-*-* } .-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 c8d65f65033..c797610e1d7 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 is placed in a section with" "" { target *-*-* } .-1 } */ int __attribute__((used,section(".data.foo"))) foo2 = 2; /* { dg-final { scan-assembler ".data.foo\n" { target R_flag_in_section } } } */ diff --git a/gcc/varasm.c b/gcc/varasm.c index 7271705198c..fde00b5520e 100644 --- a/gcc/varasm.c +++ b/gcc/varasm.c @@ -7728,10 +7728,25 @@ 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; + { + new_section->common.flags &= ~SECTION_RETAIN; + used_decl = new_section->named.decl; + no_used_decl = decl; + } + warning_at (DECL_SOURCE_LOCATION (no_used_decl), + OPT_Wattributes, + "%+qD without %<used%> attribute is placed in a " + "section with %qD with %<used%> attribute", + no_used_decl, used_decl); } else return; -- 2.28.0