Merge "Fix lookup-by-base-name for lp64"
diff --git a/libc/Android.mk b/libc/Android.mk
index f0ee09d..53a122e 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -98,6 +98,7 @@
 libc_bionic_src_files := \
     bionic/abort.cpp \
     bionic/accept.cpp \
+    bionic/accept4.cpp \
     bionic/access.cpp \
     bionic/assert.cpp \
     bionic/atof.cpp \
@@ -199,6 +200,7 @@
     bionic/sigprocmask.cpp \
     bionic/sigsuspend.cpp \
     bionic/sigwait.cpp \
+    bionic/socket.cpp \
     bionic/stat.cpp \
     bionic/statvfs.cpp \
     bionic/strerror.cpp \
@@ -215,7 +217,6 @@
     bionic/termios.cpp \
     bionic/thread_atexit.cpp \
     bionic/tmpfile.cpp \
-    bionic/typeinfo.cpp \
     bionic/umount.cpp \
     bionic/unlink.cpp \
     bionic/utimes.cpp \
@@ -269,7 +270,6 @@
     upstream-netbsd/lib/libc/gen/setjmperr.c \
     upstream-netbsd/lib/libc/gen/utime.c \
     upstream-netbsd/lib/libc/gen/utmp.c \
-    upstream-netbsd/lib/libc/inet/inet_ntop.c \
     upstream-netbsd/lib/libc/isc/ev_streams.c \
     upstream-netbsd/lib/libc/isc/ev_timers.c \
     upstream-netbsd/lib/libc/regex/regcomp.c \
@@ -365,6 +365,7 @@
     upstream-openbsd/lib/libc/net/inet_netof.c \
     upstream-openbsd/lib/libc/net/inet_network.c \
     upstream-openbsd/lib/libc/net/inet_ntoa.c \
+    upstream-openbsd/lib/libc/net/inet_ntop.c \
     upstream-openbsd/lib/libc/net/inet_pton.c \
     upstream-openbsd/lib/libc/net/ntohl.c \
     upstream-openbsd/lib/libc/net/ntohs.c \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 4a21582..2a891b7 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -231,13 +231,12 @@
 int     __signalfd4:signalfd4(int, const sigset_t*, size_t, int)  all
 
 # sockets
-int           socket(int, int, int)              arm,arm64,mips,mips64,x86_64
+int           __socket:socket(int, int, int)              arm,arm64,mips,mips64,x86_64
 int           socketpair(int, int, int, int*)    arm,arm64,mips,mips64,x86_64
 int           bind(int, struct sockaddr*, int)  arm,arm64,mips,mips64,x86_64
 int           __connect:connect(int, struct sockaddr*, socklen_t)   arm,arm64,mips,mips64,x86_64
 int           listen(int, int)                   arm,arm64,mips,mips64,x86_64
