I am writing code inside glibc and I need a hook before the program exits.
#include <stdlib.h>
#include <stdio.h>
void dump_statistics(void){
...
}
void init(void){
...
atexit(dump_statistics);
...
}
However when I compile this, it fails at link time
source.c:10: undefined reference to `atexit'
collect2: error: ld returned 1 exit status
This leads me to believe that it is not valid to call this function from within glibc. While I can see the declaration from the included header, I cannot get a definition for this function internally. Is there a different name for this function from within glibc or is a different semantic used internally?
Added Information:
I can confirm that atexit.oS is appearing in 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 contains:
$ 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
I don't have an ld-linux.so artifact.
Linking command that is failing:
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 is a file I have added and it contains the init function I am trying call atexit from; this file is incorporated into the malloc subsystem of glibc and init is called from the first use of the top level memory management API (e.g. malloc, realloc, calloc, etc.).
This leads me to believe that it is not valid to call this function from within glibc.a
Your conclusion is incorrect. In fact, GLIBC itself already calls atexit
from e.g. __gmon_start__
in csu/gmon-start.c
.
The issue likely is that a small subset of libc.a
is linked into ld-linux.so
, and that subset doesn't include atexit
. It is likely that the code you modified to call atexit
is included in ld-linux
.
As ld-linux
must be completely self-contained (can't have any unresolved dependencies), you must be careful to not add any dependencies to it.