When SECTION_RETAIN is used, definitions marked with used attribute and unmarked definitions are placed in the same section. 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 -fdiagnostics-plain-output c.c:2:49: warning: ‘foo1’ without ‘used’ attribute is placed in a section with ‘foo2’ with ‘used’ attribute [-Wattributes] [hjl@gnu-cfl-2 gcc]$ H.J. Lu (2): Switch to a new section if the SECTION_RETAIN bit doesn't match Warn used and not used symbols in the same section 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/varasm.c | 43 +++++++++++++++++++++--- 6 files changed, 112 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 -- 2.28.0