https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118715
Bug ID: 118715
Summary: Bad PLT asm generated on ARM thumb with PIC and LTO
Product: gcc
Version: 14.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: lto
Assignee: unassigned at gcc dot gnu.org
Reporter: opensource at benjaminrh dot com
Target Milestone: ---
When compiling a shared ELF for ARM thumb with PIC and LTO, the PLT trampolines
correctly prepare the offset to the correct GOT entry, but then when
dereferencing add an extra invalid offset. Without `-flto` the code is correct.
The example below was compiled on my Mac, but I've reproduced it on a
x86_64-pc-linux-gnu host.
$ cat main.c
extern void foo(void);
void _start(void) {
foo();
}
$ gcc -v
Using built-in specs.
COLLECT_GCC=/Applications/ArmGNUToolchain/14.2.rel1/arm-none-eabi/bin/arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/Applications/ArmGNUToolchain/14.2.rel1/arm-none-eabi/bin/../libexec/gcc/arm-none-eabi/14.2.1/lto-wrapper
Target: arm-none-eabi
Configured with:
/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/src/gcc/configure
--target=arm-none-eabi
--prefix=/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/build-arm-none-eabi/install
--with-gmp=/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/build-arm-none-eabi/host-tools
--with-mpfr=/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/build-arm-none-eabi/host-tools
--with-mpc=/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/build-arm-none-eabi/host-tools
--with-isl=/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/build-arm-none-eabi/host-tools
--disable-shared --disable-nls --disable-threads --disable-tls
--enable-checking=release --enable-languages=c,c++,fortran --with-newlib
--with-gnu-as --with-headers=yes --with-gnu-ld
--with-native-system-header-dir=/include
--with-sysroot=/Volumes/data/jenkins/workspace/GNU-toolchain/arm-14/build-arm-none-eabi/install/arm-none-eabi
--with-bugurl=https://bugs.linaro.org/ --with-multilib-list=aprofile,rmprofile
--with-pkgversion='Arm GNU Toolchain 14.2.Rel1 (Build arm-14.52)'
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 14.2.1 20241119 (Arm GNU Toolchain 14.2.Rel1 (Build arm-14.52))
Valid code without LTO:
/Applications/ArmGNUToolchain/14.2.rel1/arm-none-eabi/bin/arm-none-eabi-gcc -O2
-Wall -Wextra -fPIC -ffreestanding -nostdlib -mthumb -mcpu=cortex-m4 main.c -o
elf --entry=_start -shared
$ objdump --disassemble='foo@plt' elf
00000124 <foo@plt>:
124: f241 0c8c movw ip, #4236 @ 0x108c
128: f2c0 0c00 movt ip, #0
12c: 44fc add ip, pc
12e: f8dc f000 ldr.w pc, [ip]
Invalid code with LTO:
/Applications/ArmGNUToolchain/14.2.rel1/arm-none-eabi/bin/arm-none-eabi-gcc -O2
-Wall -Wextra -fPIC -flto -ffreestanding -nostdlib -mthumb -mcpu=cortex-m4
main.c -o elf --entry=_start -shared
$ objdump --disassemble='foo@plt' elf
00000124 <foo@plt>:
124: 0000 movs r0, r0
126: 0000 movs r0, r0
128: f241 0c88 movw ip, #4232 @ 0x1088
12c: f2c0 0c00 movt ip, #0
130: 44fc add ip, pc
132: f8dc f7ff ldr.w pc, [ip, #2047] @ 0x7ff