Issue 243094
Summary [PowerPC] link error at -O3 with -mcpu=power10: a never-executed `plha` of `a@PCREL+8589934590` is out of range for R_PPC64_PCREL34
Labels generated by fuzzer
Assignees
Reporter hazohelet
    Live Reproducer: https://godbolt.org/z/8YejaaYG7

Compiling the following C code for `powerpc64le` with `clang -O3 -mcpu=power10` gives an object file that cannot be linked.

```c
short a[8];
static volatile int X;
int main(void) {
 short v = 0, *p = &v;
  unsigned n, i;
  int r = 0;
  if (X)
    p = &a[0];
  for (int j = 0; j < 1; j++)
    n = (*p < 0 ? *p : 0) & 7;
  for (i = 0; i < n; i++)
    r = *p ? 0 : a[i];
  return r;
}
```

clang command:

```sh
clang --target=powerpc64le-linux-gnu -mcpu=power10 -O3 -fuse-ld=lld repro.c
```

Output:

```text
ld.lld: error: /tmp/repro-65f95d.o:(function main: .text+0x38): relocation R_PPC64_PCREL34 out of range: 8590788082 is not in [-8589934592, 8589934591]; references 'a'
>>> referenced by repro.c
>>> defined in /tmp/repro-65f95d.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

(GNU ld: ``repro.c:(.text+0x38): relocation truncated to fit: R_PPC64_PCREL34 against symbol `a' defined in .bss section in /tmp/repro-84c133.o+1fffffffe``)

`a[0]` is 0, so `*p` is 0 whichever object `p` points to, `n` is 0, and the second loop never runs.  The -O3 code still has a load of `a[0xffffffff]`, in the block for `*p == 0 && n != 0`, and its offset of 8589934590 bytes is put into the PC-relative ref like the following:

```asm
# %bb.2:                                # %for.body7.us.preheader
        plha 3, a@PCREL+8589934590(0), 1
```

x86-64 at -O3 builds the same address with `leaq a(%rip), %rax` and `movabsq $8589934590, %rcx`.

Notes:

* `-O3` with `-mcpu=power10` or `-mcpu=power11` fails. `-O2`, `-O1`, `-Os`, `-mcpu=pwr9`, `-mno-pcrel` and `powerpc64` (big-endian) link
* Also confirmed locally with clang 18.1.8 and 23.1.2

clang version:

```text
clang version 24.0.0git (https://github.com/llvm/llvm-project a55e64432a516fd6507d3ecfba7407cfe425f940)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to