Merge "bionic: Stack pointer/signal race condition."
diff --git a/libc/Android.mk b/libc/Android.mk
index 4ceb12f..1a5bdfa 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -589,6 +589,18 @@
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
 ALL_GENERATED_SOURCES += $(GEN)
+
+GEN := $(TARGET_OUT_SHARED_LIBRARIES)/crtbegin_so.o
+$(GEN): $(libc_crt_target_crtstart_so_file)
+	@mkdir -p $(dir $@)
+	$(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
+ALL_GENERATED_SOURCES += $(GEN)
+
+GEN := $(TARGET_OUT_SHARED_LIBRARIES)/crtend_so.o
+$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend_so.S
+	@mkdir -p $(dir $@)
+	$(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
+ALL_GENERATED_SOURCES += $(GEN)
 endif # TARGET_ARCH == x86 || TARGET_ARCH == arm
 
 
diff --git a/libc/bionic/stubs.c b/libc/bionic/stubs.c
index cc4c04e..1aeb581 100644
--- a/libc/bionic/stubs.c
+++ b/libc/bionic/stubs.c
@@ -25,17 +25,19 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <grp.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <pwd.h>
-#include <netdb.h>
-#include <mntent.h>
-#include <private/android_filesystem_config.h>
-#include <pthread.h>
-#include <stdlib.h>
-#include <errno.h>
+
 #include <ctype.h>
+#include <errno.h>
+#include <grp.h>
+#include <mntent.h>
+#include <netdb.h>
+#include <private/android_filesystem_config.h>
+#include <private/logd.h>
+#include <pthread.h>
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
 
 static int do_getpw_r(int by_name, const char* name, uid_t uid,
         struct passwd* dst, char* buf, size_t byte_count, struct passwd** result)
@@ -438,65 +440,62 @@
     return app_id_to_group( app_id_from_name(name), state );
 }
 
-
-struct netent* getnetbyname(const char *name)
-{
-    fprintf(stderr, "FIX ME! implement getgrnam() %s:%d\n", __FILE__, __LINE__);
-    return NULL;
+static void unimplemented_stub(const char* function) {
+  const char* fmt = "%s(3) is not implemented on Android\n";
+  __libc_android_log_print(ANDROID_LOG_WARN, "libc", fmt, function);
+  fprintf(stderr, fmt, function);
 }
 
-void endpwent(void)
-{
+#define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
+
+struct netent* getnetbyname(const char* name) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-struct mntent* getmntent(FILE* f)
-{
-    fprintf(stderr, "FIX ME! implement getmntent() %s:%d\n", __FILE__, __LINE__);
-    return NULL;
+void endpwent(void) {
+  UNIMPLEMENTED;
 }
 
-char* ttyname(int fd)
-{
-    fprintf(stderr, "FIX ME! implement ttyname() %s:%d\n", __FILE__, __LINE__);
-    return NULL;
+struct mntent* getmntent(FILE* f) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-int ttyname_r(int fd, char *buf, size_t buflen)
-{
-    fprintf(stderr, "FIX ME! implement ttyname_r() %s:%d\n", __FILE__, __LINE__);
-    return -ERANGE;
+char* ttyname(int fd) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-struct netent *getnetbyaddr(uint32_t net, int type)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+int ttyname_r(int fd, char* buf, size_t buflen) {
+  UNIMPLEMENTED;
+  return -ERANGE;
 }
 
-struct protoent *getprotobyname(const char *name)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+struct netent* getnetbyaddr(uint32_t net, int type) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-struct protoent *getprotobynumber(int proto)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+struct protoent* getprotobyname(const char* name) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-char* getusershell(void)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+struct protoent* getprotobynumber(int proto) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-void setusershell(void)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
+char* getusershell(void) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-void endusershell(void)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
+void setusershell(void) {
+  UNIMPLEMENTED;
+}
+
+void endusershell(void) {
+  UNIMPLEMENTED;
 }
diff --git a/libc/bionic/system_properties.c b/libc/bionic/system_properties.c
index 0f4e70c..caa5ca6 100644
--- a/libc/bionic/system_properties.c
+++ b/libc/bionic/system_properties.c
@@ -176,7 +176,7 @@
     addr.sun_family = AF_LOCAL;
     alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
 
-    if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen) < 0)) {
+    if(TEMP_FAILURE_RETRY(connect(s, (struct sockaddr *) &addr, alen)) < 0) {
         close(s);
         return result;
     }
diff --git a/libc/include/sys/atomics.h b/libc/include/sys/atomics.h
index 3ada8de..143bc4b 100644
--- a/libc/include/sys/atomics.h
+++ b/libc/include/sys/atomics.h
@@ -47,20 +47,20 @@
 #define __ATOMIC_INLINE__ static __inline__ __attribute__((always_inline))
 
 __ATOMIC_INLINE__ int
-__atomic_cmpxchg(int old, int _new, volatile int *ptr)
+__atomic_cmpxchg(int old_value, int new_value, volatile int* ptr)
 {
     /* We must return 0 on success */
-    return __sync_val_compare_and_swap(ptr, old, _new) != old;
+    return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
 }
 
 __ATOMIC_INLINE__ int
-__atomic_swap(int _new, volatile int *ptr)
+__atomic_swap(int new_value, volatile int *ptr)
 {
-    int prev;
+    int old_value;
     do {
-        prev = *ptr;
-    } while (__sync_val_compare_and_swap(ptr, prev, _new) != prev);
-    return prev;
+        old_value = *ptr;
+    } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value);
+    return old_value;
 }
 
 __ATOMIC_INLINE__ int
diff --git a/libc/include/time.h b/libc/include/time.h
index 8867b32..e280e0a 100644
--- a/libc/include/time.h
+++ b/libc/include/time.h
@@ -103,6 +103,7 @@
 #define CLOCK_THREAD_CPUTIME_ID    3
 #define CLOCK_REALTIME_HR          4
 #define CLOCK_MONOTONIC_HR         5
+#define CLOCK_BOOTTIME             7
 
 extern int  timer_create(int, struct sigevent*, timer_t*);
 extern int  timer_delete(timer_t);
diff --git a/libc/private/bionic_atomic_gcc_builtin.h b/libc/private/bionic_atomic_gcc_builtin.h
index e7c5761..2919f7f 100644
--- a/libc/private/bionic_atomic_gcc_builtin.h
+++ b/libc/private/bionic_atomic_gcc_builtin.h
@@ -31,18 +31,17 @@
 __bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr)
 {
     /* We must return 0 on success */
-    return __sync_bool_compare_and_swap(ptr, old_value, new_value) == 0;
+    return __sync_val_compare_and_swap(ptr, old_value, new_value) != old_value;
 }
 
 __ATOMIC_INLINE__ int32_t
 __bionic_swap(int32_t new_value, volatile int32_t* ptr)
 {
-    int32_t prev;
+    int32_t old_value;
     do {
-        prev = *ptr;
-        status = __sync_val_compare_and_swap(ptr, prev, new_value);
-    } while (status == 0);
-    return prev;
+        old_value = *ptr;
+    } while (__sync_val_compare_and_swap(ptr, old_value, new_value) != old_value);
+    return old_value;
 }
 
 __ATOMIC_INLINE__ int32_t
