Fixed atexit()

 See b.android.com/66595

Change-Id: I1d4c3db424c7f839e48756359cc095a6b1028ace
diff --git a/ndk/platforms/android-3/arch-arm/src/atexit.h b/ndk/platforms/android-3/arch-arm/src/atexit.h
index bc776a8..90aa030 100644
--- a/ndk/platforms/android-3/arch-arm/src/atexit.h
+++ b/ndk/platforms/android-3/arch-arm/src/atexit.h
@@ -26,10 +26,20 @@
  * SUCH DAMAGE.
  */
 
-extern void *__dso_handle;
+#include <stddef.h>
+
+extern void* __dso_handle;
+
+extern int __cxa_atexit(void (*)(void*), void*, void*);
 
 __attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
+void __atexit_handler_wrapper(void* func) {
+  if (func != NULL) {
+    (*(void (*)(void))func)();
+  }
+}
+
+__attribute__ ((visibility ("hidden")))
+int atexit(void (*func)(void)) {
+  return (__cxa_atexit(&__atexit_handler_wrapper, func, &__dso_handle));
 }
diff --git a/ndk/platforms/android-9/arch-mips/src/atexit.h b/ndk/platforms/android-9/arch-mips/src/atexit.h
index 759008c..3ded9bf 100644
--- a/ndk/platforms/android-9/arch-mips/src/atexit.h
+++ b/ndk/platforms/android-9/arch-mips/src/atexit.h
@@ -26,11 +26,20 @@
  * SUCH DAMAGE.
  */
 
-extern void *__dso_handle;
-extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
+#include <stddef.h>
+
+extern void* __dso_handle;
+
+extern int __cxa_atexit(void (*)(void*), void*, void*);
 
 __attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
+void __atexit_handler_wrapper(void* func) {
+  if (func != NULL) {
+    (*(void (*)(void))func)();
+  }
+}
+
+__attribute__ ((visibility ("hidden")))
+int atexit(void (*func)(void)) {
+  return (__cxa_atexit(&__atexit_handler_wrapper, func, &__dso_handle));
 }
diff --git a/ndk/platforms/android-9/arch-x86/src/atexit.h b/ndk/platforms/android-9/arch-x86/src/atexit.h
index bc776a8..90aa030 100644
--- a/ndk/platforms/android-9/arch-x86/src/atexit.h
+++ b/ndk/platforms/android-9/arch-x86/src/atexit.h
@@ -26,10 +26,20 @@
  * SUCH DAMAGE.
  */
 
-extern void *__dso_handle;
+#include <stddef.h>
+
+extern void* __dso_handle;
+
+extern int __cxa_atexit(void (*)(void*), void*, void*);
 
 __attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
+void __atexit_handler_wrapper(void* func) {
+  if (func != NULL) {
+    (*(void (*)(void))func)();
+  }
+}
+
+__attribute__ ((visibility ("hidden")))
+int atexit(void (*func)(void)) {
+  return (__cxa_atexit(&__atexit_handler_wrapper, func, &__dso_handle));
 }
diff --git a/ndk/platforms/android-L/arch-arm/src/__dso_handle.h b/ndk/platforms/android-L/arch-arm/src/__dso_handle.h
new file mode 120000
index 0000000..af149a4
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/__dso_handle.h
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/__dso_handle.h
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/__dso_handle_so.h b/ndk/platforms/android-L/arch-arm/src/__dso_handle_so.h
new file mode 120000
index 0000000..d9e53ba
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/__dso_handle_so.h
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/__dso_handle_so.h
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/atexit.h b/ndk/platforms/android-L/arch-arm/src/atexit.h
new file mode 120000
index 0000000..aae4eac
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/atexit.h
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/atexit.h
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/crtbegin_dynamic.c b/ndk/platforms/android-L/arch-arm/src/crtbegin_dynamic.c
new file mode 120000
index 0000000..4860a99
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/crtbegin_dynamic.c
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/crtbegin_dynamic.c
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/crtbegin_so.c b/ndk/platforms/android-L/arch-arm/src/crtbegin_so.c
new file mode 120000
index 0000000..2fa5dcb
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/crtbegin_so.c
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/crtbegin_so.c
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/crtbegin_static.c b/ndk/platforms/android-L/arch-arm/src/crtbegin_static.c
new file mode 120000
index 0000000..a618c0e
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/crtbegin_static.c
@@ -0,0 +1 @@
+crtbegin_dynamic.c
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/crtend.S b/ndk/platforms/android-L/arch-arm/src/crtend.S
new file mode 120000
index 0000000..0317fc2
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/crtend.S
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/crtend.S
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm/src/crtend_so.S b/ndk/platforms/android-L/arch-arm/src/crtend_so.S
new file mode 120000
index 0000000..2fd524c
--- /dev/null
+++ b/ndk/platforms/android-L/arch-arm/src/crtend_so.S
@@ -0,0 +1 @@
+../../../android-3/arch-arm/src/crtend_so.S
\ No newline at end of file
diff --git a/ndk/platforms/android-L/arch-arm64/src/atexit.h b/ndk/platforms/android-L/arch-arm64/src/atexit.h
index 4b31600..90aa030 100644
--- a/ndk/platforms/android-L/arch-arm64/src/atexit.h
+++ b/ndk/platforms/android-L/arch-arm64/src/atexit.h
@@ -26,11 +26,20 @@
  * SUCH DAMAGE.
  */
 
-extern void *__dso_handle;
+#include <stddef.h>
+
+extern void* __dso_handle;
 
 extern int __cxa_atexit(void (*)(void*), void*, void*);
 
 __attribute__ ((visibility ("hidden")))
+void __atexit_handler_wrapper(void* func) {
+  if (func != NULL) {
+    (*(void (*)(void))func)();
+  }
+}
+
+__attribute__ ((visibility ("hidden")))
 int atexit(void (*func)(void)) {
-  return (__cxa_atexit((void (*)(void*)) func, (void*) 0, &__dso_handle));
+  return (__cxa_atexit(&__atexit_handler_wrapper, func, &__dso_handle));
 }
diff --git a/ndk/platforms/android-L/arch-mips64/src/atexit.h b/ndk/platforms/android-L/arch-mips64/src/atexit.h
index 759008c..3ded9bf 100644
--- a/ndk/platforms/android-L/arch-mips64/src/atexit.h
+++ b/ndk/platforms/android-L/arch-mips64/src/atexit.h
@@ -26,11 +26,20 @@
  * SUCH DAMAGE.
  */
 
-extern void *__dso_handle;
-extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
+#include <stddef.h>
+
+extern void* __dso_handle;
+
+extern int __cxa_atexit(void (*)(void*), void*, void*);
 
 __attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
+void __atexit_handler_wrapper(void* func) {
+  if (func != NULL) {
+    (*(void (*)(void))func)();
+  }
+}
+
+__attribute__ ((visibility ("hidden")))
+int atexit(void (*func)(void)) {
+  return (__cxa_atexit(&__atexit_handler_wrapper, func, &__dso_handle));
 }