Merge "Revert "Use the new NativeDaemonConnector style.""
diff --git a/libc/arch-arm/bionic/libgcc_compat.c b/libc/arch-arm/bionic/libgcc_compat.c
index d184566..1a19701 100644
--- a/libc/arch-arm/bionic/libgcc_compat.c
+++ b/libc/arch-arm/bionic/libgcc_compat.c
@@ -113,6 +113,8 @@
     XX(__aeabi_l2d)          \
     XX(__aeabi_l2f)          \
     XX(__aeabi_lmul)         \
+    XX(__aeabi_llsl)         \
+    XX(__aeabi_llsr)         \
     XX(__aeabi_ui2d)         \
     XX(__aeabi_ui2f)         \
     XX(__aeabi_ul2d)         \
diff --git a/libc/arch-arm/include/machine/asm.h b/libc/arch-arm/include/machine/asm.h
index 7b8f053..047e54d 100644
--- a/libc/arch-arm/include/machine/asm.h
+++ b/libc/arch-arm/include/machine/asm.h
@@ -97,6 +97,12 @@
 #define	ASENTRY_NP(y)	_ENTRY(_ASM_LABEL(y))
 #define	ASEND(y)	_END(_ASM_LABEL(y))
 
+#ifdef __ELF__
+#define ENTRY_PRIVATE(y)  ENTRY(y); .hidden _C_LABEL(y)
+#else
+#define ENTRY_PRIVATE(y)  ENTRY(y)
+#endif
+
 #define	ASMSTR		.asciz
 
 #if defined(__ELF__) && defined(PIC)
diff --git a/libc/arch-x86/bionic/__set_tls.c b/libc/arch-x86/bionic/__set_tls.c
index e5e43b5..7ed4b01 100755
--- a/libc/arch-x86/bionic/__set_tls.c
+++ b/libc/arch-x86/bionic/__set_tls.c
@@ -83,6 +83,7 @@
     if (rc != 0)
     {
         /* could not set thread local area */
+        pthread_mutex_unlock(&_tls_desc_lock);
         return -1;
     }
 
diff --git a/libc/arch-x86/include/machine/asm.h b/libc/arch-x86/include/machine/asm.h
index 7a23060..49d3ea8 100644
--- a/libc/arch-x86/include/machine/asm.h
+++ b/libc/arch-x86/include/machine/asm.h
@@ -103,6 +103,12 @@
 #define _ENTRY(x) \
 	.text; _ALIGN_TEXT; .globl x; .type x,@function; x:
 
+#define _ASM_SIZE(x)    .size x, .-x;
+
+#define _END(x) \
+	.fnend; \
+	_ASM_SIZE(x)
+
 #ifdef GPROF
 # define _PROF_PROLOGUE	\
 	pushl %ebp; movl %esp,%ebp; call PIC_PLT(mcount); popl %ebp
@@ -112,8 +118,12 @@
 
 #define	ENTRY(y)	_ENTRY(_C_LABEL(y)); _PROF_PROLOGUE
 #define	NENTRY(y)	_ENTRY(_C_LABEL(y))
+#define	END(y)		_END(_C_LABEL(y))
 #define	ASENTRY(y)	_ENTRY(_ASM_LABEL(y)); _PROF_PROLOGUE
 
+#define ENTRY_PRIVATE(y)  ENTRY(y); .hidden _C_LABEL(y)
+
+
 #define	ALTENTRY(name)	.globl _C_LABEL(name); _C_LABEL(name):
 
 #define	ASMSTR		.asciz
diff --git a/libc/bionic/stubs.c b/libc/bionic/stubs.c
index 5f63427..cc4c04e 100644
--- a/libc/bionic/stubs.c
+++ b/libc/bionic/stubs.c
@@ -250,30 +250,58 @@
 static unsigned
 app_id_from_name( const char*  name )
 {
-    unsigned long  id;
+    unsigned long  userid;
+    unsigned long  appid;
     char*          end;
 
-    if (memcmp(name, "app_", 4) != 0 || !isdigit(name[4]))
+    if (name[0] != 'u' || !isdigit(name[1]))
         goto FAIL;
 
-    id = strtoul(name+4, &end, 10);
-    if (*end != '\0')
+    userid = strtoul(name+1, &end, 10);
+    if (end[0] != '_' || end[1] == 0 || !isdigit(end[2]))
         goto FAIL;
 
-    id += AID_APP;
-
-    /* check for overflow and that the value can be
-     * stored in our 32-bit uid_t/gid_t */
-    if (id < AID_APP || (unsigned)id != id)
+    if (end[1] == 'a')
+        appid = strtoul(end+2, &end, 10) + AID_APP;
+    else if (end[1] == 'i')
+        appid = strtoul(end+2, &end, 10) + AID_ISOLATED_START;
+    else
         goto FAIL;
 
-    return (unsigned)id;
+    if (end[0] != 0)
+        goto FAIL;
+
+    /* check that user id won't overflow */
+    if (userid > 1000)
+        goto FAIL;
+
+    /* check that app id is within range */
+    if (appid < AID_APP || appid >= AID_USER)
+        goto FAIL;
+
+    return (unsigned)(appid + userid*AID_USER);
 
 FAIL:
     errno = ENOENT;
     return 0;
 }
 
