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

    <tr>
        <th>Summary</th>
        <td>
            [LLD][ELF] Incorrect source location in undefined symbol error
        </td>
    </tr>

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

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

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

<pre>
    We've hit a situation where an undefined symbol error in LLD shows an incorrect source location:
```
ld.lld: error: undefined hidden symbol: std::__ktl::__hash_memory(void const*, unsigned long)
>>> referenced by span:561 (../../prebuilt/third_party/clang/linux-x64/bin/../include/c++/v1/span:561)
>>> libboot-shim.devicetree-riscv-cpu-topology-item.cc.o:(boot_shim::RiscvDevicetreeCpuTopologyItemBase::OnDone()) in archive usr/local/google/home/mcgrathr/tq/fuchsia/out/bringup.riscv64-clang/kernel.phys_riscv64/obj/zircon/kernel/phys/lib/boot-shim/libboot-shim.a
>>> referenced by alloc_checker.h:66 (../../zircon/system/ulib/fbl/include/fbl/alloc_checker.h:66)
>>> libboot-shim.devicetree-riscv-cpu-topology-item.cc.o:(boot_shim::RiscvDevicetreeCpuTopologyItemBase::OnDone()) in archive usr/local/google/home/mcgrathr/tq/fuchsia/out/bringup.riscv64-clang/kernel.phys_riscv64/obj/zircon/kernel/phys/lib/boot-shim/libboot-shim.a
```

A disassembly with `llvm-objdump --demangle -dlr` shows the correct location:
```
; ./../../prebuilt/third_party/clang/linux-x64/bin/../include/c++/v1/__string/char_traits.h:537
 390: 8566              mv      a0, s9
     392: 85d2          mv      a1, s4
 394: 00000097          auipc   ra, 0x0
                0000000000000394: R_RISCV_CALL_PLT      std::__ktl::__hash_memory(void const*, unsigned long)
                0000000000000394:  R_RISCV_RELAX        *ABS*
     398: 000080e7      jalr    ra <.Lpcrel_hi2+0x21e>
```

I noticed that the code in LLD wasn't using the usual `getLineInfoForAddress` in `DWARFContext`, but changing it to do so didn't make any difference. It looks like `llvm-objdump` is using the LLVM symbolizer library instead of directly using the DWARF library; maybe there's some smarts there that are making the difference? I haven't yet tried getting LLD to use the symbolizer library.

Originally found in Fuchsia. See here for the LLD reproducer: https://issuetracker.google.com/407569434
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVktv27oS_jX0ZmBBpuSHFl4ocQUY8EUv0qK9O4MixxITivQlKSfqrz8YyU7S11n17I4gxIr4cR7fDD-NCEE3FnHLlndsuZuJPrbOb7shRPRDJ2I7q50atl-R8fUFodURBAQdexG1s_DcokcQFnqr8KQtKghDVzsD6L3zoC0cDjsIrXsOBNNWOu9RRgiu9xLBODlaYlnJ0pKt0uudlkYlxiiWlZMpenhz0mql0F590VKIBGVZeTw-RXN7bEVojx12zg-Mby5OK5DOhsh4yfg99HbMXoFxtmG8oAiyD9MNHk_o0UpUUA8QzoJiXK4WwPgmSRivxj9nj3WvTWS8iq326ngWPg6MV9IIslkZbfuX-csqZ7yqtb3t01aaXiEBGb8b7-qyYLx6c_RjQEbXtXNxHlrdJQovWmL0iHOvg7zM5bmfR3d2xjXDXEfsEikTR0TwDW070raJlwfasHs1cH_uP1837iN2dyLghPtod84i4xuKhBdUTOFlqy8IffCUm5PCMF41zjWGcmldRz-dbLyILUHi_xmvTr1sgxaMV64npmqvbdOfkzHyVT6_cfWE3qJJzu0Qjtc12lM_Ml590146-woi6tshjATXZPLGzPTmjSjxN0UVxjh5lC3KJ_RJy7Jytfq-vK9eA50IMt5P_k61-a6K0_-_MvhvGf9YGd-pA0vLEpQOIgTsajPAs44tsFVqzKWbu_pR9d0Z5nOFnbCNQZgr49kqvUpRbBFuSvR7CWLZHby2wj9z3I_HEIlGWmmFP0YvdAxj6yyzNUtLyIqUBG6zXK3gdrG06C4sLURKMhaowcb3WcEnrOI_YxcjNp9s5oRLx6tY33Ci12fJ0sILgqYvE9EFS4v0_XXd_XB82H-6_3K8Lw-H438P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