https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122516

            Bug ID: 122516
           Summary: [avr] 'retain' attribute ignored even though
                    HAVE_GAS_SHF_GNU_RETAIN
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gjl at gcc dot gnu.org
  Target Milestone: ---

int main (void) { return 0; }

__attribute__((used,retain,section(".text.func")))
void func (void) { }

$ avr-gcc-15 retain.c -S -Os && cat retain.s
retain.c:4:1: warning: 'retain' attribute ignored [-Wattributes]
    4 | void func (void) { }
      | ^~~~

The generated assembly is:

        .section        .text.func,"ax",@progbits
.global func
        .type   func, @function
func:
        ret

and hence func() is discarded with -Wl,--gc-sections.  Expected assembly:

        .section        .text.func,"axR",@progbits

so that the function is retained by the linker.  When I add the "R" flag by
hand, then -Wl,--gc-sections doesn't kill func() any more.

Notice that auto-host.h reads:

$ grep -n SHF_GNU *.h

auto-host.h:1565:/* Define 0/1 if your assembler supports marking sections with
SHF_GNU_RETAIN
auto-host.h:1568:#define HAVE_GAS_SHF_GNU_RETAIN 1

So the R flag is supported, but defaults.h has:

/* This determines whether or not we support marking sections with
   SHF_GNU_RETAIN flag.  Also require .init_array/.fini_array section
   for constructors and destructors.  */
#ifndef SUPPORTS_SHF_GNU_RETAIN
#if HAVE_GAS_SHF_GNU_RETAIN && HAVE_INITFINI_ARRAY_SUPPORT
#define SUPPORTS_SHF_GNU_RETAIN 1
#else
#define SUPPORTS_SHF_GNU_RETAIN 0
#endif
#endif

I don't quite understand why HAVE_INITFINI_ARRAY_SUPPORT is required to retain
functions.  It's perfectly fine to retain functions for avr and maybe on many
other targets, too.

Configured with: ../../source/gcc-master/configure --target=avr --disable-nls
--with-dwarf2 --with-gnu-as --with-gnu-ld --with-long-double=64
--disable-libcc1 --disable-shared --enable-languages=c,c++

Reply via email to