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

    <tr>
        <th>Summary</th>
        <td>
            LLD 18 leaves leaves behind undef symbols from discarded sections
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld:ELF
      </td>
    </tr>

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

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

<pre>
    After https://github.com/llvm/llvm-project/commit/1981b1b6b92f7579a30c9ed32dbdf3bc749c1b40, LLD converts symbols in discarded sections into undef symbols. This happens even if there are no references to the symbol from non-discarded sections.

This change breaks building the Linux kernel with LLD in some configurations (`CONFIG_DEBUG_FORCE_WEAK_PER_CPU=y`) and does not match the behavior of previous LLD versions or the behavior of the BFD linker.

To reproduce the issue:

test.c:
```
__attribute__((__section__(".discard"))) __attribute__((unused)) char __pcpu_unique_foo;
```
module.lds:
```
SECTIONS {
 /DISCARD/ : {
  *(.discard)
 }
}
```

Then run:
```
clang test.c -c
ld.lld -r -T module.lds test.o -o test
objdump -t test
```
This will produce:
```
0000000000000000       O *UND*  0000000000000000 __pcpu_unique_foo
```
While previously it did not.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVMtu2zoQ_RpqM7AgUXJsLbRwLKsIbpAUeaBLgY-RxYYifflwb_7-QrLTJE2yqECY5HA4M2d4fJj3am8Qa7K8JMsmYTEM1tVGPSmRcCuf600f0MEQwsGTYkNoS2i7V2GIPBV2JLTV-vgyLQ7O_kQRCG2FHUc1LfJqnfOcX_CK9qvlqmJFJiqUBZVc9gUXq7ISOS8zQrdwfd2AsOaILnjwzyO32oMyIJUXzEmU4FEEZc1kDRaikdi_OKbwMCgPAzsc0HjAIxpQPYQBHQJzCMaCwx4dGoEegp2Ozpehd3YEY83iY6qUZA3JNqffOYUYmNkjcIfsyQOPSktl9nO4a2Xif_CEzqCGXyoMMyZlwNsRJ2y92kfHThgIXZOLbHt7015965rd5eO3rr292-66H7vNP9333V23_f5IiuaZXGSEVsCMBGnRg7EBRhbEMOfkOLCjsg5sDweHR2Wjn9Me0fk5kXUfHKf9ZduAVuYJ3XuMU58OzsoocPZT3kecHv-NU0AfUvFqvMjOY952HQvBKR4Ddt0Ek6677tzQs4Gm51YTSgmtTgM-uRhN9CjP52JgDrruIA6xi0b9G7HrrSXF5adljFZGjamW_qtC73fbh6vbm3sgq3MIILRtru63m7uG0BZIsXlzBoRuCF2_ll69XFo15_i_F-8TvdAHDbhovipHaDYRae4tLMTJqGWqtYSFg8UDvEI6uVlY2Hl18rX8p4zjARbhjfGPJDOHfymt4fzIX1WT_fHB6buduvB400y9yKoPTh9f57PYPwal8Tdd9TOoAFLJidrvyJjIupBVUbEE63yVZwXN87xMhpozvpRlQUW1LtYXBZ-ERdIl8rzgrFpViappRsusyIusyvNlmWZCZozLlWQ9z7goSZnhyJROJ-lKrdsnM9Hr9TIr14lmHLWfdZFSrSUpNrvrdiLrsklcPcsdj3tPykwrH_xrlKCCxnr6A-Zr0MiO6F8mjoMy8r1unbTno-4k0en6r3V3huAJbWcU_wcAAP__iUDJ1A">