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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] RISC-V shared preemptible ADD/SUB wrong success
        </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 22.1.0 links the RV32 and RV64 shared objects successfully from both GNU-as and LLVM-MC objects, exports default-visible `w1` and `w2` in `.dynsym`, emits no dynamic relocations, and writes the fixed local value `4` into `.rodata` slots of widths 64/32/16/8. The hidden-control objects also link successfully; the defect is that the default-visible case is treated like the non-preemptible case.

### Expected behavior
Link a RISC-V shared object containing naturally generated fixed-width `R_RISCV_ADD64/SUB64`, `R_RISCV_ADD32/SUB32`, `R_RISCV_ADD16/SUB16`, and `R_RISCV_ADD8/SUB8` relocation pairs for `w2 - w1` in SHF_ALLOC `.rodata`, where `w1` and `w2` are default-visible DSO function definitions. Because those definitions may be preempted independently, the linker must not statically materialize the local function-distance value into read-only data without preserving interposition semantics.

### Environment
- linker route: GNU as and llvm-mc 22 plus current ld.lld 22.1.0; GNU ld.bfd 2.45 observed to have the same wrong-output behavior
- march: rv32 and rv64 without compressed instructions for deterministic function size
- mabi: ilp32 and lp64 for GNU as; ELF32/ELF64 RISC-V default object ABI for llvm-mc
- first failing stage: link
- local stability check: True

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

#### `default.s`
```asm
.section .text.w1,"ax",@progbits
.globl w1
.type w1,@function
w1:
  ret

.section .text.w2,"ax",@progbits
.globl w2
.type w2,@function
w2:
 ret

.section .rodata,"a",@progbits
.globl qword_slot, word_slot, half_slot, byte_slot
qword_slot:
  .dword w2 - w1
word_slot:
 .word w2 - w1
half_slot:
  .half w2 - w1
byte_slot:
  .byte w2 - w1
```

#### `hidden-control.s`
```asm
.section .text.w1,"ax",@progbits
.globl w1
.hidden w1
.type w1,@function
w1:
  ret

.section .text.w2,"ax",@progbits
.globl w2
.hidden w2
.type w2,@function
w2:
  ret

.section .rodata,"a",@progbits
.globl qword_slot, word_slot, half_slot, byte_slot
qword_slot:
  .dword w2 - w1
word_slot:
  .word w2 - w1
half_slot:
  .half w2 - w1
byte_slot:
  .byte w2 - w1
```

### Reproduction notes
- This packaged root does not have a single canonical `run.ps1` wrapper.
- Use the reduced inputs under `case/` and follow the commands documented in `case/README.md`.
- Stable witness outputs, when present, are preserved 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/riscv_shared_preemptible_add_sub_wrong_success/run1..run3 show stable GNU-as and llvm-mc assembly success, stable object relocations against `w2`/`w1` for all ADD/SUB widths, stable ld.lld wrong-success shared links for RV32/RV64, and stable hidden-control success. Across all runs, `default.gas.64.lld.so` hash is `e1f6977716b22419b4902305e98fa5c41114fbe38ee68d61d3a005039261e777`, `default.gas.32.lld.so` hash is `d1e998a826391cd33498ed44090b96b4ba4ad26b80b713f292c688bf7d097ac6`, `default.llvm.64.lld.so` hash is `d007e292f7d3231b376d964141dc20daad2d48c24426867465e95377d2393c82`, and `default.llvm.32.lld.so` hash is `54ef65fdd8a07108a7289118419af079aacf20d2106dd0c26938e1127b3b76d2`. Each default lld `readelf` output says `There are no relocations in this file.` while listing global default `w1` and `w2`, and each default objdump shows `.rodata` bytes `04000000 00000000 04000000 040004`. Local records had no lld shared/preemptible ADD/SUB root. Web searches covering GitHub LLVM issues, Sourceware Bugzilla, binutils lists, `R_RISCV_ADD64 R_RISCV_SUB64 shared`, and `R_RISCV_ADD* link-time constants` found historical ADD/SUB support and PR28509 discussion but no direct current LLVM issue for this wrong-success/no-relocation output.

### Notes
upstream/llvm-project/lld/test/ELF/riscv-reloc-add.s, whose test comment states that `R_RISCV_ADD*`/`R_RISCV_SUB*` are link-time constants and otherwise not allowed in PIE/shared links; this case changes the local `.L0/.L1` endpoints into default-visible DSO functions.

Root key: `lld.riscv.shared_preemptible_add_sub_wrong_success`
Case id: `20260604-lld-riscv-shared-preemptible-add-sub-wrong-success`

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUWEtv47iT_zTKhbAgUbIsHXLIy9MBMg8k3T17CyiyZHGaIr0kZbf70y-KlPzoJLOzh8Xg3wgalsR6F6t-Vcw5udEA18nyNlneX7HR98Ze_5dkppV688mMOc2bq9aIw_UjsUwTqb0hvpeO7HupgFgQI5d6Qxh5fny5W3wlSupvYIkH5zlzkJLPPR7bWiNGDpZIR9zAlEroHWFakEfSsx2QFkATB4C8fA_EsQHJ3Kg84UY76Txorw6EcWucI763gAfsqF2aZPdJdjP9T4v4R17GYWD2EF8rkSolCKVpnmZBSRfkPH8taNDj-WtVEtczC4KY9i_g3hE3cg7OdaNSB9JZM5DW-J788tuXBXOB6unp66-LX-9mCjQKvm-N9Y4I6Nio_GInnWwVkKTK9nlSZYEOHyg-SI2_U3HQ7jAkVRY4DNI7og0RB80GyYkFZTjz0mg3u21vpYdoQie_gyB4QpEdU2MQVUbm3gT21gjmGb5yynhHTEf2UvjekapM6LqgCV3nVULXdYxXL4UAveBGe2vU0R9MORN8d-GZpLgNegjogHsMsO-Zn19d-AAzIhywwDwqLb9BOKiNXmwtwLD1x4MfhPXh-xY4ErfQs500Nh54QrWOWXgRSEwgz6TG3NLMj5ZhPDegwQYtggMXwSHoredXZPL19eb-Pnjn5cttVU6hufwc_Pby5bag734OHn35cptX0-cp8mdH6niixtCcoky2TFpHOmNjopAFiakjNXn5tH69eXr6_e4ysMh-34P9KM-YfRuN-5ffSTd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