<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/72405>72405</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[RISCV] GP relaxation broken if accesses straddle the boundary
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:RISC-V,
lld:ELF
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nemanjai
</td>
</tr>
</table>
<pre>
If we have more than one memory access (with a `R_RISCV_LO12_I` relocation on it) that depends on the same `LUI` with a `R_RISCV_HI20` relocation and one of them can be reached from the GP while the other(s) can't, the linker will relax and get rid of the `LUI`. The memory access that is reachable will be fine, but the one(s) that aren't will be relocated off of an undefined register.
To illustrate the issue:
```
$ cat a.s
.global _start
_start:
lui a1, %hi(Var0)
lw a0, %lo(Var0)(a1)
lw a1, %lo(Var0+4)(a1)
.section .sdata,"aw"
foo:
.word 0
.space 4091
Var0:
.quad 0
.size Var0, 8
```
Produce object file:
`llvm-mc -filetype=obj -triple=riscv32-unknown-elf -mattr=+relax a.s -o a.o`
Link it:
`ld.lld --relax-gp --undefined=__global_pointer$ a.o`
Disassemble:
```
llvm-objdump -dr a.out
a.out: file format elf32-littleriscv
Disassembly of section .text:
000110d4 <_start>:
110d4: 03 a5 f1 7f lw a0, 2047(gp)
110d8: 83 a5 f5 0d lw a1, 223(a1)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VUFv4zgP_TXKhbAhy3bqHHJom8n3FSiwg9nZXgPaomO1spSV5Ga6v34h2W2zbScI7Igi3yOlRwa9V0dDtGX1Dat3K5zCYN3W0IjmEdWqtfJle9fDmWDAZ4LROoIwoAFrCEYarXsB7DryHphozioMgMDW_Mfhx92ftw-H-z8Kcbhjaw6OtO0wKBtjQQUmNhEpgKQTGemjNQwEHkeKCPd_pbDPkP-_E_wDIBqZErJ9hBihQwMtgSPsBpLQOzsm7P99h_OgNKWFDQ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