【问题标题】:atexit() from within glibc?来自glibc的atexit()?
【发布时间】:2019-07-12 21:58:27
【问题描述】:

我正在 glibc 中编写代码,在程序退出之前我需要一个钩子。

#include <stdlib.h>
#include <stdio.h>

void dump_statistics(void){
...
}

void init(void){
...
atexit(dump_statistics);
...
}

但是当我编译它时,它在链接时失败

source.c:10: undefined reference to `atexit'
collect2: error: ld returned 1 exit status

这让我相信从 glibc 中调用这个函数是无效的。虽然我可以从包含的标头中看到声明,但我无法在内部获得此函数的定义。这个函数在 glibc 中有不同的名称还是在内部使用了不同的语义?

补充信息:

我可以确认 atexit.oS 出现在 libc_nonshared.a 中:

$ nm -an libc_nonshared.a
...
atexit.oS:
                 U __cxa_atexit
                 w __dso_handle
                 U _GLOBAL_OFFSET_TABLE_
0000000000000000 T atexit
0000000000000000 a atexit.c
0000000000000000 b .bss
0000000000000000 n .comment
0000000000000000 d .data
0000000000000000 N .debug_abbrev
0000000000000000 N .debug_aranges
0000000000000000 N .debug_info
0000000000000000 N .debug_line
0000000000000000 N .debug_loc
0000000000000000 N .debug_str
0000000000000000 r .eh_frame
0000000000000000 n .note.GNU-stack
0000000000000000 t .text
0000000000000000 t .text.unlikely
...

libc.so 包含:

$ nm -an libc.so | grep atexit
0000000000000000 a cxa_atexit.c
0000000000000000 a cxa_thread_atexit_impl.c
0000000000000000 a old_atexit.c
0000000000035ec0 t __internal_atexit
0000000000035f10 T __cxa_atexit
0000000000035f10 t __GI___cxa_atexit
00000000000360e0 T __cxa_thread_atexit_impl
00000000003988d8 d __elf_set___libc_atexit_element__IO_cleanup__
00000000003988d8 d __libc_atexit
00000000003988d8 d __start___libc_atexit
00000000003988e0 d __stop___libc_atexit
000000000039e8d0 b added_atexit_handler.9386

我没有 ld-linux.so 工件。

链接失败的命令:

make[4]: Entering directory '/root_dir/glibc-2.23/time'
make[4]: Leaving directory '/root_dir/glibc-2.23/time'
make[3]: Leaving directory '/root_dir/glibc-2.23/elf'
gcc   -nostdlib -nostartfiles -r -o /root_dir/glibc-2.23_build/libc_pic.os \
 -Wl,-d -Wl,--whole-archive /root_dir/glibc-2.23_build/libc_pic.a -o /root_dir/glibc-2.23_build/libc_pic.os
gcc   -shared -static-libgcc -Wl,-O1  -Wl,-z,defs -Wl,-dynamic-linker=/root_dir/glibc-2.23_install/lib/ld-linux-x86-64.so.2  -B/root_dir/glibc-2.23_build/csu/  -Wl,--version-script=/root_dir/glibc-2.23_build/libc.map -Wl,-soname=libc.so.6 -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both -nostdlib -nostartfiles -e __libc_main -L/root_dir/glibc-2.23_build -L/root_dir/glibc-2.23_build/math -L/root_dir/glibc-2.23_build/elf -L/root_dir/glibc-2.23_build/dlfcn -L/root_dir/glibc-2.23_build/nss -L/root_dir/glibc-2.23_build/nis -L/root_dir/glibc-2.23_build/rt -L/root_dir/glibc-2.23_build/resolv -L/root_dir/glibc-2.23_build/crypt -L/root_dir/glibc-2.23_build/mathvec -L/root_dir/glibc-2.23_build/nptl -Wl,-rpath-link=/root_dir/glibc-2.23_build:/root_dir/glibc-2.23_build/math:/root_dir/glibc-2.23_build/elf:/root_dir/glibc-2.23_build/dlfcn:/root_dir/glibc-2.23_build/nss:/root_dir/glibc-2.23_build/nis:/root_dir/glibc-2.23_build/rt:/root_dir/glibc-2.23_build/resolv:/root_dir/glibc-2.23_build/crypt:/root_dir/glibc-2.23_build/mathvec:/root_dir/glibc-2.23_build/nptl -o /root_dir/glibc-2.23_build/libc.so -T /root_dir/glibc-2.23_build/shlib.lds /root_dir/glibc-2.23_build/csu/abi-note.o /root_dir/glibc-2.23_build/elf/soinit.os /root_dir/glibc-2.23_build/libc_pic.os /root_dir/glibc-2.23_build/elf/sofini.os /root_dir/glibc-2.23_build/elf/interp.os /root_dir/glibc-2.23_build/elf/ld.so -lgcc
/root_dir/glibc-2.23_build/libc_pic.os: In function `soa_arena_init':
/root_dir/glibc-2.23/malloc/small-object-arena.c:103: undefined reference to `atexit'
collect2: error: ld returned 1 exit status
../Makerules:681: recipe for target '/root_dir/glibc-2.23_build/libc.so' failed
make[2]: *** [/root_dir/glibc-2.23_build/libc.so] Error 1
make[2]: Leaving directory '/root_dir/glibc-2.23/elf'
Makefile:214: recipe for target 'elf/subdir_lib' failed
make[1]: *** [elf/subdir_lib] Error 2
make[1]: Leaving directory '/root_dir/glibc-2.23'
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 2

small-object-arena.c 是我添加的一个文件,它包含我尝试从中调用 atexit 的 init 函数;该文件被合并到 glibc 的 malloc 子系统中,并在第一次使用顶级内存管理 API(例如 malloc、realloc、calloc 等)时调用 init。

【问题讨论】:

    标签: glibc


    【解决方案1】:

    这让我相信从 glibc.a 中调用这个函数是无效的

    你的结论是错误的。事实上,GLIBC 本身已经调用了atexit,例如__gmon_start__csu/gmon-start.c.

    问题可能是libc.a 的一小部分链接到ld-linux.so,而该子集 不包括atexit。您修改为调用atexit 的代码可能包含在ld-linux 中。

    由于ld-linux 必须是完全自包含的(不能有任何未解析的依赖项),所以必须小心不要向其添加任何依赖项。

    【讨论】:

    • 我在问题中添加了信息。你能解释一下 glibc 如何确定哪些编译函数放入哪个二进制文件中吗?将我对atexit() 的调用替换为__internal_atexit(),必要时添加extern 声明会不会出错?
    • @Andy 请显示导致链接失败的实际链接命令。
    • 添加到问题
    • 可能的解决方案是调用 __cxa_atexit() 代替例如extern void *__dso_handle __attribute__ ((__weak__)); __cxa_atexit ((void (*)(void *))dump_statistics, NULL, &amp;__dso_handle ? __dso_handle : NULL); 我从\malloc\mtrace.c:mtrace(void) 克隆了这个电话。这可以干净地编译并提供所需的行为。您认为这种方法有什么问题吗?你会推荐这个吗?
    【解决方案2】:

    atexit 是在 libc_nonshared.a 中定义的,而不是在 libc.so.6 中定义的。为了找到它,链接编辑器需要一个链接器脚本libc.so,它通常看起来像这样:

    /* GNU ld script
       Use the shared library, but some functions are only in
       the static library, so try that secondarily.  */
    OUTPUT_FORMAT(elf64-x86-64)
    GROUP (
      /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libc_nonshared.a
      AS_NEEDED ( /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 )
    )
    

    (此示例来自 Debian,因此是多架构路径,并且为了便于阅读而进行了包装。)

    如果libc.so 是指向libc.so.6 的符号链接,则您的系统安装已损坏,您应该考虑重新安装。

    【讨论】:

    • 他正在修改 glibc 本身。我不认为这个答案是相关的。
    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多