[ELF] Fix wrapping symbols produced during LTO codegen
authorShoaib Meenai <smeenai@fb.com>
Wed, 20 Apr 2022 01:04:17 +0000 (18:04 -0700)
committerShoaib Meenai <smeenai@fb.com>
Fri, 22 Apr 2022 23:45:21 +0000 (16:45 -0700)
commit1af25a986069f2ae8c724133fa8649bb795a7925
tree1bfc1fbf4cf82d3e89365028e5ac1984280777b8
parentb862bcbf4455e3a9f6cb1c9be16ef529415b5357
[ELF] Fix wrapping symbols produced during LTO codegen

We were previously not correctly wrapping symbols that were only
produced during LTO codegen and unreferenced before then, or symbols
only referenced from such symbols. The root cause was that we weren't
marking the wrapped symbol as used if we only saw the use after LTO
codegen, leading to the failed wrapping.

Fix this by explicitly tracking whether a symbol will become referenced
after wrapping is done. We can use this property to tell LTO to preserve
such symbols, instead of overload isUsedInRegularObj for this purpose.
Since we're no longer setting isUsedInRegularObj for all symbols which
will be wrapped, its value at the time of performing the wrapping in the
symbol table will accurately reflect whether the symbol was actually
used in an object (including in an LTO-generated object), and we can
propagate that value to the wrapped symbol and thereby ensure we wrap
correctly.

This incorrect wrapping was the only scenario I was aware of where we
produced an invalid PLT relocation, which D123985 started diagnosing,
and with it fixed, we lose the test for that diagnosis. I think it's
worth keeping the diagnosis though, in case we run into other issues in
the future which would be caught by it.

Fixes PR50675.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D124056
lld/ELF/Driver.cpp
lld/ELF/LTO.cpp
lld/ELF/SymbolTable.cpp
lld/ELF/Symbols.h
lld/test/ELF/lto/wrap-unreferenced-before-codegen.test [new file with mode: 0644]