merge in jb-mr1-release history after reset to jb-mr1-dev
diff --git a/libc/Android.mk b/libc/Android.mk
index 94cb3ea..837b5be 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -585,10 +585,6 @@
     -I$(LOCAL_PATH)/include  \
     -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)
 
-ifeq ($(TARGET_ARCH),arm)
-    libc_crt_target_cflags += -DCRT_LEGACY_WORKAROUND
-endif
-
 # Define some common includes
 # ========================================================
 libc_common_c_includes := \
@@ -763,9 +759,6 @@
 
 LOCAL_SRC_FILES := $(libc_common_src_files)
 LOCAL_CFLAGS := $(libc_common_cflags)
-ifeq ($(TARGET_ARCH),arm)
-    LOCAL_CFLAGS += -DCRT_LEGACY_WORKAROUND
-endif
 LOCAL_C_INCLUDES := $(libc_common_c_includes)
 LOCAL_MODULE := libc_common
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
@@ -849,6 +842,17 @@
 	bionic/pthread_debug.c \
 	bionic/libc_init_dynamic.c
 
+ifeq ($(TARGET_ARCH),arm)
+	LOCAL_NO_CRT := true
+	LOCAL_CFLAGS += -DCRT_LEGACY_WORKAROUND
+
+	LOCAL_SRC_FILES := \
+		arch-arm/bionic/crtbegin_so.c \
+		arch-arm/bionic/atexit_legacy.c \
+		$(LOCAL_SRC_FILES) \
+		arch-arm/bionic/crtend_so.S
+endif
+
 LOCAL_MODULE:= libc
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
diff --git a/libc/arch-arm/bionic/atexit.h b/libc/arch-arm/bionic/atexit.h
index d567bfc..bc776a8 100644
--- a/libc/arch-arm/bionic/atexit.h
+++ b/libc/arch-arm/bionic/atexit.h
@@ -26,23 +26,6 @@
  * SUCH DAMAGE.
  */
 
-/* CRT_LEGACY_WORKAROUND should only be defined when building
- * this file as part of the platform's C library.
- *
- * The C library already defines a function named 'atexit()'
- * for backwards compatibility with older NDK-generated binaries.
- *
- * For newer ones, 'atexit' is actually embedded in the C
- * runtime objects that are linked into the final ELF
- * binary (shared library or executable), and will call
- * __cxa_atexit() in order to un-register any atexit()
- * handler when a library is unloaded.
- *
- * This function must be global *and* hidden. Only the
- * code inside the same ELF binary should be able to access it.
- */
-
-#ifndef CRT_LEGACY_WORKAROUND
 extern void *__dso_handle;
 
 __attribute__ ((visibility ("hidden")))
@@ -50,4 +33,3 @@
 {
   return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
 }
-#endif
diff --git a/libc/arch-arm/bionic/atexit_legacy.c b/libc/arch-arm/bionic/atexit_legacy.c
new file mode 100644
index 0000000..4abe839
--- /dev/null
+++ b/libc/arch-arm/bionic/atexit_legacy.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <private/logd.h>
+#include <stdio.h>
+
+/*
+ * This source file should only be included by libc.so, its purpose is
+ * to support legacy ARM binaries by exporting a publicly visible
+ * implementation of atexit().
+ */
+
+extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
+
+/*
+ * Register a function to be performed at exit.
+ */
+int
+atexit(void (*func)(void))
+{
+    /*
+     * Exit functions queued by this version of atexit will not be called
+     * on dlclose(), and when they are called (at program exit), the
+     * calling library may have been dlclose()'d, causing the program to
+     * crash.
+     */
+    static char const warning[] =
+        "WARNING: generic atexit() called from legacy shared library\n";
+
+    __libc_android_log_print(ANDROID_LOG_WARN, "libc", warning);
+    fprintf(stderr, warning);
+
+    return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
+}
diff --git a/libc/arch-arm/bionic/crtbegin_so.c b/libc/arch-arm/bionic/crtbegin_so.c
index 23f76b1..f382f63 100644
--- a/libc/arch-arm/bionic/crtbegin_so.c
+++ b/libc/arch-arm/bionic/crtbegin_so.c
@@ -34,10 +34,25 @@
   __cxa_finalize(&__dso_handle);
 }
 
