[PATCH] D96838: Add GNU attribute 'retain'

Fangrui Song via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 23 13:18:05 PST 2021


MaskRay added a comment.

In D96838#2582965 <https://reviews.llvm.org/D96838#2582965>, @probinson wrote:

>> For ELF, `used` does not retain the entity, regardless of this patch.
>
> That statement does not match my experience.

You probably used C identifier name sections with `__attribute__((used, section("foo")))`. 
The `__start_foo` reference from a live section retains all input sections `foo`: https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order#c-identifier-name-sections

> Note that there are two separate variables documented in the LangRef: `llvm.used` (which corresponds to source attribute `used` and is documented as affecting linker behavior) and `llvm.compiler.used` (which is a rare construct with no corresponding source attribute, and does not affect linker behavior).  So, according to LangRef, source attribute `used` maps to `llvm.used` which is documented as affecting linker behavior, and that's exactly the behavior I am seeing in my project.

As the description of this patch suggests, `llvm.used`has linker behavior effects on macOS with all symbols, and Windows with non-local-linkage symbols, but not effect on ELF.
The behavior you observed is due to `__start_`/`__stop_` references from live input sections. (D96914 <https://reviews.llvm.org/D96914> https://lists.llvm.org/pipermail/llvm-dev/2021-February/148682.html)

> But from a user perspective, the IR representation is irrelevant, I don't actually care what the description of `llvm.used` says.  I put attribute `used` on my data, and it is still there in the final executable.  That's how things have worked for a long time, and I am concerned that you are about to break it.

You probably meant the LLD patch D96914 <https://reviews.llvm.org/D96914>. It does not change the default now.

> Note that I am not using groups or comdats or anything else funky; I'm declaring static variables in a custom section, adding attribute `used` and the necessary `__start_/__stop_` symbols, and everything works exactly as I want.

If you use a non-C-identifier-name section, then `__start_` does not work. `__attribute__((used))` does not retain it on ELF.

> Maybe there is a different patch that more directly relates to my concern, and probably we should be having this conversation on the llvm-dev thread instead of in a patch that  not many people are paying attention to.  I'm very happy to move the discussion elsewhere if that would help.

So GCC folks' opinions is that `used` is orthogonal to `retain`. From this viewpoint, `__attribute__((used))` will be better represented as `llvm.compiler.used`, but LangRef currently discourages this usage.
I am a bit busy currently, but if you move the discussion to llvm-dev, I'll be happy to discuss it there.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D96838



More information about the cfe-commits mailing list