Merge "Revert "Add support for protected local symbol lookup."" into lmp-dev
diff --git a/libc/Android.mk b/libc/Android.mk
index 727513c..32c9fa6 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -102,8 +102,6 @@
     bionic/__cmsg_nxthdr.cpp \
     bionic/connect.cpp \
     bionic/ctype.cpp \
-    bionic/__cxa_guard.cpp \
-    bionic/__cxa_pure_virtual.cpp \
     bionic/dirent.cpp \
     bionic/dup2.cpp \
     bionic/epoll_create.cpp \
@@ -142,7 +140,6 @@
     bionic/mknod.cpp \
     bionic/mntent.cpp \
     bionic/NetdClientDispatch.cpp \
-    bionic/new.cpp \
     bionic/open.cpp \
     bionic/pause.cpp \
     bionic/pipe.cpp \
@@ -230,6 +227,11 @@
     bionic/wchar.cpp \
     bionic/wctype.cpp \
 
+libc_cxa_src_files := \
+    bionic/__cxa_guard.cpp \
+    bionic/__cxa_pure_virtual.cpp \
+    bionic/new.cpp \
+
 libc_upstream_freebsd_src_files := \
     upstream-freebsd/lib/libc/gen/ldexp.c \
     upstream-freebsd/lib/libc/gen/sleep.c \
@@ -806,6 +808,28 @@
 
 
 # ========================================================
+# libc_cxa.a - Things traditionally in libstdc++
+# ========================================================
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := $(libc_cxa_src_files)
+LOCAL_CFLAGS := $(libc_common_cflags) \
+    -fvisibility=hidden \
+
+LOCAL_CONLYFLAGS := $(libc_common_conlyflags)
+LOCAL_CPPFLAGS := $(libc_common_cppflags)
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_MODULE := libc_cxa
+# GCC refuses to hide new/delete
+LOCAL_CLANG := true
+LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
+LOCAL_SYSTEM_SHARED_LIBRARIES :=
+
+include $(BUILD_STATIC_LIBRARY)
+
+
+# ========================================================
 # libc_syscalls.a
 # ========================================================
 
@@ -857,6 +881,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(libc_common_additional_dependencies)
 LOCAL_WHOLE_STATIC_LIBRARIES := \
     libc_bionic \
+    libc_cxa \
     libc_dns \
     libc_freebsd \
     libc_gdtoa \
@@ -1104,6 +1129,36 @@
 
 endif  #!user
 
+# ========================================================
+# libstdc++.so
+# ========================================================
+libstdcxx_common_src_files := \
+    bionic/__cxa_guard.cpp \
+    bionic/__cxa_pure_virtual.cpp \
+    bionic/new.cpp \
+    bionic/libc_logging.cpp \
+
+include $(CLEAR_VARS)
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_CFLAGS := $(libc_common_cflags)
+LOCAL_SRC_FILES := $(libstdcxx_common_src_files)
+LOCAL_MODULE:= libstdc++
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_SYSTEM_SHARED_LIBRARIES := libc
+include $(BUILD_SHARED_LIBRARY)
+
+# ========================================================
+# libstdc++.a
+# ========================================================
+include $(CLEAR_VARS)
+LOCAL_C_INCLUDES := $(libc_common_c_includes)
+LOCAL_CFLAGS := $(libc_common_cflags)
+LOCAL_SRC_FILES := $(libstdcxx_common_src_files)
+LOCAL_MODULE:= libstdc++
+LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
+LOCAL_SYSTEM_SHARED_LIBRARIES := libc
+include $(BUILD_STATIC_LIBRARY)
+
 
 # ========================================================
 include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/libc/bionic/malloc_debug_leak.cpp b/libc/bionic/malloc_debug_leak.cpp
index 27b6714..df0f997 100644
--- a/libc/bionic/malloc_debug_leak.cpp
+++ b/libc/bionic/malloc_debug_leak.cpp
@@ -385,6 +385,36 @@
     return ptr;
 }
 
+extern "C" size_t leak_malloc_usable_size(const void* mem) {
+    if (DebugCallsDisabled()) {
+        return g_malloc_dispatch->malloc_usable_size(mem);
+    }
+
+    if (mem == NULL) {
+        return 0;
+    }
+
+    // Check the guard to make sure it is valid.
+    const AllocationEntry* header = const_to_header(mem);
+
+    if (header->guard == MEMALIGN_GUARD) {
+        // If this is a memalign'd pointer, then grab the header from
+        // entry.
+        header = const_to_header(header->entry);
+    } else if (header->guard != GUARD) {
+        debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
+                  header->guard, header->entry);
+        return 0;
+    }
+
+    size_t ret = g_malloc_dispatch->malloc_usable_size(header);
+    if (ret != 0) {
+        // The usable area starts at 'mem' and stops at 'header+ret'.
+        return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
+    }
+    return 0;
+}
+
 extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
     if (DebugCallsDisabled()) {
         return g_malloc_dispatch->realloc(oldMem, bytes);
@@ -408,7 +438,7 @@
 
     newMem = leak_malloc(bytes);
     if (newMem != NULL) {
-        size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
+        size_t oldSize = leak_malloc_usable_size(oldMem);
         size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
         memcpy(newMem, oldMem, copySize);
         leak_free(oldMem);
@@ -462,38 +492,6 @@
     return base;
 }
 
-extern "C" size_t leak_malloc_usable_size(const void* mem) {
-    if (DebugCallsDisabled()) {
-        return g_malloc_dispatch->malloc_usable_size(mem);
-    }
-
-    if (mem != NULL) {
-        // Check the guard to make sure it is valid.
-        const AllocationEntry* header = const_to_header((void*)mem);
-
-        if (header->guard == MEMALIGN_GUARD) {
-            // If this is a memalign'd pointer, then grab the header from
-            // entry.
-            header = const_to_header(header->entry);
-        } else if (header->guard != GUARD) {
-            debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
-                      header->guard, header->entry);
-            return 0;
-        }
-
-        // TODO: Temporary workaround to avoid a crash b/16874447.
-        return header->entry->size & ~SIZE_FLAG_MASK;
-#if 0
-        size_t ret = g_malloc_dispatch->malloc_usable_size(header);
-        if (ret != 0) {
-            // The usable area starts at 'mem' and stops at 'header+ret'.
-            return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
-        }
-#endif
-    }
-    return 0;
-}
-
 extern "C" struct mallinfo leak_mallinfo() {
   return g_malloc_dispatch->mallinfo();
 }
diff --git a/libstdc++/Android.mk b/libstdc++/Android.mk
deleted file mode 100644
index ff9609a..0000000
--- a/libstdc++/Android.mk
+++ /dev/null
@@ -1,15 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES := src/libstdc++.cpp
-LOCAL_MODULE:= libstdc++
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-LOCAL_SYSTEM_SHARED_LIBRARIES := libc
-include $(BUILD_SHARED_LIBRARY)
-
-include $(CLEAR_VARS)
-LOCAL_SRC_FILES:= src/libstdc++.cpp
-LOCAL_MODULE:= libstdc++
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-LOCAL_SYSTEM_SHARED_LIBRARIES := libc
-include $(BUILD_STATIC_LIBRARY)
diff --git a/libstdc++/src/libstdc++.cpp b/libstdc++/src/libstdc++.cpp
deleted file mode 100644
index 3676aa1..0000000
--- a/libstdc++/src/libstdc++.cpp
+++ /dev/null
@@ -1 +0,0 @@
-extern "C" void __this_library_is_now_part_of_libc() {}