-int           __accept:accept(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
-int           accept4(int, struct sockaddr*, socklen_t*, int)  arm,arm64,mips,mips64,x86_64
+int           __accept4:accept4(int, struct sockaddr*, socklen_t*, int)  arm,arm64,mips,mips64,x86_64
 int           getsockname(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
 int           getpeername(int, struct sockaddr*, socklen_t*)  arm,arm64,mips,mips64,x86_64
 int           sendto(int, const void*, size_t, int, const struct sockaddr*, socklen_t)  arm,arm64,mips,mips64,x86_64
@@ -251,11 +250,10 @@
 int           sendmmsg(int, struct mmsghdr*, unsigned int, int)   arm,arm64,mips,mips64,x86_64
 
 # sockets for x86. These are done as an "indexed" call to socketcall syscall.
-int           socket:socketcall:1(int, int, int) x86
+int           __socket:socketcall:1(int, int, int) x86
 int           bind:socketcall:2(int, struct sockaddr*, int)  x86
 int           __connect:socketcall:3(int, struct sockaddr*, socklen_t)   x86
 int           listen:socketcall:4(int, int)                   x86
-int           __accept:socketcall:5(int, struct sockaddr*, socklen_t*)  x86
 int           getsockname:socketcall:6(int, struct sockaddr*, socklen_t*)  x86
 int           getpeername:socketcall:7(int, struct sockaddr*, socklen_t*)  x86
 int           socketpair:socketcall:8(int, int, int, int*)    x86
@@ -266,7 +264,7 @@
 int           getsockopt:socketcall:15(int, int, int, void*, socklen_t*)    x86
 int           sendmsg:socketcall:16(int, const struct msghdr*, unsigned int)  x86
 int           recvmsg:socketcall:17(int, struct msghdr*, unsigned int)   x86
-int           accept4:socketcall:18(int, struct sockaddr*, socklen_t*, int)  x86
+int           __accept4:socketcall:18(int, struct sockaddr*, socklen_t*, int)  x86
 int           recvmmsg:socketcall:19(int, struct mmsghdr*, unsigned int, int, const struct timespec*)   x86
 int           sendmmsg:socketcall:20(int, struct mmsghdr*, unsigned int, int)   x86
 
diff --git a/libc/arch-arm/syscalls/__accept.S b/libc/arch-arm/syscalls/__accept.S
deleted file mode 100644
index bae11bc..0000000
--- a/libc/arch-arm/syscalls/__accept.S
+++ /dev/null
@@ -1,14 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    mov     ip, r7
-    ldr     r7, =__NR_accept
-    swi     #0
-    mov     r7, ip
-    cmn     r0, #(MAX_ERRNO + 1)
-    bxls    lr
-    neg     r0, r0
-    b       __set_errno
-END(__accept)
diff --git a/libc/arch-arm/syscalls/accept4.S b/libc/arch-arm/syscalls/__accept4.S
similarity index 88%
rename from libc/arch-arm/syscalls/accept4.S
rename to libc/arch-arm/syscalls/__accept4.S
index 6d14e79..9a6874b 100644
--- a/libc/arch-arm/syscalls/accept4.S
+++ b/libc/arch-arm/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     mov     ip, r7
     ldr     r7, =__NR_accept4
     swi     #0
@@ -11,4 +11,4 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno
-END(accept4)
+END(__accept4)
diff --git a/libc/arch-arm/syscalls/socket.S b/libc/arch-arm/syscalls/__socket.S
similarity index 89%
rename from libc/arch-arm/syscalls/socket.S
rename to libc/arch-arm/syscalls/__socket.S
index 05fa529..d63028b 100644
--- a/libc/arch-arm/syscalls/socket.S
+++ b/libc/arch-arm/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     mov     ip, r7
     ldr     r7, =__NR_socket
     swi     #0
@@ -11,4 +11,4 @@
     bxls    lr
     neg     r0, r0
     b       __set_errno
-END(socket)
+END(__socket)
diff --git a/libc/arch-arm64/syscalls/__accept.S b/libc/arch-arm64/syscalls/__accept.S
deleted file mode 100644
index 21b68bc..0000000
--- a/libc/arch-arm64/syscalls/__accept.S
+++ /dev/null
@@ -1,22 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    stp     x29, x30, [sp, #-16]!
-    mov     x29,  sp
-    str     x8,       [sp, #-16]!
-
-    mov     x8, __NR_accept
-    svc     #0
-
-    ldr     x8,       [sp], #16
-    ldp     x29, x30, [sp], #16
-
-    cmn     x0, #(MAX_ERRNO + 1)
-    cneg    x0, x0, hi
-    b.hi    __set_errno
-
-    ret
-END(__accept)
-.hidden __accept
diff --git a/libc/arch-arm64/syscalls/accept4.S b/libc/arch-arm64/syscalls/__accept4.S
similarity index 88%
rename from libc/arch-arm64/syscalls/accept4.S
rename to libc/arch-arm64/syscalls/__accept4.S
index 3c9227f..c66cd2e 100644
--- a/libc/arch-arm64/syscalls/accept4.S
+++ b/libc/arch-arm64/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     stp     x29, x30, [sp, #-16]!
     mov     x29,  sp
     str     x8,       [sp, #-16]!
@@ -18,4 +18,5 @@
     b.hi    __set_errno
 
     ret
-END(accept4)
+END(__accept4)
+.hidden __accept4
diff --git a/libc/arch-arm64/syscalls/socket.S b/libc/arch-arm64/syscalls/__socket.S
similarity index 88%
rename from libc/arch-arm64/syscalls/socket.S
rename to libc/arch-arm64/syscalls/__socket.S
index 37a3851..aa9da22 100644
--- a/libc/arch-arm64/syscalls/socket.S
+++ b/libc/arch-arm64/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     stp     x29, x30, [sp, #-16]!
     mov     x29,  sp
     str     x8,       [sp, #-16]!
@@ -18,4 +18,5 @@
     b.hi    __set_errno
 
     ret
-END(socket)
+END(__socket)
+.hidden __socket
diff --git a/libc/arch-mips/syscalls/__accept.S b/libc/arch-mips/syscalls/__accept.S
deleted file mode 100644
index d8141fe..0000000
--- a/libc/arch-mips/syscalls/__accept.S
+++ /dev/null
@@ -1,19 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    .set noreorder
-    .cpload t9
-    li v0, __NR_accept
-    syscall
-    bnez a3, 1f
-    move a0, v0
-    j ra
-    nop
-1:
-    la t9,__set_errno
-    j t9
-    nop
-    .set reorder
-END(__accept)
diff --git a/libc/arch-mips/syscalls/accept4.S b/libc/arch-mips/syscalls/__accept4.S
similarity index 89%
rename from libc/arch-mips/syscalls/accept4.S
rename to libc/arch-mips/syscalls/__accept4.S
index ea1dc60..8634a35 100644
--- a/libc/arch-mips/syscalls/accept4.S
+++ b/libc/arch-mips/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     .set noreorder
     .cpload t9
     li v0, __NR_accept4
@@ -16,4 +16,4 @@
     j t9
     nop
     .set reorder
-END(accept4)
+END(__accept4)
diff --git a/libc/arch-mips/syscalls/socket.S b/libc/arch-mips/syscalls/__socket.S
similarity index 89%
rename from libc/arch-mips/syscalls/socket.S
rename to libc/arch-mips/syscalls/__socket.S
index 2056bcd..6508911 100644
--- a/libc/arch-mips/syscalls/socket.S
+++ b/libc/arch-mips/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     .set noreorder
     .cpload t9
     li v0, __NR_socket
@@ -16,4 +16,4 @@
     j t9
     nop
     .set reorder
-END(socket)
+END(__socket)
diff --git a/libc/arch-mips64/syscalls/__accept.S b/libc/arch-mips64/syscalls/__accept.S
deleted file mode 100644
index 4404a68..0000000
--- a/libc/arch-mips64/syscalls/__accept.S
+++ /dev/null
@@ -1,26 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    .set push
-    .set noreorder
-    li v0, __NR_accept
-    syscall
-    bnez a3, 1f
-    move a0, v0
-    j ra
-    nop
-1:
-    move t0, ra
-    bal     2f
-    nop
-2:
-    .cpsetup ra, t1, 2b
-    LA t9,__set_errno
-    .cpreturn
-    j t9
-    move ra, t0
-    .set pop
-END(__accept)
-.hidden __accept
diff --git a/libc/arch-mips64/syscalls/accept4.S b/libc/arch-mips64/syscalls/__accept4.S
similarity index 87%
rename from libc/arch-mips64/syscalls/accept4.S
rename to libc/arch-mips64/syscalls/__accept4.S
index 8b5cadb..e68bdb6 100644
--- a/libc/arch-mips64/syscalls/accept4.S
+++ b/libc/arch-mips64/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     .set push
     .set noreorder
     li v0, __NR_accept4
@@ -22,4 +22,5 @@
     j t9
     move ra, t0
     .set pop
-END(accept4)
+END(__accept4)
+.hidden __accept4
diff --git a/libc/arch-mips64/syscalls/socket.S b/libc/arch-mips64/syscalls/__socket.S
similarity index 87%
rename from libc/arch-mips64/syscalls/socket.S
rename to libc/arch-mips64/syscalls/__socket.S
index 2020e2e..d1e0058 100644
--- a/libc/arch-mips64/syscalls/socket.S
+++ b/libc/arch-mips64/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     .set push
     .set noreorder
     li v0, __NR_socket
@@ -22,4 +22,5 @@
     j t9
     move ra, t0
     .set pop
-END(socket)
+END(__socket)
+.hidden __socket
diff --git a/libc/arch-x86/syscalls/__accept.S b/libc/arch-x86/syscalls/__accept.S
deleted file mode 100644
index 31cb350..0000000
--- a/libc/arch-x86/syscalls/__accept.S
+++ /dev/null
@@ -1,27 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    pushl   %ebx
-    pushl   %ecx
-    .cfi_def_cfa_offset 8
-    .cfi_rel_offset ebx, 0
-    .cfi_rel_offset ecx, 4
-    mov     $5, %ebx
-    mov     %esp, %ecx
-    addl    $12, %ecx
-    movl    $__NR_socketcall, %eax
-    int     $0x80
-    cmpl    $-MAX_ERRNO, %eax
-    jb      1f
-    negl    %eax
-    pushl   %eax
-    call    __set_errno
-    addl    $4, %esp
-    orl     $-1, %eax
-1:
-    popl    %ecx
-    popl    %ebx
-    ret
-END(__accept)
diff --git a/libc/arch-x86/syscalls/accept4.S b/libc/arch-x86/syscalls/__accept4.S
similarity index 94%
rename from libc/arch-x86/syscalls/accept4.S
rename to libc/arch-x86/syscalls/__accept4.S
index dc3c1f5..573eddb 100644
--- a/libc/arch-x86/syscalls/accept4.S
+++ b/libc/arch-x86/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     pushl   %ebx
     pushl   %ecx
     .cfi_def_cfa_offset 8
@@ -24,4 +24,4 @@
     popl    %ecx
     popl    %ebx
     ret
-END(accept4)
+END(__accept4)
diff --git a/libc/arch-x86/syscalls/socket.S b/libc/arch-x86/syscalls/__socket.S
similarity index 94%
rename from libc/arch-x86/syscalls/socket.S
rename to libc/arch-x86/syscalls/__socket.S
index 282681e..5e52d8f 100644
--- a/libc/arch-x86/syscalls/socket.S
+++ b/libc/arch-x86/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     pushl   %ebx
     pushl   %ecx
     .cfi_def_cfa_offset 8
@@ -24,4 +24,4 @@
     popl    %ecx
     popl    %ebx
     ret
-END(socket)
+END(__socket)
diff --git a/libc/arch-x86_64/syscalls/__accept.S b/libc/arch-x86_64/syscalls/__accept.S
deleted file mode 100644
index ff0f5e7..0000000
--- a/libc/arch-x86_64/syscalls/__accept.S
+++ /dev/null
@@ -1,17 +0,0 @@
-/* Generated by gensyscalls.py. Do not edit. */
-
-#include <private/bionic_asm.h>
-
-ENTRY(__accept)
-    movl    $__NR_accept, %eax
-    syscall
-    cmpq    $-MAX_ERRNO, %rax
-    jb      1f
-    negl    %eax
-    movl    %eax, %edi
-    call    __set_errno
-    orq     $-1, %rax
-1:
-    ret
-END(__accept)
-.hidden __accept
diff --git a/libc/arch-x86_64/syscalls/accept4.S b/libc/arch-x86_64/syscalls/__accept4.S
similarity index 85%
rename from libc/arch-x86_64/syscalls/accept4.S
rename to libc/arch-x86_64/syscalls/__accept4.S
index d66d952..cf4837f 100644
--- a/libc/arch-x86_64/syscalls/accept4.S
+++ b/libc/arch-x86_64/syscalls/__accept4.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(accept4)
+ENTRY(__accept4)
     movq    %rcx, %r10
     movl    $__NR_accept4, %eax
     syscall
@@ -14,4 +14,5 @@
     orq     $-1, %rax
 1:
     ret
-END(accept4)
+END(__accept4)
+.hidden __accept4
diff --git a/libc/arch-x86_64/syscalls/socket.S b/libc/arch-x86_64/syscalls/__socket.S
similarity index 85%
rename from libc/arch-x86_64/syscalls/socket.S
rename to libc/arch-x86_64/syscalls/__socket.S
index 9665ee4..67621fc 100644
--- a/libc/arch-x86_64/syscalls/socket.S
+++ b/libc/arch-x86_64/syscalls/__socket.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(socket)
+ENTRY(__socket)
     movl    $__NR_socket, %eax
     syscall
     cmpq    $-MAX_ERRNO, %rax
@@ -13,4 +13,5 @@
     orq     $-1, %rax
 1:
     ret
-END(socket)
+END(__socket)
+.hidden __socket
diff --git a/libc/bionic/NetdClient.cpp b/libc/bionic/NetdClient.cpp
index 72d90b7..5b0f4fd 100644
--- a/libc/bionic/NetdClient.cpp
+++ b/libc/bionic/NetdClient.cpp
@@ -18,11 +18,11 @@
 #error NetdClient.cpp should NOT be included in static libc builds.
 #endif
 
-#include <private/NetdClient.h>
-#include <private/libc_logging.h>
-#include <pthread.h>
+#include "private/libc_logging.h"
+#include "private/NetdClientDispatch.h"
 
 #include <dlfcn.h>
+#include <pthread.h>
 
 template <typename FunctionType>
 static void netdClientInitFunction(void* handle, const char* symbol, FunctionType* function) {
@@ -40,16 +40,19 @@
         // default implementations of functions that it would've overridden.
         return;
     }
-    netdClientInitFunction(netdClientHandle, "netdClientInitAccept", &__netdClientDispatch.accept);
+    netdClientInitFunction(netdClientHandle, "netdClientInitAccept4",
+                           &__netdClientDispatch.accept4);
     netdClientInitFunction(netdClientHandle, "netdClientInitConnect",
                            &__netdClientDispatch.connect);
+    netdClientInitFunction(netdClientHandle, "netdClientInitNetIdForResolv",
+                           &__netdClientDispatch.netIdForResolv);
+    netdClientInitFunction(netdClientHandle, "netdClientInitSocket", &__netdClientDispatch.socket);
 }
 
 static pthread_once_t netdClientInitOnce = PTHREAD_ONCE_INIT;
 
 extern "C" __LIBC_HIDDEN__ void netdClientInit() {
     if (pthread_once(&netdClientInitOnce, netdClientInitImpl)) {
-        __libc_format_log(ANDROID_LOG_ERROR, "netdClient",
-                          "Unable to initialize netd_client component.");
+        __libc_format_log(ANDROID_LOG_ERROR, "netdClient", "Failed to initialize netd_client");
     }
 }
diff --git a/libc/bionic/NetdClientDispatch.cpp b/libc/bionic/NetdClientDispatch.cpp
index adfe16d..67fa197 100644
--- a/libc/bionic/NetdClientDispatch.cpp
+++ b/libc/bionic/NetdClientDispatch.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include <private/NetdClient.h>
+#include "private/NetdClientDispatch.h"
 
 #ifdef __i386__
 #define __socketcall __attribute__((__cdecl__))
@@ -22,10 +22,19 @@
 #define __socketcall
 #endif
 
-extern "C" __socketcall int __accept(int, sockaddr*, socklen_t*);
+extern "C" __socketcall int __accept4(int, sockaddr*, socklen_t*, int);
 extern "C" __socketcall int __connect(int, const sockaddr*, socklen_t);
+extern "C" __socketcall int __socket(int, int, int);
 
-NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
-    __accept,
+static unsigned fallBackNetIdForResolv(unsigned netId) {
+    return netId;
+}
+
+// This structure is modified only at startup (when libc.so is loaded) and never
+// afterwards, so it's okay that it's read later at runtime without a lock.
+__LIBC_HIDDEN__ NetdClientDispatch __netdClientDispatch __attribute__((aligned(32))) = {
+    __accept4,
     __connect,
+    __socket,
+    fallBackNetIdForResolv,
 };
diff --git a/libc/bionic/accept.cpp b/libc/bionic/accept.cpp
index 46b4efc..7f7aa06 100644
--- a/libc/bionic/accept.cpp
+++ b/libc/bionic/accept.cpp
@@ -14,9 +14,8 @@
  * limitations under the License.
  */
 
-#include <private/NetdClient.h>
 #include <sys/socket.h>
 
 int accept(int sockfd, sockaddr* addr, socklen_t* addrlen) {
-    return __netdClientDispatch.accept(sockfd, addr, addrlen);
+    return accept4(sockfd, addr, addrlen, 0);
 }
diff --git a/libc/private/NetdClient.h b/libc/bionic/accept4.cpp
similarity index 69%
rename from libc/private/NetdClient.h
rename to libc/bionic/accept4.cpp
index b2ce7a6..9f58dc1 100644
--- a/libc/private/NetdClient.h
+++ b/libc/bionic/accept4.cpp
@@ -14,16 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef PRIVATE_NETD_CLIENT_H
-#define PRIVATE_NETD_CLIENT_H
+#include "private/NetdClientDispatch.h"
 
 #include <sys/socket.h>
 
-struct NetdClientDispatch {
-    int (*accept)(int, sockaddr*, socklen_t*);
-    int (*connect)(int, const sockaddr*, socklen_t);
-};
-
-extern NetdClientDispatch __netdClientDispatch;
-
-#endif  // PRIVATE_NETD_CLIENT_H
+int accept4(int sockfd, sockaddr* addr, socklen_t* addrlen, int flags) {
+    return __netdClientDispatch.accept4(sockfd, addr, addrlen, flags);
+}
diff --git a/libc/bionic/connect.cpp b/libc/bionic/connect.cpp
index c5db46b..1673f4a 100644
--- a/libc/bionic/connect.cpp
+++ b/libc/bionic/connect.cpp
@@ -14,7 +14,8 @@
  * limitations under the License.
  */
 
-#include <private/NetdClient.h>
+#include "private/NetdClientDispatch.h"
+
 #include <sys/socket.h>
 
 int connect(int sockfd, const sockaddr* addr, socklen_t addrlen) {
diff --git a/libc/private/NetdClient.h b/libc/bionic/socket.cpp
similarity index 69%
copy from libc/private/NetdClient.h
copy to libc/bionic/socket.cpp
index b2ce7a6..2f9e145 100644
--- a/libc/private/NetdClient.h
+++ b/libc/bionic/socket.cpp
@@ -14,16 +14,10 @@
  * limitations under the License.
  */
 
-#ifndef PRIVATE_NETD_CLIENT_H
-#define PRIVATE_NETD_CLIENT_H
+#include "private/NetdClientDispatch.h"
 
 #include <sys/socket.h>
 
-struct NetdClientDispatch {
-    int (*accept)(int, sockaddr*, socklen_t*);
-    int (*connect)(int, const sockaddr*, socklen_t);
-};
-
-extern NetdClientDispatch __netdClientDispatch;
-
-#endif  // PRIVATE_NETD_CLIENT_H
+int socket(int domain, int type, int protocol) {
+    return __netdClientDispatch.socket(domain, type, protocol);
+}
diff --git a/libc/bionic/typeinfo.cpp b/libc/bionic/typeinfo.cpp
deleted file mode 100644
index 90180fb..0000000
--- a/libc/bionic/typeinfo.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Copyright (C) 2008 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 <typeinfo>
-#include <stdlib.h>
-
-type_info::type_info() {
-}
-
-type_info::~type_info() {
-}
-
-char const* type_info::name() const {
-  return "N/A";
-}
-
-bool type_info::operator==(type_info const& /*rhs*/) const {
-  return false;
-}
-
-bool type_info::operator!=(type_info const& /*rhs*/) const {
-  return false;
-}
-
-bool type_info::before(type_info const& /*rhs*/) const {
-  return false;
-}
-
-type_info::type_info(type_info const& /*rhs*/) {
-}
diff --git a/libc/dns/gethnamaddr.c b/libc/dns/gethnamaddr.c
index 4da99b2..1afad6d 100644
--- a/libc/dns/gethnamaddr.c
+++ b/libc/dns/gethnamaddr.c
@@ -60,6 +60,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <arpa/nameser.h>
+#include "NetdClientDispatch.h"
 #include "resolv_netid.h"
 #include "resolv_private.h"
 #include "resolv_cache.h"
@@ -760,6 +761,8 @@
 	proxy = android_open_proxy();
 	if (proxy == NULL) goto exit;
 
+	netid = __netdClientDispatch.netIdForResolv(netid);
+
 	/* This is writing to system/netd/DnsProxyListener.cpp and changes
 	 * here need to be matched there */
 	if (fprintf(proxy, "gethostbyname %u %s %d",
@@ -796,6 +799,8 @@
 	const char * addrStr = inet_ntop(af, addr, buf, sizeof(buf));
 	if (addrStr == NULL) goto exit;
 
+	netid = __netdClientDispatch.netIdForResolv(netid);
+
 	if (fprintf(proxy, "gethostbyaddr %s %d %d %u",
 			addrStr, len, af, netid) < 0) {
 		goto exit;
diff --git a/libc/dns/net/getaddrinfo.c b/libc/dns/net/getaddrinfo.c
index 4c120d9..be692e3 100644
--- a/libc/dns/net/getaddrinfo.c
+++ b/libc/dns/net/getaddrinfo.c
@@ -92,6 +92,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <netdb.h>
+#include "NetdClientDispatch.h"
 #include "resolv_cache.h"
 #include "resolv_netid.h"
 #include "resolv_private.h"
@@ -449,6 +450,8 @@
 		return EAI_NODATA;
 	}
 
+	netid = __netdClientDispatch.netIdForResolv(netid);
+
 	// Send the request.
 	proxy = fdopen(sock, "r+");
 	if (fprintf(proxy, "getaddrinfo %s %s %d %d %d %d %u",
diff --git a/libc/include/sys/ucontext.h b/libc/include/sys/ucontext.h
index f150ac7..f86d9f4 100644
--- a/libc/include/sys/ucontext.h
+++ b/libc/include/sys/ucontext.h
@@ -71,7 +71,8 @@
   stack_t uc_stack;
   mcontext_t uc_mcontext;
   sigset_t uc_sigmask;
-  /* TODO: uc_regspace */
+  char __padding[128 - sizeof(sigset_t)];
+  unsigned long uc_regspace[128] __attribute__((__aligned__(8)));
 } ucontext_t;
 
 #elif defined(__aarch64__)
diff --git a/libc/private/NetdClient.h b/libc/private/NetdClientDispatch.h
similarity index 60%
copy from libc/private/NetdClient.h
copy to libc/private/NetdClientDispatch.h
index b2ce7a6..8d8947d 100644
--- a/libc/private/NetdClient.h
+++ b/libc/private/NetdClientDispatch.h
@@ -14,16 +14,23 @@
  * limitations under the License.
  */
 
-#ifndef PRIVATE_NETD_CLIENT_H
-#define PRIVATE_NETD_CLIENT_H
+#ifndef PRIVATE_NETD_CLIENT_DISPATCH_H
+#define PRIVATE_NETD_CLIENT_DISPATCH_H
 
+#include <sys/cdefs.h>
 #include <sys/socket.h>
 
+__BEGIN_DECLS
+
 struct NetdClientDispatch {
-    int (*accept)(int, sockaddr*, socklen_t*);
-    int (*connect)(int, const sockaddr*, socklen_t);
+    int (*accept4)(int, struct sockaddr*, socklen_t*, int);
+    int (*connect)(int, const struct sockaddr*, socklen_t);
+    int (*socket)(int, int, int);
+    unsigned (*netIdForResolv)(unsigned);
 };
 
-extern NetdClientDispatch __netdClientDispatch;
+extern __LIBC_HIDDEN__ struct NetdClientDispatch __netdClientDispatch;
 
-#endif  // PRIVATE_NETD_CLIENT_H
+__END_DECLS
+
+#endif  // PRIVATE_NETD_CLIENT_DISPATCH_H
diff --git a/libc/upstream-netbsd/lib/libc/inet/inet_ntop.c b/libc/upstream-netbsd/lib/libc/inet/inet_ntop.c
deleted file mode 100644
index d27a5b1..0000000
--- a/libc/upstream-netbsd/lib/libc/inet/inet_ntop.c
+++ /dev/null
@@ -1,231 +0,0 @@
-/*	$NetBSD: inet_ntop.c,v 1.11 2014/02/10 16:30:54 christos Exp $	*/
-
-/*
- * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
- * Copyright (c) 1996-1999 by Internet Software Consortium.
- *
- * 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 ISC DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC 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/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp";
-#else
-__RCSID("$NetBSD: inet_ntop.c,v 1.11 2014/02/10 16:30:54 christos Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
-#include "port_before.h"
-
-#include "namespace.h"
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <arpa/nameser.h>
-
-#include <assert.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "port_after.h"
-
-#ifdef __weak_alias
-__weak_alias(inet_ntop,_inet_ntop)
-#endif
-
-/*%
- * WARNING: Don't even consider trying to compile this on a system where
- * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
- */
-
-static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
-static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
-
-/* char *
- * inet_ntop(af, src, dst, size)
- *	convert a network format address to presentation format.
- * return:
- *	pointer to presentation format address (`dst'), or NULL (see errno).
- * author:
- *	Paul Vixie, 1996.
- */
-const char *
-inet_ntop(int af, const void *src, char *dst, socklen_t size)
-{
-
-	_DIAGASSERT(src != NULL);
-	_DIAGASSERT(dst != NULL);
-
-	switch (af) {
-	case AF_INET:
-		return inet_ntop4(src, dst, size);
-	case AF_INET6:
-		return inet_ntop6(src, dst, size);
-	default:
-		errno = EAFNOSUPPORT;
-		return NULL;
-	}
-	/* NOTREACHED */
-}
-
-/* const char *
- * inet_ntop4(src, dst, size)
- *	format an IPv4 address, more or less like inet_ntoa()
- * return:
- *	`dst' (as a const)
- * notes:
- *	(1) uses no statics
- *	(2) takes a u_char* not an in_addr as input
- * author:
- *	Paul Vixie, 1996.
- */
-static const char *
-inet_ntop4(const u_char *src, char *dst, socklen_t size)
-{
-	char tmp[sizeof "255.255.255.255"];
-	int l;
-
-	_DIAGASSERT(src != NULL);
-	_DIAGASSERT(dst != NULL);
-
-	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
-	    src[0], src[1], src[2], src[3]);
-	if (l <= 0 || (socklen_t) l >= size)
-		return NULL;
-	strlcpy(dst, tmp, size);
-	return dst;
-}
-
-/* const char *
- * inet_ntop6(src, dst, size)
- *	convert IPv6 binary address into presentation (printable) format
- * author:
- *	Paul Vixie, 1996.
- */
-static const char *
-inet_ntop6(const u_char *src, char *dst, socklen_t size)
-{
-	/*
-	 * Note that int32_t and int16_t need only be "at least" large enough
-	 * to contain a value of the specified size.  On some systems, like
-	 * Crays, there is no such thing as an integer variable with 16 bits.
-	 * Keep this in mind if you think this function should have been coded
-	 * to use pointer overlays.  All the world's not a VAX.
-	 */
-	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
-	char *tp, *ep;
-	struct { int base, len; } best, cur;
-	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
-	int i;
-	int advance;
-
-	_DIAGASSERT(src != NULL);
-	_DIAGASSERT(dst != NULL);
-
-	/*
-	 * Preprocess:
-	 *	Copy the input (bytewise) array into a wordwise array.
-	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
-	 */
-	memset(words, '\0', sizeof words);
-	for (i = 0; i < NS_IN6ADDRSZ; i++)
-		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
-	best.base = -1;
-	best.len = 0;
-	cur.base = -1;
-	cur.len = 0;
-	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
-		if (words[i] == 0) {
-			if (cur.base == -1)
-				cur.base = i, cur.len = 1;
-			else
-				cur.len++;
-		} else {
-			if (cur.base != -1) {
-				if (best.base == -1 || cur.len > best.len)
-					best = cur;
-				cur.base = -1;
-			}
-		}
-	}
-	if (cur.base != -1) {
-		if (best.base == -1 || cur.len > best.len)
-			best = cur;
-	}
-	if (best.base != -1 && best.len < 2)
-		best.base = -1;
-
-	/*
-	 * Format the result.
-	 */
-	tp = tmp;
-	ep = tmp + sizeof(tmp);
-	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
-		/* Are we inside the best run of 0x00's? */
-		if (best.base != -1 && i >= best.base &&
-		    i < (best.base + best.len)) {
-			if (i == best.base)
-				*tp++ = ':';
-			continue;
-		}
-		/* Are we following an initial run of 0x00s or any real hex? */
-		if (i != 0) {
-			if (tp + 1 >= ep)
-				goto out;
-			*tp++ = ':';
-		}
-		/* Is this address an encapsulated IPv4? */
-		if (i == 6 && best.base == 0 &&
-		    (best.len == 6 ||
-		    (best.len == 7 && words[7] != 0x0001) ||
-		    (best.len == 5 && words[5] == 0xffff))) {
-			if (!inet_ntop4(src + 12, tp, (socklen_t)(ep - tp)))
-				goto out;
-			tp += strlen(tp);
-			break;
-		}
-		advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]);
-		if (advance <= 0 || advance >= ep - tp)
-			goto out;
-		tp += advance;
-	}
-	/* Was it a trailing run of 0x00's? */
-	if (best.base != -1 && (best.base + best.len) == 
-	    (NS_IN6ADDRSZ / NS_INT16SZ)) {
-		if (tp + 1 >= ep)
-			goto out;
-		*tp++ = ':';
-	}
-	if (tp + 1 >= ep)
-		goto out;
-	*tp++ = '\0';
-
-	/*
-	 * Check for overflow, copy, and we're done.
-	 */
-	if ((size_t)(tp - tmp) > size)
-		goto out;
-	strlcpy(dst, tmp, size);
-	return dst;
-out:
-	errno = ENOSPC;
-	return NULL;
-}
-
-/*! \file */
diff --git a/libc/upstream-openbsd/lib/libc/net/inet_ntop.c b/libc/upstream-openbsd/lib/libc/net/inet_ntop.c
index 359acd8..f991a07 100644
--- a/libc/upstream-openbsd/lib/libc/net/inet_ntop.c
+++ b/libc/upstream-openbsd/lib/libc/net/inet_ntop.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: inet_ntop.c,v 1.9 2014/02/05 14:20:43 millert Exp $	*/
+/*	$OpenBSD: inet_ntop.c,v 1.10 2014/05/17 18:16:14 tedu Exp $	*/
 
 /* Copyright (c) 1996 by Internet Software Consortium.
  *
@@ -71,11 +71,11 @@
 static const char *
 inet_ntop4(const u_char *src, char *dst, size_t size)
 {
-	static const char fmt[] = "%u.%u.%u.%u";
 	char tmp[sizeof "255.255.255.255"];
 	int l;
 
-	l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
+	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
+	    src[0], src[1], src[2], src[3]);
 	if (l <= 0 || l >= size) {
 		errno = ENOSPC;
 		return (NULL);
diff --git a/libm/Android.mk b/libm/Android.mk
index d7d8bc1..4dfb216 100644
--- a/libm/Android.mk
+++ b/libm/Android.mk
@@ -239,6 +239,7 @@
     -Wno-sign-compare \
     -Wno-uninitialized \
     -Wno-unknown-pragmas \
+    -fvisibility=hidden \
 
 libm_common_includes := $(LOCAL_PATH)/upstream-freebsd/lib/msun/src/
 
diff --git a/libm/include/fenv.h b/libm/include/fenv.h
index 6966e0d..73ecc80 100644
--- a/libm/include/fenv.h
+++ b/libm/include/fenv.h
@@ -34,6 +34,7 @@
 #include <machine/fenv.h>
 
 __BEGIN_DECLS
+#pragma GCC visibility push(default)
 
 int feclearexcept(int);
 int fegetexceptflag(fexcept_t *, int);
@@ -64,6 +65,7 @@
 extern const fenv_t __fe_dfl_env;
 #define FE_DFL_ENV  (&__fe_dfl_env)
 
+#pragma GCC visibility pop
 __END_DECLS
 
 #endif  /* ! _FENV_H_ */
diff --git a/libm/include/math.h b/libm/include/math.h
index c264583..4faec33 100644
--- a/libm/include/math.h
+++ b/libm/include/math.h
@@ -20,6 +20,9 @@
 #include <sys/cdefs.h>
 #include <limits.h>
 
+__BEGIN_DECLS
+#pragma GCC visibility push(default)
+
 /*
  * ANSI/POSIX
  */
@@ -165,7 +168,7 @@
  * effect of raising floating-point exceptions, so they are not declared
  * as __pure2.  In C99, FENV_ACCESS affects the purity of these functions.
  */
-__BEGIN_DECLS
+
 /*
  * ANSI/POSIX
  */
@@ -282,6 +285,7 @@
 double	drem(double, double);
 int	finite(double) __pure2;
 int	isnanf(float) __pure2;
+long double significandl(long double);
 
 /*
  * Reentrant version of gamma & lgamma; passes signgam back by reference
@@ -464,6 +468,7 @@
 void sincosl(long double, long double*, long double*);
 #endif /* _GNU_SOURCE */
 
+#pragma GCC visibility pop
 __END_DECLS
 
 #endif /* !_MATH_H_ */
diff --git a/tests/math_test.cpp b/tests/math_test.cpp
index 63ac719..b0d541a 100644
--- a/tests/math_test.cpp
+++ b/tests/math_test.cpp
@@ -1067,8 +1067,6 @@
   ASSERT_FLOAT_EQ(1.5375f, significandf(12.3f));
 }
 
-extern "C" long double significandl(long double); // BSD's <math.h> doesn't declare this.
-
 TEST(math, significandl) {
   ASSERT_DOUBLE_EQ(0.0L, significandl(0.0L));
   ASSERT_DOUBLE_EQ(1.2L, significandl(1.2L));
diff --git a/tests/wchar_test.cpp b/tests/wchar_test.cpp
index c8aec2d..50b8608 100644
--- a/tests/wchar_test.cpp
+++ b/tests/wchar_test.cpp
@@ -306,19 +306,19 @@
   // 2-byte UTF-8.
   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
-  ASSERT_EQ(0x00a2, out);
+  ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
   ASSERT_TRUE(mbsinit(ps));
   // 3-byte UTF-8.
   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
   ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
-  ASSERT_EQ(0x20ac, out);
+  ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
   ASSERT_TRUE(mbsinit(ps));
   // 4-byte UTF-8.
   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
   ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
   ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
-  ASSERT_EQ(0x24b62, out);
+  ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
   ASSERT_TRUE(mbsinit(ps));
 
   // Invalid 2-byte
@@ -341,9 +341,9 @@
   const char* valid = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
   ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
   ASSERT_EQ(L'A', out[0]);
-  ASSERT_EQ(0x00a2, out[1]);
-  ASSERT_EQ(0x20ac, out[2]);
-  ASSERT_EQ(0x24b62, out[3]);
+  ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
+  ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
+  ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
   ASSERT_EQ('e', *valid);
 
   const char* invalid = "A" "\xc2\x20" "ef";