Problem linking statically with RISCV linux/glibc toolchain

803 views
Skip to first unread message

Mandeep Singh Grang

unread,
Nov 15, 2017, 2:09:09 PM11/15/17
to RISC-V SW Dev, pal...@dabbelt.com, a...@asbradbury.org, apa...@codeaurora.org
Hi,

I am having problems linking statically using the RISCV linux/glibc toolchain.

I followed the instructions at: https://github.com/riscv/riscv-gnu-toolchain to build a RISCV linux/glibc toolchain.
I then used clang to compile a simple program:

> echo "void main() {}" > a.c
> clang -target riscv32 a.c -nostdlib -static

 Here's my link line:

> GCC=<Path to RISCV linux/glibc toolchain>
> "$GCC/bin/riscv32-ld" --sysroot=$GCC/sysroot $GCC/sysroot/usr/lib32/ilp32/crt1.o $GCC/lib/gcc/riscv32-unknown-linux-gnu/7.2.0/crtbegin.o -L$GCC/sysroot/usr/lib32/ilp32 -L$GCC/lib/gcc/riscv32/7.2.0 -L$GCC/sysroot/lib a.o --start-group -lc --end-group -lgcc $GCC/lib/gcc/riscv32-unknown-linux-gnu/7.2.0/crtend.o -o a.out

This always produces a dynamic exe.

> file a.out
a.out: ELF 32-bit LSB executable, UCB RISC-V, version 1 (SYSV), dynamically linked, interpreter /lib32/ld.so.1, for GNU/Linux 3.0.0, not stripped

Forcing -static on the link line produces undefined reference errors:

> "$GCC/bin/riscv32-ld" -static --sysroot=$GCC/sysroot $GCC/sysroot/usr/lib32/ilp32/crt1.o $GCC/lib/gcc/riscv32-unknown-linux-gnu/7.2.0/crtbegin.o -L$GCC/sysroot/usr/lib32/ilp32 -L$GCC/lib/gcc/riscv32/7.2.0 -L$GCC/sysroot/lib a.o --start-group -lc --end-group -lgcc $GCC/lib/gcc/riscv32-unknown-linux-gnu/7.2.0/crtend.o -o a.out

ERROR: riscv-gnu-toolchain/riscv-glibc/libio/iofclose.c:73: undefined reference to `_Unwind_Resume'

However, with the RISCV elf/newlib toolchain I am able to link statically without problems.

Can someone please help?

--Mandeep

Jim Wilson

unread,
Nov 15, 2017, 2:51:17 PM11/15/17
to Mandeep Singh Grang, RISC-V SW Dev, Palmer Dabbelt, a...@asbradbury.org, apa...@codeaurora.org
On Wed, Nov 15, 2017 at 11:09 AM, Mandeep Singh Grang <mgr...@codeaurora.org> wrote:
Forcing -static on the link line produces undefined reference errors:

> "$GCC/bin/riscv32-ld" -static --sysroot=$GCC/sysroot $GCC/sysroot/usr/lib32/ilp32/crt1.o $GCC/lib/gcc/riscv32-unknown-linux-gnu/7.2.0/crtbegin.o -L$GCC/sysroot/usr/lib32/ilp32 -L$GCC/lib/gcc/riscv32/7.2.0 -L$GCC/sysroot/lib a.o --start-group -lc --end-group -lgcc $GCC/lib/gcc/riscv32-unknown-linux-gnu/7.2.0/crtend.o -o a.out

ERROR: riscv-gnu-toolchain/riscv-glibc/libio/iofclose.c:73: undefined reference to `_Unwind_Resume'

It is unwise to call the linker directly, as correct linking requires a lot of special options.  This knowledge is encoded in gcc.  If you run gcc with -static and -v you will see the linker options and libraries you need for correct linking.  You are missing a -lgcc_eh here for static linking.  Dynamic linking uses -lgcc_s, but since the shared libc was linked against that, it may be found even if you don't explicitly mention it on the link line.  I'm not familiar with clang, but it is probably similar.

Jim


Reply all
Reply to author
Forward
0 new messages