Issue 240311
Summary [LLD][ELF][PPC64] Small-model TLS LD-to-LE relaxation leaves the incoming value in r3
Labels lld
Assignees
Reporter basavesh
    ## Summary

LLD mishandles `R_PPC64_GOT_TLSLD16` when relaxing a small-model PPC64
local-dynamic TLS sequence in an executable.

Assertion builds reach `llvm_unreachable`. Release builds can instead replace
the sequence with:

```text
nop
nop
addi 3,3,4096
addi 3,3,-32768
```

This sequence never reads the thread pointer in `r13`. The result is calculated
from whatever value was already in `r3`.

The reproducer below puts an input bit in `r3` before the TLS sequence. Both
inputs exit 0, but the branch after the relaxed sequence follows a different
path. GNU ld 2.46 links the identical object using `r13`, and its two paths are
the same.

The wrong sequence is present in LLVM 23.1.3. I reproduced the runtime result
with the apt.llvm.org LLVM 23.1.6 build. LLVM main
`3fc59d072a82694a77fe1d700ccf9bd71464181f` still lacks the required cases on
2026-11-09.

## Reproducer

The commands below require:

- `llvm-mc`, `llvm-objdump`, and a release build of LLD 23;
- `qemu-ppc64le`;
- a PPC64LE sysroot containing `lib64/ld64.so.2`;
- `powerpc64le-linux-gnu-ld.bfd` for the control.

Save this as `repro.s`:

```asm
        .abiversion 2
        .text
        .globl _start
        .type _start,@function
_start:
.Lstart_gep:
        addis 2,12,.TOC.-.Lstart_gep@ha
        addi 2,2,.TOC.-.Lstart_gep@l
.Lstart_lep:
        .localentry _start,.Lstart_lep-.Lstart_gep

        # Make the executable use the dynamic loader.
        bl dummy
        nop

        # Parse argv[1] as 0 or 1 without branching.
        ld 4,8(4)
        lbz 3,0(4)
 addi 3,3,-48
        sldi 3,3,32

        # Correct LD-to-LE relaxation must replace r3 with an address
        # calculated from the thread pointer in r13.
        addi 3,2,x@got@tlsld
        bl __tls_get_addr(x@tlsld)
 nop
        addi 3,3,x@dtprel
        b ct_window_start

 .globl ct_window_start
ct_window_start:
        srdi 5,3,32
        cmpdi 5,0
        beq .Llow
        nop
        b .Ljoin
.Llow:
 nop
.Ljoin:
        .globl ct_window_end
ct_window_end:
        li 3,0
 li 0,1
        sc
        b .
        .size _start,.-_start

 .section .tbss,"awT",@nobits
        .align 2
x:
        .long 0
```

Save this as `dummy.s`:

```asm
        .abiversion 2
 .text
        .globl dummy
        .type dummy,@function
dummy:
 blr
        .size dummy,.-dummy
```

Build the objects and the small shared object:

```sh
llvm-mc -triple=powerpc64le-linux-gnu -filetype=obj repro.s -o repro.o
llvm-mc -triple=powerpc64le-linux-gnu -filetype=obj dummy.s -o dummy.o

ld.lld -shared dummy.o -soname libdummy.so -o libdummy.so
```

Link one executable with LLD and one with GNU ld:

```sh
ld.lld repro.o ./libdummy.so -o lld-output -e _start \
 --dynamic-linker /lib64/ld64.so.2 -rpath '$ORIGIN'

powerpc64le-linux-gnu-ld.bfd repro.o ./libdummy.so \
  -o bfd-output -e _start \
  --dynamic-linker /lib64/ld64.so.2 -rpath '$ORIGIN'
```

An assertion-enabled LLD build may stop at:

```text
unsupported relocation for TLS LD to LE relaxation
```

A release build completes the link and emits wrong code.

## Disassembly

Run:

