From 6714e2d127506a8a30c55ddbafcdb27e88df7762 Mon Sep 17 00:00:00 2001 From: kib Date: Mon, 22 Apr 2019 10:02:34 +0000 Subject: [PATCH] MFC r346225: Fix order of destructors between main binary and libraries. --- lib/libc/stdlib/Symbol.map | 1 + lib/libc/stdlib/atexit.c | 1 + libexec/rtld-elf/rtld.c | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map index 65a0ee9abf7..8682901bca6 100644 --- a/lib/libc/stdlib/Symbol.map +++ b/lib/libc/stdlib/Symbol.map @@ -129,4 +129,5 @@ FBSDprivate_1.0 { _system; __libc_system; __cxa_thread_call_dtors; + __libc_atexit; }; diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index eda57032f67..65dc633048e 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -140,6 +140,7 @@ atexit(void (*func)(void)) error = atexit_register(&fn); return (error); } +__weak_reference(atexit, __libc_atexit); /** * Register a block to be performed at exit. diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index b0fbff0f3c5..d120008b62a 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -140,6 +140,7 @@ static int rtld_dirname(const char *, char *); static int rtld_dirname_abs(const char *, char *); static void *rtld_dlopen(const char *name, int fd, int mode); static void rtld_exit(void); +static void rtld_nop_exit(void); static char *search_library_path(const char *, const char *, const char *, int *); static char *search_library_pathfds(const char *, const char *, int *); @@ -278,6 +279,8 @@ char *ld_path_rtld = _PATH_RTLD; char *ld_standard_library_path = STANDARD_LIBRARY_PATH; char *ld_env_prefix = LD_; +static void (*rtld_exit_ptr)(void); + /* * Fill in a DoneList with an allocation large enough to hold all of * the currently-loaded objects. Keep this as a macro since it calls @@ -760,6 +763,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) *ld_bind_now != '\0', SYMLOOK_EARLY, &lockstate) == -1) rtld_die(); + rtld_exit_ptr = rtld_exit; if (obj_main->crt_no_init) preinit_main(); objlist_call_init(&initlist, &lockstate); @@ -782,7 +786,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) dbg("transferring control to program entry point = %p", obj_main->entry); /* Return the exit procedure and the program entry point. */ - *exit_proc = rtld_exit; + *exit_proc = rtld_exit_ptr; *objp = obj_main; return (func_ptr_type) obj_main->entry; } @@ -2641,6 +2645,7 @@ objlist_call_init(Objlist *list, RtldLockState *lockstate) Obj_Entry *obj; char *saved_msg; Elf_Addr *init_addr; + void (*reg)(void (*)(void)); int index; /* @@ -2669,7 +2674,16 @@ objlist_call_init(Objlist *list, RtldLockState *lockstate) */ elm->obj->init_done = true; hold_object(elm->obj); + reg = NULL; + if (elm->obj == obj_main && obj_main->crt_no_init) { + reg = (void (*)(void (*)(void)))get_program_var_addr( + "__libc_atexit", lockstate); + } lock_release(rtld_bind_lock, lockstate); + if (reg != NULL) { + reg(rtld_exit); + rtld_exit_ptr = rtld_nop_exit; + } /* * It is legal to have both DT_INIT and DT_INIT_ARRAY defined. @@ -2983,6 +2997,11 @@ rtld_exit(void) lock_release(rtld_bind_lock, &lockstate); } +static void +rtld_nop_exit(void) +{ +} + /* * Iterate over a search path, translate each element, and invoke the * callback on the result. -- 2.45.0