[PATCH] D97448: ELF: Create unique SHF_GNU_RETAIN sections for llvm.used global objects

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 9 01:01:10 PDT 2021


MaskRay added a comment.

In D97448#2933741 <https://reviews.llvm.org/D97448#2933741>, @nikic wrote:

> Thanks for the reference! The part that wasn't obvious to me (both from the patch summary here and the gold bug) is that using SHF_GNU_RETAIN with an older binutils version will not only ignore the retain semantics (which is entirely expected), but will lead to silent miscompiles unrelated to retain semantics. From the description here and rnk's comment, it sounded like there aren't any toolchain compatibility concerns.
>
> It would be good to update LangRef in this regard. Currently the `llvm.used` documentation says:
>
>> This is commonly used to represent references from inline asms and other things the compiler cannot “see”, and corresponds to “attribute((used))” in GNU C.

LangRef has mentioned the linker behavior since 2009 (rGae76db5edd9251d76af488d601be01d903805f8e <https://reviews.llvm.org/rGae76db5edd9251d76af488d601be01d903805f8e>), despite the fact that ELF did not really distinguish `llvm.used` and `llvm.compiler.used` before this patch.

GCC never adds linker semantics to `__attribute__((used))`.
The armcc documentation mentions the linker behavior "Data marked with __attribute__((used)) is tagged in the object file to avoid removal by linker unused section removal."
I don't have access to armcc to check whether/how this is actually satisfied.
Clang Mach-O may use the semantics for a long time.

> And the `llvm.compiler.used` documentation says:
>
>> This is a rare construct that should only be used in rare circumstances, and should not be exposed to source languages.
>
> While now the recommendation seems to be the other way around: Frontends should map "used"-like attributes to `llvm.compiler.used` rather than `llvm.used`, and using the latter may be dangerous if the frontend doesn't control the used toolchain.

Using is the latter is not more dangerous:/ The compiler just did not do what LangRef promised on ELF for a long time.
Rust likely misused `llvm.used`...

That said, sounds good to make `llvm.compiler.used` more supported, since the GCC/Clang/(Rust?) behavior has become more popular.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97448/new/

https://reviews.llvm.org/D97448



More information about the llvm-commits mailing list