+ p->fns[p->ind++] = *fptr;
+ __atexit_new_registration = 1;
+ _MUTEX_UNLOCK(&atexit_mutex);
+ return 0;
+}
+
+/*
+ * Register a function to be performed at exit.
+ */
+int
+atexit(void (*func)(void))
+{
+ struct atexit_fn fn;
+ int error;
+
+ fn.fn_type = ATEXIT_FN_STD;
+ fn.fn_ptr.std_func = func;
+ fn.fn_arg = NULL;
+ fn.fn_dso = NULL;
+
+#if defined(__DYNAMIC__) && !TARGET_OS_IPHONE
+ // <rdar://problem/14596032&15173956>
+ struct dl_info info;
+ if (dladdr(func, &info)) {
+ fn.fn_dso = info.dli_fbase;
+ }
+#endif
+
+ error = atexit_register(&fn);
+ return (error);
+}
+
+#ifdef __BLOCKS__
+int
+atexit_b(void (^block)(void))
+{
+ struct atexit_fn fn;
+ int error;
+
+ fn.fn_type = ATEXIT_FN_BLK;
+ fn.fn_ptr.block = Block_copy(block);
+ fn.fn_arg = NULL;
+ fn.fn_dso = NULL;
+
+ error = atexit_register(&fn);
+ return (error);
+}
+#endif /* __BLOCKS__ */
+
+/*
+ * Register a function to be performed at exit or when an shared object
+ * with given dso handle is unloaded dynamically.
+ */
+int
+__cxa_atexit(void (*func)(void *), void *arg, void *dso)
+{
+ struct atexit_fn fn;
+ int error;
+
+ fn.fn_type = ATEXIT_FN_CXA;
+ fn.fn_ptr.cxa_func = func;;
+ fn.fn_arg = arg;
+ fn.fn_dso = dso;
+
+ error = atexit_register(&fn);
+ return (error);
+}
+
+static bool
+__cxa_in_range(const struct __cxa_range_t ranges[],
+ unsigned int count,
+ const void* fn)
+{
+ uintptr_t addr = (uintptr_t)fn;
+
+ unsigned int i;
+ for (i = 0; i < count; ++i) {
+ const struct __cxa_range_t *r = &ranges[i];
+ if (addr < (uintptr_t)r->addr) {
+ continue;
+ }
+ if (addr < ((uintptr_t)r->addr + r->length)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/*
+ * Call handlers registered via __cxa_atexit/atexit that are in a
+ * a range specified.
+ * Note: rangeCount==0, means call all handlers.
+ */
+void
+__cxa_finalize_ranges(const struct __cxa_range_t ranges[], unsigned int count)
+{
+ struct atexit *p;
+ struct atexit_fn *fn;
+ int n;
+ _MUTEX_LOCK(&atexit_mutex);
+
+restart:
+ for (p = __atexit; p; p = p->next) {
+ for (n = p->ind; --n >= 0;) {
+ fn = &p->fns[n];
+
+ if (fn->fn_type == ATEXIT_FN_EMPTY) {
+ continue; // already been called
+ }
+
+ // Verify that the entry is within the range being unloaded.
+ if (count > 0) {
+ if (fn->fn_type == ATEXIT_FN_CXA) {
+ // for __cxa_atexit(), call if *dso* is in range be unloaded
+ if (!__cxa_in_range(ranges, count, fn->fn_dso)) {
+ continue; // not being unloaded yet
+ }
+ } else if (fn->fn_type == ATEXIT_FN_STD) {
+ // for atexit, call if *function* is in range be unloaded
+ if (!__cxa_in_range(ranges, count, fn->fn_ptr.std_func)) {
+ continue; // not being unloaded yet
+ }
+#ifdef __BLOCKS__
+ } else if (fn->fn_type == ATEXIT_FN_BLK) {
+ // for atexit_b, call if block code is in range be unloaded
+ void *a = ((struct Block_layout *)fn->fn_ptr.block)->invoke;
+ if (!__cxa_in_range(ranges, count, a)) {
+ continue; // not being unloaded yet
+ }
+#endif // __BLOCKS__
+ }
+ }
+
+ // Clear the entry to indicate that this handler has been called.
+ int fn_type = fn->fn_type;
+ fn->fn_type = ATEXIT_FN_EMPTY;
+
+ // Detect recursive registrations.
+ __atexit_new_registration = 0;
+ _MUTEX_UNLOCK(&atexit_mutex);
+
+ // Call the handler.
+ if (fn_type == ATEXIT_FN_CXA) {
+ fn->fn_ptr.cxa_func(fn->fn_arg);
+ } else if (fn_type == ATEXIT_FN_STD) {
+ fn->fn_ptr.std_func();
+#ifdef __BLOCKS__
+ } else if (fn_type == ATEXIT_FN_BLK) {
+ fn->fn_ptr.block();
+#endif // __BLOCKS__
+ }
+
+ // Call any recursively registered handlers.
+ _MUTEX_LOCK(&atexit_mutex);
+ if (__atexit_new_registration) {
+ goto restart;
+ }
+ }
+ }