Merge "flesh out sysconf(3) and the _POSIX_* constants"
diff --git a/libc/Android.mk b/libc/Android.mk
index 3e37ca7..ef1fb9b 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -479,6 +479,7 @@
     upstream-openbsd/lib/libc/stdlib/getenv.c \
     upstream-openbsd/lib/libc/stdlib/insque.c \
     upstream-openbsd/lib/libc/stdlib/lsearch.c \
+    upstream-openbsd/lib/libc/stdlib/reallocarray.c \
     upstream-openbsd/lib/libc/stdlib/remque.c \
     upstream-openbsd/lib/libc/stdlib/setenv.c \
     upstream-openbsd/lib/libc/stdlib/strtoimax.c \
diff --git a/libc/arch-arm/include/machine/endian.h b/libc/arch-arm/include/machine/endian.h
index 8d9723d..04bba20 100644
--- a/libc/arch-arm/include/machine/endian.h
+++ b/libc/arch-arm/include/machine/endian.h
@@ -67,11 +67,7 @@
 
 #endif  /* __GNUC__ */
 
-#if defined(__ARMEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
 #define __STRICT_ALIGNMENT
 #include <sys/types.h>
 #include <sys/endian.h>
diff --git a/libc/arch-arm64/include/machine/endian.h b/libc/arch-arm64/include/machine/endian.h
index 87a038d..4743733 100644
--- a/libc/arch-arm64/include/machine/endian.h
+++ b/libc/arch-arm64/include/machine/endian.h
@@ -29,9 +29,6 @@
 #ifndef _AARCH64_ENDIAN_H_
 #define _AARCH64_ENDIAN_H_
 
-#include <sys/types.h>
-#include <sys/endian.h>
-
 #ifdef __GNUC__
 
 #define __swap16md(x) ({                                        \
@@ -49,10 +46,8 @@
 
 #endif  /* __GNUC__ */
 
-#if defined(__AARCH64EB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
+#include <sys/types.h>
+#include <sys/endian.h>
 
 #endif /* _AARCH64_ENDIAN_H_ */
diff --git a/libc/arch-mips/bionic/atexit.h b/libc/arch-mips/bionic/atexit.h
deleted file mode 100644
index 759008c..0000000
--- a/libc/arch-mips/bionic/atexit.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-extern void *__dso_handle;
-extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
-
-__attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
-}
diff --git a/libc/arch-mips/bionic/crtbegin.c b/libc/arch-mips/bionic/crtbegin.c
index 28e8817..50e9eeb 100644
--- a/libc/arch-mips/bionic/crtbegin.c
+++ b/libc/arch-mips/bionic/crtbegin.c
@@ -91,4 +91,4 @@
 );
 
 #include "../../arch-common/bionic/__dso_handle.h"
-#include "atexit.h"
+#include "../../arch-common/bionic/atexit.h"
diff --git a/libc/arch-mips/include/machine/endian.h b/libc/arch-mips/include/machine/endian.h
index 41a9004..9270e9d 100644
--- a/libc/arch-mips/include/machine/endian.h
+++ b/libc/arch-mips/include/machine/endian.h
@@ -58,11 +58,7 @@
 #endif  /* __mips32r2__ */
 #endif  /* __GNUC__ */
 
-#if defined(__MIPSEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
 #define __STRICT_ALIGNMENT
 #include <sys/types.h>
 #include <sys/endian.h>
diff --git a/libc/include/sgidefs.h b/libc/arch-mips/include/sgidefs.h
similarity index 100%
rename from libc/include/sgidefs.h
rename to libc/arch-mips/include/sgidefs.h
diff --git a/libc/arch-mips64/bionic/atexit.h b/libc/arch-mips64/bionic/atexit.h
deleted file mode 100644
index 759008c..0000000
--- a/libc/arch-mips64/bionic/atexit.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-extern void *__dso_handle;
-extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
-
-__attribute__ ((visibility ("hidden")))
-int atexit(void (*func)(void))
-{
-  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
-}
diff --git a/libc/arch-mips64/bionic/crtbegin.c b/libc/arch-mips64/bionic/crtbegin.c
index 2ea31ad..1374fea 100644
--- a/libc/arch-mips64/bionic/crtbegin.c
+++ b/libc/arch-mips64/bionic/crtbegin.c
@@ -91,4 +91,4 @@
 );
 
 #include "../../arch-common/bionic/__dso_handle.h"
-#include "atexit.h"
+#include "../../arch-common/bionic/atexit.h"
diff --git a/libc/arch-mips64/bionic/crtbegin_so.c b/libc/arch-mips64/bionic/crtbegin_so.c
deleted file mode 100644
index d664ce6..0000000
--- a/libc/arch-mips64/bionic/crtbegin_so.c
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-extern void __cxa_finalize(void *);
-extern void *__dso_handle;
-
-__attribute__((visibility("hidden"),destructor))
-void __on_dlclose() {
-  __cxa_finalize(&__dso_handle);
-}
-
-#include "../../arch-common/bionic/__dso_handle_so.h"
-#include "atexit.h"
diff --git a/libc/arch-mips64/include/machine/endian.h b/libc/arch-mips64/include/machine/endian.h
index 41a9004..9270e9d 100644
--- a/libc/arch-mips64/include/machine/endian.h
+++ b/libc/arch-mips64/include/machine/endian.h
@@ -58,11 +58,7 @@
 #endif  /* __mips32r2__ */
 #endif  /* __GNUC__ */
 
-#if defined(__MIPSEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
 #define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
 #define __STRICT_ALIGNMENT
 #include <sys/types.h>
 #include <sys/endian.h>
diff --git a/libc/arch-x86_64/include/machine/endian.h b/libc/arch-x86_64/include/machine/endian.h
index 7889a37..8a3b0c5 100644
--- a/libc/arch-x86_64/include/machine/endian.h
+++ b/libc/arch-x86_64/include/machine/endian.h
@@ -56,6 +56,7 @@
 #endif	/* __GNUC__ */
 
 #define _BYTE_ORDER _LITTLE_ENDIAN
+#include <sys/types.h>
 #include <sys/endian.h>
 
 #endif /* _MACHINE_ENDIAN_H_ */
