来自glibc的atexit()吗?

时间:2019-07-12 21:58:27

标签: glibc

我正在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。

2 个答案:

答案 0 :(得分:0)

  

这使我相信从glibc.a中调用此函数是无效的。

您的结论不正确。实际上,GLIBC本身已经从(例如)呼叫atexit__gmon_start__中的csu/gmon-start.c

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

由于ld-linux必须是完全独立的(不能有任何未解决的依赖项),因此请务必不要在其中添加任何依赖项。

答案 1 :(得分:0)

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的符号链接,则说明您的系统安装已损坏,应该考虑重新安装。