| Issue |
239161
|
| Summary |
[LLD][RISC-V] Partial TLS-LE relaxation removes address producers while leaving an unrelaxed LO12 load
|
| Labels |
lld
|
| Assignees |
|
| Reporter |
basavesh
|
## Summary
LLD performs RISC-V TLS local-exec relaxation independently for each relocation carrying an `R_RISCV_RELAX` marker.
If the `R_RISCV_TPREL_HI20` and `R_RISCV_TPREL_ADD` relocations are marked relaxable but the dependent `R_RISCV_TPREL_LO12_I` relocation is not, LLD removes the `lui` and `add` instructions while leaving the load based on the original destination register.
The resulting code silently loads through the stale incoming value of `a0` instead of using `tp`.
This selectively marked relocation group is produced directly by GNU `as` when `.option norelax` is placed before the final load.
## Versions tested
Reproduced with:
- Ubuntu LLD 21.1.8
- A source build from LLVM main at commit `b1a3b4cd3c9c3502e91679a98f27fed98bb6ae8e`
- GNU assembler 2.46
- QEMU RISC-V 10.2.1
I also reviewed LLVM main at commit `d5d76941eac790e196a64f3319d1cc89ec530d4d`. The relevant `lld/ELF/Arch/RISCV.cpp` source is unchanged from the tested source build, so the issue appears to remain present in current main.
## Reduced reproducer
Create `selective.s`:
```asm
.section .text
.globl ct_load
.type ct_load,@function
.option push
.option relax
ct_load:
lui a0, %tprel_hi(tvar)
add a0, a0, tp, %tprel_add(tvar)
.option norelax
ld a0, %tprel_lo(tvar)(a0)
.option pop
ret
.size ct_load, .-ct_load
.section .tdata,"awT",@progbits
.balign 8
.globl tvar
.type tvar,@tls_object
tvar:
.dword 7
.size tvar, .-tvar
```
Create `driver.s`:
```asm
.section .rodata
.balign 8
probe:
.dword 13
.section .data
.balign 8
tls_block:
.dword 7
.section .text
.option norelax
.globl runtime_entry
.type runtime_entry,@function
runtime_entry:
lla a0, probe
lla tp, tls_block
call ct_load
# Exit with loaded_value XOR 7.
# Correct behavior therefore exits with status 0.
li t0, 7
xor a0, a0, t0
li a7, 93
ecall
.size runtime_entry, .-runtime_entry
```
Assemble and inspect the relocations:
```console
$ riscv64-linux-gnu-as -march=rv64gc -o selective.o selective.s
$ riscv64-linux-gnu-as -march=rv64gc -o driver.o driver.s
$ riscv64-linux-gnu-readelf -rW selective.o
Relocation section '.rela.text' contains 5 entries:
0000000000000000 R_RISCV_TPREL_HI20 tvar + 0
0000000000000000 R_RISCV_RELAX
0000000000000004 R_RISCV_TPREL_ADD tvar + 0
0000000000000004 R_RISCV_RELAX
0000000000000008 R_RISCV_TPREL_LO12_I tvar + 0
```
Link with relaxation enabled:
```console
$ ld.lld -e runtime_entry -o affected.elf driver.o selective.o
$ riscv64-linux-gnu-objdump -d affected.elf | sed -n '/<ct_load>:/,+4p'
0000000000011266 <ct_load>:
11266: 00053503 ld a0,0(a0)
1126a: 8082 ret
$ qemu-riscv64 ./affected.elf
$ echo $?
10
```
The load reads the value 13 through the stale pointer initially placed in `a0`, instead of reading 7 relative to `tp`.
## Controls
Disabling relaxation preserves the complete address calculation and returns the correct value:
```console
$ ld.lld --no-relax -e runtime_entry -o no-relax.elf driver.o selective.o
$ qemu-riscv64 ./no-relax.elf
$ echo $?
0
```
Its `ct_load` body is:
```asm
lui a0,0x0
add a0,a0,tp
ld a0,0(a0)
ret
```
A fully marked canonical group also behaves correctly. Move `.option norelax` in `selective.s` from before the `ld` to after it, reassemble, and link normally. LLD then produces:
```asm
ld a0,0(tp)
ret
```
and the executable exits with status 0.
## Expected behavior
LLD should not remove the address-producing `lui` and `add` while leaving their dependent LO12 load unrelaxed.
For a partially marked TLS-LE group, it should either:
- leave the dependent sequence intact; or
- transform the complete dependency consistently.
It should not silently emit a load based on an unrelated incoming register value.
## Source analysis
In `lld/ELF/Arch/RISCV.cpp`, `relaxable` determines whether an individual relocation is relaxable by checking whether it is followed by `R_RISCV_RELAX`.
The main relaxation loop then calls `relaxTlsLe` independently for each marked `R_RISCV_TPREL_*` relocation. `relaxTlsLe` removes marked HI20 and ADD instructions, but there does not appear to be a group-level check establishing that the dependent LO12 relocation can also be transformed.
Consequently, the two address producers can be removed while the unmarked consumer remains.
## Scope
The test input is generated by GNU `as` from explicit `.option relax` and `.option norelax` directives. In the normal GCC and Clang output I checked, the complete TLS-LE group was consistently either marked or unmarked.
I am reporting this as a linker correctness issue. I am not making a security-impact claim.
## Related reports and duplicate review
A similar behavior in GNU BFD is tracked separately as [Sourceware bug 34957](https://sourceware.org/bugzilla/show_bug.cgi?id=34957).
The following LLVM issues appear related to RISC-V relaxation but distinct from this TLS-LE partial-group case:
- #72405 concerns GP relaxation with multiple consumers.
- #149583 concerns symbol offsets after call relaxation.
- #162610 concerns stale relocations emitted with `--emit-relocs`.
## AI assistance disclosure
LLM was used to assist with reproducer validation, duplicate checking, and drafting this issue. I have reviewed the final technical claims and reproduction evidence before submission.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs