[all-commits] [llvm/llvm-project] 8afdac: Add GNU attribute 'retain'

Fangrui Song via All-commits all-commits at lists.llvm.org
Fri Feb 26 16:38:59 PST 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 8afdacba9dcd36fc838eb86fca86f7f903040030
      https://github.com/llvm/llvm-project/commit/8afdacba9dcd36fc838eb86fca86f7f903040030
  Author: Fangrui Song <i at maskray.me>
  Date:   2021-02-26 (Fri, 26 Feb 2021)

  Changed paths:
    M clang/include/clang/Basic/Attr.td
    M clang/include/clang/Basic/AttrDocs.td
    M clang/lib/CodeGen/CGDecl.cpp
    M clang/lib/CodeGen/CodeGenModule.cpp
    M clang/lib/Sema/SemaDecl.cpp
    A clang/test/CodeGen/attr-retain.c
    A clang/test/CodeGenCXX/attr-retain.cpp
    A clang/test/Sema/attr-retain.c

  Log Message:
  -----------
  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


  Commit: 47c5576d7d586624c38f76bd3168e05f6ef1f838
      https://github.com/llvm/llvm-project/commit/47c5576d7d586624c38f76bd3168e05f6ef1f838
  Author: Fangrui Song <i at maskray.me>
  Date:   2021-02-26 (Fri, 26 Feb 2021)

  Changed paths:
    M llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
    M llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
    M llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
    M llvm/test/CodeGen/PowerPC/func-addr-consts.ll
    M llvm/test/CodeGen/PowerPC/no-dead-strip.ll
    A llvm/test/CodeGen/X86/elf-retain.ll

  Log Message:
  -----------
  ELF: Create unique SHF_GNU_RETAIN sections for llvm.used global objects

If a global object is listed in `@llvm.used`, place it in a unique section with
the `SHF_GNU_RETAIN` flag. The section is a GC root under `ld --gc-sections`
with LLD>=13 or GNU ld>=2.36.

For front ends which do not expect to see multiple sections of the same name,
consider emitting `@llvm.compiler.used` instead of `@llvm.used`.

SHF_GNU_RETAIN is restricted to ELFOSABI_GNU and ELFOSABI_FREEBSD in
binutils. We don't do the restriction - see the rationale in D95749.

The integrated assembler has supported SHF_GNU_RETAIN since D95730.
GNU as>=2.36 supports section flag 'R'.
We don't need to worry about GNU ld support because older GNU ld just ignores
the unknown SHF_GNU_RETAIN.

With this change, `__attribute__((retain))` functions/variables emitted
by clang will get the SHF_GNU_RETAIN flag.

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


Compare: https://github.com/llvm/llvm-project/compare/233ba2709bde...47c5576d7d58


More information about the All-commits mailing list