<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/202128>202128</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISC-V][lld] lld.riscv.explicit_gp_pcrel_addi_not_relaxed
</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
GNU ld.bfd rewrites the data_a materialization to addi a0,gp,-2044 while keeping the data_b reference non-gp, but ld.lld leaves both address materializations as auipc+addi even though __global_pointer$ is explicitly defined at 0x12800 and data_a is in range at 0x12004.
### Expected behavior
A small executable explicitly defines __global_pointer$ in the linker script, places writable data_a inside gp range and read-only data_b outside that range, and materializes both addresses with auipc+addi. Under --relax --relax-gp only the writable in-range data_a reference should be rewritten to gp-relative form.
### Environment
- linker route: gas+ld.lld with gas+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:
#### `gp.ld`
```ld
SECTIONS {
.text 0x10000 : { *(.text) }
.rodata 0x11000 : { *(.rodata) }
.data 0x12000 : { *(.data) }
__global_pointer$ = ADDR(.data) + 0x800;
}
```
#### `pcgp-relax-02.s`
```asm
.text
.globl _start
_start:
.L1: auipc a0, %pcrel_hi(data_a)
.L2: auipc a1, %pcrel_hi(data_b)
addi a0, a0, %pcrel_lo(.L1)
addi a1, a1, %pcrel_lo(.L2)
.data
.word 0x0
.globl data_a
data_a:
.word 0x1
.section .rodata
.globl data_b
data_b:
.word 0x2
```
### 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_explicit_gp_pcrel_relax/run1..run3 show stable GNU ld.bfd output with addi a0,gp,-2044 # 12004 ///<data_a///> and stable ld.lld output that keeps auipc a0,0x2; addi a0,a0,0x4 from the same source and gp.ld script.
### Notes
upstream///binutils-gdb/ld/testsuite/ld-riscv-elf/pcgp-relax-02.s with explicit-gp linker-script reduction
Root key: `lld.riscv.explicit_gp_pcrel_addi_not_relaxed`
Case id: `20260526-lld-explicit-gp-pcrel-relax`
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUV8lu67wVfhpmcyCBpmzHXnjhTP0D_L0Fkty2O4MSjyU2FClwcJL79AVJyc51nA6AkUjkGb8zijsnW424IYsbsri74sF3xm7-KbmppW7_MGHGZuur2oiPzSNYrkFqb8B30sFbJxWCRREaqVvg8PT4fFv8HZTUr2jBo_MNd1jCSxfJBmtEaNCCdOB6rhRht8C1gEfo-AGhRtTgEKMs3yE43kc2F5SHxmgnnUft1QfwxhrnwHcWI4EN2pWE3hG6Hf-yKv_gOfQ9tx_5-C8_foISZb0XYPHNSo8u6RHc8x2Hnnu0kiv5i3tpNHgDXAgJnBJ22w6E3RaMzuej16-Iw2RoElCDxT1a1A2CNrpIHFAHH3UqJUAhP6CD2vguCrbo3LlOB9wBD3JoCLtJyvGAGnxnQtvBbtcqU3O1G4zUHi1h8wglvg9KNjICI3AvNQrgHuj7jK0oTfiODkoHUscQtjhRUDr_Brn79wEbjwJq7PhBGpsJtjlygO_YBM9rhV_1u8uW6oTVmBuusXLwEaFB8QYdxHgkcZOx2kmB0A6TwTpGjYvC6KgoI26CT1S-4z7TTSl1AvYM8qhKxtdPKJfwUwu0UBQWFX-f_hftAElbtPton9RFtmg09BR115mgImBjenlMSdQOSZyXB4S9sf13gOuDtEb3qH2-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