https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99587
--- Comment #6 from Fangrui Song <i at maskray dot me> ---
(In reply to Jakub Jelinek from comment #5)
> (In reply to Florian Weimer from comment #4)
> > For retain, something along these lines might work:
> >
> > diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c
> > index c1f652d1dc9..cdae464ab8a 100644
> > --- a/gcc/c-family/c-attribs.c
> > +++ b/gcc/c-family/c-attribs.c
> > @@ -329,8 +329,10 @@ const struct attribute_spec c_common_attribute_table[]
> > =
> > handle_used_attribute, NULL },
> > { "unused", 0, 0, false, false, false, false,
> > handle_unused_attribute, NULL },
> > +#if SUPPORTS_SHF_GNU_RETAIN
> > { "retain", 0, 0, true, false, false, false,
> > handle_retain_attribute, NULL },
> > +#endif
> > { "externally_visible", 0, 0, true, false, false, false,
> > handle_externally_visible_attribute, NULL },
> > { "no_reorder", 0, 0, true, false, false, false,
> >
> > In other cases, it's more difficult because those are subtarget-dependent.
>
> Doing the above would "fix" __has_attribute, but on the other side would mean
> the compiler would not know how many and what kind of operands the attribute
> has, whether it is for function declarations, other declarations, types or
> what
> etc., so for invalid code it would have inconsistent diagnostics.
Are you willing to properly fix it? :)
I implemented the attribute on clang (https://reviews.llvm.org/D97447).
__has_attribute(retain) is always 1 and there is no ignored diagnostic,
regardless of the target (even if non-ELF), and __has_attribute(retain) works
in assembly mode as well. This is intentional so that: with bleeding-edge
toolchain, non-ELF targets don't need macros to decide whether 'retain' should
be added.
Ultimately, I want the glibc static linking problem with ld -z start-stop-gc
fixed
https://sourceware.org/pipermail/libc-alpha/2021-March/123833.html
(glibc has -Wattributes, so __has_attribute(retain)=1 && "warning: ‘retain’
attribute ignored" can cause some inconvenience.)
And I hope eventually ld -z start-stop-gc can be the default.