For ELF targets, GCC 11 will set SHF_GNU_RETAIN on the section of a
__attribute__((retain)) function/variable to prevent linker garbage
collection. (See AttrDocs.td for the linker support).
This patch adds retain functions/variables to the llvm.used list, which has
the desired linker GC semantics. Note: retain does not imply used,
so an unused function/variable can be dropped by Sema.
Before 'retain' was introduced, previous ELF solutions require inline asm or
linker tricks, e.g. asm volatile(".reloc 0, R_X86_64_NONE, target");
(architecture dependent) or define a non-local symbol in the section and use
ld -u. There was no elegant source-level solution.
With D97448, __attribute__((retain)) will set SHF_GNU_RETAIN on ELF targets.
I think it would be helpful to add more context here, since each linker has its own terminology to describe section GC. Here's my attempt at starting this paragraph:
Whether this attribute has any effect on the linker depends on the target and the linker. Most linkers support the feature of section garbage collection (--gc-sections), also known as "dead stripping" (ld64 -dead_strip) or discarding unreferenced sections (link.exe /OPT:REF). On COFF and Mach-O targets (Windows and Apple platforms), the `used` attribute prevents symbols from being removed by linker section GC. On ELF targets, ...
I'm not sure how to link to the docs for the various command line flags from rst, but I think it would be helpful.