zig icon indicating copy to clipboard operation
zig copied to clipboard

unable to build glibc for target riscv64-linux-gnu

Open andrewrk opened this issue 6 years ago • 7 comments

[nix-shell:~/dev/zig/build]$ ./zig build-exe --c-source hello.c -target riscv64-linux-gnu -lc
/home/andy/dev/zig/lib/libc/glibc/sysdeps/riscv/start.S:37:10: fatal error: 'entry.h' file not found
#include <entry.h>
         ^~~~~~~~~
1 error generated.

The following command failed:
/home/andy/dev/zig/build/zig cc -MD -MV -MF /home/andy/.local/share/zig/stage1/tmp/jy58t2gTUqIE-start.o.d -nostdinc -fno-spell-checking -isystem /home/andy/dev/zig/lib/include -target riscv64-unknown-linux-gnu -g -fno-omit-frame-pointer -D_DEBUG -fno-stack-protector -fPIC -o /home/andy/.local/share/zig/stage1/tmp/jy58t2gTUqIE-start.o -c /home/andy/dev/zig/lib/libc/glibc/sysdeps/riscv/start.S -I /home/andy/dev/zig/lib/libc/glibc/include -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix/sysv/linux/riscv -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/riscv/nptl -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix/sysv/linux/generic -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix/sysv/linux/include -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix/sysv/linux -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/nptl -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/pthread -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix/sysv -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix/riscv -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/unix -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/riscv -I /home/andy/dev/zig/lib/libc/glibc/sysdeps/generic -I /home/andy/dev/zig/lib/libc/glibc -I /home/andy/dev/zig/lib/libc/include/riscv64-linux-gnu -I /home/andy/dev/zig/lib/libc/include/generic-glibc -I /home/andy/dev/zig/lib/libc/include/riscv64-linux-any -I /home/andy/dev/zig/lib/libc/include/any-linux-any -D_LIBC_REENTRANT -include /home/andy/dev/zig/lib/libc/glibc/include/libc-modules.h -DMODULE_NAME=libc -Wno-nonportable-include-path -include /home/andy/dev/zig/lib/libc/glibc/include/libc-symbols.h -DPIC -DSHARED -DTOP_NAMESPACE=glibc -DASSEMBLER -g -Wa,--noexecstack

andrewrk avatar Sep 29 '19 16:09 andrewrk

Related: #4459

andrewrk avatar Feb 17 '20 04:02 andrewrk

This now seems to fail with:

$ zig build-exe --c-source hello.c -target riscv64-linux-gnu -lc
In file included from hello.c:1:
In file included from /home/daurnimator/src/zig/lib/libc/include/generic-glibc/stdio.h:27:
In file included from /home/daurnimator/src/zig/lib/libc/include/generic-glibc/bits/libc-header-start.h:33:
In file included from /home/daurnimator/src/zig/lib/libc/include/generic-glibc/features.h:489:
/home/daurnimator/src/zig/lib/libc/include/riscv64-linux-gnu/gnu/stubs.h:11:11: fatal error: 'gnu/stubs-lp64d.h' file not found
# include <gnu/stubs-lp64d.h>
          ^~~~~~~~~~~~~~~~~~~
1 error generated.

The following command failed:
/home/daurnimator/src/zig/build/zig clang -c -MD -MV -MF zig-cache/tmp/uZEbEh5O-XWK-hello.o.d -nostdinc -fno-spell-checking -isystem /home/daurnimator/src/zig/lib/include -isystem /home/daurnimator/src/zig/lib/libc/include/riscv64-linux-gnu -isystem /home/daurnimator/src/zig/lib/libc/include/generic-glibc -isystem /home/daurnimator/src/zig/lib/libc/include/riscv64-linux-any -isystem /home/daurnimator/src/zig/lib/libc/include/any-linux-any -target riscv64-unknown-linux-gnu -Xclang -target-feature -Xclang +64bit,+a,+c,+d,-e,+f,+m,-relax,-reserve-x1,-reserve-x10,-reserve-x11,-reserve-x12,-reserve-x13,-reserve-x14,-reserve-x15,-reserve-x16,-reserve-x17,-reserve-x18,-reserve-x19,-reserve-x2,-reserve-x20,-reserve-x21,-reserve-x22,-reserve-x23,-reserve-x24,-reserve-x25,-reserve-x26,-reserve-x27,-reserve-x28,-reserve-x29,-reserve-x3,-reserve-x30,-reserve-x31,-reserve-x4,-reserve-x5,-reserve-x6,-reserve-x7,-reserve-x8,-reserve-x9,-rvc-hints -mno-relax -g -fno-omit-frame-pointer -fsanitize=undefined -fsanitize-trap=undefined -D_DEBUG -Og -fstack-protector-strong --param ssp-buffer-size=4 -fPIC -o zig-cache/tmp/uZEbEh5O-XWK-hello.o hello.c

daurnimator avatar Mar 31 '20 04:03 daurnimator

