[MC] .addrsig_sym: ignore unregistered symbols
authorFangrui Song <i@maskray.me>
Tue, 11 Oct 2022 22:07:14 +0000 (15:07 -0700)
committerFangrui Song <i@maskray.me>
Tue, 11 Oct 2022 22:07:14 +0000 (15:07 -0700)
commitd8162a7196b3916d3bb51b2597c9e417e4132150
tree2f06aa718a76a145bf2f99870de782706e178fe8
parent62066929310fc95bea1bacf8ec26acbb4f216d4c
[MC] .addrsig_sym: ignore unregistered symbols

.addrsig_sym forces registering the symbol regardless whether it is otherwise
registered. This creates an undefined symbol which is inconvenient/undesired:

* `extern int x; void f() { (void)x; }` has inconsistent behavior whether `x` is emitted as an undefined symbol.
  `-O0 -faddrsig` makes `x` undefined while other -O levels and -fno-addrsig eliminate the symbol.
* In ThinLTO, after a non-prevailing linkonce_odr definition is converted to available_externally, and then a declaration,
  the addrsig code emits a symbol while the symbol is otherwise unseen.

D135427 fixed a bug that a non-prevailing `__cxx_global_var_init` was
incorrectly retained. However, the IR declaration causes an undesired
`.addrsig_sym __cxx_global_var_init`. This can be addressed in a way similar
to D101512 (`isTransitiveUsedByMetadataOnly`) but the increased
`OutStreamer->emitAddrsigSym(getSymbol(&GV));` complexity makes me nervous.
Just ignoring unregistered symbols circumvents the problem.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D135642
llvm/docs/Extensions.rst
llvm/lib/MC/ELFObjectWriter.cpp
llvm/lib/MC/MCObjectStreamer.cpp
llvm/lib/MC/MachObjectWriter.cpp
llvm/lib/MC/WinCOFFObjectWriter.cpp
llvm/test/MC/COFF/addrsig.s
llvm/test/MC/ELF/addrsig-error.s [deleted file]
llvm/test/MC/ELF/addrsig.s
llvm/test/MC/MachO/addrsig.s