[ppc64le] unknown relocation (18) against symbol exit
When cross-compiling libucontext-1.3.2 for ppc64le, I'm running into the following linker error from ld.lld:
$ CC=clang CFLAGS='--target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2' LDFLAGS='--target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -static-libgcc -fuse-ld=lld' make ARCH=ppc64le
clang -std=gnu99 -D_DEFAULT_SOURCE -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -c -o arch/ppc64/makecontext.o arch/ppc64/makecontext.c
clang -std=gnu99 -D_DEFAULT_SOURCE -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -c -o arch/ppc64/retfromsyscall.o arch/ppc64/retfromsyscall.c
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/getcontext.o arch/ppc64/getcontext.S
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/setcontext.o arch/ppc64/setcontext.S
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/startcontext.o arch/ppc64/startcontext.S
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/swapcontext.o arch/ppc64/swapcontext.S
clang -fPIC -o libucontext.so -shared -Wl,-soname,libucontext.so.1 -Wl,-z,noexecstack arch/ppc64/makecontext.o arch/ppc64/retfromsyscall.o arch/ppc64/getcontext.o arch/ppc64/setcontext.o arch/ppc64/startcontext.o arch/ppc64/swapcontext.o --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -static-libgcc -fuse-ld=lld
ld.lld: error: arch/ppc64/startcontext.o:(.text+0x20): unknown relocation (18) against symbol exit
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:127: libucontext.so] Error 1
Using ld.mold gives a similar error, using ld.bfd makes bfd segfault.
The error is caused by Clang creating an object file with an unrecognized relocation type (0x12 == 18) from the arch/ppc64/startcontext.S assembly input:
$ clang --target=ppc64le-alpine-linux-musl -Iarch/common -Wa,--noexecstack -c -o arch/ppc64/startcontext.o arch/ppc64/startcontext.S
$ readelf -r arch/ppc64/startcontext.o
Relocation section '.rela.text' at offset 0xc8 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 000300000012 unrecognized: 12 0000000000000000 exit + 0
OTOH, GCC produces valid output:
$ /opt/cross/O2/usr/bin/ppc64le-alpine-linux-musl-gcc -Iarch/common -Wa,--noexecstack -c -o arch/ppc64/startcontext.o arch/ppc64/startcontext.S
$ readelf -r arch/ppc64/startcontext.o
Relocation section '.rela.text' at offset 0x158 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 00070000000e R_PPC64_GOT16 0000000000000000 exit + 0
This is with Clang 20 from Alpine Linux 3.22:
$ clang -v
Alpine clang version 20.1.8
Target: x86_64-alpine-linux-musl
Thread model: posix
InstalledDir: /usr/lib/llvm20/bin
Configuration file: /etc/clang20/x86_64-alpine-linux-musl.cfg
System configuration file directory: /etc/clang20
Found candidate GCC installation: /usr/lib/gcc/x86_64-alpine-linux-musl/14.2.0
Selected GCC installation: /usr/lib/gcc/x86_64-alpine-linux-musl/14.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
Please let me know if you need any other information.
@llvm/issue-subscribers-backend-powerpc
Author: mhx (mhx)
$ CC=clang CFLAGS='--target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2' LDFLAGS='--target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -static-libgcc -fuse-ld=lld' make ARCH=ppc64le
clang -std=gnu99 -D_DEFAULT_SOURCE -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -c -o arch/ppc64/makecontext.o arch/ppc64/makecontext.c
clang -std=gnu99 -D_DEFAULT_SOURCE -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -c -o arch/ppc64/retfromsyscall.o arch/ppc64/retfromsyscall.c
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/getcontext.o arch/ppc64/getcontext.S
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/setcontext.o arch/ppc64/setcontext.S
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/startcontext.o arch/ppc64/startcontext.S
clang -fPIC -DPIC --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -Iinclude -Iarch/ppc64 -Iarch/common -Iarch/common/include -DFORCE_SOFT_FLOAT -DEXPORT_UNPREFIXED -Wa,--noexecstack -c -o arch/ppc64/swapcontext.o arch/ppc64/swapcontext.S
clang -fPIC -o libucontext.so -shared -Wl,-soname,libucontext.so.1 -Wl,-z,noexecstack arch/ppc64/makecontext.o arch/ppc64/retfromsyscall.o arch/ppc64/getcontext.o arch/ppc64/setcontext.o arch/ppc64/startcontext.o arch/ppc64/swapcontext.o --target=ppc64le-alpine-linux-musl --sysroot=/opt/cross/O2 -static-libgcc -fuse-ld=lld
ld.lld: error: arch/ppc64/startcontext.o:(.text+0x20): unknown relocation (18) against symbol exit
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:127: libucontext.so] Error 1
Using ld.mold gives a similar error, using ld.bfd makes bfd segfault.
The error is caused by Clang creating an object file with an unrecognized relocation type (0x12 == 18) from the arch/ppc64/startcontext.S assembly input:
$ clang --target=ppc64le-alpine-linux-musl -Iarch/common -Wa,--noexecstack -c -o arch/ppc64/startcontext.o arch/ppc64/startcontext.S
$ readelf -r arch/ppc64/startcontext.o
Relocation section '.rela.text' at offset 0xc8 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 000300000012 unrecognized: 12 0000000000000000 exit + 0
OTOH, GCC produces valid output:
$ /opt/cross/O2/usr/bin/ppc64le-alpine-linux-musl-gcc -Iarch/common -Wa,--noexecstack -c -o arch/ppc64/startcontext.o arch/ppc64/startcontext.S
$ readelf -r arch/ppc64/startcontext.o
Relocation section '.rela.text' at offset 0x158 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
000000000020 00070000000e R_PPC64_GOT16 0000000000000000 exit + 0
This is with Clang 20 from Alpine Linux 3.22:
$ clang -v
Alpine clang version 20.1.8
Target: x86_64-alpine-linux-musl
Thread model: posix
InstalledDir: /usr/lib/llvm20/bin
Configuration file: /etc/clang20/x86_64-alpine-linux-musl.cfg
System configuration file directory: /etc/clang20
Found candidate GCC installation: /usr/lib/gcc/x86_64-alpine-linux-musl/14.2.0
Selected GCC installation: /usr/lib/gcc/x86_64-alpine-linux-musl/14.2.0
Candidate multilib: .;@<!-- -->m64
Selected multilib: .;@<!-- -->m64
Please let me know if you need any other information.
I've also filed a bug against binutils / ld.bfd for the segfault and Andreas Schwab mentioned
18 is R_PPC_PLTREL24, which is only defined for EM_PPC, but not for EM_PPC64.
Is the GCC behaviour actually desired? This is an unconditional branch that should have a 24-bit relocation, yet GCC is emitting a 16-bit relocation. That doesn't sound right. Why b exit@GOT in the file?
Is the GCC behaviour actually desired?
I have no idea! :)
This is an unconditional branch that should have a 24-bit relocation, yet GCC is emitting a 16-bit relocation. That doesn't sound right. Why
b exit@GOTin the file?
Again, no idea. It's apparently in there since the very first ppc64 commit: https://github.com/kaniini/libucontext/commit/9e5aba6b4824897c388efa7a47ef6a7daaddda23
There's some more context here: https://github.com/rui314/mold/issues/1490
Is the GCC behaviour actually desired?
I have no idea! :)
This is an unconditional branch that should have a 24-bit relocation, yet GCC is emitting a 16-bit relocation. That doesn't sound right. Why
b exit@GOTin the file?Again, no idea. It's apparently in there since the very first ppc64 commit: kaniini/libucontext@9e5aba6
I agree with Nemanja here, I think the source is incorrect to use the @GOT relocation specifier.
What gcc is emitting will direct the linker to materialize a got entry to store the address of exit, calculate the offset from the toc pointer to said got entry and fill in the offset to the call instruction with that offset. You will end up branching to random code because that offset is meaningless in the context of a function call. If you drop the @got then the linker will manifest a plt stub to call exit which seems to be the intent.
I checked with my latest build for clang and I don't see a R_PPC_PLTREL24 being emitted, instead we emit an error witch I think is acceptable:
test.s:12:5: error: unsupported relocation type
b exit@GOT
^
Closing this as the compiler now diagnosis the error and I believe the library that motivated this has been fixed to use valid assembly now.