无法使用 Openwrt 工具链构建静态二进制文件:未定义对 _Unwind_Resume 和 __gcc_personality_v0 的引用

Unable to build static binaries with Openwrt toolchain : Undefined reference to _Unwind_Resume and __gcc_personality_v0

我无法使用 openwrt 工具链构建静态二进制文件。

# staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/bin/i486-openwrt-linux-gnu-gcc -o /tmp/main /tmp/main.c -static
/tmp/main.c: In function 'main':
/tmp/main.c:3:2: warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]
  printf("Hello world");
  ^
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iofclose.o): In function `_IO_new_fclose':
iofclose.c:(.text+0x1a6): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iofclose.o):(.eh_frame+0x13): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iofflush.o): In function `_IO_fflush':
iofflush.c:(.text+0xe9): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iofflush.o):(.eh_frame+0x13): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(wfileops.o): In function `_IO_wfile_underflow':
wfileops.c:(.text+0x4e6): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(wfileops.o):(.eh_frame+0x5b): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(fileops.o): In function `_IO_new_file_underflow':
fileops.c:(.text+0x361): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(fileops.o):(.eh_frame+0xe7): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(backtrace.o): In function `backtrace_helper':
backtrace.c:(.text+0x21): undefined reference to `_Unwind_GetIP'
backtrace.c:(.text+0x43): undefined reference to `_Unwind_GetGR'
backtrace.c:(.text+0x4e): undefined reference to `_Unwind_GetCFA'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(backtrace.o): In function `__backtrace':
backtrace.c:(.text+0x9a): undefined reference to `_Unwind_Backtrace'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iofputs.o): In function `_IO_fputs':
iofputs.c:(.text+0x107): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iofputs.o):(.eh_frame+0x13): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iogetdelim.o): In function `_IO_getdelim':
iogetdelim.c:(.text+0x22b): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(iogetdelim.o):(.eh_frame+0x13): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(fseek.o): In function `fseek':
fseek.c:(.text+0xe2): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(fseek.o):(.eh_frame+0x13): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(ftello.o): In function `__ftello':
ftello.c:(.text+0x13c): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(ftello.o):(.eh_frame+0x13): undefined reference to `__gcc_personality_v0'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(ioseekoff.o): In function `_IO_seekoff':
ioseekoff.c:(.text+0x1ab): undefined reference to `_Unwind_Resume'
/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/../../../../i486-openwrt-linux-gnu/lib/libc.a(ioseekoff.o):(.eh_frame+0x67): undefined reference to `__gcc_personality_v0'
collect2: error: ld returned 1 exit status

我的主机 gcc 是 Debian 8 上的 4.9.2 版。 我使用以下配置构建了 openwrt 工具链:

GCC 编译器版本:gcc 4.8.x 具有 Linaro 增强功能

C 库实现:使用 (e)glibc

(e)glibc 版本:glibc 2.21

工具链信息:

# staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/bin/i486-openwrt-linux-gnu-gcc -v
Reading specs from /home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib/gcc/i486-openwrt-linux-gnu/4.8.3/specs
COLLECT_GCC=staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/bin/i486-openwrt-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/libexec/gcc/i486-openwrt-linux-gnu/4.8.3/lto-wrapper
Target: i486-openwrt-linux-gnu
Configured with: /home/elison/cop-poc/copernicus-openwrt/build_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/gcc-linaro-4.8-2014.04/configure --with-bugurl=https://dev.openwrt.org/ --with-pkgversion='OpenWrt/Linaro GCC 4.8-2014.04 unknown' --prefix=/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21 --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=i486-openwrt-linux-gnu --with-gnu-ld --enable-target-optspace --disable-libgomp --disable-libmudflap --disable-multilib --disable-nls --with-host-libstdcxx=-lstdc++ --with-gmp=/home/elison/cop-poc/copernicus-openwrt/staging_dir/host --with-mpfr=/home/elison/cop-poc/copernicus-openwrt/staging_dir/host --with-mpc=/home/elison/cop-poc/copernicus-openwrt/staging_dir/host --disable-decimal-float --disable-libssp --enable-__cxa_atexit --with-headers=/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/include --disable-libsanitizer --enable-languages=c,c++ --enable-shared --enable-threads --with-slibdir=/home/elison/cop-poc/copernicus-openwrt/staging_dir/toolchain-i386_i686_gcc-4.8-linaro_glibc-2.21/lib
Thread model: posix
gcc version 4.8.3 (OpenWrt/Linaro GCC 4.8-2014.04 unknown) 

这里提到的答案没有帮助:

添加 --enable-static 作为额外的 gcc 配置选项也没有帮助。

正如在 Internet 上的其他地方发现的那样,将 -lgcc_eh 添加到链接器标志也无济于事。

aarch64-openwrt-linux-gcc thread.c -static -lpthread -lc -lgcc_eh

有效