https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128758
Bug ID: 128758
Summary: __attribute__((selectany)) for functions
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: pali at kernel dot org
Target Milestone: ---
gcc for C currently does not support marking function with selectany attribute.
It prints error:
'selectany' attribute applies only to initialized variables with external
linkage
The expected behavior is that the linker will choose one of them, same as for
variable marked with that attribute. This functionality is already supported by
GNU linker also for functions, not just variables, by using the asm directive
".linkonce" on the section where the (function or variable) symbol is
specified.
This behavior of choosing just one function if multiple definitions are present
is already provided by gcc for C++ language when using C++ inline functions. So
the support itself is already available in gcc, just not present for C.
It looks like that emulation the selectany attribute for functions could be
achieved by this construction:
__attribute__((section(".text$my_function")))
int my_function(void) {
asm(".linkonce");
/* function implementation */
return 0;
}
It would be nice if gcc allows selectany attribute also for functions by
marking it with .linkonce like above and like it is already doing for variables
(just without same_size).