Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!

Bug 938012

Summary: sys-libs/musl: wchar_t is broken with -fshort-wchar on amd64
Product: Gentoo Linux Reporter: Viorel Munteanu <ceamac>
Component: Current packagesAssignee: Anthony Basile <blueness>
Status: CONFIRMED ---    
Severity: normal CC: lu_zero, musl, toolchain, vimproved
Priority: Normal Keywords: PullRequest
Version: unspecified   
Hardware: All   
OS: Linux   
See Also: https://github.com/gentoo/gentoo/pull/38263
https://bugs.gentoo.org/show_bug.cgi?id=939135
Whiteboard:
Package list:
Runtime testing required: ---
Bug Depends on:    
Bug Blocks: 933080    

Description Viorel Munteanu gentoo-dev 2024-08-16 13:07:56 UTC
arch/x86_64/bits/alltypes.h.in defines wchar_t to int unconditionally.

gcc -fshort-wchar defines correct macros, but they are ignored:

# gcc -fshort-wchar -E - -dM < /dev/null | grep WCHAR
#define __WCHAR_MAX__ 0xffff
#define __WCHAR_MIN__ 0
#define __WCHAR_WIDTH__ 16
#define __GCC_ATOMIC_WCHAR_T_LOCK_FREE 2
#define __WCHAR_TYPE__ short unsigned int
#define __SIZEOF_WCHAR_T__ 2

In this case alltypes.h should define wchar_t to __WCHAR_TYPE__, like other arches (for example arch/i386/bits/alltypes.h.in).
Comment 1 Viorel Munteanu gentoo-dev 2024-08-16 13:09:24 UTC
Portage 3.0.65 (python 3.12.5-final-0, default/linux/amd64/23.0/musl, gcc-14, musl-1.2.5-r1, 6.6.38-gentoo-clang-r1 x86_64)
=================================================================
System uname: Linux-6.6.38-gentoo-clang-r1-x86_64-AMD_Ryzen_7_3700X_8-Core_Processor-with-libc
KiB Mem:    32832480 total,  14323956 free
KiB Swap:   50331644 total,  50331644 free
Timestamp of repository musl: Fri, 16 Aug 2024 10:33:58 +0000
Head commit of repository musl: ce8316c7b0308c28677312edffcdb3a4f29c4a1e

sh bash 5.2_p32
ld GNU ld (Gentoo 2.42 p6) 2.42.0
app-misc/pax-utils:        1.3.7::gentoo
app-shells/bash:           5.2_p32::gentoo
dev-build/autoconf:        2.72-r1::gentoo
dev-build/automake:        1.17-r1::gentoo
dev-build/cmake:           3.30.2::gentoo
dev-build/libtool:         2.4.7-r4::gentoo
dev-build/make:            4.4.1-r1::gentoo
dev-build/meson:           1.5.1::gentoo
dev-lang/perl:             5.40.0::gentoo
dev-lang/python:           3.12.5::gentoo, 3.13.0_rc1_p1::gentoo
sys-apps/baselayout:       2.15::gentoo
sys-apps/openrc:           0.54.2::gentoo
sys-apps/sandbox:          2.39::gentoo
sys-devel/binutils:        2.42-r2::gentoo, 2.43::gentoo
sys-devel/binutils-config: 5.5.2::gentoo
sys-devel/gcc:             13.3.1_p20240614::gentoo, 14.2.0::gentoo
sys-devel/gcc-config:      2.11::gentoo
sys-kernel/linux-headers:  6.9::gentoo (virtual/os-headers)
sys-libs/musl:             1.2.5-r1::gentoo
Repositories:

gentoo
    location: /var/db/repos/gentoo
    sync-type: rsync
    sync-uri: rsync://rsync.gentoo.org/gentoo-portage
    priority: -1000
    volatile: True
    sync-rsync-extra-opts: 
    sync-rsync-verify-jobs: 1
    sync-rsync-verify-max-age: 24
    sync-rsync-verify-metamanifest: yes

musl
    location: /var/db/repos/musl
    sync-type: git
    sync-uri: https://github.com/gentoo-mirror/musl.git
    masters: gentoo
    volatile: False

