<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64429>64429</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [LLD] LTO error in 17.0.0-rc1: "relocation refers to a symbol in a discarded section"
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          vient
      </td>
    </tr>
</table>

<pre>
    Hi, so I tried 17.0.0-rc1 on our codebase that has no problem compiling with 16.0.6 or GCC 12/13.
LTO build with clang and lld 17.0.0-rc1 produces error
```
ld.lld-17: error: relocation refers to a symbol in a discarded section: std::stack<long, std::deque<long, std::allocator<long>>>::~stack()
>>> defined in /usr/lib/xxx.a
>>> referenced by regex_compiler.tcc:276 (/usr/bin/../lib/gcc/x86_64-linux-gnu/12.1.0/../../../../include/c++/12.1.0/bits/regex_compiler.tcc:276)
>>>               lto.tmp:(std::__detail::_Compiler<std::__cxx11::regex_traits<char>>::_M_quantifier())
>>> referenced by regex_automaton.tcc:228 (/usr/bin/../lib/gcc/x86_64-linux-gnu/12.1.0/../../../../include/c++/12.1.0/bits/regex_automaton.tcc:228)
>>>               lto.tmp:(std::__detail::_StateSeq<std::__cxx11::regex_traits<char>>::_M_clone())
clang++-17: error: linker command failed with exit code 1 (use -v to see invocation)
```

Here it what I found:
* Same build with clang 16 ends successfully.
* Same build with ld using LTO with LLVM gold plugin ends successfully — that's why I think that the problem is in LLD itself and not any shared part with gold plugin.
* Deleting some seemingly random code block from our codebase makes the problem go away. This code block does not use std::stack at all.

I did not manage to create small reproducer and I can't share our whole codebase. Creating this issue in hopes that someone knows what may be going on or can give advice on identifying problem (producing debug output from lld for example).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVkuv4jYY_TVm84kocSDAggUDQ-dKjLqYUbfIsT8SF8fO-MFj099e2YF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