[llvm-dev] Symbols in 'llvm.used' stripped by --gc-sections

Johan Engelen via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 1 10:11:29 PDT 2019

Hi all,
  The documentation of `@llvm.used` says:

"If a symbol appears in the @llvm.used list, then the compiler, assembler,
and linker are required to treat the symbol as if there is a reference to
the symbol that it cannot see (which is why they have to be named)."

We've always understood this as: the symbol will survive into the final
executable, also when it is in its own section and with `--gc-sections`.
But I think that's not true.
My understanding now is that `@llvm.used` is completely ignored by the
linker when creating a final whole-program binary. That is, the symbol is
emitted by the compiler into the object file, so not stripped e.g. by DCE.
But when the linker combines multiple object files into the final
executable, the symbol will be removed by `--gc-sections` when the linker
sees no reference to it.
It appears that the only way to keep an unreferenced symbol while linking
with `--gc-sections` (compiling with `-f{function,data}-sections`) is to
put it in a segment that is kept as specified by the linkerscript (`KEEP`).

I hope someone can tell me that my current understanding is correct, and
that we have to use another way to keep a symbol until the very end (tips
welcome, without modifying the system default linker script...).

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190401/20f6b89c/attachment.html>

More information about the llvm-dev mailing list