ACCEPT_KEYWORDS="amd64 ~amd64"
ACCEPT_LICENSE="@FREE"
CBUILD="x86_64-pc-linux-musl"
CFLAGS="-O2 -march=znver2 -mno-3dnow -mno-rdrnd -mno-rdseed -msahf -pipe -frecord-gcc-switches -frecord-gcc-switches"
CHOST="x86_64-pc-linux-musl"
CONFIG_PROTECT="/etc /usr/share/gnupg/qualified.txt"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/sandbox.d"
CXXFLAGS="-O2 -march=znver2 -mno-3dnow -mno-rdrnd -mno-rdseed -msahf -pipe -frecord-gcc-switches -frecord-gcc-switches"
DISTDIR="/var/cache/distfiles"
ENV_UNSET="CARGO_HOME DBUS_SESSION_BUS_ADDRESS DISPLAY GDK_PIXBUF_MODULE_FILE GOBIN GOPATH MAIL PERL5LIB PERL5OPT PERLPREFIX PERL_CORE PERL_MB_OPT PERL_MM_OPT WINEPREFIX XAUTHORITY XDG_CACHE_HOME XDG_CONFIG_HOME XDG_DATA_HOME XDG_RUNTIME_DIR XDG_STATE_HOME root"
FCFLAGS="-O2 -march=znver2 -mno-3dnow -mno-rdrnd -mno-rdseed -msahf -pipe -frecord-gcc-switches -frecord-gcc-switches"
FEATURES="assume-digests binpkg-docompress binpkg-dostrip binpkg-logs binpkg-multi-instance buildpkg buildpkg-live config-protect-if-modified distlocks ebuild-locks fixlafiles ipc-sandbox merge-sync merge-wait network-sandbox news parallel-fetch pid-sandbox pkgdir-index-trusted preserve-libs protect-owned qa-unresolved-soname-deps sandbox sfperms strict unknown-features-warn unmerge-logs unmerge-orphans userfetch userpriv usersandbox usersync xattr"
FFLAGS="-O2 -march=znver2 -mno-3dnow -mno-rdrnd -mno-rdseed -msahf -pipe -frecord-gcc-switches -frecord-gcc-switches"
GENTOO_MIRRORS="http://gentoo.mirror.web4u.cz/ http://tux.rainside.sk/gentoo/ http://ftp.belnet.be/pub/rsync.gentoo.org/gentoo/ https://mirrors.ircam.fr/pub/gentoo-distfiles/"
INSTALL_MASK="charset.alias /usr/share/locale/locale.alias"
LANG="C.UTF8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,-z,pack-relative-relocs -Wl,--defsym=__gentoo_check_ldflags__=0"
LEX="flex"
MAKEOPTS="-j8"
PKGDIR="/var/cache/binpkgs"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--exclude=* --dry-run"
PORTAGE_TMPDIR="/var/tmp"
SHELL="/bin/bash"
USE="acl amd64 bzip2 cet crypt iconv ipv6 libtirpc ncurses nls openmp pam pcre pic readline seccomp ssl test-rust unicode xattr zlib" ABI_X86="64" ADA_TARGET="gcc_12" APACHE2_MODULES="authn_core authz_core socache_shmcb unixd actions alias auth_basic authn_anon authn_dbm authn_file authz_dbm authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir env expires ext_filter file_cache filter headers include info log_config logio mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="karbon sheets words" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" CPU_FLAGS_X86="mmx mmxext sse sse2" ELIBC="musl" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock greis isync itrax mtk3301 ntrip navcom oceanserver oncore rtcm104v2 rtcm104v3 sirf skytraq superstar2 tsip tripmate tnt ublox" INPUT_DEVICES="libinput" KERNEL="linux" LCD_DEVICES="bayrad cfontz glk hd44780 lb216 lcdm001 mtxorb text" LUA_SINGLE_TARGET="lua5-1" LUA_TARGETS="lua5-1" OFFICE_IMPLEMENTATION="libreoffice" PHP_TARGETS="php8-2" POSTGRES_TARGETS="postgres15" PYTHON_SINGLE_TARGET="python3_12" PYTHON_TARGETS="python3_12" RUBY_TARGETS="ruby31 ruby32" VIDEO_CARDS="amdgpu fbdev intel nouveau radeon radeonsi vesa dummy" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipp2p iface geoip fuzzy condition tarpit sysrq proto logmark ipmark dhcpmac delude chaos account"
Unset:  ADDR2LINE, AR, ARFLAGS, AS, ASFLAGS, CC, CCLD, CONFIG_SHELL, CPP, CPPFLAGS, CTARGET, CXX, CXXFILT, ELFEDIT, EMERGE_DEFAULT_OPTS, EXTRA_ECONF, F77FLAGS, FC, GCOV, GPROF, LC_ALL, LD, LFLAGS, LIBTOOL, LINGUAS, MAKE, MAKEFLAGS, NM, OBJCOPY, OBJDUMP, PORTAGE_BINHOST, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS, PYTHONPATH, RANLIB, READELF, RUSTFLAGS, SIZE, STRINGS, STRIP, YACC, YFLAGS
Comment 2 Mike Gilbert gentoo-dev 2024-08-16 15:14:13 UTC
We currently need to work around this in sys-apps/systemd-utils.

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=34cd105a4b61cdbc9b9d6f722425fbbeaf405f13
Comment 3 Mike Gilbert gentoo-dev 2024-08-16 15:45:23 UTC
Some notes:

gcc installs /usr/lib/gcc/../include/stddef.h. This header defines wchar_t correctly.