```sh
llvm-objdump -d --no-show-raw-insn lld-output | \
  sed -n '/<_start>:/,/<ct_window_end>:/p'

llvm-objdump -d --no-show-raw-insn bfd-output | \
  sed -n '/<_start>:/,/<ct_window_end>:/p'
```

LLD output around the TLS sequence:

```text
nop
nop
addi 3,3,4096
addi 3,3,-32768
```

GNU ld output for the same `repro.o`:

```text
nop
addi 3,13,4096
nop
addi 3,3,-32768
```

The GNU ld output overwrites the old value in `r3` using the thread pointer.
The LLD output does not.

## Runtime check

Save this as `normalize.py`:

```python
#!/usr/bin/env python3
import re
import subprocess
import sys

image, log = sys.argv[1:]
symbols = subprocess.check_output(
    ["readelf", "-sW", image], text=True
).splitlines()
values = {}

for line in symbols:
    fields = line.split()
    if fields and fields[-1] in {"ct_window_start", "ct_window_end"}:
        values[fields[-1]] = int(fields[1], 16)

start = values["ct_window_start"]
end = values["ct_window_end"]
text = open(log, encoding="utf-8").read()
pcs = [
    int(value, 16)
    for value in re.findall(r"Trace .*?\[[0-9a-fA-F]+/([0-9a-fA-F]+)/", text)
]
inside = [pc for pc in pcs if start <= pc < end]
if not inside or inside[0] != start:
    raise SystemExit("test window was not found in the trace")

print(",".join(f"+0x{pc - start:x}" for pc in inside))
```

Set `SYSROOT` and run both inputs with both executables:

```sh
SYSROOT=/path/to/ppc64le/sysroot

for image in lld-output bfd-output; do
  for input in 0 1; do
 log="$image-$input.log"
    qemu-ppc64le -d exec,nochain -D "$log" \
 -L "$SYSROOT" "./$image" "$input"
    rc=$?
    trace=$(python3 normalize.py "$image" "$log")
    printf '%s input=%s exit=%s trace=%s\n' \
      "$image" "$input" "$rc" "$trace"
 done
done
```

Observed output:

```text
lld-output input=0 exit=0 trace=+0x0,+0xc
lld-output input=1 exit=0 trace=+0x0,+0x14
bfd-output input=0 exit=0 trace=+0x0,+0xc
bfd-output input=1 exit=0 trace=+0x0,+0xc
```

I repeated each row three times and obtained the same result.

## Cause

Current LLD dispatches all of these relocations to `relaxTlsLdToLe`:

```cpp
case R_PPC64_GOT_TLSLD16:
case R_PPC64_GOT_TLSLD16_HA:
case R_PPC64_GOT_TLSLD16_HI:
case R_PPC64_GOT_TLSLD16_LO:
case R_PPC64_GOT_TLSLD_PCREL34:
case R_PPC64_TLSLD:
  if (rel.expr == R_TPREL) {
    relaxTlsLdToLe(loc, rel, val);
    return;
  }
```

However, `relaxTlsLdToLe` has no case for `R_PPC64_GOT_TLSLD16` or
`R_PPC64_GOT_TLSLD16_HI`.

The small-model relocation should be handled like the `_LO` form so that it
emits the `r13`-based instruction. The `_HI` form should be handled with the
corresponding high-half case.

The existing PPC64 local-dynamic test contains these relocation types, but it
links that input only as a shared object. LD-to-LE relaxation is performed for
the executable link, which is where this fails.

## Related issue

#64424 concerns a different initial-exec-to-local-exec relaxation failure for
X-form load/store instructions. It does not cover small-model local-dynamic
`R_PPC64_GOT_TLSLD16` relaxation or the generated wrong code above.

I am filing this publicly as a linker wrong-code bug. The constant-time example
shows a possible security consequence, but I have not identified affected
production software or a concrete exploit.

AI disclosure: I have used AI/LLM to format this report. 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to