【问题标题】:segfault before main() is called in cross-compiled armhf在交叉编译的 armhf 中调用 main() 之前的段错误
【发布时间】:2021-11-06 22:02:24
【问题描述】:

我终于在 Ubuntu Xenial x86_64 主机上交叉编译并链接了一个二进制文件,以便在 Raspberry Pi 4 的 armhf 上运行。

我的工具链来自ARM 并放在$TOOLCHAIN

我的 sysroot 是一个循环挂载的 Raspberry OS 映像,放置在 $RASPBIAN_ROOT 中。

这是一个示例编译行:

$TOOLCHAIN/gcc-arm-8.3-2019.03/bin/arm-linux-gnueabihf-g++ -std=c++11 --sysroot=$RASPBIAN_ROOT \
  -D_ARM_ -DOTHER_DEFINES \
  -v -w -fexceptions -fpermissive -pipe \
  -mcpu=cortex-a72 -mfpu=neon-vfpv4 -mfloat-abi=hard \
  -Wabi-tag -D_GLIBCXX_USE_CXX11_ABI=0 \
  -fno-use-cxa-atexit \
  -g \
  -I . -I .. -I extlib1/include -I extlib2/include -I $RASPBIAN_ROOT/usr/include/libxml2 \
  -I $TOOLCHAIN/gcc-arm-8.3-2019.03/arm-none-linux-gnueabihf/libc/usr/include \
  -I $RASPBIAN_ROOT/usr/include/arm-linux-gnueabihf \
  -I $RASPBIAN_ROOT/usr/include \
  -c File.cpp -o obj/linux/armhf/debug/File.o

还有一些.c 文件。这是连接线:

$TOOLCHAIN/gcc-arm-8.3-2019.03/bin/arm-linux-gnueabihf-ld.gold \
-L ../localdependency \
-L $RASPBIAN_ROOT/opt/vc/lib \
-L $RASPBIAN_ROOT/usr/lib/arm-linux-gnueabihf \
-L $RASPBIAN_ROOT/lib \
-L$RASPBIAN_ROOT/usr/lib \
-L $RASPBIAN_ROOT/usr/lib/gcc/arm-linux-gnueabihf/8 -o bin/linux/armhf/debug/executable obj/linux/armhf/debug/File.o ... \
$RASPBIAN_ROOT/usr/lib/gcc/arm-linux-gnueabihf/8/crtbegin.o \
$RASPBIAN_ROOT/usr/lib/gcc/arm-linux-gnueabihf/8/crtend.o \
$RASPBIAN_ROOT/usr/lib/gcc/arm-linux-gnueabihf/8/libgcc_eh.a \
--verbose --sysroot=$RASPBIAN_ROOT \
-lbcm_host -lvcos -lvchiq_arm -lcurl -lxml2 \
-lpthread -lz -lm -ldl -lstdc++ -lc -lgcc -lgcc_s

...这似乎产生了一个可执行文件。 (为便于阅读,这两行都进行了编辑。)

但是,在 pi 上运行时,我遇到了段错误:

$ ./executable param1 param2
Segmentation fault

如果我尝试调试,它甚至没有达到main()

$ gdb executable
GNU gdb (Raspbian 8.2.1-2) 8.2.1

...

(gdb) b main
Breakpoint 1 at 0x2c7c74: file main.cpp, line 7029.
(gdb) r param1 param2
Starting program: /home/user/executable param1 param2
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.

Program received signal SIGSEGV, Segmentation fault.
0xb6f97420 in __libc_enable_asynccancel () at ../nptl/cancellation.c:33
33  ../nptl/cancellation.c: No such file or directory.
(gdb) bt
#0  0xb6f97420 in __libc_enable_asynccancel () at ../nptl/cancellation.c:33
#1  0xb6f77968 in __GI___libc_write (nbytes=407, buf=0xb6fdc838 <banner>, fd=1) at ../sysdeps/unix/sysv/linux/write.c:26
#2  __GI___libc_write (fd=fd@entry=1, buf=0xb6fdc838 <banner>, nbytes=nbytes@entry=407) at ../sysdeps/unix/sysv/linux/write.c:24
#3  0xb6ec98d8 in __libc_print_version () at version.c:71
#4  __libc_main () at version.c:71
#5  0x00000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) 

虽然我看到“警告:无法找到动态链接器断点函数”。 (并且我试图弄清楚 ld.so 是从哪里来的)我更倾向于相信我的 toolchain/sysroot/libs 没有正确配置。

一个简单的 helloWorld 交叉编译并执行良好:

#include <iostream>

int main(int argc, char* argv[]) {
    int i = 1;
    std::cout << "hello world " << i << std::endl;
    return 0;
}
$ ./rpihello 
hello world 1

$ ldd rpihello 
    linux-vdso.so.1 (0xbef33000)
    /usr/lib/arm-linux-gnueabihf/libarmmem-${PLATFORM}.so => /usr/lib/arm-linux-gnueabihf/libarmmem-v7l.so (0xb6f90000)
    libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0xb6e30000)
    libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0xb6dae000)
    libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6d81000)
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6c33000)
    /lib/ld-linux-armhf.so.3 (0xb6fa5000)

编辑 1

由于nptl/cancellation.c 似乎来自 libc,我仔细检查了我是否拥有所有必要的软件包,并且我相信我有(否则我会在 Pi 遇到广泛的问题):

pi$ cat /etc/ld.so.conf.d/\*
/opt/vc/lib
# Multiarch support
/usr/local/lib/arm-linux-gnueabihf
/lib/arm-linux-gnueabihf
/usr/lib/arm-linux-gnueabihf
/usr/lib/arm-linux-gnueabihf/libfakeroot
# libc default configuration
/usr/local/lib

pi$ apt list --installed | grep ^libc
libc-ares2/now 1.14.0-1 armhf [installed,upgradable to: 1.14.0-1+deb10u1]
libc-bin/stable,now 2.28-10+rpi1 armhf [installed]
libc-dev-bin/stable,now 2.28-10+rpi1 armhf [installed,automatic]
libc-l10n/stable,now 2.28-10+rpi1 all [installed,automatic]
libc6-dbg/stable,now 2.28-10+rpi1 armhf [installed]
libc6-dev/stable,now 2.28-10+rpi1 armhf [installed]
libc6-pic/stable,now 2.28-10+rpi1 armhf [installed]
libc6/stable,now 2.28-10+rpi1 armhf [installed]
libcc1-0/stable,now 8.3.0-6+rpi1 armhf [installed,automatic]

pi$ apt list --installed | grep ^libgcc
libgcc-6-dev/stable,now 6.5.0-1+rpi1+b1 armhf [installed,automatic]
libgcc-8-dev/stable,now 8.3.0-6+rpi1 armhf [installed,automatic]
libgcc1-dbg/stable,now 1:8.3.0-6+rpi1 armhf [installed,automatic]
libgcc1/stable,now 1:8.3.0-6+rpi1 armhf [installed]

所以我仍然认为我以某种方式破坏了交叉编译/链接。


编辑 2

我已经设法在树莓派上编译了我的代码库,并且生成的二进制文件显示出与交叉编译的结果相同的结果。所以要么我编译错误,要么我的 sysroot 和 pi 的系统都有点无聊。


编辑 3

rpi $ readelf -dW myexecutable

Dynamic section at offset 0x664e58 contains 48 entries:
  Tag        Type                         Name/Value
 0x00000003 (PLTGOT)                     0x6716d4
 0x00000002 (PLTRELSZ)                   5856 (bytes)
 0x00000017 (JMPREL)                     0x2d940
 0x00000014 (PLTREL)                     REL
 0x00000011 (REL)                        0x2d2e0
 0x00000012 (RELSZ)                      1632 (bytes)
 0x00000013 (RELENT)                     8 (bytes)
 0x00000015 (DEBUG)                      0x0
 0x00000006 (SYMTAB)                     0x8148
 0x0000000b (SYMENT)                     16 (bytes)
 0x00000005 (STRTAB)                     0xfb48
 0x0000000a (STRSZ)                      94185 (bytes)
 0x6ffffef5 (GNU_HASH)                   0x26b34
 0x00000004 (HASH)                       0x2930c
 0x00000001 (NEEDED)                     Shared library: [libbcm_host.so]
 0x00000001 (NEEDED)                     Shared library: [libvcos.so]
 0x00000001 (NEEDED)                     Shared library: [libvchiq_arm.so]
 0x00000001 (NEEDED)                     Shared library: [libjson.so]
 0x00000001 (NEEDED)                     Shared library: [libmylocallib.so]
...
 0x00000001 (NEEDED)                     Shared library: [libpthread.so.0]
 0x00000001 (NEEDED)                     Shared library: [libz.so.1]
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libdl.so.2]
 0x00000001 (NEEDED)                     Shared library: [libstdc++.so.6]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x00000019 (INIT_ARRAY)                 0x66dd60
 0x0000001b (INIT_ARRAYSZ)               244 (bytes)
 0x0000001a (FINI_ARRAY)                 0x66de54
 0x0000001c (FINI_ARRAYSZ)               4 (bytes)
 0x6ffffff0 (VERSYM)                     0x2c1b0
 0x6ffffffe (VERNEED)                    0x2d0f0
 0x6fffffff (VERNEEDNUM)                 9
 0x00000000 (NULL)                       0x0

