varasm: Two SECTION_RETAIN fixes [PR100130]
authorRichard Sandiford <richard.sandiford@arm.com>
Wed, 21 Apr 2021 08:08:44 +0000 (09:08 +0100)
committerRichard Sandiford <richard.sandiford@arm.com>
Wed, 21 Apr 2021 08:08:44 +0000 (09:08 +0100)
commitb67778b59932fdc64dc89a97550349e973ca0352
tree5d72991773b38f84f60f8f849481c986648a1212
parentcb60e56d4ac2958c422c003221ca0cff6a537e0b
varasm: Two SECTION_RETAIN fixes [PR100130]

switch_to_section warns if we try to output a retain decl in a
section without a retain flag, or if we try to output a non-retain
decl in a section with a retain flag.  However, the warning only
applied if we were trying to “switch” to the current section.
This works if all decls that use a section are generated consecutively,
but not if there is an unrelated decl in between.

This patch makes the check unconditional, but suppresses the warning
if we're writing the section's named.decl (i.e. the decl from which
the section name and original flags were derived).

Also, the warning didn't fire for -fsection-anchors, for two reasons:
we allowed retain and non-retain decls to be put into the same block,
and we didn't pass a decl to switch_to_section.

Although these are arguably separate bugs, it isn't easy to fix them
independently without temporarily regressing -fsection-anchor targets.

gcc/
PR middle-end/100130
* varasm.c (get_block_for_decl): Make sure that any use of the
retain attribute matches the section's retain flag.
(switch_to_section): Check for retain mismatches even when
changing sections, but do not warn if the given decl is the
section's named.decl.
(output_object_block): Pass the first decl in the block (if any)
to switch_to_section.

gcc/testsuite/
PR middle-end/100130
* c-c++-common/attr-retain-10.c: New test.
* c-c++-common/attr-retain-11.c: Likewise.
gcc/testsuite/c-c++-common/attr-retain-10.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/attr-retain-11.c [new file with mode: 0644]
gcc/varasm.c