HardenedBSD/libexec/rtld-elf
Konstantin Belousov 760e34772c Fix order of destructors between main binary and libraries.
Since inits for the main binary are run from rtld (for some time), the
rtld_exit atexit(3) handler, which is passed from rtld to the program
entry and installed by csu, is installed after any atexit(3) handlers
installed by main binary constructors.  This means that rtld_exit() is
fired before main binary handlers.

Typical C++ static constructors are executed from init (either binary
or libs) but use atexit(3) to ensure that destructors are called in
the right order, independent of the linking order.  Also, C++
libraries finalizers call __cxa_finalize(3) to flush library'
atexit(3) entries.  Since atexit(3) entry is cleared after being run,
this would be mostly innocent, except that, atexit(rtld_exit) done
after main binary constructors, makes destructors from libraries
executed before destructors for main.

Fix by reordering atexit(rtld_exit) before inits for main binary, same
as it happened when inits were called by csu.  Do it using new private
libc symbol with pre-defined ABI.

Reported. tested, and reviewed by:	kan
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2019-04-15 13:03:09 +00:00
..
aarch64
amd64
arm
i386
mips
powerpc
powerpc64
riscv
sparc64
tests
debug.c
debug.h
libmap.c
libmap.conf
libmap.h
Makefile
Makefile.depend
map_object.c
paths.h
rtld_lock.c
rtld_lock.h
rtld_malloc.c
rtld_malloc.h
rtld_printf.c
rtld_printf.h
rtld_tls.h
rtld_utrace.h
rtld.1
rtld.c
rtld.h
Symbol.map
xmalloc.c