diff --git a/libc/private/bionic_atomic_inline.h b/libc/private/bionic_atomic_inline.h
index 821ad39..6819af6 100644
--- a/libc/private/bionic_atomic_inline.h
+++ b/libc/private/bionic_atomic_inline.h
@@ -50,11 +50,13 @@
 #define  __ATOMIC_INLINE__  static __inline__ __attribute__((always_inline))
 
 #ifdef __arm__
-#  include <bionic_atomic_arm.h>
+#  include "bionic_atomic_arm.h"
 #elif defined(__i386__)
-#  include <bionic_atomic_x86.h>
+#  include "bionic_atomic_x86.h"
+#elif defined(__mips__)
+#  include "bionic_atomic_mips.h"
 #else
-#  include <bionic_atomic_gcc_builtin.h>
+#  include "bionic_atomic_gcc_builtin.h"
 #endif
 
 #define ANDROID_MEMBAR_FULL  __bionic_memory_barrier
diff --git a/libc/private/bionic_atomic_mips.h b/libc/private/bionic_atomic_mips.h
new file mode 100644
index 0000000..28fe88d
--- /dev/null
+++ b/libc/private/bionic_atomic_mips.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#ifndef BIONIC_ATOMIC_MIPS_H
+#define BIONIC_ATOMIC_MIPS_H
+
+/* Define a full memory barrier, this is only needed if we build the
+ * platform for a multi-core device.
+ */
+#if defined(ANDROID_SMP) && ANDROID_SMP == 1
+__ATOMIC_INLINE__ void
+__bionic_memory_barrier()
+{
+    __asm__ __volatile__ ( "sync" : : : "memory" );
+}
+#else
+__ATOMIC_INLINE__ void
+__bionic_memory_barrier()
+{
+    /* A simple compiler barrier */
+    __asm__ __volatile__ ( "" : : : "memory" );
+}
+#endif
+
+/* Compare-and-swap, without any explicit barriers. Note that this function
+ * returns 0 on success, and 1 on failure. The opposite convention is typically
+ * used on other platforms.
+ */
+__ATOMIC_INLINE__ int
+__bionic_cmpxchg(int32_t old_value, int32_t new_value, volatile int32_t* ptr)
+{
+    int32_t prev, status;
+    __asm__ __volatile__ ("1: move %[status], %[new_value]  \n"
+                          "   ll %[prev], 0(%[ptr])         \n"
+                          "   bne %[old_value], %[prev], 2f \n"
+                          "   sc   %[status], 0(%[ptr])     \n"
+                          "   beqz %[status], 1b            \n"
+                          "2:                               \n"
+                          : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
+                          : [new_value]"r"(new_value), [old_value]"r"(old_value), [ptr]"r"(ptr)
+                          : "memory");
+    return prev != old_value;
+}
+
+
+/* Swap, without any explicit barriers */
+__ATOMIC_INLINE__ int32_t
+__bionic_swap(int32_t new_value, volatile int32_t *ptr)
+{
+   int32_t prev, status;
+    __asm__ __volatile__ ("1:  move %[status], %[new_value] \n"
+                          "    ll %[prev], 0(%[ptr])        \n"
+                          "    sc %[status], 0(%[ptr])      \n"
+                          "    beqz %[status], 1b           \n"
+                          : [prev]"=&r"(prev), [status]"=&r"(status), "+m"(*ptr)
+                          : [ptr]"r"(ptr), [new_value]"r"(new_value)
+                          : "memory");
+    return prev;
+}
+
+/* Atomic increment, without explicit barriers */
+__ATOMIC_INLINE__ int32_t
+__bionic_atomic_inc(volatile int32_t *ptr)
+{
+    int32_t prev, status;
+    __asm__ __volatile__ ("1:  ll %[prev], 0(%[ptr])        \n"
+                          "    addiu %[status], %[prev], 1  \n"
+                          "    sc   %[status], 0(%[ptr])    \n"
+                          "    beqz %[status], 1b           \n"
+                          : [prev]"=&r" (prev), [status]"=&r"(status), "+m" (*ptr)
+                          : [ptr]"r"(ptr)
+                          : "memory");
+    return prev;
+}
+
+/* Atomic decrement, without explicit barriers */
+__ATOMIC_INLINE__ int32_t
+__bionic_atomic_dec(volatile int32_t *ptr)
+{
+    int32_t prev, status;
+    __asm__ __volatile__ ("1:  ll %[prev], 0(%[ptr])        \n"
+                          "    addiu %[status], %[prev], -1 \n"
+                          "    sc   %[status], 0(%[ptr])    \n"
+                          "    beqz %[status], 1b           \n"
+                          : [prev]"=&r" (prev), [status]"=&r"(status), "+m" (*ptr)
+                          : [ptr]"r"(ptr)
+                          : "memory");
+    return prev;
+}
+#endif /* BIONIC_ATOMIC_MIPS_H */
diff --git a/libc/stdio/vfprintf.c b/libc/stdio/vfprintf.c
index dac8496..427fc7f 100644
--- a/libc/stdio/vfprintf.c
+++ b/libc/stdio/vfprintf.c
@@ -559,7 +559,7 @@
 #endif /* FLOATING_POINT */
 /* the Android security team suggests removing support for %n
  * since it has no real practical value, and could lead to
- * running malicious code (for really bugy programs that
+ * running malicious code (for really buggy programs that
  * send to printf() user-generated formatting strings).
  */
 #if 0