musl installs /usr/include/stddef.h, which overrides gcc's copy. /usr/include/stddef.h pulls in arch/x86_64/bits/alltypes.h, which defines wchar_t incorrectly.

Two possible solutions:

1. musl fixes its copy of stddef.h and alltypes.h
2. musl dropes stddef.h and lets gcc provide it.

I think the latter probably makes more sense in the long run, but I don't know the reason for musl providing stddef.h in the first place.
Comment 4 Mike Gilbert gentoo-dev 2024-08-16 16:00:48 UTC
(In reply to Mike Gilbert from comment #3)
> gcc installs /usr/lib/gcc/../include/stddef.h. This header defines wchar_t
> correctly.

clang installs a similar file which also works correctly: /usr/lib/clang/.../include/stddef.h
Comment 5 Violet Purcell 2024-08-19 15:56:10 UTC
from #musl:

<dalias> -fshort-wchar is not something you can use except possibly in a freestanding environment
<vimproved> yes, this is specifically being used in a freestanding situation
<dalias> then -ffreestanding -nostdinc should be passed, and gcc's compiler headers include path added back

the real issues here are two different things:
1. on musl, both clang and gcc put /usr/include before the compiler include path, which causes musl's stddef.h to be used
2. systemd (as usual) does not care about musl, so they don't pass -nostdinc to the systemd-boot build along with -ffreestanding. Seems to me that this isn't a musl issue.
Comment 6 Rich Felker 2024-08-19 15:57:53 UTC
The problem is that you'r trying to build a bare-metal freestanding binary using a toolchain that targets a hosted musl environment. musl absolutely does not support -fshort-wchar. The archs where __WCHAR_TYPE__ is used are ones where gcc and clang disagree about whether the 32-bit wchar_t is int or long; the intent was never to support ABI-breaking, Unicode-incompatible definitions of wchar_t.

If you're building bare-metal binaries using a toolchain that targets musl by default, you should be suppressing the headers for the hosted musl environment and using the gcc-provided headers. If you are building for a hosted musl environment, you need the musl headers, not the gcc ones.
Comment 7 Viorel Munteanu gentoo-dev 2024-08-23 16:27:08 UTC
I see this as a bug in musl, because -ffreestanding should not require -nostdinc.  -fshort-wchar is a feature of the compiler, so libc is not supposed to support it, only to not stand in its way.

That being said, I patched sys-boot/gnu-efi to compile with -ffreestanding -nostdinc, and now most of its reverse dependencies fail compilation with the same error:

Success: app-crypt/sbsigntools.
Fail because of wchar_t: app-crypt/efitools, sys-apps/fwupd-efi, sys-boot/elilo, sys-boot/refind.
Fail for unrelated causes: app-crypt/pesign, sys-boot/syslinux.

From this list I fixed sys-boot/refind (in the same PR).
Comment 8 Larry the Git Cow gentoo-dev 2024-08-26 04:34:44 UTC
The bug has been referenced in the following commit(s):

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=e0d73710476266df0532c7cbaae7da5404bd4eab

commit e0d73710476266df0532c7cbaae7da5404bd4eab
Author:     Viorel Munteanu <ceamac@gentoo.org>
AuthorDate: 2024-08-23 15:07:47 +0000
Commit:     Viorel Munteanu <ceamac@gentoo.org>
CommitDate: 2024-08-26 04:28:53 +0000

    sys-boot/refind: fix build on musl
    
    Bug: https://bugs.gentoo.org/938012
    Closes: https://github.com/gentoo/gentoo/pull/38263
    Signed-off-by: Viorel Munteanu <ceamac@gentoo.org>

 .../refind-0.14.2-fix-freestanding-on-musl.patch   |  63 ++++++++
 sys-boot/refind/refind-0.14.2-r2.ebuild            | 172 +++++++++++++++++++++
 2 files changed, 235 insertions(+)

https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=c4a94ba86475049c1806eb81fe28200ee3020a59

commit c4a94ba86475049c1806eb81fe28200ee3020a59
Author:     Viorel Munteanu <ceamac@gentoo.org>
AuthorDate: 2024-08-23 14:39:13 +0000
Commit:     Viorel Munteanu <ceamac@gentoo.org>
CommitDate: 2024-08-26 04:28:37 +0000

    sys-boot/gnu-efi: Fix compilation on musl amd64
    
    On amd64, musl typedefs wchar_t to int, conflicting with -fshort-wchar.
    Also, /usr/include is searched before the compiler provided include
    directory.
    As a workaround, use -nostdinc and switch them around.
    
    Closes: https://bugs.gentoo.org/933080
    Bug: https://bugs.gentoo.org/938012
    Signed-off-by: Viorel Munteanu <ceamac@gentoo.org>

 sys-boot/gnu-efi/gnu-efi-3.0.18-r4.ebuild | 128 ++++++++++++++++++++++++++++++
 1 file changed, 128 insertions(+)