Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Apr 2019 10:02:34 +0000 (UTC)
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r346538 - in stable/11: lib/libc/stdlib libexec/rtld-elf
Message-ID:  <201904221002.x3MA2YGD096175@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kib
Date: Mon Apr 22 10:02:34 2019
New Revision: 346538
URL: https://svnweb.freebsd.org/changeset/base/346538

Log:
  MFC r346225:
  Fix order of destructors between main binary and libraries.

Modified:
  stable/11/lib/libc/stdlib/Symbol.map
  stable/11/lib/libc/stdlib/atexit.c
  stable/11/libexec/rtld-elf/rtld.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/lib/libc/stdlib/Symbol.map
==============================================================================
--- stable/11/lib/libc/stdlib/Symbol.map	Mon Apr 22 10:01:20 2019	(r346537)
+++ stable/11/lib/libc/stdlib/Symbol.map	Mon Apr 22 10:02:34 2019	(r346538)
@@ -129,4 +129,5 @@ FBSDprivate_1.0 {
 	_system;
 	__libc_system;
 	__cxa_thread_call_dtors;
+	__libc_atexit;
 };

Modified: stable/11/lib/libc/stdlib/atexit.c
==============================================================================
--- stable/11/lib/libc/stdlib/atexit.c	Mon Apr 22 10:01:20 2019	(r346537)
+++ stable/11/lib/libc/stdlib/atexit.c	Mon Apr 22 10:02:34 2019	(r346538)
@@ -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.

Modified: stable/11/libexec/rtld-elf/rtld.c
==============================================================================
--- stable/11/libexec/rtld-elf/rtld.c	Mon Apr 22 10:01:20 2019	(r346537)
+++ stable/11/libexec/rtld-elf/rtld.c	Mon Apr 22 10:02:34 2019	(r346538)
@@ -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_Entr
       *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_Entr
     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 *lockst
     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 *lockst
 	 */
 	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.
@@ -2981,6 +2995,11 @@ rtld_exit(void)
     if (!libmap_disable)
         lm_fini();
     lock_release(rtld_bind_lock, &lockstate);
+}
+
+static void
+rtld_nop_exit(void)
+{
 }
 
 /*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201904221002.x3MA2YGD096175>