https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103889

--- Comment #1 from John Doe <bau2wux4sh at pkv73 dot anonbox.net> ---
[Hm, somehow bugzilla didn't add the description? Adding it again here.]

When compiling GCC with --disable-multilib on 64-Bit RISC-V (RV64), the gccgo
frontend does not seem to be able to find it's standard library:

        $ gccgo -v -o hello_world hello_world.go
        Using built-in specs.
        COLLECT_GCC=/usr/bin/gccgo
       
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/riscv64-alpine-linux-musl/11.2.1/lto-wrapper
        Target: riscv64-alpine-linux-musl
        Configured with:
/home/buildozer/aports/main/gcc/src/gcc-11.2.1_git20211128/configure
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--build=riscv64-alpine-linux-musl --host=riscv64-alpine-linux-musl
--target=riscv64-alpine-linux-musl --with-pkgversion='Alpine
11.2.1_git20211128' --enable-checking=release --disable-fixed-point
--disable-libstdcxx-pch --disable-multilib --disable-nls --disable-werror
--disable-symvers --enable-__cxa_atexit --enable-default-pie
--enable-default-ssp --enable-cloog-backend
--enable-languages=c,c++,objc,go,fortran,jit --with-arch=rv64gc
--with-abi=lp64d --enable-autolink-libatomic --disable-libquadmath
--disable-libssp --disable-libmpx --disable-libmudflap --disable-libsanitizer
--enable-shared --enable-threads --enable-tls --disable-libitm
--enable-host-shared --with-system-zlib --with-linker-hash-style=gnu
        Thread model: posix
        Supported LTO compression algorithms: zlib
        gcc version 11.2.1 20211128 (Alpine 11.2.1_git20211128)
        COLLECT_GCC_OPTIONS='-g1' '-v' '-o' 'hello_world' '-shared-libgcc'
'-march=rv64gc' '-mabi=lp64d' '-march=rv64imafdc'
         /usr/libexec/gcc/riscv64-alpine-linux-musl/11.2.1/go1 hello_world.go
-quiet -dumpbase hello_world.go -dumpbase-ext .go -march=rv64gc -mabi=lp64d
-march=rv64imafdc -g1 -version -L/usr/lib/gcc/riscv64-alpine-linux-musl/11.2.1
-L/usr/lib/gcc/riscv64-alpine-linux-musl/11.2.1/../../../../riscv64-alpine-linux-musl/lib
-o /tmp/ccLBoAel.s
        GNU Go (Alpine 11.2.1_git20211128) version 11.2.1 20211128
(riscv64-alpine-linux-musl)
                compiled by GNU C version 11.2.1 20211128, GMP version 6.2.1,
MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.22-GMP

        GGC heuristics: --param ggc-min-expand=100 --param
ggc-min-heapsize=131072
        GNU Go (Alpine 11.2.1_git20211128) version 11.2.1 20211128
(riscv64-alpine-linux-musl)
                compiled by GNU C version 11.2.1 20211128, GMP version 6.2.1,
MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.22-GMP

        GGC heuristics: --param ggc-min-expand=100 --param
ggc-min-heapsize=131072
        hello_world.go:3:11: error: import file 'fmt' not found
            3 | import "fmt"
              |           ^
        hello_world.go:6:9: error: reference to undefined name 'fmt'
            6 |         fmt.Println("Hello, World!")
              |         ^

However, compiling on x86_64 works as expected:

        $ gccgo -v -o hello_world hello_world.go
        Using built-in specs.
        COLLECT_GCC=gccgo
       
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/lto-wrapper
        Target: x86_64-alpine-linux-musl
        Configured with:
/home/buildozer/aports/main/gcc/src/gcc-11.2.1_git20211128/configure
--prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--build=x86_64-alpine-linux-musl --host=x86_64-alpine-linux-musl
--target=x86_64-alpine-linux-musl --with-pkgversion='Alpine 11.2.1_git20211128'
--enable-checking=release --disable-fixed-point --disable-libstdcxx-pch
--disable-multilib --disable-nls --disable-werror --disable-symvers
--enable-__cxa_atexit --enable-default-pie --enable-default-ssp
--enable-cloog-backend --enable-languages=c,c++,d,objc,go,fortran,ada,jit
--disable-libssp --disable-libmpx --disable-libmudflap --disable-libsanitizer
--enable-shared --enable-threads --enable-tls --enable-host-shared
--with-system-zlib --with-linker-hash-style=gnu
        Thread model: posix
        Supported LTO compression algorithms: zlib
        gcc version 11.2.1 20211128 (Alpine 11.2.1_git20211128)
        COLLECT_GCC_OPTIONS='-g1' '-v' '-o' 'hello_world' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
         /usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/go1 hello_world.go
-quiet -dumpbase hello_world.go -dumpbase-ext .go -mtune=generic -march=x86-64
-g1 -version -L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib/../lib
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib -L/lib/../lib
-L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../.. -o /tmp/ccjMeGOf.s
        GNU Go (Alpine 11.2.1_git20211128) version 11.2.1 20211128
(x86_64-alpine-linux-musl)
                compiled by GNU C version 11.2.1 20211128, GMP version 6.2.1,
MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.22-GMP

        GGC heuristics: --param ggc-min-expand=100 --param
ggc-min-heapsize=131072
        GNU Go (Alpine 11.2.1_git20211128) version 11.2.1 20211128
(x86_64-alpine-linux-musl)
                compiled by GNU C version 11.2.1 20211128, GMP version 6.2.1,
MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.22-GMP

        GGC heuristics: --param ggc-min-expand=100 --param
ggc-min-heapsize=131072
        COLLECT_GCC_OPTIONS='-g1' '-v' '-o' 'hello_world' '-shared-libgcc'
'-mtune=generic' '-march=x86-64'
        
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/as
-v --gdwarf-5 --64 -o /tmp/ccKNJKMa.o /tmp/ccjMeGOf.s
        GNU assembler version 2.37 (x86_64-alpine-linux-musl) using BFD version
(GNU Binutils) 2.37
       
COMPILER_PATH=/usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/:/usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/:/usr/libexec/gcc/x86_64-alpine-linux-musl/:/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/:/usr/lib/gcc/x86_64-alpine-linux-musl/:/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/bin/
       
LIBRARY_PATH=/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/:/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib/:/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../:/lib/:/usr/lib/
        COLLECT_GCC_OPTIONS='-g1' '-v' '-o' 'hello_world' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'hello_world.'
         /usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/collect2 -plugin
/usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-alpine-linux-musl/11.2.1/lto-wrapper
-plugin-opt=-fresolution=/tmp/ccDpLAAf.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
--eh-frame-hdr --hash-style=gnu -m elf_x86_64 --as-needed -dynamic-linker
/lib/ld-musl-x86_64.so.1 -pie -z relro -z now -o hello_world
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/Scrt1.o
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/crti.o
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/crtbeginS.o
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib/../lib
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib -L/lib/../lib
-L/usr/lib/../lib
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../x86_64-alpine-linux-musl/lib
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../.. /tmp/ccKNJKMa.o
-lgobegin -lgo -lm -lssp_nonshared -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/crtendS.o
/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib/crtn.o
        COLLECT_GCC_OPTIONS='-g1' '-v' '-o' 'hello_world' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'hello_world.'

By comparing the two verbose outputs, it seems that the some -L arguments
passed to the x86_64 version area not passed to the riscv64 version of the
gccgo compiler. Most notably,
-L/usr/lib/gcc/x86_64-alpine-linux-musl/11.2.1/../../../../lib (which is a
relative path to /usr/lib) is missing in the riscv64 invocation. This missing
search path seems to be the root cause of the issue as invoking gccgo on
riscv64 as `gccgo -L/usr/lib -o hello_world hello_world.go` works as expected.

I am not sure why this search path is missing on riscv64. Any guidance on
debugging this further would be appreciated.

As one can see in the output above, I am using Alpine's GCC version which
includes a few patches for gccgo since the vanilla gccgo version does not work
with musl libc by default. These patches are currently in the process of being
upstreamed and riscv64 is the only architecture where gccgo doesn't work
out-of-the-box at the moment (x86_64, x86, ppc64le, armhf, armv7, and aarch64
work fine).

The patches can be found here: https://git.alpinelinux.org/aports/tree/main/gcc

My current hypothesis is that this might be related to the `gcc
-print-multi-os-directory` output. On x86_64 this outputs `../lib` and on
riscv64 it simply outputs `.`.

Reply via email to