| Issue |
239812
|
| Summary |
[LLD][LoongArch] TLS-LE relaxation uses a stale register when R_LARCH_RELAX precedes its paired relocation
|
| Labels |
lld
|
| Assignees |
|
| Reporter |
basavesh
|
## Summary
LLD's LoongArch TLS local-exec relaxation depends on each normal relocation
being immediately followed by its same-offset `R_LARCH_RELAX` marker.
If the marker precedes its paired `R_LARCH_TLS_LE_LO12_R` relocation, LLD
removes the TLS address-producing `lu12i.w` and `add.d` instructions but does
not rewrite the remaining `addi.d` to use `tp`.
The resulting function returns an address based on the stale incoming value of
`a0` instead of `tp`.
The two test objects below contain identical instructions, symbols, relocation
types, offsets, and addends. Their only difference is the order of the two
relocations at offset 8.
## Versions tested
Reproduced with:
- Ubuntu LLD 21.1.8
- GNU assembler, readelf, and objdump 2.46
- QEMU LoongArch 10.2.1
- An LLVM assertions build at commit
`336ad6e9bb9e0d140831fa1e4bc36e03ec88a808`
I also reviewed LLVM main at commit
`4c6bf50c561f3bd8fb8f9107d1bbd0dcdc31400b`. The relevant ordering and
`relaxable()` logic remain present, so the issue appears to remain in current
main.
## Reduced reproducer
Create `tlsle.s`:
```asm
.section .text.tls_addr,"ax",@progbits
.globl tls_addr
.type tls_addr,@function
tls_addr:
lu12i.w $a0, 0
add.d $a0, $a0, $tp
addi.d $a0, $a0, 0
ret
.size tls_addr, .-tls_addr
# Complete TLS-LE relocation set. The first two pairs use the usual
# relocation-then-hint order. HINT_FIRST changes only the final pair.
.reloc tls_addr + 0, R_LARCH_TLS_LE_HI20_R, tvar
.reloc tls_addr + 0, R_LARCH_RELAX, 0
.reloc tls_addr + 4, R_LARCH_TLS_LE_ADD_R, tvar
.reloc tls_addr + 4, R_LARCH_RELAX, 0
.ifdef HINT_FIRST
.reloc tls_addr + 8, R_LARCH_RELAX, 0
.reloc tls_addr + 8, R_LARCH_TLS_LE_LO12_R, tvar
.else
.reloc tls_addr + 8, R_LARCH_TLS_LE_LO12_R, tvar
.reloc tls_addr + 8, R_LARCH_RELAX, 0
.endif
# Freestanding runtime. Set tp to a block containing 7, pass zero in a0,
# call tls_addr(), load through its result, and exit with value XOR 7.
.section .text.runtime,"ax",@progbits
.option push
.option norelax
.globl runtime_entry
.type runtime_entry,@function
runtime_entry:
la.local $tp, tls_block
move $a0, $zero
bl tls_addr
ld.d $a0, $a0, 0
xori $a0, $a0, 7
addi.d $a7, $zero, 93
syscall 0
.size runtime_entry, .-runtime_entry
.option pop
.section .data,"aw",@progbits
.balign 8
tls_block:
.dword 7
.section .tbss,"awT",@nobits
.globl tvar
.type tvar,@tls_object
.size tvar,8
tvar:
.zero 8
```
Assemble the canonical and hint-first variants:
```console
$ loongarch64-linux-gnu-as tlsle.s -o canonical.o
$ loongarch64-linux-gnu-as --defsym HINT_FIRST=1 tlsle.s -o hint-first.o
```
Inspect their relocations:
```console
$ loongarch64-linux-gnu-readelf -rW canonical.o
...
0000000000000000 R_LARCH_TLS_LE_HI20_R tvar + 0
0000000000000000 R_LARCH_RELAX
0000000000000004 R_LARCH_TLS_LE_ADD_R tvar + 0
0000000000000004 R_LARCH_RELAX
0000000000000008 R_LARCH_TLS_LE_LO12_R tvar + 0
0000000000000008 R_LARCH_RELAX
$ loongarch64-linux-gnu-readelf -rW hint-first.o
...
0000000000000000 R_LARCH_TLS_LE_HI20_R tvar + 0
0000000000000000 R_LARCH_RELAX
0000000000000004 R_LARCH_TLS_LE_ADD_R tvar + 0
0000000000000004 R_LARCH_RELAX
0000000000000008 R_LARCH_RELAX
0000000000000008 R_LARCH_TLS_LE_LO12_R tvar + 0
```
Link both objects:
```console
$ ld.lld -m elf64loongarch -static -e runtime_entry \
canonical.o -o canonical.elf
$ ld.lld -m elf64loongarch -static -e runtime_entry \
hint-first.o -o hint-first.elf
```
Disassemble `tls_addr`:
```console
$ loongarch64-linux-gnu-objdump -d canonical.elf | \
sed -n '/<tls_addr>:/,+3p'
0000000000020190 <tls_addr>:
20190: 02c00044 addi.d $a0, $tp, 0
20194: 4c000020 ret
$ loongarch64-linux-gnu-objdump -d hint-first.elf | \
sed -n '/<tls_addr>:/,+3p'
0000000000020190 <tls_addr>:
20190: 02c00084 addi.d $a0, $a0, 0
20194: 4c000020 ret
```
Run the freestanding programs:
```console
$ qemu-loongarch64 ./canonical.elf
$ echo $?
0
$ qemu-loongarch64 ./hint-first.elf
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
$ echo $?
139
```
The canonical output returns `tp + 0`, loads 7, and exits zero. The hint-first
output returns the incoming `a0`, which the runtime set to zero, and
consequently faults while loading through address zero.
## Expected behavior
The order of two same-offset relocation records should not change the generated
program's semantics.
LLD should either:
- associate `R_LARCH_RELAX` with its same-offset relocation regardless of their
relative order;
- preserve the complete TLS-LE sequence when it cannot consistently relax every
dependent member; or
- reject the input with a diagnostic.
It should not silently delete the address producers while leaving the consumer
based on an unrelated incoming register.
## Input validity
The objects contain the complete TLS-LE relocation set and one `R_LARCH_RELAX`
marker at the same address as each normal relocation.
The [LoongArch ELF
ABI](https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc) describes
`R_LARCH_RELAX` as a marker paired with another relocation at the same address.
I could not find a requirement that the marker must appear after the paired
relocation in the relocation table.
GNU `as` emits the canonical order for ordinary TLS syntax. The hint-first case
therefore requires `.reloc`, another object producer, or an object
transformation. This report does not claim that current Clang or GNU `as` emits
the trigger during ordinary compilation.
## Source analysis
At current main, `scanSectionImpl` performs a stable sort using only the
relocation offset:
https://github.com/llvm/llvm-project/blob/4c6bf50c561f3bd8fb8f9107d1bbd0dcdc31400b/lld/ELF/Arch/LoongArch.cpp#L752-L755
Equal-offset relocations therefore retain their input order.
`relaxable()` then considers relocation `i` relaxable only if relocation `i+1`
is `R_LARCH_RELAX`:
https://github.com/llvm/llvm-project/blob/4c6bf50c561f3bd8fb8f9107d1bbd0dcdc31400b/lld/ELF/Arch/LoongArch.cpp#L1105-L1107
For the hint-first LO12 pair, `R_LARCH_TLS_LE_LO12_R` is not followed by a
marker and is not rewritten to use `tp`. The earlier HI20 and ADD relocations
are followed by their markers, so their instructions are removed. This leaves
the stale-register `addi.d $a0,$a0,0`.
## Controls
`--no-relax` preserves the complete TLS-LE address calculation for both
objects.
GNU ld 2.46 and mold 2.40.4 also preserve TP-relative behavior for the
hint-first object.
## Scope
This is being reported as an LLD correctness issue. The reproducer demonstrates
silent wrong code from a complete same-offset relocation set. I am not making a
security-impact or CVE claim.
## Related reports and duplicate review
I did not find an existing LLVM issue for this LoongArch same-offset
relocation-order case.
Related but distinct reports:
- LLVM issue #239161 concerns a RISC-V TLS-LE group with only some members
marked relaxable.
- [Sourceware bug
34961](https://sourceware.org/bugzilla/show_bug.cgi?id=34961) concerns
analogous LoongArch TLS-LE relocation-order behavior in GNU ld/BFD.
## AI assistance disclosure
An LLM was used to assist with reducing and validating the reproducer, checking
current source, duplicate searching, and drafting this issue. I executed the
commands above, verified the resulting relocations, disassembly, and runtime
behavior, and reviewed the technical claims before submission.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs