【问题标题】:Statically Linked Binaries - Segmentation fault静态链接的二进制文件 - 分段错误
【发布时间】:2017-03-06 02:08:07
【问题描述】:

我正在尝试为 net-tools-1.60 构建静态二进制文件,以便它们可以在任何系统上自行运行。我从 SourceForge 下载了 net-tools 的源代码并应用了 3 个补丁。然后我用make SHARED=0 CC='gcc -static'编译成功。编译后的二进制文件显示为“静态链接”并且有些工作,但有些得到“分段错误”。主要是 arp 和 route 不起作用。我也试过LDFLAGS="--static" make -j 4,但生成的二进制文件仍然显示“动态链接”。

这是在带有 4.4.0-64-generic 内核的 Ubuntu 16.04 上。任何想法如何让它工作?

警告

requires at runtime the shared libraries from the glibc version used for linking
/home/user/Desktop/net-tools-1.60/lib/inet.c:404: warning: Using 'setprotoent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/user/Desktop/net-tools-1.60/lib/inet.c:414: warning: Using 'endprotoent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/user/Desktop/net-tools-1.60/lib/inet.c:386: warning: Using 'getservent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/user/Desktop/net-tools-1.60/lib/inet.c:385: warning: Using 'setservent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/user/Desktop/net-tools-1.60/lib/inet.c:403: warning: Using 'endservent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

崩溃

user@ubuntu:~/Desktop/net-tools-1.60$ file arp
arp: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=fc19dbe5121b2a3eb7aee3e6e0fc8de7490d6263, not stripped
user@ubuntu:~/Desktop/net-tools-1.60$ ./arp 
Segmentation fault (core dumped)

GDB

(gdb) run
Starting program: /home/user/Desktop/net-tools-1.60/arp 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) where
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff6c83fa9 in __pthread_initialize_minimal_internal () at nptl-init.c:471
#2  0x00007ffff6c83589 in _init () at ../sysdeps/x86_64/crti.S:72
#3  0x00007ffff70a4670 in ?? () from /lib/x86_64-linux-gnu/libnss_myhostname.so.2
#4  0x00000000004acd5a in call_init.part ()
#5  0x00000000004acf15 in _dl_init ()
#6  0x00000000004999a5 in dl_open_worker ()
#7  0x0000000000497164 in _dl_catch_error ()
#8  0x0000000000499309 in _dl_open ()
#9  0x00000000004563d2 in do_dlopen ()
#10 0x0000000000497164 in _dl_catch_error ()
#11 0x00000000004565be in __libc_dlopen_mode ()
#12 0x0000000000451e6d in __nss_next2 ()
#13 0x000000000044dbac in gethostbyaddr_r ()
#14 0x000000000044d9be in gethostbyaddr ()
#15 0x00000000004031f2 in INET_rresolve (name=name@entry=0x6e68e0 <buff> "", sin=0x7fffffffd440, 
    numeric=<optimized out>, netmask=netmask@entry=4294967040, len=128) at inet.c:200
#16 0x0000000000403354 in INET_sprint (sap=<optimized out>, numeric=<optimized out>) at inet.c:246
#17 0x0000000000401877 in arp_show (name=0x0) at arp.c:581
#18 0x0000000000400b53 in main (argc=1, argv=0x7fffffffe008) at arp.c:768
(gdb)

更新 所以我只是在较旧的 Ubuntu 11.10 32 位上构建了这些工具。生成的二进制文件在我的 Ubuntu 16.04 64 位上运行良好。我认为 16.04 中可能存在错误。

【问题讨论】:

    标签: compilation segmentation-fault static-libraries static-linking


    【解决方案1】:

    编译后的二进制文件显示为“静态链接”并且有些工作,但有些得到“分段错误”

    这是预期的结果(假设崩溃发生在系统不是构建它们的系统上)。

    与普遍的看法相反,Linux 上的完全静态库比动态链接库的可移植性更少。

    您应该收到如下所示的链接时警告:

    warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
    

    Explanation.

    更新:

    在构建程序的同一系统上预计不会发生崩溃。此崩溃看起来像 GLIBC bug。

    最终结果是一样的:不要静态链接;不会有好的结果。

    【讨论】:

    • 是的,有一些警告。可以修复吗?这个想法是从外部磁盘运行工具而不接触它们将运行的系统。顺便说一句,崩溃发生在我正在构建的同一系统上。尚未在另一台计算机上尝试过。
    • @user1781482 same 系统不会发生崩溃。编辑您的帖子以显示来自 GDB 的崩溃堆栈跟踪。
    • 我不确定如何查看崩溃堆栈跟踪。我不是专业人士。我怎么做?谢谢。
    • @user1781482 尝试gdb ./arp,然后尝试run。一旦 GDB 以 SIGSEGV 停止,使用 where 命令,并使用 GDB 会话的记录编辑您的问题。
    • 那么还有其他选择吗?我正在尝试获得一个完全从外部驱动器运行的“便携式”工具,独立于系统
    猜你喜欢
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多