+/* CRT_LEGACY_WORKAROUND should only be defined when building
+ * this file as part of the platform's C library.
+ *
+ * The C library already defines a function named 'atexit()'
+ * for backwards compatibility with older NDK-generated binaries.
+ *
+ * For newer ones, 'atexit' is actually embedded in the C
+ * runtime objects that are linked into the final ELF
+ * binary (shared library or executable), and will call
+ * __cxa_atexit() in order to un-register any atexit()
+ * handler when a library is unloaded.
+ *
+ * This function must be global *and* hidden. Only the
+ * code inside the same ELF binary should be able to access it.
+ */
+
 #ifdef CRT_LEGACY_WORKAROUND
 #include "__dso_handle.h"
 #else
-#include "__dso_handle_so.h"
-#endif
-
+#include "__dso_handle_so.c"
 #include "atexit.h"
+#endif
diff --git a/libc/arch-arm/bionic/eabi.c b/libc/arch-arm/bionic/eabi.c
index 3f26f2b..51a5b97 100644
--- a/libc/arch-arm/bionic/eabi.c
+++ b/libc/arch-arm/bionic/eabi.c
@@ -30,22 +30,6 @@
 
 extern int  __cxa_atexit(void (*)(void*), void*, void* );
 
-/* Temporary hack: this variable should not be part of the C library
- * itself, but placed in the .bss section of each executable or
- * shared library instead.
- *
- * We keep it here temporarily until the build system has been
- * modified properly to use crtbegin_so.S and crtend_so.S when
- * generating shared libraries.
- *
- * It must be a 'weak' symbol to avoid conflicts with the definitions
- * that have been moved to crtbegin_static.S and crtbegin_dynamic.S
- *
- * For the record, it is used for static C++ object construction
- * and destruction. See http://www.codesourcery.com/public/cxx-abi/abi.html#dso-dtor
- */
-void* __attribute__((weak)) __dso_handle;
-
 /* The "C++ ABI for ARM" document states that static C++ constructors,
  * which are called from the .init_array, should manually call
  * __aeabi_atexit() to register static destructors explicitely.
diff --git a/libc/bionic/dlmalloc.h b/libc/bionic/dlmalloc.h
index b34165e..a00a583 100644
--- a/libc/bionic/dlmalloc.h
+++ b/libc/bionic/dlmalloc.h
@@ -24,6 +24,7 @@
 #define REALLOC_ZERO_BYTES_FREES 1
 #define USE_DL_PREFIX 1
 #define USE_LOCKS 1
+#define LOCK_AT_FORK 1
 #define USE_RECURSIVE_LOCK 0
 #define USE_SPIN_LOCKS 0
 
diff --git a/libc/kernel/common/linux/legacy_ion.h b/libc/kernel/common/linux/legacy_ion.h
deleted file mode 100644
index 0f872ec..0000000
--- a/libc/kernel/common/linux/legacy_ion.h
+++ /dev/null
@@ -1,70 +0,0 @@
-/****************************************************************************
- ****************************************************************************
- ***
- ***   This header was automatically generated from a Linux kernel header
- ***   of the same name, to make information necessary for userspace to
- ***   call into the kernel available to libc.  It contains only constants,
- ***   structures, and macros generated from the original header, and thus,
- ***   contains no copyrightable information.
- ***
- ***   To edit the content of this header, modify the corresponding
- ***   source file (e.g. under external/kernel-headers/original/) then
- ***   run bionic/libc/kernel/tools/update_all.py
- ***
- ***   Any manual change here will be lost the next time this script will
- ***   be run. You've been warned!
- ***
- ****************************************************************************
- ****************************************************************************/
-#ifndef _LINUX_ION_H
-#define _LINUX_ION_H
-#include <linux/types.h>
-struct ion_handle;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-enum ion_heap_type {
- ION_HEAP_TYPE_SYSTEM,
- ION_HEAP_TYPE_SYSTEM_CONTIG,
- ION_HEAP_TYPE_CARVEOUT,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
- ION_HEAP_TYPE_CUSTOM,
- ION_NUM_HEAPS,
-};
-#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
-#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
-#define ION_FLAG_CACHED 1  
-struct ion_allocation_data {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
- size_t len;
- size_t align;
- unsigned int heap_mask;
- unsigned int flags;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
- struct ion_handle *handle;
-};
-struct ion_fd_data {
- struct ion_handle *handle;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
- int fd;
-};
-struct ion_handle_data {
- struct ion_handle *handle;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-};
-struct ion_custom_data {
- unsigned int cmd;
- unsigned long arg;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-};
-#define ION_IOC_MAGIC 'I'
-#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0,   struct ion_allocation_data)
-#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
-#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
-#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
-#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#endif
-
diff --git a/libc/kernel/common/linux/msm_ion.h b/libc/kernel/common/linux/msm_ion.h
index 8b03320..5e85ea1 100644
--- a/libc/kernel/common/linux/msm_ion.h
+++ b/libc/kernel/common/linux/msm_ion.h
@@ -18,7 +18,7 @@
  ****************************************************************************/
 #ifndef __LINUX_MSM_ION_H__
 #define __LINUX_MSM_ION_H__