diff --git a/libc/stdlib/assert.c b/libc/stdlib/assert.c
index 816b050..7c0a860 100644
--- a/libc/stdlib/assert.c
+++ b/libc/stdlib/assert.c
@@ -32,23 +32,23 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <private/logd.h>
 
-void
-__assert(const char *file, int line, const char *failedexpr)
-{
-	(void)fprintf(stderr,
-	    "assertion \"%s\" failed: file \"%s\", line %d\n",
-	    failedexpr, file, line);
-	abort();
-	/* NOTREACHED */
+// We log to stderr for the benefit of "adb shell" users, and the log for the benefit
+// of regular app developers who want to see their asserts.
+
+void __assert(const char* file, int line, const char* failed_expression) {
+  const char* fmt = "%s:%d: assertion \"%s\" failed\n";
+  __libc_android_log_print(ANDROID_LOG_FATAL, "libc", fmt, file, line, failed_expression);
+  fprintf(stderr, fmt, file, line, failed_expression);
+  abort();
+  /* NOTREACHED */
 }
 
-void
-__assert2(const char *file, int line, const char *func, const char *failedexpr)
-{
-	(void)fprintf(stderr,
-	    "assertion \"%s\" failed: file \"%s\", line %d, function \"%s\"\n",
-	    failedexpr, file, line, func);
-	abort();
-	/* NOTREACHED */
+void __assert2(const char* file, int line, const char* function, const char* failed_expression) {
+  const char* fmt = "%s:%d: %s: assertion \"%s\" failed\n";
+  __libc_android_log_print(ANDROID_LOG_FATAL, "libc", fmt, file, line, function, failed_expression);
+  fprintf(stderr, fmt, file, line, function, failed_expression);
+  abort();
+  /* NOTREACHED */
 }
diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
index f4bcab9..55b7132 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -131,6 +131,7 @@
 	if (__atexit_invalid)
 		return;
 
+	_ATEXIT_LOCK();
 	call_depth++;
 
 	for (p = __atexit; p != NULL; p = p->next) {
@@ -149,6 +150,7 @@
 				p->fns[n].fn_ptr.cxa_func = NULL;
 				mprotect(p, pgsize, PROT_READ);
 			}
+			_ATEXIT_UNLOCK();
 #if ANDROID
                         /* it looks like we should always call the function
                          * with an argument, even if dso is not NULL. Otherwise
@@ -162,6 +164,7 @@
 			else
 				(*fn.fn_ptr.std_func)();
 #endif /* !ANDROID */
+			_ATEXIT_LOCK();
 		}
 	}
 