diff --git a/libc/bionic/flockfile.cpp b/libc/bionic/flockfile.cpp
index 3381e8e..b73907cb 100644
--- a/libc/bionic/flockfile.cpp
+++ b/libc/bionic/flockfile.cpp
@@ -36,12 +36,20 @@
 // struct __sfileext (see fileext.h).
 
 void flockfile(FILE* fp) {
+  if (!__sdidinit) {
+    __sinit();
+  }
+
   if (fp != NULL) {
     pthread_mutex_lock(&_FLOCK(fp));
   }
 }
 
 int ftrylockfile(FILE* fp) {
+  if (!__sdidinit) {
+    __sinit();
+  }
+
   // The specification for ftrylockfile() says it returns 0 on success,
   // or non-zero on error. So return an errno code directly on error.
   if (fp == NULL) {
@@ -52,6 +60,10 @@
 }
 
 void funlockfile(FILE* fp) {
+  if (!__sdidinit) {
+    __sinit();
+  }
+
   if (fp != NULL) {
     pthread_mutex_unlock(&_FLOCK(fp));
   }
diff --git a/libc/bionic/fts.c b/libc/bionic/fts.c
index c491b6a..31a4b2f 100644
--- a/libc/bionic/fts.c
+++ b/libc/bionic/fts.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: fts.c,v 1.46 2014/05/25 17:47:04 tedu Exp $	*/
+/*	$OpenBSD: fts.c,v 1.48 2014/11/20 04:14:15 guenther Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -49,11 +49,12 @@
 static void	 fts_padjust(FTS *, FTSENT *);
 static int	 fts_palloc(FTS *, size_t);
 static FTSENT	*fts_sort(FTS *, FTSENT *, int);
-static u_short	 fts_stat(FTS *, FTSENT *, int);
+static u_short	 fts_stat(FTS *, FTSENT *, int, int);
 static int	 fts_safe_changedir(FTS *, FTSENT *, int, char *);
 
 #define ALIGNBYTES (sizeof(uintptr_t) - 1)
 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) &~ ALIGNBYTES)
+void* reallocarray(void*, size_t, size_t);
 
 #define	ISDOT(a)	(a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2])))
 
@@ -119,7 +120,7 @@
 		p->fts_level = FTS_ROOTLEVEL;
 		p->fts_parent = parent;
 		p->fts_accpath = p->fts_name;
-		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW));
+		p->fts_info = fts_stat(sp, p, ISSET(FTS_COMFOLLOW), -1);
 
 		/* Command-line "." and ".." are real directories. */
 		if (p->fts_info == FTS_DOT)
@@ -273,7 +274,7 @@
 
 	/* Any type of file may be re-visited; re-stat and re-turn. */
 	if (instr == FTS_AGAIN) {
-		p->fts_info = fts_stat(sp, p, 0);
+		p->fts_info = fts_stat(sp, p, 0, -1);
 		return (p);
 	}
 
@@ -285,7 +286,7 @@
 	 */
 	if (instr == FTS_FOLLOW &&
 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) {
-		p->fts_info = fts_stat(sp, p, 1);
+		p->fts_info = fts_stat(sp, p, 1, -1);
 		if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
 			if ((p->fts_symfd = open(".", O_RDONLY, 0)) < 0) {
 				p->fts_errno = errno;
@@ -374,7 +375,7 @@
 		if (p->fts_instr == FTS_SKIP)
 			goto next;
 		if (p->fts_instr == FTS_FOLLOW) {
-			p->fts_info = fts_stat(sp, p, 1);
+			p->fts_info = fts_stat(sp, p, 1, -1);
 			if (p->fts_info == FTS_D && !ISSET(FTS_NOCHDIR)) {
 				if ((p->fts_symfd =
 				    open(".", O_RDONLY, 0)) < 0) {
@@ -719,10 +720,11 @@
 			if (ISSET(FTS_NOCHDIR)) {
 				p->fts_accpath = p->fts_path;
 				memmove(cp, p->fts_name, p->fts_namelen + 1);
-			} else
+				p->fts_info = fts_stat(sp, p, 0, dirfd(dirp));
+			} else {
 				p->fts_accpath = p->fts_name;
-			/* Stat it. */
-			p->fts_info = fts_stat(sp, p, 0);
+				p->fts_info = fts_stat(sp, p, 0, -1);
+			}
 
 			/* Decrement link count if applicable. */
 			if (nlinks > 0 && (p->fts_info == FTS_D ||
@@ -789,13 +791,20 @@
 }
 
 static u_short
-fts_stat(FTS *sp, FTSENT *p, int follow)
+fts_stat(FTS *sp, FTSENT *p, int follow, int dfd)
 {
 	FTSENT *t;
 	dev_t dev;
 	ino_t ino;
 	struct stat *sbp, sb;
 	int saved_errno;
+	const char *path;
+
+	if (dfd == -1) {
+		path = p->fts_accpath;
+		dfd = AT_FDCWD;
+	} else
+		path = p->fts_name;
 
 	/* If user needs stat info, stat buffer already allocated. */
 	sbp = ISSET(FTS_NOSTAT) ? &sb : p->fts_statp;
@@ -806,16 +815,16 @@
 	 * fail, set the errno from the stat call.
 	 */
 	if (ISSET(FTS_LOGICAL) || follow) {
-		if (stat(p->fts_accpath, sbp)) {
+		if (fstatat(dfd, path, sbp, 0)) {
 			saved_errno = errno;
-			if (!lstat(p->fts_accpath, sbp)) {
+			if (!fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
 				errno = 0;
 				return (FTS_SLNONE);
 			}
 			p->fts_errno = saved_errno;
 			goto err;
 		}
-	} else if (lstat(p->fts_accpath, sbp)) {
+	} else if (fstatat(dfd, path, sbp, AT_SYMLINK_NOFOLLOW)) {
 		p->fts_errno = errno;
 err:		memset(sbp, 0, sizeof(struct stat));
 		return (FTS_NS);
@@ -873,8 +882,8 @@
 		struct _ftsent **a;
 
 		sp->fts_nitems = nitems + 40;
-		if ((a = realloc(sp->fts_array,
-		    sp->fts_nitems * sizeof(FTSENT *))) == NULL) {
+		if ((a = reallocarray(sp->fts_array,
+		    sp->fts_nitems, sizeof(FTSENT *))) == NULL) {
 			if (sp->fts_array)
 				free(sp->fts_array);
 			sp->fts_array = NULL;
diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp
index 681e038..90aa7b8 100644
--- a/libc/bionic/locale.cpp
+++ b/libc/bionic/locale.cpp
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include <errno.h>
 #include <locale.h>
 #include <pthread.h>
 #include <stdlib.h>
diff --git a/libc/bionic/mntent.cpp b/libc/bionic/mntent.cpp
index f5a8eaa..4afacda 100644
--- a/libc/bionic/mntent.cpp
+++ b/libc/bionic/mntent.cpp
@@ -27,6 +27,7 @@
  */
 
 #include <mntent.h>
+#include <string.h>
 
 #include "private/ThreadLocalBuffer.h"
 
diff --git a/libc/bionic/pthread_atfork.cpp b/libc/bionic/pthread_atfork.cpp
index b845f7d..82e2b59 100644
--- a/libc/bionic/pthread_atfork.cpp
+++ b/libc/bionic/pthread_atfork.cpp
@@ -28,6 +28,7 @@
 
 #include <errno.h>
 #include <pthread.h>
+#include <stdlib.h>
 
 struct atfork_t {
   atfork_t* next;
diff --git a/libc/bionic/pthread_internals.cpp b/libc/bionic/pthread_internals.cpp
index 19c00d4..2270d96 100644
--- a/libc/bionic/pthread_internals.cpp
+++ b/libc/bionic/pthread_internals.cpp
@@ -28,6 +28,8 @@
 
 #include "pthread_internal.h"
 
+#include <stdlib.h>
+
 #include "private/bionic_futex.h"
 #include "private/bionic_tls.h"
 #include "private/ScopedPthreadMutexLocker.h"
diff --git a/libc/bionic/pthread_key.cpp b/libc/bionic/pthread_key.cpp
index 27eab27..b47ef22 100644
--- a/libc/bionic/pthread_key.cpp
+++ b/libc/bionic/pthread_key.cpp
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include <errno.h>
 #include <pthread.h>
 
 #include "private/bionic_tls.h"
diff --git a/libc/bionic/raise.cpp b/libc/bionic/raise.cpp
index f69d90b..0dd0ad7 100644
--- a/libc/bionic/raise.cpp
+++ b/libc/bionic/raise.cpp
@@ -27,6 +27,7 @@
  */
 
 #include <pthread.h>
+#include <signal.h>
 
 int raise(int sig) {
   int rc = pthread_kill(pthread_self(), sig);
diff --git a/libc/bionic/stubs.cpp b/libc/bionic/stubs.cpp
index b1e38be..88e5ac5 100644
--- a/libc/bionic/stubs.cpp
+++ b/libc/bionic/stubs.cpp
@@ -40,10 +40,10 @@
 #include "private/android_filesystem_config.h"
 #include "private/ErrnoRestorer.h"
 #include "private/libc_logging.h"
+#include "private/ThreadLocalBuffer.h"
 
-// Thread-specific state for the non-reentrant functions.
-static pthread_once_t stubs_once = PTHREAD_ONCE_INIT;
-static pthread_key_t stubs_key;
+GLOBAL_INIT_THREAD_LOCAL_BUFFER(stubs);
+
 struct stubs_state_t {
   passwd passwd_;
   group group_;
@@ -113,39 +113,14 @@
   return do_getpw_r(0, NULL, uid, pwd, buf, byte_count, result);
 }
 
-static stubs_state_t* stubs_state_alloc() {
-  stubs_state_t*  s = static_cast<stubs_state_t*>(calloc(1, sizeof(*s)));
-  if (s != NULL) {
-    s->group_.gr_mem = s->group_members_;
-  }
-  return s;
-}
-
-static void stubs_state_free(void* ptr) {
-  stubs_state_t* state = static_cast<stubs_state_t*>(ptr);
-  free(state);
-}
-
-static void __stubs_key_init() {
-  pthread_key_create(&stubs_key, stubs_state_free);
-}
-
 static stubs_state_t* __stubs_state() {
-  pthread_once(&stubs_once, __stubs_key_init);
-  stubs_state_t* s = static_cast<stubs_state_t*>(pthread_getspecific(stubs_key));
-  if (s == NULL) {
-    s = stubs_state_alloc();
-    if (s == NULL) {
-      errno = ENOMEM;  // Just in case.
-    } else {
-      if (pthread_setspecific(stubs_key, s) != 0) {
-        stubs_state_free(s);
-        errno = ENOMEM;
-        s = NULL;
-      }
-    }
+  LOCAL_INIT_THREAD_LOCAL_BUFFER(stubs_state_t*, stubs, sizeof(stubs_state_t));
+
+  if (stubs_tls_buffer != NULL) {
+    memset(stubs_tls_buffer, 0, sizeof(stubs_state_t));
+    stubs_tls_buffer->group_.gr_mem = stubs_tls_buffer->group_members_;
   }
-  return s;
+  return stubs_tls_buffer;
 }
 
 static passwd* android_iinfo_to_passwd(stubs_state_t* state,
@@ -167,7 +142,6 @@
   gr->gr_name   = (char*) iinfo->name;
   gr->gr_gid    = iinfo->aid;
   gr->gr_mem[0] = gr->gr_name;
-  gr->gr_mem[1] = NULL;
   return gr;
 }
 
@@ -208,18 +182,27 @@
 }
 
 // Translate a user/group name to the corresponding user/group id.
+// all_a1234 -> 0 * AID_USER + AID_SHARED_GID_START + 1234 (group name only)
 // u0_a1234 -> 0 * AID_USER + AID_APP + 1234
 // u2_i1000 -> 2 * AID_USER + AID_ISOLATED_START + 1000
 // u1_system -> 1 * AID_USER + android_ids['system']
 // returns 0 and sets errno to ENOENT in case of error
-static unsigned app_id_from_name(const char* name) {
-  if (name[0] != 'u' || !isdigit(name[1])) {
+static unsigned app_id_from_name(const char* name, bool is_group) {
+  char* end;
+  unsigned long userid;
+  bool is_shared_gid = false;
+
+  if (is_group && name[0] == 'a' && name[1] == 'l' && name[2] == 'l') {
+    end = const_cast<char*>(name+3);
+    userid = 0;
+    is_shared_gid = true;
+  } else if (name[0] == 'u' && isdigit(name[1])) {
+    userid = strtoul(name+1, &end, 10);
+  } else {
     errno = ENOENT;
     return 0;
   }
 
-  char* end;
-  unsigned long userid = strtoul(name+1, &end, 10);
   if (end[0] != '_' || end[1] == 0) {
     errno = ENOENT;
     return 0;
@@ -227,8 +210,17 @@
 
   unsigned long appid = 0;
   if (end[1] == 'a' && isdigit(end[2])) {
-    // end will point to \0 if the strtoul below succeeds.
-    appid = strtoul(end+2, &end, 10) + AID_APP;
+    if (is_shared_gid) {
+      // end will point to \0 if the strtoul below succeeds.
+      appid = strtoul(end+2, &end, 10) + AID_SHARED_GID_START;
+      if (appid > AID_SHARED_GID_END) {
+        errno = ENOENT;
+        return 0;
+      }
+    } else {
+      // end will point to \0 if the strtoul below succeeds.
+      appid = strtoul(end+2, &end, 10) + AID_APP;
+    }
   } else if (end[1] == 'i' && isdigit(end[2])) {
     // end will point to \0 if the strtoul below succeeds.
     appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
@@ -263,12 +255,11 @@
   return (unsigned)(appid + userid*AID_USER);
 }
 
-static void print_app_name_from_appid_userid(const uid_t appid,
-    const uid_t userid, char* buffer, const int bufferlen) {
+static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
+  const uid_t appid = uid % AID_USER;
+  const uid_t userid = uid / AID_USER;
   if (appid >= AID_ISOLATED_START) {
     snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
-  } else if (userid == 0 && appid >= AID_SHARED_GID_START) {
-    snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
   } else if (appid < AID_APP) {
     for (size_t n = 0; n < android_id_count; n++) {
       if (android_ids[n].aid == appid) {
@@ -281,10 +272,23 @@
   }
 }
 
-static void print_app_name_from_uid(const uid_t uid, char* buffer, const int bufferlen) {
-  const uid_t appid = uid % AID_USER;
-  const uid_t userid = uid / AID_USER;
-  return print_app_name_from_appid_userid(appid, userid, buffer, bufferlen);
+static void print_app_name_from_gid(const gid_t gid, char* buffer, const int bufferlen) {
+  const uid_t appid = gid % AID_USER;
+  const uid_t userid = gid / AID_USER;
+  if (appid >= AID_ISOLATED_START) {
+    snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
+  } else if (userid == 0 && appid >= AID_SHARED_GID_START && appid <= AID_SHARED_GID_END) {
+    snprintf(buffer, bufferlen, "all_a%u", appid - AID_SHARED_GID_START);
+  } else if (appid < AID_APP) {
+    for (size_t n = 0; n < android_id_count; n++) {
+      if (android_ids[n].aid == appid) {
+        snprintf(buffer, bufferlen, "u%u_%s", userid, android_ids[n].name);
+        return;
+      }
+    }
+  } else {
+    snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
+  }
 }
 
 // Translate a uid into the corresponding name.
@@ -301,12 +305,9 @@
     return NULL;
   }
 
+  print_app_name_from_uid(uid, state->app_name_buffer_, sizeof(state->app_name_buffer_));
+
   const uid_t appid = uid % AID_USER;
-  const uid_t userid = uid / AID_USER;
-
-  print_app_name_from_appid_userid(appid, userid, state->app_name_buffer_,
-                                   sizeof(state->app_name_buffer_));
-
   if (appid < AID_APP) {
       snprintf(state->dir_buffer_, sizeof(state->dir_buffer_), "/");
   } else {
@@ -332,14 +333,13 @@
     return NULL;
   }
 
-  print_app_name_from_uid(gid, state->group_name_buffer_,
-                          sizeof(state->group_name_buffer_));
+  print_app_name_from_gid(gid, state->group_name_buffer_, sizeof(state->group_name_buffer_));
 
   group* gr = &state->group_;
   gr->gr_name   = state->group_name_buffer_;
   gr->gr_gid    = gid;
   gr->gr_mem[0] = gr->gr_name;
-  gr->gr_mem[1] = NULL;
+
   return gr;
 }
 
@@ -367,7 +367,7 @@
   if (pw != NULL) {
     return pw;
   }
-  return app_id_to_passwd(app_id_from_name(login), state);
+  return app_id_to_passwd(app_id_from_name(login, false), state);
 }
 
 // All users are in just one group, the one passed in.
@@ -395,7 +395,6 @@
   if (gr != NULL) {
     return gr;
   }
-
   return app_id_to_group(gid, state);
 }
 
@@ -408,8 +407,7 @@
   if (android_name_to_group(&state->group_, name) != 0) {
     return &state->group_;
   }
-
-  return app_id_to_group(app_id_from_name(name), state);
+  return app_id_to_group(app_id_from_name(name, true), state);
 }
 
 // We don't have an /etc/networks, so all inputs return NULL.
diff --git a/libc/arch-mips/bionic/crtbegin_so.c b/libc/include/machine/pthread_types.h
similarity index 75%
rename from libc/arch-mips/bionic/crtbegin_so.c
rename to libc/include/machine/pthread_types.h
index d664ce6..900541c 100644
--- a/libc/arch-mips/bionic/crtbegin_so.c
+++ b/libc/include/machine/pthread_types.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2008 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,13 +26,23 @@
  * SUCH DAMAGE.
  */
 
-extern void __cxa_finalize(void *);
-extern void *__dso_handle;
+#ifndef _MACHINE_PTHREAD_TYPES_H_
+#define _MACHINE_PTHREAD_TYPES_H_
 
-__attribute__((visibility("hidden"),destructor))
-void __on_dlclose() {
-  __cxa_finalize(&__dso_handle);
-}
+#include <sys/types.h>
 
-#include "../../arch-common/bionic/__dso_handle_so.h"
-#include "atexit.h"
+typedef long pthread_t;
+
+typedef struct {
+  uint32_t flags;
+  void* stack_base;
+  size_t stack_size;
+  size_t guard_size;
+  int32_t sched_policy;
+  int32_t sched_priority;
+#ifdef __LP64__
+  char __reserved[16];
+#endif
+} pthread_attr_t;
+
+#endif /* _MACHINE_PTHREAD_TYPES_H_ */
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 86a1005..24dba1b 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -29,11 +29,12 @@
 #ifndef _PTHREAD_H_
 #define _PTHREAD_H_
 
-#include <time.h>
-#include <signal.h>
-#include <sched.h>
 #include <limits.h>
+#include <machine/pthread_types.h>
+#include <sched.h>
+#include <sys/cdefs.h>
 #include <sys/types.h>
+#include <time.h>
 
 #if defined(__LP64__)
   #define __RESERVED_INITIALIZER , {0}
@@ -76,18 +77,6 @@
 
 #define PTHREAD_COND_INITIALIZER  {0 __RESERVED_INITIALIZER}
 
-typedef struct {
-  uint32_t flags;
-  void* stack_base;
-  size_t stack_size;
-  size_t guard_size;
-  int32_t sched_policy;
-  int32_t sched_priority;
-#ifdef __LP64__
-  char __reserved[16];
-#endif
-} pthread_attr_t;
-
 typedef long pthread_mutexattr_t;
 typedef long pthread_condattr_t;
 
@@ -118,7 +107,6 @@
 #endif
 
 typedef int pthread_key_t;
-typedef long pthread_t;
 
 typedef volatile int pthread_once_t;
 
@@ -195,8 +183,6 @@
 int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
 int pthread_key_delete(pthread_key_t);
 
-int pthread_kill(pthread_t, int);
-
 int pthread_mutexattr_destroy(pthread_mutexattr_t*) __nonnull((1));
 int pthread_mutexattr_getpshared(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
 int pthread_mutexattr_gettype(const pthread_mutexattr_t*, int*) __nonnull((1, 2));
@@ -236,8 +222,6 @@
 
 int pthread_setspecific(pthread_key_t, const void*);
 
-int pthread_sigmask(int, const sigset_t*, sigset_t*);
-
 typedef void (*__pthread_cleanup_func_t)(void*);
 
 typedef struct __pthread_cleanup_t {
diff --git a/libc/include/sched.h b/libc/include/sched.h
index 6155ab7..4f9e2a6 100644
--- a/libc/include/sched.h
+++ b/libc/include/sched.h
@@ -39,9 +39,8 @@
 #define SCHED_OTHER SCHED_NORMAL
 
 struct sched_param {
-  int __sched_priority;
+  int sched_priority;
 };
-#define sched_priority __sched_priority
 
 extern int sched_setscheduler(pid_t, int, const struct sched_param*);
 extern int sched_getscheduler(pid_t);
diff --git a/libc/include/signal.h b/libc/include/signal.h
index e23e65b..6d89ef7 100644
--- a/libc/include/signal.h
+++ b/libc/include/signal.h
@@ -29,12 +29,13 @@
 #ifndef _SIGNAL_H_
 #define _SIGNAL_H_
 
-#include <errno.h>
-#include <sys/cdefs.h>
-#include <limits.h>		/* For LONG_BIT */
-#include <string.h>		/* For memset() */
-#include <sys/types.h>
 #include <asm/sigcontext.h>
+#include <errno.h>
+#include <limits.h>
+#include <machine/pthread_types.h>
+#include <string.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 
 #if defined(__LP64__) || defined(__mips__)
 /* For 64-bit (and mips), the kernel's struct sigaction doesn't match the POSIX one,
@@ -129,6 +130,9 @@
 extern void psiginfo(const siginfo_t*, const char*);
 extern void psignal(int, const char*);
 
+extern int pthread_kill(pthread_t, int);
+extern int pthread_sigmask(int, const sigset_t*, sigset_t*);
+
 __END_DECLS
 
 #endif /* _SIGNAL_H_ */
diff --git a/libc/private/kernel_sigset_t.h b/libc/private/kernel_sigset_t.h
index b2d6386..9415fcf 100644
--- a/libc/private/kernel_sigset_t.h
+++ b/libc/private/kernel_sigset_t.h
@@ -17,6 +17,8 @@
 #ifndef LIBC_PRIVATE_KERNEL_SIGSET_T_H_
 #define LIBC_PRIVATE_KERNEL_SIGSET_T_H_
 
+#include <signal.h>
+
 // Our sigset_t is wrong for ARM and x86. It's 32-bit but the kernel expects 64 bits.
 // This means we can't support real-time signals correctly until we can change the ABI.
 // In the meantime, we can use this union to pass an appropriately-sized block of memory
diff --git a/libc/stdio/fileext.h b/libc/stdio/fileext.h
index dc89fff..25b7bda 100644
--- a/libc/stdio/fileext.h
+++ b/libc/stdio/fileext.h
@@ -34,6 +34,8 @@
 
 #include <pthread.h>
 
+__BEGIN_DECLS
+
 /*
  * file extension
  */
@@ -61,4 +63,6 @@
 	_FILEEXT_INIT(f); \
 } while (0)
 
+__END_DECLS
+
 #endif /* _FILEEXT_H_ */
diff --git a/libc/stdio/glue.h b/libc/stdio/glue.h
index 4ead20a..a9e5d10 100644
--- a/libc/stdio/glue.h
+++ b/libc/stdio/glue.h
@@ -32,6 +32,10 @@
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
 /*
  * The first few FILEs are statically allocated; others are dynamically
  * allocated and linked in via this glue structure.
@@ -44,3 +48,5 @@
 
 /* This was referenced by a couple of different pieces of middleware and the Crystax NDK. */
 __LIBC64_HIDDEN__ extern struct glue __sglue;
+
+__END_DECLS
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 13188ee..46b11f1 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -41,6 +41,8 @@
 #include "wcio.h"
 #include "fileext.h"
 
+__BEGIN_DECLS
+
 /*
  * Android <= KitKat had getc/putc macros in <stdio.h> that referred
  * to __srget/__swbuf, so those symbols need to be public for LP32
@@ -137,3 +139,5 @@
 wint_t __fputwc_unlock(wchar_t wc, FILE *fp);
 
 #pragma GCC visibility pop
+
+__END_DECLS
diff --git a/libc/stdio/wcio.h b/libc/stdio/wcio.h
index 584a3f2..2c1fa3c 100644
--- a/libc/stdio/wcio.h
+++ b/libc/stdio/wcio.h
@@ -32,6 +32,10 @@
 #ifndef _WCIO_H_
 #define _WCIO_H_
 
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+
 /* minimal requirement of SUSv2 */
 #define WCIO_UNGETWC_BUFSIZE 1
 
@@ -78,4 +82,6 @@
 #define WCIO_INIT(fp) \
 	memset(&(_EXT(fp)->_wcio), 0, sizeof(struct wchar_io_data))
 
+__END_DECLS
+
 #endif /*_WCIO_H_*/
diff --git a/libc/upstream-openbsd/android/include/openbsd-compat.h b/libc/upstream-openbsd/android/include/openbsd-compat.h
index 268b3b6..8386ba5 100644
--- a/libc/upstream-openbsd/android/include/openbsd-compat.h
+++ b/libc/upstream-openbsd/android/include/openbsd-compat.h
@@ -59,6 +59,9 @@
 /* We have OpenBSD's getentropy_linux.c, but we don't mention getentropy in any header. */
 __LIBC_HIDDEN__ extern int getentropy(void*, size_t);
 
+/* OpenBSD has this as API, but we just use it internally. */
+__LIBC_HIDDEN__ void* reallocarray(void*, size_t, size_t);
+
 /* LP32 NDK ctype.h contained references to these. */
 __LIBC64_HIDDEN__ extern const short* _tolower_tab_;
 __LIBC64_HIDDEN__ extern const short* _toupper_tab_;
diff --git a/libc/upstream-openbsd/lib/libc/stdlib/reallocarray.c b/libc/upstream-openbsd/lib/libc/stdlib/reallocarray.c
new file mode 100644
index 0000000..7accd99
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/stdlib/reallocarray.c
@@ -0,0 +1,38 @@
+/*	$OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $	*/
+/*
+ * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
+ */
+#define MUL_NO_OVERFLOW	(1UL << (sizeof(size_t) * 4))
+
+void *
+reallocarray(void *optr, size_t nmemb, size_t size)
+{
+	if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
+	    nmemb > 0 && SIZE_MAX / nmemb < size) {
+		errno = ENOMEM;
+		return NULL;
+	}
+	return realloc(optr, size * nmemb);
+}
diff --git a/tests/Android.build.mk b/tests/Android.build.mk
index 0754a7b..5b2b417 100644
--- a/tests/Android.build.mk
+++ b/tests/Android.build.mk
@@ -42,6 +42,8 @@
 
 LOCAL_FORCE_STATIC_EXECUTABLE := $($(module)_force_static_executable)
 
+LOCAL_ALLOW_UNDEFINED_SYMBOLS := $($(module)_allow_undefined_symbols)
+
 ifneq ($($(module)_multilib),)
     LOCAL_MULTILIB := $($(module)_multilib)
 endif
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index 88f0b19..ea20869 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -278,6 +278,44 @@
   ASSERT_EQ(0, dlclose(handle));
 }
 
+TEST(dlfcn, dlopen_check_order_reloc_grandchild) {
+  // This is how this one works:
+  // we lookup and call grandchild_get_answer which is defined in '_2.so'
+  // and in turn calls external get_answer_impl() defined in '_c_1.so and _c_2.so'
+  // the correct _impl() is implemented by '_c_1.so';
+  //
+  // Here is the picture of subtree:
+  //
+  // libtest_check_order_reloc_siblings.so
+  // |
+  // +-> ..._2.so <- grandchild_get_answer()
+  //     |
+  //     +-> ..._c.so <- empty
+  //     |   |
+  //     |   +-> _c_1.so <- exports correct answer_impl()
+  //     |   |
+  //     |   +-> _c_2.so <- exports incorrect answer_impl()
+  //     |
+  //     +-> ..._d.so <- empty
+
+  void* handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_NOLOAD);
+  ASSERT_TRUE(handle == nullptr);
+#ifdef __BIONIC__
+  // TODO: glibc returns nullptr on dlerror() here. Is it bug?
+  ASSERT_STREQ("dlopen failed: library \"libtest_check_order_reloc_siblings.so\" wasn't loaded and RTLD_NOLOAD prevented it", dlerror());
+#endif
+
+  handle = dlopen("libtest_check_order_reloc_siblings.so", RTLD_NOW | RTLD_LOCAL);
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+
+  typedef int (*fn_t) (void);
+  fn_t fn = reinterpret_cast<fn_t>(dlsym(handle, "check_order_reloc_grandchild_get_answer"));
+  ASSERT_TRUE(fn != nullptr) << dlerror();
+  ASSERT_EQ(42, fn());
+
+  ASSERT_EQ(0, dlclose(handle));
+}
+
 TEST(dlfcn, dlopen_check_order_reloc_nephew) {
   // This is how this one works:
   // we lookup and call nephew_get_answer which is defined in '_2.so'
diff --git a/tests/ftw_test.cpp b/tests/ftw_test.cpp
index 7ffbfe0..38b3d49 100644
--- a/tests/ftw_test.cpp
+++ b/tests/ftw_test.cpp
@@ -14,27 +14,36 @@
  * limitations under the License.
  */
 
-#include <gtest/gtest.h>
-#include "TemporaryFile.h"
-
 #include <ftw.h>
+
 #include <stdlib.h>
 #include <sys/stat.h>
 
+#include <gtest/gtest.h>
+
 void sanity_check_ftw(const char* fpath, const struct stat* sb, int tflag) {
   ASSERT_TRUE(fpath != NULL);
   ASSERT_TRUE(sb != NULL);
-  bool is_dir = S_ISDIR(sb->st_mode);
-  ASSERT_TRUE((is_dir && tflag == FTW_D) || (!is_dir && tflag == FTW_F));
+  if (S_ISDIR(sb->st_mode)) {
+    ASSERT_TRUE(tflag == FTW_D || tflag == FTW_DNR || tflag == FTW_DP) << fpath;
+  } else if (S_ISLNK(sb->st_mode)) {
+    ASSERT_EQ(FTW_SL, tflag) << fpath;
+  } else {
+    ASSERT_EQ(FTW_F, tflag) << fpath;
+  }
 }
 
-void sanity_check_nftw(
-    const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
+void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
   sanity_check_ftw(fpath, sb, tflag);
-  // either the parent dir or the file
-  bool is_dir = S_ISDIR(sb->st_mode);
-  ASSERT_TRUE(
-    (is_dir && ftwbuf->level == 0) || (!is_dir && ftwbuf->level == 1));
+
+  size_t slash_count = 0;
+  const char* p = fpath;
+  while ((p = strchr(p + 1, '/')) != NULL) {
+    ++slash_count;
+  }
+
+  ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath;
+  ASSERT_EQ(slash_count, static_cast<size_t>(ftwbuf->level)) << fpath;
 }
 
 int check_ftw(const char* fpath, const struct stat* sb, int tflag) {
@@ -47,39 +56,28 @@
   return 0;
 }
 
-int check_nftw(
-  const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
+int check_nftw(const char* fpath, const struct stat* sb, int tflag, struct FTW* ftwbuf) {
   sanity_check_nftw(fpath, sb, tflag, ftwbuf);
   return 0;
 }
 
-int check_nftw64(
-  const char* fpath, const struct stat64* sb, int tflag, struct FTW* ftwbuf) {
-  sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb),
-    tflag, ftwbuf);
+int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, struct FTW* ftwbuf) {
+  sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf);
   return 0;
 }
 
 TEST(ftw, ftw) {
-  TemporaryDir td;
-  TemporaryFile tf(td.dirname);
-  ftw(td.dirname, check_ftw, 1);
+  ASSERT_EQ(0, ftw("/sys", check_ftw, 128));
 }
 
 TEST(ftw, ftw64) {
-  TemporaryDir td;
-  TemporaryFile tf(td.dirname, mkstemp64);
-  ftw64(td.dirname, check_ftw64, 1);
+  ASSERT_EQ(0, ftw64("/sys", check_ftw64, 128));
 }
 
 TEST(ftw, nftw) {
-  TemporaryDir td;
-  TemporaryFile tf(td.dirname);
-  nftw(td.dirname, check_nftw, 1, 0);
+  ASSERT_EQ(0, nftw("/sys", check_nftw, 128, 0));
 }
 
 TEST(ftw, nftw64) {
-  TemporaryDir td;
-  TemporaryFile tf(td.dirname, mkstemp64);
-  nftw64(td.dirname, check_nftw64, 1, 0);
+  ASSERT_EQ(0, nftw64("/sys", check_nftw64, 128, 0));
 }
diff --git a/tests/gtest_ex.h b/tests/gtest_ex.h
new file mode 100644
index 0000000..fe1d894
--- /dev/null
+++ b/tests/gtest_ex.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include <gtest/gtest.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+template<typename F>
+void test_isolated(F test) {
+  int pid = fork();
+  ASSERT_NE(-1, pid) << strerror(errno);
+
+  if (pid == 0) {
+    test();
+    _exit(testing::Test::HasFailure() ? 1 : 0);
+  }
+
+  int status;
+  ASSERT_EQ(pid, waitpid(pid, &status, 0));
+  ASSERT_TRUE(WIFEXITED(status));
+  ASSERT_EQ(0, WEXITSTATUS(status)) << "Forked test has failed, see above..";
+}
diff --git a/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk b/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
index 0f1a2b4..bd35a51 100644
--- a/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
+++ b/tests/libs/Android.build.dlopen_check_order_reloc_siblings.mk
@@ -37,12 +37,13 @@
 # ..._2.so - empty
 # -----------------------------------------------------------------------------
 libtest_check_order_reloc_siblings_2_src_files := \
-    empty.cpp
+    dlopen_check_order_reloc_grandchild_answer.cpp
 
 libtest_check_order_reloc_siblings_2_shared_libraries := \
     libtest_check_order_reloc_siblings_c \
     libtest_check_order_reloc_siblings_d
 
+libtest_check_order_reloc_siblings_2_allow_undefined_symbols := true
 module := libtest_check_order_reloc_siblings_2
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
@@ -86,6 +87,10 @@
     dlopen_check_order_reloc_answer_impl.cpp
 
 libtest_check_order_reloc_siblings_c_cflags := -D__ANSWER=2
+libtest_check_order_reloc_siblings_c_shared_libraries := \
+    libtest_check_order_reloc_siblings_c_1 \
+    libtest_check_order_reloc_siblings_c_2
+
 module := libtest_check_order_reloc_siblings_c
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
@@ -119,6 +124,26 @@
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
+# ..._c_1.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_c_1_src_files := \
+    dlopen_check_order_reloc_grandchild_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_c_1_cflags := -D__ANSWER=42
+module := libtest_check_order_reloc_siblings_c_1
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
+# ..._c_2.so
+# -----------------------------------------------------------------------------
+libtest_check_order_reloc_siblings_c_2_src_files := \
+    dlopen_check_order_reloc_grandchild_answer_impl.cpp
+
+libtest_check_order_reloc_siblings_c_2_cflags := -D__ANSWER=0
+module := libtest_check_order_reloc_siblings_c_2
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
 # libtest_check_order_reloc_siblings.so
 # -----------------------------------------------------------------------------
 libtest_check_order_reloc_siblings_src_files := \
diff --git a/tests/libs/dlopen_check_order_reloc_grandchild_answer.cpp b/tests/libs/dlopen_check_order_reloc_grandchild_answer.cpp
new file mode 100644
index 0000000..afb5f2c
--- /dev/null
+++ b/tests/libs/dlopen_check_order_reloc_grandchild_answer.cpp
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+extern "C" int check_order_reloc_grandchild_get_answer_impl();
+
+extern "C" int check_order_reloc_grandchild_get_answer() {
+  return check_order_reloc_grandchild_get_answer_impl();
+}
+
diff --git a/tests/libs/dlopen_check_order_reloc_grandchild_answer_impl.cpp b/tests/libs/dlopen_check_order_reloc_grandchild_answer_impl.cpp
new file mode 100644
index 0000000..32d2b24
--- /dev/null
+++ b/tests/libs/dlopen_check_order_reloc_grandchild_answer_impl.cpp
@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+extern "C" int check_order_reloc_grandchild_get_answer_impl() {
+  return __ANSWER;
+}
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index b5e12a5..797468e 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -19,6 +19,7 @@
 #include "private/ScopeGuard.h"
 #include "BionicDeathTest.h"
 #include "ScopedSignalHandler.h"
+#include "gtest_ex.h"
 
 #include <errno.h>
 #include <inttypes.h>
@@ -721,22 +722,24 @@
 static void AtForkChild1() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 1; }
 static void AtForkChild2() { g_atfork_child_calls = (g_atfork_child_calls << 4) | 2; }
 
-TEST(pthread, pthread_atfork) {
-  ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
-  ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
+TEST(pthread, pthread_atfork_smoke) {
+  test_isolated([] {
+    ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
+    ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
 
-  int pid = fork();
-  ASSERT_NE(-1, pid) << strerror(errno);
+    int pid = fork();
+    ASSERT_NE(-1, pid) << strerror(errno);
 
-  // Child and parent calls are made in the order they were registered.
-  if (pid == 0) {
-    ASSERT_EQ(0x12, g_atfork_child_calls);
-    _exit(0);
-  }
-  ASSERT_EQ(0x12, g_atfork_parent_calls);
+    // Child and parent calls are made in the order they were registered.
+    if (pid == 0) {
+      ASSERT_EQ(0x12, g_atfork_child_calls);
+      _exit(0);
+    }
+    ASSERT_EQ(0x12, g_atfork_parent_calls);
 
-  // Prepare calls are made in the reverse order.
-  ASSERT_EQ(0x21, g_atfork_prepare_calls);
+    // Prepare calls are made in the reverse order.
+    ASSERT_EQ(0x21, g_atfork_prepare_calls);
+  });
 }
 
 TEST(pthread, pthread_attr_getscope) {
diff --git a/tests/stubs_test.cpp b/tests/stubs_test.cpp
index 9b0c231..89d67cb 100644
--- a/tests/stubs_test.cpp
+++ b/tests/stubs_test.cpp
@@ -16,98 +16,246 @@
 
 #include <gtest/gtest.h>
 
-#include <sys/types.h>
-#include <sys/cdefs.h>
+// Below are the header files we want to test.
+#include <grp.h>
 #include <pwd.h>
+
 #include <errno.h>
 #include <limits.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
 #include <unistd.h>
 
-#if defined(__BIONIC__)
-#define CHECK_GETPWNAM_FOR(username, uid, uid_type) \
-    SCOPED_TRACE(username); \
-    ASSERT_NO_FATAL_FAILURE(check_getpwnam(username, uid, uid_type));
-
-typedef enum {
+enum uid_type_t {
   TYPE_SYSTEM,
   TYPE_APP
-} uid_type_t;
+};
 
-static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
-  errno = 0;
-  passwd* pwd = getpwuid(uid);
+#if defined(__BIONIC__)
+
+static void check_passwd(const passwd* pwd, const char* username, uid_t uid, uid_type_t uid_type) {
   ASSERT_TRUE(pwd != NULL);
-  ASSERT_EQ(0, errno);
-  EXPECT_STREQ(username, pwd->pw_name);
-  EXPECT_EQ(uid, pwd->pw_uid);
-  EXPECT_EQ(uid, pwd->pw_gid);
+  ASSERT_STREQ(username, pwd->pw_name);
+  ASSERT_EQ(uid, pwd->pw_uid);
+  ASSERT_EQ(uid, pwd->pw_gid);
   ASSERT_EQ(NULL, pwd->pw_passwd);
 #ifdef __LP64__
   ASSERT_EQ(NULL, pwd->pw_gecos);
 #endif
 
   if (uid_type == TYPE_SYSTEM) {
-    EXPECT_STREQ("/", pwd->pw_dir);
-  } else if (uid_type == TYPE_APP) {
-    EXPECT_STREQ("/data", pwd->pw_dir);
+    ASSERT_STREQ("/", pwd->pw_dir);
+  } else {
+    ASSERT_STREQ("/data", pwd->pw_dir);
   }
-
-  EXPECT_STREQ("/system/bin/sh", pwd->pw_shell);
+  ASSERT_STREQ("/system/bin/sh", pwd->pw_shell);
 }
-#else
-#define CHECK_GETPWNAM_FOR(username, uid, uid_type) \
-  GTEST_LOG_(INFO) << "This test does nothing.\n";
+
+static void check_getpwuid(const char* username, uid_t uid, uid_type_t uid_type) {
+  errno = 0;
+  passwd* pwd = getpwuid(uid);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwuid");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_getpwnam(const char* username, uid_t uid, uid_type_t uid_type) {
+  errno = 0;
+  passwd* pwd = getpwnam(username);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwnam");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_getpwuid_r(const char* username, uid_t uid, uid_type_t uid_type) {
+  passwd pwd_storage;
+  char buf[512];
+  int result;
+
+  errno = 0;
+  passwd* pwd = NULL;
+  result = getpwuid_r(uid, &pwd_storage, buf, sizeof(buf), &pwd);
+  ASSERT_EQ(0, result);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwuid_r");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_getpwnam_r(const char* username, uid_t uid, uid_type_t uid_type) {
+  passwd pwd_storage;
+  char buf[512];
+  int result;
+
+  errno = 0;
+  passwd* pwd = NULL;
+  result = getpwnam_r(username, &pwd_storage, buf, sizeof(buf), &pwd);
+  ASSERT_EQ(0, result);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getpwnam_r");
+  check_passwd(pwd, username, uid, uid_type);
+}
+
+static void check_get_passwd(const char* username, uid_t uid, uid_type_t uid_type) {
+  check_getpwuid(username, uid, uid_type);
+  check_getpwnam(username, uid, uid_type);
+  check_getpwuid_r(username, uid, uid_type);
+  check_getpwnam_r(username, uid, uid_type);
+}
+
+#else // !defined(__BIONIC__)
+
+static void check_get_passwd(const char* /* username */, uid_t /* uid */, uid_type_t /* uid_type */) {
+  GTEST_LOG_(INFO) << "This test is about uid/username translation for Android, which does nothing on libc other than bionic.\n";
+}
+
 #endif
 
 TEST(getpwnam, system_id_root) {
-  CHECK_GETPWNAM_FOR("root", 0, TYPE_SYSTEM);
+  check_get_passwd("root", 0, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, system_id_system) {
-  CHECK_GETPWNAM_FOR("system", 1000, TYPE_SYSTEM);
+  check_get_passwd("system", 1000, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_radio) {
-  CHECK_GETPWNAM_FOR("radio", 1001, TYPE_SYSTEM);
+  check_get_passwd("radio", 1001, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_nobody) {
-  CHECK_GETPWNAM_FOR("nobody", 9999, TYPE_SYSTEM);
-}
-
-TEST(getpwnam, app_id_all_a0) {
-  CHECK_GETPWNAM_FOR("all_a0", 50000, TYPE_APP);
-}
-
-TEST(getpwnam, app_id_u1_a40000) {
-  CHECK_GETPWNAM_FOR("u1_a40000", 150000, TYPE_APP);
+  check_get_passwd("nobody", 9999, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_u0_a0) {
-  CHECK_GETPWNAM_FOR("u0_a0", 10000, TYPE_APP);
+  check_get_passwd("u0_a0", 10000, TYPE_APP);
 }
 
 TEST(getpwnam, app_id_u0_a1234) {
-  CHECK_GETPWNAM_FOR("u0_a1234", 11234, TYPE_APP);
+  check_get_passwd("u0_a1234", 11234, TYPE_APP);
 }
 
-TEST(getpwnam, app_id_u0_a9999) {
-  CHECK_GETPWNAM_FOR("u0_a9999", 19999, TYPE_APP);
+// Test the difference between uid and shared gid.
+TEST(getpwnam, app_id_u0_a49999) {
+  check_get_passwd("u0_a49999", 59999, TYPE_APP);
 }
 
-// nonsensical, but expected
+TEST(getpwnam, app_id_u0_i1) {
+  check_get_passwd("u0_i1", 99001, TYPE_APP);
+}
+
 TEST(getpwnam, app_id_u1_root) {
-  CHECK_GETPWNAM_FOR("u1_root", 100000, TYPE_SYSTEM);
+  check_get_passwd("u1_root", 100000, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_u1_radio) {
-  CHECK_GETPWNAM_FOR("u1_radio", 101001, TYPE_SYSTEM);
+  check_get_passwd("u1_radio", 101001, TYPE_SYSTEM);
 }
 
 TEST(getpwnam, app_id_u1_a0) {
-  CHECK_GETPWNAM_FOR("u1_a0", 110000, TYPE_APP);
+  check_get_passwd("u1_a0", 110000, TYPE_APP);
+}
+
+TEST(getpwnam, app_id_u1_a40000) {
+  check_get_passwd("u1_a40000", 150000, TYPE_APP);
 }
 
 TEST(getpwnam, app_id_u1_i0) {
-  CHECK_GETPWNAM_FOR("u1_i0", 199000, TYPE_APP);
+  check_get_passwd("u1_i0", 199000, TYPE_APP);
+}
+
+#if defined(__BIONIC__)
+
+static void check_group(const group* grp, const char* group_name, gid_t gid) {
+  ASSERT_TRUE(grp != NULL);
+  ASSERT_STREQ(group_name, grp->gr_name);
+  ASSERT_EQ(gid, grp->gr_gid);
+  ASSERT_TRUE(grp->gr_mem != NULL);
+  ASSERT_STREQ(group_name, grp->gr_mem[0]);
+  ASSERT_TRUE(grp->gr_mem[1] == NULL);
+}
+
+static void check_getgrgid(const char* group_name, gid_t gid) {
+  errno = 0;
+  group* grp = getgrgid(gid);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getgrgid");
+  check_group(grp, group_name, gid);
+}
+
+static void check_getgrnam(const char* group_name, gid_t gid) {
+  errno = 0;
+  group* grp = getgrnam(group_name);
+  ASSERT_EQ(0, errno);
+  SCOPED_TRACE("getgrnam");
+  check_group(grp, group_name, gid);
+}
+
+static void check_get_group(const char* group_name, gid_t gid) {
+  check_getgrgid(group_name, gid);
+  check_getgrnam(group_name, gid);
+}
+
+#else // !defined(__BIONIC__)
+
+static void check_get_group(const char* /* group_name */, gid_t /* gid */) {
+  GTEST_LOG_(INFO) << "This test is about gid/group_name translation for Android, which does nothing on libc other than bionic.\n";
+}
+
+#endif
+
+TEST(getgrnam, system_id_root) {
+  check_get_group("root", 0);
+}
+
+TEST(getgrnam, system_id_system) {
+  check_get_group("system", 1000);
+}
+
+TEST(getgrnam, app_id_radio) {
+  check_get_group("radio", 1001);
+}
+
+TEST(getgrnam, app_id_nobody) {
+  check_get_group("nobody", 9999);
+}
+
+TEST(getgrnam, app_id_u0_a0) {
+  check_get_group("u0_a0", 10000);
+}
+
+TEST(getgrnam, app_id_u0_a1234) {
+  check_get_group("u0_a1234", 11234);
+}
+
+TEST(getgrnam, app_id_u0_a9999) {
+  check_get_group("u0_a9999", 19999);
+}
+
+// Test the difference between uid and shared gid.
+TEST(getgrnam, app_id_all_a9999) {
+  check_get_group("all_a9999", 59999);
+}
+
+TEST(getgrnam, app_id_u0_i1) {
+  check_get_group("u0_i1", 99001);
+}
+
+TEST(getgrnam, app_id_u1_root) {
+  check_get_group("u1_root", 100000);
+}
+
+TEST(getgrnam, app_id_u1_radio) {
+  check_get_group("u1_radio", 101001);
+}
+
+TEST(getgrnam, app_id_u1_a0) {
+  check_get_group("u1_a0", 110000);
+}
+
+TEST(getgrnam, app_id_u1_a40000) {
+  check_get_group("u1_a40000", 150000);
+}
+
+TEST(getgrnam, app_id_u1_i0) {
+  check_get_group("u1_i0", 199000);
 }