-#include <linux/legacy_ion.h>
+#include <linux/ion.h>
 enum msm_ion_heap_types {
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ION_HEAP_TYPE_IOMMU = ION_HEAP_TYPE_CUSTOM + 1,
@@ -96,10 +96,10 @@
  unsigned long flags;
 };
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MAGIC, 7,   struct ion_flush_data)
-#define ION_IOC_INV_CACHES _IOWR(ION_IOC_MAGIC, 8,   struct ion_flush_data)
-#define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MAGIC, 9,   struct ion_flush_data)
-#define ION_IOC_GET_FLAGS _IOWR(ION_IOC_MAGIC, 10,   struct ion_flag_data)
+#define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MAGIC, 20,   struct ion_flush_data)
+#define ION_IOC_INV_CACHES _IOWR(ION_IOC_MAGIC, 21,   struct ion_flush_data)
+#define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MAGIC, 22,   struct ion_flush_data)
+#define ION_IOC_GET_FLAGS _IOWR(ION_IOC_MAGIC, 23,   struct ion_flag_data)
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #endif
 
diff --git a/libc/private/__dso_handle_so.c b/libc/private/__dso_handle_so.c
index 198e64b..732799b 100644
--- a/libc/private/__dso_handle_so.c
+++ b/libc/private/__dso_handle_so.c
@@ -29,4 +29,4 @@
 
 __attribute__ ((visibility ("hidden")))
 __attribute__ ((section (".data")))
-void *__dso_handle;
+void *__dso_handle = &__dso_handle;
diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
index 55b7132..23a2636 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -104,17 +104,6 @@
 	return (ret);
 }
 
-#ifdef CRT_LEGACY_WORKAROUND
-/*
- * Register a function to be performed at exit.
- */
-int
-atexit(void (*func)(void))
-{
-	return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
-}
-#endif
-
 /*
  * Call all handlers registered with __cxa_atexit() for the shared
  * object owning 'dso'.
diff --git a/libc/upstream-dlmalloc/malloc.c b/libc/upstream-dlmalloc/malloc.c
index 0a1f6b2..d951841 100644
--- a/libc/upstream-dlmalloc/malloc.c
+++ b/libc/upstream-dlmalloc/malloc.c
@@ -3099,6 +3099,9 @@
 
 /* Initialize mparams */
 static int init_mparams(void) {
+  /* BEGIN android-added: move pthread_atfork outside of lock */
+  int first_run = 0;
+  /* END android-added */
 #ifdef NEED_GLOBAL_LOCK_INIT
   if (malloc_global_mutex_status <= 0)
     init_malloc_global_mutex();
@@ -3109,6 +3112,9 @@
     size_t magic;
     size_t psize;
     size_t gsize;
+    /* BEGIN android-added: move pthread_atfork outside of lock */
+    first_run = 1;
+    /* END android-added */
 
 #ifndef WIN32
     psize = malloc_getpagesize;
@@ -3153,9 +3159,11 @@
     gm->mflags = mparams.default_mflags;
     (void)INITIAL_LOCK(&gm->mutex);
 #endif
-#if LOCK_AT_FORK
+    /* BEGIN android-removed: move pthread_atfork outside of lock */
+#if 0 && LOCK_AT_FORK
     pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child);
 #endif
+    /* END android-removed */
 
     {
 #if USE_DEV_RANDOM
@@ -3184,6 +3192,13 @@
   }
 
   RELEASE_MALLOC_GLOBAL_LOCK();
+  /* BEGIN android-added: move pthread_atfork outside of lock */
+#if LOCK_AT_FORK
+  if (first_run != 0) {
+    pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child);
+  }
+#endif
+  /* END android-added */
   return 1;
 }
 
diff --git a/tests/Android.mk b/tests/Android.mk
index ba1112e..1219afd 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -26,6 +26,7 @@
 #   adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
 include $(CLEAR_VARS)
 LOCAL_MODULE := bionic-unit-tests
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_SRC_FILES := $(test_src_files)
 include $(BUILD_NATIVE_TEST)
 
@@ -35,6 +36,7 @@
 # implementation for testing the tests themselves.
 include $(CLEAR_VARS)
 LOCAL_MODULE := bionic-unit-tests-glibc
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 LOCAL_SRC_FILES := $(test_src_files)
 include $(BUILD_HOST_NATIVE_TEST)