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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] ld.lld PCREL lo addend overflow wrong success (before LLVM 22)
        </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 18.1.8 and 21.1.3 link the legal-dangerous `pcrel-lo-addend-2a/2b` inputs successfully after warning that the non-zero `%pcrel_lo` addend is ignored, and both final loads collapse to the same `lw a0,0x7fe(a5)` instruction. ld.lld 22.1.0 rejects the same objects, matching the GNU tests' required link-time failure.

### Expected behavior
Link `%pcrel_hi/%pcrel_lo` pairs whose non-zero `%pcrel_lo` addends can change the required high-part value; the linker must reject these dangerous addend-overflow cases instead of silently discarding the addend.

### Environment
- linker route: GNU as plus ld.lld version replay with official GNU ld legality controls
- march: rv32ic
- mabi: ilp32
- first failing stage: link
- local stability check: True

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

#### `pcrel-lo-addend-2a.d`
```text
#name: %pcrel_lo overflow with an addend (2a)
#source: pcrel-lo-addend-2a.s
#as: -march=rv32ic
#ld: -m[riscv_choose_ilp32_emul] --no-relax
#error: .*dangerous relocation: %pcrel_lo overflow with an addend, the value of %pcrel_hi is 0x1000 without any addend, but may be 0x2000 after adding the %pcrel_lo addend
```

#### `pcrel-lo-addend-2a.s`
```asm
        .text
        .globl _start
        .align 3
_start:
        nop
        .LA0: auipc     a5,%pcrel_hi(ll)
        lw      a0,%pcrel_lo(.LA0)(a5)
        lw      a0,%pcrel_lo(.LA0+0x4)(a5)
        ret
        .globl ll
        .data
        .align 3
        .zero 2024
ll:
        .word 0
        .word 0
```

#### `pcrel-lo-addend-2b.d`
```text
#name: %pcrel_lo overflow with an addend (2b)
#source: pcrel-lo-addend-2b.s
#as: -march=rv32ic
#ld: -m[riscv_choose_ilp32_emul] --no-relax
#error: .*dangerous relocation: %pcrel_lo overflow with an addend, the value of %pcrel_hi is 0x1000 without any addend, but may be 0x2000 after adding the %pcrel_lo addend
```

#### `pcrel-lo-addend-2b.s`
```asm
        .text
        .globl _start
        .align 3
_start:
        nop
        .LA0: auipc     a5,%pcrel_hi(ll)
        lw      a0,%pcrel_lo(.LA0)(a5)
        lw      a0,%pcrel_lo(.LA0+0x1000)(a5)
        ret
        .globl ll
        .data
        .align 3
        .zero 2024
ll:
        .word 0
        .word 0
```

### 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/lld_pcrel_lo_addend_overflow_wrong_success_before_22/run1..run3 show stable GNU as assembly success for both `2a` and `2b`, stable ld.lld 21.1.3 warnings that the non-zero `%pcrel_lo` addend is ignored followed by wrong-success outputs where the two loads are identical, and stable ld.lld 22.1.0 rejections whose stderr ends with `without an associated R_RISCV_PCREL_HI20 relocation`. The official GNU tests `pcrel-lo-addend-2a.d` and `2b.d` require a fatal dangerous-relocation diagnostic.

### Notes
upstream////binutils-gdb//ld//testsuite//ld-riscv-elf//pcrel-lo-addend-2a.s; D:/search_ascii/upstream////binutils-gdb//ld//testsuite//ld-riscv-elf//pcrel-lo-addend-2b.s

Root key: `lld.riscv.pcrel_lo_addend_overflow_wrong_success_before_22`
Case id: `20260527-lld-pcrel-lo-addend-overflow-wrong-success`

</pre>
<img width="1" height="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