我觉得这很有趣:

rpi $ file myexecutable
myexecutable: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /usr/lib/libc.so.1, with debug_info, not stripped
rpi $ file `which ls`
/bin/ls: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 3.2.0, BuildID[sha1]=67a394390830ea3ab4e83b5811c66fea9784ee69, stripped

我的可执行文件不应该使用 ld 作为解释器吗?


编辑 4

added[2] --dynamic-linker=/lib/ld-linux-armhf.so.3 到我的链接器行,但现在我没有堆栈:

Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) bt
#0  0x00000000 in ?? ()
(gdb) 

运行strace executable最后几行是:

rt_sigprocmask(SIG_BLOCK, NULL, ~[ILL TRAP BUS FPE KILL SEGV STOP RTMIN RT_1], 8) = 0
rt_sigaction(SIGILL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0xb5e55120}, NULL, 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
clock_gettime(CLOCK_MONOTONIC_COARSE, {tv_sec=6336, tv_nsec=768744979}) = 0
gettimeofday({tv_sec=1631533208, tv_usec=497816}, NULL) = 0
openat(AT_FDCWD, "/dev/urandom", O_RDONLY) = 3
read(3, "\314\4bJn\36\16\240f\203\216I\257\306\251r\202\v\232\263\210\374\222\276TN\33,[\3A\233", 32) = 32
close(3)                                = 0
openat(AT_FDCWD, "/dev/urandom", O_RDONLY) = 3
read(3, "\301\231\203~G6Ey_\262Y\241\34\216F\2448w\2365Z\213\376\223\260\322zwo\334+e", 32) = 32
close(3)                                = 0
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---
+++ killed by SIGSEGV +++
Segmentation fault

/dev/urandom 确实存在。


编辑 5

$ readelf -h executable
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          40514564 (bytes into file)
  Flags:                             0x5000400, Version5 EABI, hard-float ABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         8
  Size of section headers:           40 (bytes)
  Number of section headers:         37
  Section header string table index: 36

我认为入口点地址不应该是 0x0。


编辑 6

我已经在 pi3 和 arm-qemu chroot 上编译了代码 - 结果相同。

【问题讨论】:

  • readelf -dW executable 的输出是什么? (您可能必须使用readelf 的跨工具版本。)
  • @FlorianWeimer 我在 pi 上运行了readelf。我觉得解释器的差异很奇怪。
  • 从交叉编译转移到原生构建是好的。我希望您使用不同的构建命令/选项,也许在编辑中添加它们?您显示的选项比我认为必要的多 10 倍。当您进行本地构建时,还请告诉我们您拥有什么操作系统(例如 Raspbian 还是什么?32 位还是 64 位?)。请注意,如果您的可执行文件(或某些子集)可以使用您的操作系统的股票 gcc 构建,而无需自定义 sysroot,并且可能是静态链接的,例如gcc 文件.cpp -o 文件.o ; ./File.o 这可以让你找到一个工作方向,祝你好运。
  • @JoeKul 在树莓派上使用 Raspbian buster 本地编译是不切实际的(试图支持所有)。我正在尝试在我的开发 x86_64 Ubuntu Xenial VM 中设置一个 armhf chroot。初步测试看起来很有希望。

标签: gcc raspberry-pi linker arm cross-compiling


【解决方案1】:

我找到的解决方案是使用来自 raspberrypi.org 的 toolchain 进行交叉编译,并将 --sysroot= 设置为 Raspbian 映像。他们的releases 或多或少地遵循 Debian,并且始终支持所有现有的 Raspberry Pie。
CXXLD 都指向它们的 g++,这意味着由 GCC 确定链接标志。如有必要,我可以使用-Wl,..
除了-Wabi-tag -D_GLIBCXX_USE_CXX11_ABI=0CFLAGS 还挺标准的。


Raspbian 在 Buster 10.3 (2020-02-13) 和 10.4 (2020-05-28) 之间更改了下载 URL 格式,并开始提供完整版和精简版。我用的是后者。
由于这些映像通常几乎没有可用空间,我首先extend 映像文件、分区 (p2) 和底层文件系统,因此我可以通过 quemu chroot 到 sysroot 来安装满足我项目依赖关系的 -dev 包-手臂。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多