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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] ld.lld RISC-V TLSDESC->IE explicit addend drop
        </td>
    </tr>

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

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

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

<pre>
    I ran into this while reducing a RISC-V linker testcase. The reproducer is small, and I have been seeing the same result consistently across three reruns.

### Summary
ld.lld links successfully, relaxes `%tlsdesc_hi(ext_tls+4)` to IE form, and emits `R_RISCV_TLS_TPREL32/64 ext_tls + 0` instead of preserving `+ 4`. The equivalent offset-symbol control `%tlsdesc_hi(ext_tls_off)`, where `ext_tls_off` is actually placed at TLS offset 4 in the provider DSO, is correct and emits `R_RISCV_TLS_TPREL32/64 ext_tls_off + 0`. The plain `%tlsdesc_hi(ext_tls)` control is also correct, isolating the failure to explicit addend handling during TLSDESC->IE relaxation rather than generic IE relocation emission.

### Expected behavior
Link valid RISC-V executables against a shared object where the input TLSDESC sequence uses `R_RISCV_TLSDESC_HI20` with an explicit addend, for example `%tlsdesc_hi(ext_tls+4)`. If ld.lld relaxes the sequence to IE form, the resulting dynamic `R_RISCV_TLS_TPREL32/64` relocation must still preserve the requested TLS offset addend.

### Environment
- linker route: GNU as 2.45 input, current ld.lld 22.1.0 executable links against a DSO built by either ld.lld or GNU ld.bfd, RV32/RV64 addend variant plus offset-symbol and plain controls; GNU ld.bfd 2.45 provides a duplicate contrast by keeping TLSDESC and reproducing the existing addend-drop root instead of the new IE-relaxation root.
- march: rv32imac and rv64imac
- mabi: ilp32 and lp64
- first failing stage: link
- local stability check: True

### Reduced testcase
These are the reduced input files from the minimized reproducer I used locally:

#### `main_addend_rv64.s`
```asm
.text
.globl _start
_start:
.Ltls:
  auipc a0, %tlsdesc_hi(ext_tls+4)
  ld t0, %tlsdesc_load_lo(.Ltls)(a0)
  addi a0, a0, %tlsdesc_add_lo(.Ltls)
 jalr t0, t0, %tlsdesc_call(.Ltls)
  add a0, a0, tp
 ret
```

#### `main_offset_symbol_rv64.s`
```asm
.text
.globl _start
_start:
.Ltls:
  auipc a0, %tlsdesc_hi(ext_tls_off)
  ld t0, %tlsdesc_load_lo(.Ltls)(a0)
  addi a0, a0, %tlsdesc_add_lo(.Ltls)
 jalr t0, t0, %tlsdesc_call(.Ltls)
  add a0, a0, tp
 ret
```

#### `main_plain_rv64.s`
```asm
.text
.globl _start
_start:
.Ltls:
  auipc a0, %tlsdesc_hi(ext_tls)
  ld t0, %tlsdesc_load_lo(.Ltls)(a0)
  addi a0, a0, %tlsdesc_add_lo(.Ltls)
 jalr t0, t0, %tlsdesc_call(.Ltls)
  add a0, a0, tp
 ret
```

#### `provider_rv64.s`
```asm
.section .tbss,"awT",@nobits
.globl ext_tls
ext_tls:
  .zero 4
.globl ext_tls_off
ext_tls_off:
  .zero 4
```

### Reproduction notes
- Reproduction command from the packaged case directory:
 `powershell -ExecutionPolicy Bypass -File ./case/run.ps1`
- The reduced inputs live under `case/` and the stable witness outputs live under `verify/run1..run3/`.

### What I checked
- Reduced inputs are preserved under case/.
- Stable witness outputs are preserved under verify/run1..run3/.
- The strict recheck says stable normalized run signatures across three runs: True.
- Tracker guidance link: https://llvm.org/docs/HowToSubmitABug.html
- evidence summary: 3 clean reproductions under hunt/verify/lld_tlsdesc_hi20_addend_ie_relax_addend_dropped/run1..run3` are stable: each run reports 4 ld.lld rows as `wrong_tls_tprel_addend_dropped`, 4 GNU ld.bfd rows as the already-known `wrong_tlsdesc_addend_dropped`, 8 offset-symbol controls as `ok_offset_symbol_control`, and 8 plain controls as `ok_plain_control`. Representative run1 RV64 `addend.64.lld-provider.lld.elf` has `.rela.dyn` `0x123a8 R_RISCV_TLS_TPREL64 ext_tls + 0` and IE-form disassembly `auipc a0,0x1; ld a0,0xdc(a0); add a0,a0,tp`. The matching RV64 `offset_symbol.64.lld-provider.lld.elf` emits `R_RISCV_TLS_TPREL64 ext_tls_off + 0`, proving the requested offset 4 is representable when carried by the symbol instead of an explicit addend. The provider-linker dimension is clean: building the DSO with ld.lld or GNU ld.bfd does not change ld.lld's output classification. Local records only had the distinct shared-object TLSDESC addend roots and the older `tlsbin` relocation-retention root; this new case still has a dynamic relocation, but with the wrong addend after IE relaxation. Web searches across LLVM GitHub issues/PRs, Sourceware Bugzilla/list archives, and broad web queries for `R_RISCV_TLS_TPREL64`, TLSDESC, IE relaxation, `%tlsdesc_hi`, and explicit addends found TLSDESC implementation patches and source references but no direct current report for this lld failure.

### Notes
upstream/binutils-gdb-full/ld/testsuite/ld-riscv-elf/tls.d` expects `R_RISCV_TLS_TPREL64 +4` and `R_RISCV_TLSDESC +4`, while `tlsbin.d` expects executable `R_RISCV_TLSDESC +4`. Sourceware's TLSDESC relaxation patch explicitly propagates the HI20 addend when converting to IE/LE (`rel->r_addend += hi->hi_addend`): https://sourceware.org/pipermail/binutils/2024-February/132566.html . Current lld source documents that RISC-V supports TLSDESC->IE/LE optimization and then creates TLS GOT dynamic relocations with addend 0 in the GOT-reloc path: https://codebrowser.dev/llvm/lld/ELF/Relocations.cpp.html and https://codebrowser.dev/llvm/lld/ELF/SyntheticSections.cpp.html .

Root key: `lld.riscv.tlsdesc_ie_relax_explicit_addend_dropped`
Case id: `20260605-lld-riscv-tlsdesc-ie-relax-explicit-addend-dropped`

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzkWUtz2zgS_jX0pUssCnrEPvgQx1biKu9kyvJm9qYCgZaIGAQYPGQrv36rAVIPPzKze5nDVKUckwQaja-fX5t7rzYG8bKYXRWz6zMeQ2Pd5X8Ut7Uymy82jtn44qy2cnd5C44bUCZYCI3y8NQojeBQRqHMBjjc3y4_jb6BVuYRHQT0QXCPJTw0tKxzVkaBDpQH33KtC_YJuJFwCw3fItSIB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