Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Apr 2019 10:01:20 +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-12@freebsd.org
Subject:   svn commit: r346537 - in stable/12: lib/libc/stdlib libexec/rtld-elf
Message-ID:  <201904221001.x3MA1K4d092158@repo.freebsd.org>

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

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

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

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

Modified: stable/12/lib/libc/stdlib/atexit.c
==============================================================================
--- stable/12/lib/libc/stdlib/atexit.c	Mon Apr 22 08:58:33 2019	(r346536)
+++ stable/12/lib/libc/stdlib/atexit.c	Mon Apr 22 10:01:20 2019	(r346537)
@@ -142,6 +142,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/12/libexec/rtld-elf/rtld.c
==============================================================================
--- stable/12/libexec/rtld-elf/rtld.c	Mon Apr 22 08:58:33 2019	(r346536)
+++ stable/12/libexec/rtld-elf/rtld.c	Mon Apr 22 10:01:20 2019	(r346537)
@@ -151,6 +151,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 *);
@@ -295,6 +296,8 @@ const char *ld_path_rtld = _PATH_RTLD;
 const char *ld_standard_library_path = STANDARD_LIBRARY_PATH;
 const 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
@@ -756,6 +759,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);
@@ -778,7 +782,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;
 }
@@ -2657,6 +2661,7 @@ objlist_call_init(Objlist *list, RtldLockState *lockst
     Obj_Entry *obj;
     char *saved_msg;
     Elf_Addr *init_addr;
+    void (*reg)(void (*)(void));
     int index;
 
     /*
@@ -2685,7 +2690,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.
@@ -2997,6 +3011,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?201904221001.x3MA1K4d092158>