https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128663
Bug ID: 128663
Summary: miscompilation with LTO and mixed cross/native armv7
build (distcc)
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: lto
Assignee: unassigned at gcc dot gnu.org
Reporter: npfhrotynz-ptnqh.myvf at noclue dot notk.org
Target Milestone: ---
Compiling C files with a cross toolchain with LTO enabled, and linking with a
native toolchain on the target host can apparently cause miscompilations (SIMD
constants are missing, leading to wrong calculations)
I have the following two files:
- helper.c
```
double repro_one(int a) {
return -20.0 + (((double) a) * 0.5);
}
double repro_two(int a) {
return -141.0 + a;
}
```
- main.c (not using stdio/string.h because my distcc host does not have a
sysroot, but it doesn't change anything to use them anyway)
```
int strlen(const char*);
int printf(const char*, ...);
double repro_one(int a);
double repro_two(int a);
int main(int argc, char *argv[]) {
int a = argc > 1 ? strlen(argv[1]) : 16;
printf("%f\n", repro_one(a));
printf("%f\n", repro_two(a));
return 0;
}
```
The generated program is different (and unfortunately wrong) if compiled with a
cross toolchain before linking on target on armv7.
compilation command:
```
armv7-alpine-linux-musleabihf-gcc -Os -flto=auto -c helper.c
armv7-alpine-linux-musleabihf-gcc -Os -flto=auto -c main.c
```
link command:
```
armv7-alpine-linux-musleabihf-gcc -Os -flto=auto -o a.out helper.o main.o
```
building+linking on the same side (either both cross or both native) works and
provides the same output:
```
$ llvm-objdump --disassemble-symbols=main a.out
a.out: file format elf32-littlearm
Disassembly of section .text:
00000410 <main>:
410: b510 push {r4, lr}
412: 2801 cmp r0, #0x1
414: ed2d 8b02 vpush {d8}
418: dd20 ble 0x45c <main+0x4c> @ imm = #0x40
41a: 6848 ldr r0, [r1, #0x4]
41c: f7ff efea blx 0x3f4 <.plt+0x44> @ imm = #-0x2c
420: ee07 0a90 vmov s15, r0
424: eeb6 6b00 vmov.f64 d6, #5.000000e-01
428: eeb8 8be7 vcvt.f64.s32 d8, s15
42c: eeb3 7b04 vmov.f64 d7, #2.000000e+01
430: ee18 7b06 vnmls.f64 d7, d8, d6
434: 4c0c ldr r4, [pc, #0x30] @ 0x468 <main+0x58>
436: ec53 2b17 vmov r2, r3, d7
43a: 447c add r4, pc
43c: 4620 mov r0, r4
43e: f7ff efc2 blx 0x3c4 <.plt+0x14> @ imm = #-0x7c
442: ed9f 7b07 vldr d7, [pc, #28] @ 0x460 <main+0x50>
446: ee38 7b47 vsub.f64 d7, d8, d7
44a: 4620 mov r0, r4
44c: ec53 2b17 vmov r2, r3, d7
450: f7ff efb8 blx 0x3c4 <.plt+0x14> @ imm = #-0x90
454: ecbd 8b02 vpop {d8}
458: 2000 movs r0, #0x0
45a: bd10 pop {r4, pc}
45c: 2010 movs r0, #0x10
45e: e7df b 0x420 <main+0x10> @ imm = #-0x42
$ for addr in 0x468 0x460; do xxd -s $addr -l 8 a.out; done
00000468: 2e02 0000 4ff0 000b ....O...
00000460: 0000 0000 00a0 6140 ......a@
```
building on the x86 host with cross toolchain and linking on armv7 generates a
different program with missing constants for the SIMD instructions (the third
one is a normal ldr, and the constant is present):
```
$ llvm-objdump --disassemble-symbols=main a.out
a.out: file format elf32-littlearm
Disassembly of section .text:
00000410 <main>:
410: b510 push {r4, lr}
412: 2801 cmp r0, #0x1
414: ed2d 8b02 vpush {d8}
418: dd20 ble 0x45c <main+0x4c> @ imm = #0x40
41a: 6848 ldr r0, [r1, #0x4]
41c: f7ff efea blx 0x3f4 <.plt+0x44> @ imm = #-0x2c
420: ee07 0a90 vmov s15, r0
424: ed9f 6b0e vldr d6, [pc, #56] @ 0x460 <main+0x50>
428: eeb8 8be7 vcvt.f64.s32 d8, s15
42c: ed9f 7b0e vldr d7, [pc, #56] @ 0x468 <main+0x58>
430: ee18 7b06 vnmls.f64 d7, d8, d6
434: 4c10 ldr r4, [pc, #0x40] @ 0x478 <main+0x68>
436: ec53 2b17 vmov r2, r3, d7
43a: 447c add r4, pc
43c: 4620 mov r0, r4
43e: f7ff efc2 blx 0x3c4 <.plt+0x14> @ imm = #-0x7c
442: ed9f 7b0b vldr d7, [pc, #44] @ 0x470 <main+0x60>
446: ee38 7b47 vsub.f64 d7, d8, d7
44a: 4620 mov r0, r4
44c: ec53 2b17 vmov r2, r3, d7
450: f7ff efb8 blx 0x3c4 <.plt+0x14> @ imm = #-0x90
454: ecbd 8b02 vpop {d8}
458: 2000 movs r0, #0x0
45a: bd10 pop {r4, pc}
45c: 2010 movs r0, #0x10
45e: e7df b 0x420 <main+0x10> @ imm = #-0x42
$ for addr in 0x460 0x468 0x478 0x470; do xxd -s $addr -l 8 a.out; done
00000460: 0000 0000 0000 0000 ........
00000468: 0000 0000 0000 0000 ........
00000478: 3e02 0000 4ff0 000b >...O...
00000470: 0000 0000 0000 0000 ........
```
Since constants are missing, this returns 0/16 instead of -12/-125, but it's
curious to note that repro_one() is optimized differently as well.
This can be reproduced on alpine 3.24 (the cross toolchain is available in the
postmarketos repo, https://mirror.postmarketos.org/postmarketos/v26.06 for
alpine v3.24, package gcc-armv7. I can provide a Dockerfile or whatever if
helpful) - I first reproduced this with distcc and I suspect it is more common
to do it that way, but it reproduces without distcc, building directly on each
machine and transferring files manually.
So two questions:
- is this actually supported? If not, it'd be great if gcc could throw an error
on link like it does when building with different gcc versions.
We've actually been running with this bug for over three years (gcc 13.2.1), so
I suspect this particular error is quite focused, but I'm worried there are
other errors now with little way to check...
- if supported, how do I go around to debug this further :-)
Thanks!
(note, I've had segfaults before building networkmanager that I didn't bother
chasing down, I'm not sure if it's related but in that it only fails for armv7
it's similar:
https://github.com/distcc/distcc/issues/502#issuecomment-3995992763
segfault is much easier to detect than "builds but computation is wrong", so I
took the time for this one...)