https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127138
Bug ID: 127138
Summary: gcc on musl targets does not link libssp_nonshared.a,
so -fstack-protector PIC code fails to link on 32-bit
x86 and PowerPC
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: driver
Assignee: unassigned at gcc dot gnu.org
Reporter: sunil.dora1988 at gmail dot com
Target Milestone: ---
On musl targets gcc emits calls to __stack_chk_fail_local in PIC/PIE
code on some architectures (32-bit x86, PowerPC). musl's libc
provides __stack_chk_fail but deliberately not the _local alias, and
unlike glibc there is no libc_nonshared.a linked in automatically.
So gcc cannot link its own output:
$ echo 'void f(const char *s){char b[16];__builtin_strcpy(b,s);}' > t.c
$ i686-linux-musl-gcc -fstack-protector-strong -fPIC -O2 -c t.c
$ i686-linux-musl-gcc -shared t.o -o t.so
ld: t.o: in function `f':
t.c:(.text+0x3f): undefined reference to `__stack_chk_fail_local'
musl-based distributions (Alpine, OpenEmbedded) all ship the missing
symbol in a small libssp_nonshared.a and carry a gcc patch that adds
-lssp_nonshared to the link line when stack protection is enabled.
That patch was sent to gcc-patches in 2023:
https://gcc.gnu.org/pipermail/gcc-patches/2023-April/615317.html
Jakub noted v1 also affected glibc; I am sending a v2 restricted to
musl-default toolchains.
Verified with gcc 16.2.0 by building i686-linux-musl and
i686-linux-gnu cross compilers with the v2 patch: the musl one adds
-lssp_nonshared under -fstack-protector-*, the glibc one is
unchanged.