@@ -178,6 +181,7 @@
 		}
 		__atexit = NULL;
 	}
+	_ATEXIT_UNLOCK();
 }
 
 /*
diff --git a/libc/tools/zoneinfo/generate b/libc/tools/zoneinfo/generate
index 3e21d0b..ab2617f 100755
--- a/libc/tools/zoneinfo/generate
+++ b/libc/tools/zoneinfo/generate
@@ -1,82 +1,131 @@
-#!/bin/bash
+#!/usr/bin/python
 # Run with no arguments from any directory, with no special setup required.
 
-# Abort if any command returns an error exit status, or if an undefined
-# variable is used.
-set -e
-set -u
+import ftplib
+import hashlib
+import os
+import re
+import shutil
+import string
+import subprocess
+import sys
+import tarfile
+import tempfile
 
-echo "Looking for bionic..."
-bionic_dir=$(cd $(dirname $0)/../../.. && pwd)
-bionic_zoneinfo_dir=$bionic_dir/libc/zoneinfo
-bionic_zoneinfo_tools_dir=$bionic_dir/libc/tools/zoneinfo
-if [[ ! -d "$bionic_zoneinfo_dir" || ! -d "$bionic_zoneinfo_tools_dir" ]]; then
-  echo "Can't find bionic's zoneinfo directories!"
-  exit 1
-fi
+# Find the bionic directory, searching upward from this script.
+bionic_libc_tools_zoneinfo_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
+bionic_libc_tools_dir = os.path.dirname(bionic_libc_tools_zoneinfo_dir)
+bionic_libc_dir = os.path.dirname(bionic_libc_tools_dir)
+bionic_dir = os.path.dirname(bionic_libc_dir)
+bionic_libc_zoneinfo_dir = '%s/libc/zoneinfo' % bionic_dir
+if not os.path.isdir(bionic_libc_tools_zoneinfo_dir) or not os.path.isdir(bionic_libc_zoneinfo_dir):
+  print "Couldn't find bionic/libc/tools/zoneinfo!"
+  sys.exit(1)
+print 'Found bionic in %s...' % bionic_dir
 
-echo "Switching to temporary directory..."
-temp_dir=`mktemp -d`
-cd $temp_dir
-trap "rm -rf $temp_dir; exit" INT TERM EXIT
+
+regions = ['africa', 'antarctica', 'asia', 'australasia', 'backward', 'etcetera', 'europe', 'northamerica', 'southamerica']
+
+
+def current_tzdata_version():
+  return open('%s/zoneinfo.version' % bionic_libc_zoneinfo_dir).readline().rstrip('\n')
+
+
+def md5_file(filename):
+  md5 = hashlib.md5()
+  f = open(filename, 'rb')
+  while True:
+    data = f.read(8192)
+    if not data:
+      break
+    md5.update(data)
+  return md5.hexdigest()
+
+
+def upgrade_to(ftp, filename):
+  version = re.search('tzdata(.+)\.tar\.gz', filename).group(1)
+
+  # Switch to a temporary directory.
+  tmp_dir = tempfile.mkdtemp('-tzdata')
+  os.chdir(tmp_dir)
+  print 'Created temporary directory "%s"...' % tmp_dir
+
+  print 'Downloading %s...' % filename
+  ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
+  print 'MD5: %s' % md5_file(filename)
+
+  print 'Extracting...'
+  os.mkdir('extracted')
+  tar = tarfile.open(filename, 'r')
+  tar.extractall('extracted')
+
+  print 'Calling zic(1)...'
+  os.mkdir('data')
+  for region in regions:
+    if region != 'backward':
+      subprocess.check_call(['zic', '-d', 'data', 'extracted/%s' % region])
+
+  # Collect the data ZoneCompactor needs.
+  links = []
+  zones = []
+  for region in regions:
+    for line in open('extracted/%s' % region).readlines():
+      fields = string.split(line)
+      if len(fields) == 0:
+        continue
+      elif fields[0] == 'Link':
+        links.append('%s %s %s\n' % (fields[0], fields[1], fields[2]))
+        zones.append(fields[2])
+      elif fields[0] == 'Zone':
+        zones.append(fields[1])
+  zones.sort()
+
+  # Write it into the "setup" file.
+  setup = open('setup', 'w')
+  for link in links:
+    setup.write(link)
+  for zone in zones:
+    setup.write('%s\n' % zone)
+  setup.close()
+
+  print 'Calling ZoneCompactor...'
+  subprocess.check_call(['javac', '-d', '.',
+                         '%s/ZoneCompactor.java' % bionic_libc_tools_zoneinfo_dir,
+                         '%s/ZoneInfo.java' % bionic_libc_tools_zoneinfo_dir])
+  subprocess.check_call(['java', 'ZoneCompactor', 'setup', 'data'])
+
+  print 'Updating bionic from %s to %s...' % (current_tzdata_version(), version)
+  # Move the .dat and .idx files...
+  os.remove('%s/zoneinfo.dat' % bionic_libc_zoneinfo_dir)
+  shutil.move('zoneinfo.dat', bionic_libc_zoneinfo_dir)
+  os.remove('%s/zoneinfo.idx' % bionic_libc_zoneinfo_dir)
+  shutil.move('zoneinfo.idx', bionic_libc_zoneinfo_dir)
+  # Write the .version file...
+  zoneinfo_version = open('%s/zoneinfo.version' % bionic_libc_zoneinfo_dir, 'wb+')
+  zoneinfo_version.write('%s\n' % version)
+  zoneinfo_version.close()
+
 
 # URL from "Sources for Time Zone and Daylight Saving Time Data"
 # http://www.twinsun.com/tz/tz-link.htm
-echo "Looking for new tzdata..."
-wget -N --no-verbose 'ftp://munnari.oz.au/pub/tzdata*.tar.gz'
-zoneinfo_version_file=$bionic_zoneinfo_dir/zoneinfo.version
-if [ -f "$zoneinfo_version_file" ]; then
-  current_version=tzdata`sed s/\n// < $zoneinfo_version_file`
-else
-  current_version=missing
-fi
-latest_archive=`ls -r -v tzdata*.tar.gz | head -n1`
-latest_version=`basename $latest_archive .tar.gz`
-if [ "$current_version" == "$latest_version" ]; then
-  echo "You already have the latest tzdata ($latest_version)!"
-  exit 1
-fi
 
-md5_sum=`md5sum $latest_archive`
-echo "MD5: $md5_sum"
+print 'Looking for new tzdata...'
+ftp = ftplib.FTP('ftp.iana.org')
+ftp.login()
+ftp.cwd('tz/releases')
+tzdata_filenames = []
+for filename in ftp.nlst():
+  if filename.startswith('tzdata20'):
+    tzdata_filenames.append(filename)
+tzdata_filenames.sort()
 
-echo "Extracting $latest_version..."
-mkdir $latest_version
-tar -C $latest_version -zxf $latest_archive
+# If you're several releases behind, we'll walk you through the upgrades one by one.
+current_version = current_tzdata_version()
+current_filename = 'tzdata%s.tar.gz' % current_version
+for filename in tzdata_filenames:
+  if filename > current_filename:
+    upgrade_to(ftp, filename)
+    sys.exit(0)
 
-echo "Compiling $latest_version..."
-mkdir data
-for i in \
-    africa \
-    antarctica \
-    asia \
-    australasia \
-    etcetera \
-    europe \
-    factory \
-    northamerica \
-    solar87 \
-    solar88 \
-    solar89 \
-    southamerica
-do
-    zic -d data $latest_version/$i
-done
-
-echo "Compacting $latest_version..."
-(
-    cat $latest_version/* | grep '^Link' | awk '{print $1, $2, $3}'
-    (
-        cat $latest_version/* | grep '^Zone' | awk '{print $2}'
-        cat $latest_version/* | grep '^Link' | awk '{print $3}'
-    ) | LC_ALL="C" sort
-) | grep -v Riyadh8 > setup
-
-javac -d . \
-    $bionic_zoneinfo_tools_dir/ZoneCompactor.java \
-    $bionic_zoneinfo_tools_dir/ZoneInfo.java
-java ZoneCompactor setup data
-
-echo "Updating bionic to $latest_version..."
-mv zoneinfo.dat zoneinfo.idx $bionic_zoneinfo_dir
-echo $latest_version | sed 's/tzdata//' > $bionic_zoneinfo_dir/zoneinfo.version
+print 'You already have the latest tzdata (%s)!' % current_version
+sys.exit(0)
diff --git a/libc/unistd/abort.c b/libc/unistd/abort.c
index a3f8c54..4dffbae 100644
--- a/libc/unistd/abort.c
+++ b/libc/unistd/abort.c
@@ -34,11 +34,6 @@
 #include "thread_private.h"
 #include "atexit.h"
 
-/* temporary, for bug hunting */
-#include "logd.h"
-#define debug_log(format, ...)  \
-    __libc_android_log_print(ANDROID_LOG_DEBUG, "libc-abort", (format), ##__VA_ARGS__ )
-
 #ifdef __arm__
 __LIBC_HIDDEN__ void
 __libc_android_abort(void)
@@ -51,7 +46,7 @@
 	static int cleanup_called = 0;
 	sigset_t mask;
 
-  
+
 	sigfillset(&mask);
 	/*
 	 * don't block SIGABRT to give any handler a chance; we ignore
@@ -79,7 +74,12 @@
 
     /* temporary, for bug hunting */
     /* seg fault seems to produce better debuggerd results than SIGABRT */
+#ifdef __mips__
+    /* An access that will generate SIGSEGV rather than SIGBUS. */
+    *((char*)0xdeadc0c0) = 39;
+#else
     *((char*)0xdeadbaad) = 39;
+#endif
     /* -- */
 
 	(void)kill(getpid(), SIGABRT);
diff --git a/libc/zoneinfo/Android.mk b/libc/zoneinfo/Android.mk
index ef700e8..716aab2 100644
--- a/libc/zoneinfo/Android.mk
+++ b/libc/zoneinfo/Android.mk
@@ -1,26 +1,67 @@
 LOCAL_PATH:= $(call my-dir)
+
+############################################
 include $(CLEAR_VARS)
+LOCAL_MODULE := zoneinfo.dat
+LOCAL_SRC_FILES := $(LOCAL_MODULE)
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_PATH := $(TARGET_OUT)/usr/share/zoneinfo
+include $(BUILD_PREBUILT)
 
-ALL_PREBUILT += $(TARGET_OUT)/usr/share/zoneinfo/zoneinfo.dat
-$(TARGET_OUT)/usr/share/zoneinfo/zoneinfo.dat : $(LOCAL_PATH)/zoneinfo.dat | $(ACP)
-	$(transform-prebuilt-to-target)
+############################################
+include $(CLEAR_VARS)
+LOCAL_MODULE := zoneinfo.idx
+LOCAL_SRC_FILES := $(LOCAL_MODULE)
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_PATH := $(TARGET_OUT)/usr/share/zoneinfo
+include $(BUILD_PREBUILT)
 
-ALL_PREBUILT += $(TARGET_OUT)/usr/share/zoneinfo/zoneinfo.idx
-$(TARGET_OUT)/usr/share/zoneinfo/zoneinfo.idx : $(LOCAL_PATH)/zoneinfo.idx | $(ACP)
-	$(transform-prebuilt-to-target)
+############################################
+include $(CLEAR_VARS)
+LOCAL_MODULE := zoneinfo.version
+LOCAL_SRC_FILES := $(LOCAL_MODULE)
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_PATH := $(TARGET_OUT)/usr/share/zoneinfo
+include $(BUILD_PREBUILT)
 
-ALL_PREBUILT += $(TARGET_OUT)/usr/share/zoneinfo/zoneinfo.version
-$(TARGET_OUT)/usr/share/zoneinfo/zoneinfo.version : $(LOCAL_PATH)/zoneinfo.version | $(ACP)
-	$(transform-prebuilt-to-target)
 
 # The host build doesn't use bionic, but it does use bionic's zoneinfo data
 ifeq ($(WITH_HOST_DALVIK),true)
-    ALL_PREBUILT += $(HOST_OUT)/usr/share/zoneinfo/zoneinfo.dat
-    $(eval $(call copy-one-file,$(LOCAL_PATH)/zoneinfo.dat,$(HOST_OUT)/usr/share/zoneinfo/zoneinfo.dat))
 
-    ALL_PREBUILT += $(HOST_OUT)/usr/share/zoneinfo/zoneinfo.idx
-    $(eval $(call copy-one-file,$(LOCAL_PATH)/zoneinfo.idx,$(HOST_OUT)/usr/share/zoneinfo/zoneinfo.idx))
+############################################
+include $(CLEAR_VARS)
+LOCAL_MODULE := zoneinfo-host.dat
+LOCAL_IS_HOST_MODULE := true
+LOCAL_SRC_FILES := zoneinfo.dat
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_STEM := $(LOCAL_SRC_FILES)
+LOCAL_MODULE_PATH := $(HOST_OUT)/usr/share/zoneinfo
+include $(BUILD_PREBUILT)
 
-    ALL_PREBUILT += $(HOST_OUT)/usr/share/zoneinfo/zoneinfo.version
-    $(eval $(call copy-one-file,$(LOCAL_PATH)/zoneinfo.version,$(HOST_OUT)/usr/share/zoneinfo/zoneinfo.version))
+############################################
+include $(CLEAR_VARS)
+LOCAL_MODULE := zoneinfo-host.idx
+LOCAL_IS_HOST_MODULE := true
+LOCAL_SRC_FILES := zoneinfo.idx
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_STEM := $(LOCAL_SRC_FILES)
+LOCAL_MODULE_PATH := $(HOST_OUT)/usr/share/zoneinfo
+include $(BUILD_PREBUILT)
+
+############################################
+include $(CLEAR_VARS)
+LOCAL_MODULE := zoneinfo-host.version
+LOCAL_IS_HOST_MODULE := true
+LOCAL_SRC_FILES := zoneinfo.version
+LOCAL_MODULE_CLASS := ETC
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_STEM := $(LOCAL_SRC_FILES)
+LOCAL_MODULE_PATH := $(HOST_OUT)/usr/share/zoneinfo
+include $(BUILD_PREBUILT)
+
 endif
diff --git a/libc/zoneinfo/zoneinfo.dat b/libc/zoneinfo/zoneinfo.dat
index cc1cb38..82d6b9c 100644
--- a/libc/zoneinfo/zoneinfo.dat
+++ b/libc/zoneinfo/zoneinfo.dat
Binary files differ
diff --git a/libc/zoneinfo/zoneinfo.idx b/libc/zoneinfo/zoneinfo.idx
index 0483c28..2268706 100644
--- a/libc/zoneinfo/zoneinfo.idx
+++ b/libc/zoneinfo/zoneinfo.idx
Binary files differ
diff --git a/libc/zoneinfo/zoneinfo.version b/libc/zoneinfo/zoneinfo.version
index 9412b94..e606761 100644
--- a/libc/zoneinfo/zoneinfo.version
+++ b/libc/zoneinfo/zoneinfo.version
@@ -1 +1 @@
-2012c
+2012d
diff --git a/libstdc++/Android.mk b/libstdc++/Android.mk
index 8bc181f..7d27aa8 100644
--- a/libstdc++/Android.mk
+++ b/libstdc++/Android.mk
@@ -3,7 +3,7 @@
 # Common C++ flags to build this library.
 # Note that we need to access private Bionic headers
 # and define ANDROID_SMP accordingly.
-libstdc++_cflags := -Ibionic/libc/private
+libstdc++_cflags := -Ibionic/libc/
 ifeq ($(TARGET_CPU_SMP),true)
     libstdc++_cflags += -DANDROID_SMP=1
 else
diff --git a/libstdc++/src/one_time_construction.cpp b/libstdc++/src/one_time_construction.cpp
index 1eac3b1..3cfb213 100644
--- a/libstdc++/src/one_time_construction.cpp
+++ b/libstdc++/src/one_time_construction.cpp
@@ -11,8 +11,8 @@
 #include <stddef.h>
 #include <sys/atomics.h>
 #include <endian.h>
-#include <bionic_futex.h>
-#include <bionic_atomic_inline.h>
+#include <private/bionic_futex.h>
+#include <private/bionic_atomic_inline.h>
 
 // ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
 // one time construction:
diff --git a/libstdc++/src/pure_virtual.cpp b/libstdc++/src/pure_virtual.cpp
index 663c1e9..affb80f 100644
--- a/libstdc++/src/pure_virtual.cpp
+++ b/libstdc++/src/pure_virtual.cpp
@@ -1,10 +1,8 @@
+#undef NDEBUG
+#include <assert.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-
-extern "C" void __cxa_pure_virtual()
-{
-    fprintf(stderr, "Pure virtual function called.  Are you calling virtual methods from a destructor?\n");
-    abort();
+extern "C" void __cxa_pure_virtual() {
+  // We can't call __libc_android_log_write from libstdc++ because it's private, so cheat.
+  assert(!"Pure virtual function called. Are you calling virtual methods from a destructor?");
+  /* NOTREACHED */
 }
-
diff --git a/linker/linker.c b/linker/linker.c
index b96e072..0a93130 100644
--- a/linker/linker.c
+++ b/linker/linker.c
@@ -648,16 +648,15 @@
 static unsigned long
 is_prelinked(int fd, const char *name)
 {
-    off_t sz;
-    prelink_info_t info;
-
-    sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
+    off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
     if (sz < 0) {
         DL_ERR("lseek() failed!");
         return 0;
     }
 
-    if (TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)) != sizeof(info))) {
+    prelink_info_t info;
+    int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
+    if (rc != sizeof(info)) {
         WARN("Could not read prelink_info_t structure for `%s`\n", name);
         return 0;
     }