diff options
| author | Fangrui Song <i@maskray.me> | 2021-02-26 11:59:53 -0800 |
|---|---|---|
| committer | Fangrui Song <i@maskray.me> | 2021-02-26 16:37:50 -0800 |
| commit | 8afdacba9dcd36fc838eb86fca86f7f903040030 (patch) | |
| tree | bba9167727ae7c03a880fa94619cbebdaf5339db /clang/lib/CodeGen/CodeGenModule.cpp | |
| parent | 233ba2709bde54ea820cdaba0405d46b2c197e01 (diff) | |
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
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
| -rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 564b07582376..0d499a564039 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1896,6 +1896,8 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, if (D) { if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) { + if (D->hasAttr<RetainAttr>()) + addUsedGlobal(GV); if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>()) GV->addAttribute("bss-section", SA->getName()); if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>()) @@ -1907,6 +1909,8 @@ void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, } if (auto *F = dyn_cast<llvm::Function>(GO)) { + if (D->hasAttr<RetainAttr>()) + addUsedGlobal(F); if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>()) if (!D->getAttr<SectionAttr>()) F->addFnAttr("implicit-section-name", SA->getName()); |
