HomePhabricator

Add GNU attribute 'retain'

Authored by MaskRay on Feb 26 2021, 11:59 AM.

Description

Add GNU attribute 'retain'

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.

Differential Revision: https://reviews.llvm.org/D97447

Details

Committed
MaskRayFeb 26 2021, 4:37 PM
Differential Revision
D97447: Add GNU attribute 'retain'
Parents
rG233ba2709bde: [Driver] Fix a warning about the the initialization order
Branches
Unknown
Tags
Unknown