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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V][lld] ld.lld undefined weak zero misrelaxed to GP relative in medlow lowdata layout
        </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 20.1.8 link successfully but mis-relax the undefined weak zero address to addi t0,gp,-16 under the high-text/low-data medlow layout, while ld.lld 21.1.3 and GNU ld 2.45 produce addi t0,zero,0 for the same object and linker script.

### Expected behavior
Startup code materializes the address of an undefined weak data symbol in medlow code using the normal lui/addi absolute sequence. Because the weak symbol is unresolved, a correct linker should resolve the address to the link-time constant 0 without introducing a dependency on the runtime gp register.

### Environment
- linker route: gas + ld.lld version replay with 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:

#### `case.s`
```asm
        .text
        .globl _start
_start:
        lui t0, %hi(symbolW)
        addi t0, t0, %lo(symbolW)

        .data
        .weak symbolW
```

#### `layout.ld`
```ld
ENTRY(_start)
MEMORY
{
        rom (rx)  : ORIGIN = 0x800000000, LENGTH = 0x1000
        ram (!rx) : ORIGIN = 0x10, LENGTH = 0x1000
}
SECTIONS {
        .text : {
                *(.text*)
        } >rom

        .data : {
                *(.data*)
        } >ram

        __global_pointer$ = .;

        .got : {
                *(.got*)
        } > ram
}
```

### 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_undefweak_zero_gp_relax_medlow_lowdata/run1..run3 show stable gas assembly success, stable old-ld.lld outputs beginning with addi t0,gp,-16 # 0 /<__global_pointer$-0x10/>, and stable GNU ld plus ld.lld 21.1.3 control outputs beginning with addi t0,zero,0. The old outputs make the resolved weak-zero address depend on gp, which is not part of the source semantics.

### Notes
upstream///binutils-gdb/ld/testsuite/ld-riscv-elf/code-model.s plus code-model-02.ld

Root key: `lld.riscv.undefined_weak_zero_misrelaxed_to_gp_relative_in_medlow_lowdata_layout`
Case id: `20260526-lld-undefweak-zero-gp-misrelax-medlow-lowdata`

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEV1tv2zgW_jXMy4EEmbId58EPubkNsE0XSWa782RQ4rHEDUVqeXHq_vrBoSQ7zbidwkht8tyvH4X3qjGIa7a4YYu7CxFDa936v0rYSpnms40zPru6qKw8rB_ACQPKBAuhVR7eWqURHMpYK9OAgKeH59vsP6CVeUUHAX2ohcccXloi652VsUYHyoPvhNaM34IwEh6gFXuECtGARyRZoUXwoiM2H3WA2hqvfEAT9AFE7az3EFqHROCi8Tkr7lhxPf7l5fCB59h1wh2GYy1zrSXMVvksXyXFvEhfyV7wsa7R-13U-gBVDNApnznU4nsyJhqJO2VQwhuKV_iBzoKQ0iEZkr4qCAXjt03P-G02WyYOl3hb1bRZwO-B8Y22b5kUQUCHUts30OJgY6BIDNEcjeSzfJaXychPj38AneTzBYwhfKeODGH8toCddaeo2ep_WIfEPibD10714Rdhuv_eYx1QQoWt2CvrBoLnIFyIPdRWInQioFNCqx_ok6LJe7sDYT7GJ7noD11lNSgzOZsERT8l2FjXCQ06KsY3ySVReatjQPD4_4imxhxusBbRY2JIoiepHqJx6K3eo0yVBLV1jtyeXG5t1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