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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] ld.lld undefined weak PCREL HI20 false overflow rv64 high text
        </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
All locally tested ld.lld versions (`14.0.6`, `18.1.8`, `20.1.8`, `21.1.3`, `22.1.0`) reject the legal input at link time with `R_RISCV_PCREL_HI20 out of range`, while GNU ld.bfd 2.45 links the same object and folds the weak references to zero-valued `li a5,0` instructions.

### Expected behavior
Link a normal RV64 executable at a high text address while materializing the address of an undefined weak function `f`, which should resolve to zero instead of overflowing a PC-relative relocation.

### Environment
- linker route: GNU as plus ld.lld with GNU ld.bfd control
- march: rv64i
- mabi: lp64
- first failing stage: link
- local stability check: True

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

#### `weakref64.d`
```text

.*:     file format elf64-(little|big)riscv


Disassembly of section \.text:

0000000090000000 <_start>:
    90000000:   00000793        li      a5,0
    90000004:   02078663                beqz    a5,90000030 <_start\+0x30>
    90000008:   00000793                li      a5,0
 9000000c:      02078263                beqz    a5,90000030 <_start\+0x30>
 90000010:      ff010113                addi    sp,sp,-16
    90000014:   00113423        sd      ra,8\(sp\)
    90000018:   00000097        auipc   ra,0x0
    9000001c:   000000e7                jalr    zero # 0 <_start\-0x90000000>
    90000020:   00813083        ld      ra,8\(sp\)
    90000024:   01010113                addi    sp,sp,16
 90000028:      00000317                auipc   t1,0x0
    9000002c:   00000067        jr      zero # 0 <_start\-0x90000000>
    90000030:   00008067        ret
```

#### `weakref64.s`
```asm
        .option nopic
        .text
        .align  1
        .globl  _start
        .type   _start, @function
_start:
        lui     a5,%hi(f)
        addi    a5,a5,%lo(f)
        beq     a5,zero,.L1
        lla     a5,f
        beqz    a5,.L1
        addi    sp,sp,-16
        sd      ra,8(sp)
        call    f
        ld      ra,8(sp)
        addi    sp,sp,16
        tail    f
.L1:
        ret
        .size   _start, .-_start
        .weak   f
```

### 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_weakref64_undefined_weak_pcrel_false_overflow/run1..run3 show stable GNU as assembly success for `weakref64.s`, stable GNU ld.bfd control outputs whose first two materializations are `li a5,0`, and stable ld.lld 18.1.8/21.1.3/22.1.0 link-time rejections on `R_RISCV_PCREL_HI20` against weak `f` at `-Ttext 0x90000000`.

### Notes
upstream////binutils-gdb//ld//testsuite//ld-riscv-elf//weakref64.s; D:/search_ascii/upstream////binutils-gdb//ld//testsuite//ld-riscv-elf//weakref64.d

Root key: `lld.riscv.undefined_weak_pcrel_hi20_false_overflow_rv64_high_text`
Case id: `20260527-lld-weakref64-false-overflow`

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