+static void
+print_app_uid_name(uid_t  uid, char* buffer, int bufferlen)
+{
+    uid_t appid;
+    uid_t userid;
+
+    appid = uid % AID_USER;
+    userid = uid / AID_USER;
+
+    if (appid < AID_ISOLATED_START) {
+        snprintf(buffer, bufferlen, "u%u_a%u", userid, appid - AID_APP);
+    } else {
+        snprintf(buffer, bufferlen, "u%u_i%u", userid, appid - AID_ISOLATED_START);
+    }
+}
+
 /* translate a uid into the corresponding app_<uid>
  * passwd structure (sets errno to ENOENT on failure)
  */
@@ -287,8 +315,7 @@
         return NULL;
     }
 
-    snprintf( state->app_name_buffer, sizeof state->app_name_buffer,
-              "app_%u", uid - AID_APP );
+    print_app_uid_name(uid, state->app_name_buffer, sizeof state->app_name_buffer);
 
     pw->pw_name  = state->app_name_buffer;
     pw->pw_dir   = "/data";
@@ -306,14 +333,15 @@
 app_id_to_group(gid_t  gid, stubs_state_t*  state)
 {
     struct group*  gr = &state->group;
+    int appid;
+    int userid;
 
     if (gid < AID_APP) {
         errno = ENOENT;
         return NULL;
     }
 
-    snprintf(state->group_name_buffer, sizeof state->group_name_buffer,
-             "app_%u", gid - AID_APP);
+    print_app_uid_name(gid, state->group_name_buffer, sizeof state->group_name_buffer);
 
     gr->gr_name   = state->group_name_buffer;
     gr->gr_gid    = gid;
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 4006882..8d3d5d7 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -452,8 +452,10 @@
  * fdprintf is a better name, and some programs that use fdprintf use a
  * #define fdprintf dprintf for compatibility
  */
+__BEGIN_DECLS
 int fdprintf(int, const char*, ...);
 int vfdprintf(int, const char*, __va_list);
+__END_DECLS
 #endif /* _GNU_SOURCE */
 
 #endif /* _STDIO_H_ */
diff --git a/libc/include/sys/cdefs_elf.h b/libc/include/sys/cdefs_elf.h
index 1e57470..0887fa5 100644
--- a/libc/include/sys/cdefs_elf.h
+++ b/libc/include/sys/cdefs_elf.h
@@ -96,9 +96,24 @@
 #endif
 
 /* GCC visibility helper macro */
+/* This must be used to tag non-static functions that are private, i.e.
+ * never exposed by the shared library. */
 #define __LIBC_HIDDEN__							\
 	__attribute__ ((visibility ("hidden")))
 
+/* This must be used to tag non-static functions that are public, i.e.
+ * exposed by the shared library, and part of the stable NDK ABI */
+#define __LIBC_ABI_PUBLIC__ \
+        __attribute__ ((visibility ("default")))
+
+/* This must be used to tag non-static functions that must be exported
+ * by the shared library, but whose implementation is private to the
+ * platform. For now this is equivalent to __LIBC_ABI_PUBLIC__, but we
+ * may want to change this later.
+ */
+#define __LIBC_ABI_PRIVATE__ \
+        __attribute__ ((visibility ("default")))
+
 #define	__IDSTRING(_n,_s)		__SECTIONSTRING(.ident,_s)
 
 #define	__RCSID(_s)			__IDSTRING(rcsid,_s)
diff --git a/linker/dlfcn.c b/linker/dlfcn.c
index 529511f..ac7e5d3 100644
--- a/linker/dlfcn.c
+++ b/linker/dlfcn.c
@@ -42,7 +42,7 @@
 #define likely(expr)   __builtin_expect (expr, 1)
 #define unlikely(expr) __builtin_expect (expr, 0)
 
-static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
+pthread_mutex_t dl_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
 
 static void set_dlerror(int err)
 {