The ld.so bits have been committed last week.  So here is a new
version of the diff.  This version exports __cxa_thread_atexit_impl
such that gcc picks up our implementation.  __cxa_thread_atexit
becomes a weak alias such that static linking succeeds.  I rebuilt the
gcc 4.9 port with this diff and verified that it does indeed run the
new code.

ok?  anybody who wants to ride the libc minor bump?


Index: lib/libc/Symbols.list
===================================================================
RCS file: /cvs/src/lib/libc/Symbols.list,v
retrieving revision 1.61
diff -u -p -r1.61 Symbols.list
--- lib/libc/Symbols.list       4 Nov 2017 22:53:57 -0000       1.61
+++ lib/libc/Symbols.list       4 Dec 2017 19:04:51 -0000
@@ -1466,6 +1466,8 @@ random
 /* stdlib */
 _Exit
 __cxa_atexit
+__cxa_thread_atexit
+__cxa_thread_atexit_impl
 __cxa_finalize
 __isthreaded
 a64l
Index: lib/libc/shlib_version
===================================================================
RCS file: /cvs/src/lib/libc/shlib_version,v
retrieving revision 1.195
diff -u -p -r1.195 shlib_version
--- lib/libc/shlib_version      4 Nov 2017 22:53:57 -0000       1.195
+++ lib/libc/shlib_version      4 Dec 2017 19:04:51 -0000
@@ -1,4 +1,4 @@
 major=92
-minor=0
+minor=1
 # note: If changes were made to include/thread_private.h or if system
 # calls were added/changed then librthread/shlib_version also be updated.
Index: lib/libc/include/thread_private.h
===================================================================
RCS file: /cvs/src/lib/libc/include/thread_private.h,v
retrieving revision 1.32
diff -u -p -r1.32 thread_private.h
--- lib/libc/include/thread_private.h   4 Nov 2017 22:53:57 -0000       1.32
+++ lib/libc/include/thread_private.h   4 Dec 2017 19:04:52 -0000
@@ -394,6 +394,7 @@ void        _spinunlock(volatile _atomic_lock_t
 void   _rthread_debug(int, const char *, ...)
                __attribute__((__format__ (printf, 2, 3)));
 pid_t  _thread_dofork(pid_t (*_sys_fork)(void));
+void   _thread_finalize(void);
 
 /*
  * Threading syscalls not declared in system headers
Index: lib/libc/stdlib/atexit.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/atexit.c,v
retrieving revision 1.24
diff -u -p -r1.24 atexit.c
--- lib/libc/stdlib/atexit.c    10 Nov 2015 04:14:03 -0000      1.24
+++ lib/libc/stdlib/atexit.c    4 Dec 2017 19:04:52 -0000
@@ -31,12 +31,24 @@
 
 #include <sys/types.h>
 #include <sys/mman.h>
+#include <dlfcn.h>
+#include <elf.h>
+#pragma weak _DYNAMIC
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include "atexit.h"
 #include "atfork.h"
 #include "thread_private.h"
+#include "tib.h"
+
+typeof(dlctl) dlctl asm("_dlctl") __attribute__((weak));
+
+struct thread_atexit_fn {
+       void (*func)(void *);
+       void *arg;
+       struct thread_atexit_fn *next;
+};
 
 struct atexit *__atexit;
 static int restartloop;
@@ -121,6 +133,45 @@ atexit(void (*fn)(void))
 }
 DEF_STRONG(atexit);
 
+__weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl);
+
+int
+__cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso)
+{
+       struct thread_atexit_fn *fnp;
+       struct tib *tib = TIB_GET();
+
+       fnp = calloc(1, sizeof(struct thread_atexit_fn));
+       if (fnp == NULL)
+               return -1;
+
+#ifndef NO_PIC
+       if (_DYNAMIC)
+               dlctl(NULL, DL_REFERENCE, dso);
+#endif
+
+       fnp->func = func;
+       fnp->arg = arg;
+       fnp->next = tib->tib_atexit;
+       tib->tib_atexit = fnp;
+
+       return 0;
+}
+DEF_STRONG(__cxa_thread_atexit_impl);
+
+void
+_thread_finalize(void)
+{
+       struct tib *tib = TIB_GET();
+
+       while (tib->tib_atexit) {
+               struct thread_atexit_fn *fnp = tib->tib_atexit;
+               tib->tib_atexit = fnp->next;
+               fnp->func(fnp->arg);
+               free(fnp);
+       }
+}
+
 /*
  * Call all handlers registered with __cxa_atexit() for the shared
  * object owning 'dso'.
@@ -133,6 +184,9 @@ __cxa_finalize(void *dso)
        struct atexit_fn fn;
        int n, pgsize = getpagesize();
        static int call_depth;
+
+       if (dso == NULL)
+               _thread_finalize();
 
        _ATEXIT_LOCK();
        call_depth++;
Index: lib/libc/stdlib/atexit.h
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/atexit.h,v
retrieving revision 1.10
diff -u -p -r1.10 atexit.h
--- lib/libc/stdlib/atexit.h    25 Oct 2015 18:01:24 -0000      1.10
+++ lib/libc/stdlib/atexit.h    4 Dec 2017 19:04:52 -0000
@@ -46,7 +46,9 @@ extern struct atexit *__atexit;               /* poin
 __END_HIDDEN_DECLS
 
 int    __cxa_atexit(void (*)(void *), void *, void *);
+int    __cxa_thread_atexit(void (*)(void *), void *, void *);
 void   __cxa_finalize(void *);
 
 PROTO_NORMAL(__cxa_atexit);
+PROTO_STD_DEPRECATED(__cxa_thread_atexit);
 PROTO_NORMAL(__cxa_finalize);
Index: lib/libc/thread/rthread.c
===================================================================
RCS file: /cvs/src/lib/libc/thread/rthread.c,v
retrieving revision 1.6
diff -u -p -r1.6 rthread.c
--- lib/libc/thread/rthread.c   4 Nov 2017 22:53:57 -0000       1.6
+++ lib/libc/thread/rthread.c   4 Dec 2017 19:04:52 -0000
@@ -137,6 +137,7 @@ pthread_exit(void *retval)
                oclfn->fn(oclfn->arg);
                free(oclfn);
        }
+       _thread_finalize();
        _rthread_tls_destructors(thread);
 
        if (_thread_cb.tc_thread_release != NULL)

Reply via email to