When SECTION_RETAIN is used, definitions marked with used attribute and unmarked definitions are placed in a section with the same name. Instead of issue an error:
[hjl@gnu-cfl-2 gcc]$ /usr/gcc-11.0.0-x32/bin/gcc -S c.c -fdiagnostics-plain-output c.c:2:49: error: ‘foo1’ causes a section type conflict with ‘foo2’ c.c:1:54: note: ‘foo2’ was declared here [hjl@gnu-cfl-2 gcc]$ the first patch switches to a new section if the SECTION_RETAIN bit doesn't match. The second optional patch issues a warning: [hjl@gnu-cfl-2 gcc]$ ./xgcc -B./ -S c.c c.c:2:49: warning: ‘foo1’ without ‘used’ attribute and ‘foo2’ with ‘used’ attribute are placed in a section with the same name [-Wattributes] 2 | const int __attribute__((section(".data.foo"))) foo1 = 1; | ^~~~ c.c:1:54: note: ‘foo2’ was declared here 1 | const int __attribute__((used,section(".data.foo"))) foo2 = 2; | [hjl@gnu-cfl-2 gcc]$ Changes from V3: 1. Add DECL_P (decl) to switch_to_section (). 2. Update comments for get_section (). 3. Require .init_array/.fini_array support for SHF_GNU_RETAIN. Changes from V2: 1. Add (new_section->common.flags & SECTION_NAMED) check since SHF_GNU_RETAIN section must be named. 2. Move c-c++-common/attr-used-9.c to the fist patch since there are no new warnings. 3. Check new warnings only for R_flag_in_section target. H.J. Lu (3): Switch to a new section if the SECTION_RETAIN bit doesn't match Warn used and not used symbols in section with the same name Require .init_array/.fini_array support for SHF_GNU_RETAIN gcc/defaults.h | 11 +++++ gcc/output.h | 2 +- gcc/testsuite/c-c++-common/attr-used-5.c | 27 ++++++++++++ gcc/testsuite/c-c++-common/attr-used-6.c | 27 ++++++++++++ gcc/testsuite/c-c++-common/attr-used-7.c | 9 ++++ gcc/testsuite/c-c++-common/attr-used-8.c | 9 ++++ gcc/testsuite/c-c++-common/attr-used-9.c | 28 ++++++++++++ gcc/testsuite/lib/target-supports.exp | 2 +- gcc/varasm.c | 56 ++++++++++++++++++++---- 9 files changed, 161 insertions(+), 10 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 -- 2.29.2