The fix for this issue is likely just copying a few files over from glibc 2.31 into zig's source repo.

andrewrk avatar Mar 31 '20 04:03 andrewrk

After #10339, the next problem:

$ ./zig build-exe hello.c -lc -target riscv64-linux-gnu
error(compilation): clang failed with stderr: /home/andy/Downloads/zig/lib/libc/glibc/sysdeps/riscv/start.S:48:2: error: unknown directive
 .cfi_label .Ldummy
 ^

error(compilation): /home/andy/Downloads/zig/lib/libc/glibc/sysdeps/riscv/start.S:1:1: unable to build C object: clang exited with code 1
error: unable to build glibc CRT file: BuildingLibCObjectFailed

.cfi_label seems to be an undocumented feature of GAS that clang does not support.

Not sure how to work around that:

ENTRY (ENTRY_POINT)
	/* Terminate call stack by noting ra is undefined.  Use a dummy
	   .cfi_label to force starting the FDE.  */
	.cfi_label .Ldummy
	cfi_undefined (ra)
	call  load_gp
	mv    a5, a0  /* rtld_fini.  */
	/* main may be in a shared library.  */
	la   a0, main
	REG_L a1, 0(sp)      /* argc.  */
	addi  a2, sp, SZREG  /* argv.  */
	andi  sp, sp, ALMASK /* Align stack. */
	li    a3, 0	     /* Used to be init.  */
	li    a4, 0	     /* Used to be fini.  */
	mv    a6, sp  /* stack_end.  */

	call  __libc_start_main@plt
	ebreak
END (ENTRY_POINT)

andrewrk avatar Dec 16 '21 01:12 andrewrk

Digging into old glibc mail archive, I found https://sourceware.org/pipermail/libc-alpha/2019-January/100734.html

Using a pair of cfi_startproc and cfi_endproc should work equivalently like cfi_label. aarch64 also uses this way https://github.com/bminor/glibc/blob/b92a49359f33a461db080a33940d73f47c756126/sysdeps/unix/sysv/linux/aarch64/clone.S#L72

alexfanqi avatar Sep 21 '22 06:09 alexfanqi

I just hit this problem, while trying to cross-compile Go code (with C dependencies) to target riscv64-linux-gnu:

/usr/local/go/pkg/tool/linux_amd64/link: running zig failed: exit status 1
/zig/lib/libc/glibc/sysdeps/riscv/start-2.33.S:48:2: error: unknown directive
 .cfi_label .Ldummy

Is is possible to give a "for dummies" instruction on how to ad-hoc fix this? I need to change some file under /zig/lib/libc/glibc/sysdeps/riscv/ ?

joonas-fi avatar Oct 28 '22 13:10 joonas-fi

Similar problem.

  • https://github.com/messense/cargo-zigbuild/issues/70

kassane avatar Nov 03 '22 22:11 kassane

From my understanding of upstream issue linked in, the cfi_label is there to push the cfi_undefined ra into FDE of the entry function instead of leaving it in CIE that is shared for many FDE s. As shown in https://sourceware.org/bugzilla/show_bug.cgi?id=29749#c2, without cfi_label, gas puts cfi_undefined ra in CIE. And I found that llvm-mc puts it in FDE. So I think for llvm, the cfi_label line can be simply removed.

alexfanqi avatar Nov 23 '22 09:11 alexfanqi

From my understanding of upstream issue linked in, the cfi_label is there to push the cfi_undefined ra into FDE of the entry function instead of leaving it in CIE that is shared for many FDE s. As shown in https://sourceware.org/bugzilla/show_bug.cgi?id=29749#c2, without cfi_label, gas puts cfi_undefined ra in CIE. And I found that llvm-mc puts it in FDE. So I think for llvm, the cfi_label line can be simply removed.

Just hit this problem too, removing that directive and compiling with zig build -Dtarget=riscv64-linux-gnu.2.34 makes it work

Beyley avatar Apr 12 '23 01:04 Beyley

Wanted to try this out myself but I ran into this on aarch64-linux:

$ zig build-exe hello.c -lc -target riscv64-linux-gnu
thread 30581 panic: access of union field 'success' while field 'failure' is active

Copying the command out with --verbose-cc set gave me this:

In file included from hello.c:1:
In file included from zig/lib/zig/libc/include/generic-glibc/stdio.h:27:
In file included from zig/lib/zig/libc/include/generic-glibc/bits/libc-header-start.h:33:
In file included from zig/lib/zig/libc/include/generic-glibc/features.h:539:
zig/lib/zig/libc/include/riscv64-linux-gnu/gnu/stubs.h:17:11: fatal error: 'gnu/stubs-lp64d.h' file not found
zig/lib/zig/libc/glibc/sysdeps/riscv/start-2.33.S:48:2: error: unknown directive
 .cfi_label .Ldummy
 ^

riscv64-linux-gnu.2.34 hits the same issue and this is with a master build of Zig.

RossComputerGuy avatar Feb 04 '24 04:02 RossComputerGuy