Implement libportable for unknown arch 64bit.

Change-Id: I1e1fd8714b907bfccd5fa8df12a94697886e59cb
diff --git a/ndk/sources/android/libportable/Android.mk b/ndk/sources/android/libportable/Android.mk
index dde8e19..dc851bd 100644
--- a/ndk/sources/android/libportable/Android.mk
+++ b/ndk/sources/android/libportable/Android.mk
@@ -31,63 +31,13 @@
 # Uncomment the next line to easily enable Lib-Portable logging during development.
 # LOCAL_CFLAGS += -DLOG_NDEBUG=0
 
-ifeq ($(TARGET_ARCH),mips)
-libportable_arch_src_files += \
-			arch-mips/clone.c \
-			arch-mips/epoll.c \
-			arch-mips/errno.c \
-			arch-mips/eventfd.c \
-			arch-mips/fcntl.c \
-			arch-mips/filefd.c \
-			arch-mips/flags.c \
-			arch-mips/inotify.c \
-			arch-mips/ioctl.c \
-			arch-mips/mmap.c \
-			arch-mips/open.c \
-			arch-mips/poll.c \
-			arch-mips/pipe.c \
-			arch-mips/pthread.c \
-			arch-mips/resource.c \
-			arch-mips/signal.c \
-			arch-mips/socket.c \
-			arch-mips/sockopt.c \
-			arch-mips/stat.c \
-			arch-mips/statfs.c \
-			arch-mips/syscall.c \
-			arch-mips/timer.c \
-			arch-mips/timerfd.c \
-			arch-mips/waitpid.c \
-			arch-mips/fenv.c \
-			arch-mips/md_swap.c
-
-libportable_arch_src_files += \
-			arch-mips/_setjmp.S \
-			arch-mips/setjmp.S \
-			arch-mips/sigsetjmp.S
-
-endif
-
-ifeq ($(TARGET_ARCH),arm)
-libportable_arch_src_files += \
-			arch-arm/unwind.c \
-			arch-arm/fenv.c \
-			arch-arm/md_swap.c
-endif
-
-ifeq ($(TARGET_ARCH),x86)
-libportable_arch_src_files += \
-			arch-x86/epoll.c \
-			arch-x86/fcntl.c \
-			arch-x86/ioctl.c \
-			arch-x86/open.c \
-			arch-x86/stat.c \
-			arch-x86/fenv.c \
-			arch-x86/md_swap.c
-endif
+libportable_src_files = \
+  $(patsubst $(LOCAL_PATH)/%,%,$(wildcard $(LOCAL_PATH)/arch-$(TARGET_ARCH)/*.c)) \
+  $(patsubst $(LOCAL_PATH)/%,%,$(wildcard $(LOCAL_PATH)/arch-$(TARGET_ARCH)/*.S))
+$(info $(libportable_src_files))
 
 LOCAL_SRC_FILES := \
-        $(libportable_common_src_files) \
-        $(libportable_arch_src_files)
+  $(libportable_src_files)
 
 LOCAL_WHOLE_STATIC_LIBRARIES += cpufeatures
 
diff --git a/ndk/sources/android/libportable/arch-arm64/vfs.c b/ndk/sources/android/libportable/arch-arm64/vfs.c
new file mode 100644
index 0000000..94d6793
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-arm64/vfs.c
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2012, 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 <portability.h>
+#include <vfs_portable.h>
diff --git a/ndk/sources/android/libportable/arch-mips64/fenv.c b/ndk/sources/android/libportable/arch-mips64/fenv.c
new file mode 100644
index 0000000..0721c62
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-mips64/fenv.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright 2013, 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 <portability.h>
+#include <sys/types.h>
+#include <fenv.h>
+#include <fenv_portable.h>
+
+static inline int mips64_change_except(int flags) {
+  int mips64flags = 0;
+  int exception = flags & FE_ALL_EXCEPT_PORTABLE;
+
+  // exception flags
+  if (exception & FE_INVALID_PORTABLE)
+      mips64flags |= FE_INVALID;
+  if (exception & FE_DIVBYZERO_PORTABLE)
+      mips64flags |= FE_DIVBYZERO;
+  if (exception & FE_OVERFLOW_PORTABLE)
+      mips64flags |= FE_OVERFLOW;
+  if (exception & FE_UNDERFLOW_PORTABLE)
+      mips64flags |= FE_UNDERFLOW;
+  if (exception & FE_INEXACT_PORTABLE)
+      mips64flags |= FE_INEXACT;
+
+  return mips64flags;
+}
+
+static inline int mips64_change_rounding(int flags) {
+  int mips64flags = 0;
+  int rounding = flags & 0x03;
+
+  // rounding flags
+  switch(rounding)
+  {
+    case FE_TONEAREST_PORTABLE:
+      mips64flags = FE_TONEAREST;
+      break;
+    case FE_DOWNWARD_PORTABLE:
+      mips64flags = FE_DOWNWARD;
+      break;
+    case FE_UPWARD_PORTABLE:
+      mips64flags = FE_UPWARD;
+      break;
+    case FE_TOWARDZERO_PORTABLE:
+      mips64flags = FE_TOWARDZERO;
+      break;
+  }
+  return mips64flags;
+}
+
+static inline int mips64_get_except(int mips64flags) {
+  int flags = 0;
+  int exception = mips64flags & FE_ALL_EXCEPT;
+
+  // exception flags
+  if (exception & FE_INVALID)
+      flags |= FE_INVALID_PORTABLE;
+  if (exception & FE_DIVBYZERO)
+      flags |= FE_DIVBYZERO_PORTABLE;
+  if (exception & FE_OVERFLOW)
+      flags |= FE_OVERFLOW_PORTABLE;
+  if (exception & FE_UNDERFLOW)
+      flags |= FE_UNDERFLOW_PORTABLE;
+  if (exception & FE_INEXACT)
+      flags |= FE_INEXACT_PORTABLE;
+  return flags;
+}
+
+static inline int mips64_get_rounding(int mips64flags) {
+  int flags = 0;
+  int rounding = mips64flags & _FCSR_RMASK;
+
+  // rounding flags
+  switch(rounding)
+  {
+    case FE_TONEAREST:
+      flags = FE_TONEAREST_PORTABLE;
+      break;
+    case FE_DOWNWARD:
+      flags = FE_DOWNWARD_PORTABLE;
+      break;
+    case FE_UPWARD:
+      flags = FE_UPWARD_PORTABLE;
+      break;
+    case FE_TOWARDZERO:
+      flags = FE_TOWARDZERO_PORTABLE;
+      break;
+  }
+  return flags;
+}
+
+
+int WRAP(feclearexcept)(int flag) {
+  return REAL(feclearexcept)(mips64_change_except(flag));
+}
+
+int WRAP(fegetexceptflag)(fexcept_t_portable *obj, int flag) {
+  int ret = REAL(fegetexceptflag)((fexcept_t*)obj, mips64_change_except(flag));
+  *obj = (fexcept_t_portable) mips64_get_except(*obj);
+  return ret;
+}
+
+int WRAP(feraiseexcept)(int flag) {
+  return REAL(feraiseexcept)(mips64_change_except(flag));
+}
+
+int WRAP(fesetexceptflag)(const fexcept_t_portable *obj, int flag) {
+  const fexcept_t mips64obj = mips64_change_except(*obj);
+  int mips64flag = mips64_change_except(flag);
+  return REAL(fesetexceptflag)(&mips64obj, mips64flag);
+}
+
+int WRAP(fetestexcept)(int flag) {
+  int ret = REAL(fetestexcept)(mips64_change_except(flag));
+  return mips64_get_except(ret);
+}
+
+int WRAP(fegetround)(void) {
+  int round = REAL(fegetround)();
+  return mips64_get_rounding(round);
+}
+
+int WRAP(fesetround)(int round) {
+  return REAL(fesetround)(mips64_change_rounding(round));
+}
+
+int WRAP(fegetenv)(fenv_t_portable *obj) {
+  return REAL(fegetenv)((fenv_t*)obj);
+}
+
+int WRAP(feholdexcept)(fenv_t_portable *obj) {
+  return REAL(feholdexcept)((fenv_t*)obj);
+}
+
+int WRAP(fesetenv)(const fenv_t_portable *obj) {
+  return REAL(fesetenv)((const fenv_t*)obj);
+}
+
+int WRAP(feupdateenv)(const fenv_t_portable *obj) {
+  return REAL(feupdateenv)((const fenv_t*)obj);
+}
+
+int WRAP(fegetexcept)(void) {
+  int flag = REAL(fegetexcept)();
+  return mips64_get_except(flag);
+}
+
diff --git a/ndk/sources/android/libportable/arch-mips64/socket.c b/ndk/sources/android/libportable/arch-mips64/socket.c
new file mode 100644
index 0000000..be2c7e6
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-mips64/socket.c
@@ -0,0 +1,261 @@
+/*
+ * Copyright 2012, 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 <portability.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <fcntl.h>
+#include <netdb.h>
+
+#include <socket_portable.h>
+#include <fcntl_portable.h>
+#include <netdb_portable.h>
+#include <portability.h>
+
+#define PORTABLE_TAG "socket_portable"
+#include <log_portable.h>
+
+
+#if SOCK_STREAM==SOCK_STREAM_PORTABLE
+#error Bad build environment
+#endif
+
+/* LTP defaults to using O_NONBLOCK if SOCK_NONBLOCK is not defined. */
+#ifndef SOCK_NONBLOCK_PORTABLE
+# define SOCK_NONBLOCK_PORTABLE O_NONBLOCK_PORTABLE
+#endif
+#ifndef SOCK_NONBLOCK
+# define SOCK_NONBLOCK O_NONBLOCK
+#endif
+
+/* Current NDK headers do not define SOCK_CLOEXEC or O_CLOEXEC */
+#if !defined(SOCK_CLOEXEC_PORTABLE) && defined(O_CLOEXEC_PORTABLE)
+# define SOCK_CLOEXEC_PORTABLE O_CLOEXEC_PORTABLE
+#endif
+#if !defined(SOCK_CLOEXEC) && defined(O_CLOEXEC)
+# define SOCK_CLOEXEC O_CLOEXEC
+#endif
+
+
+/*
+ * Portable to Native socktype mapper.
+ */
+static inline int socktype_pton(int portable_type)
+{
+    int native_type = 0;
+
+    ALOGV("%s(portable_type:0x%x) {", __func__, portable_type);
+
+    if (portable_type & SOCK_NONBLOCK_PORTABLE) {
+        native_type |= SOCK_NONBLOCK;
+        portable_type &= ~SOCK_NONBLOCK_PORTABLE;
+    }
+
+#if defined(SOCK_CLOEXEC_PORTABLE) && defined(SOCK_CLOEXEC)
+    if (portable_type & SOCK_CLOEXEC_PORTABLE) {
+        native_type |= SOCK_CLOEXEC;
+        portable_type &= ~SOCK_CLOEXEC_PORTABLE;
+    }
+#endif
+
+    switch (portable_type) {
+    case SOCK_STREAM_PORTABLE: native_type |= SOCK_STREAM; break;
+    case SOCK_DGRAM_PORTABLE: native_type |= SOCK_DGRAM; break;
+    case SOCK_RAW_PORTABLE: native_type |= SOCK_RAW; break;
+    case SOCK_RDM_PORTABLE: native_type |= SOCK_RDM; break;
+    case SOCK_SEQPACKET_PORTABLE: native_type |= SOCK_SEQPACKET; break;
+    case SOCK_PACKET_PORTABLE: native_type |= SOCK_PACKET; break;
+    default:
+        ALOGE("%s: case default: native_type:0x%x |= portable_type:0x%x:[UNKNOWN!];", __func__,
+                                 native_type,        portable_type);
+
+        native_type |= portable_type;
+        break;
+    }
+    ALOGV("%s: return(native_type:%d); }", __func__, native_type);
+    return native_type;
+}
+
+
+/*
+ * Native to Portable socktype mapper.
+ */
+static inline int socktype_ntop(int native_type)
+{
+    int portable_type = 0;
+
+    ALOGV("%s(native_type:0x%x) {", __func__, native_type);
+
+    if (native_type & SOCK_NONBLOCK) {
+        portable_type |= SOCK_NONBLOCK_PORTABLE;
+        native_type &= ~SOCK_NONBLOCK;
+    }
+
+#if defined(SOCK_CLOEXEC_PORTABLE) && defined(SOCK_CLOEXEC)
+    if (native_type & SOCK_CLOEXEC) {
+        portable_type |= SOCK_CLOEXEC_PORTABLE;
+        native_type &= ~SOCK_CLOEXEC;
+    }
+#endif
+
+    switch (native_type) {
+    case SOCK_STREAM: portable_type |= SOCK_STREAM_PORTABLE; break;
+    case SOCK_DGRAM: portable_type |= SOCK_DGRAM_PORTABLE; break;
+    case SOCK_RAW: portable_type |= SOCK_RAW_PORTABLE; break;
+    case SOCK_RDM: portable_type |= SOCK_RDM_PORTABLE; break;
+    case SOCK_SEQPACKET: portable_type |= SOCK_SEQPACKET_PORTABLE; break;
+    case SOCK_PACKET: portable_type |= SOCK_PACKET_PORTABLE; break;
+    default:
+        portable_type |= native_type;
+        ALOGE("%s: case default: portable_type:0x%x |= native_type:0x%x:[UNKNOWN!];", __func__,
+                                 portable_type,        native_type);
+    }
+    ALOGV("%s: return(portable_type:%d); }", __func__, portable_type);
+    return portable_type;
+}
+
+
+extern int REAL(socket)(int, int, int);
+
+int WRAP(socket)(int domain, int type, int protocol) {
+    int rv;
+
+    ALOGV(" ");
+    ALOGV("%s(domain:%d, type:%d, protocol:%d) {", __func__,
+              domain,    type,    protocol);
+
+    rv = REAL(socket)(domain, socktype_pton(type), protocol);
+
+    ALOGV("%s: return(rv:%d); }", __func__, rv);
+    return rv;
+}
+
+
+int WRAP(socketpair)(int domain, int type, int protocol, int sv[2]) {
+    int rv;
+
+    ALOGV(" ");
+    ALOGV("%s(domain:%d, type:%d, protocol:%d, sv[2]:%p) {", __func__,
+              domain,    type,    protocol,    sv);
+
+    rv = REAL(socketpair)(domain, socktype_pton(type), protocol, sv);
+
+    if ((rv != 0) || invalid_pointer(sv)) {
+        ALOGV("%s: return(rv:%d); }", __func__,
+                          rv);
+    } else {
+        ALOGV("%s: return(rv:%d); sv[0]:%d; sv[1]:%d;}", __func__,
+                          rv,     sv[0],    sv[1]);
+    }
+    return rv;
+}
+
+#define PRINT_ADDRINFO(p) {                                                                      \
+    ALOGV("%s: p:%p->{ai_flags:%d, ai_family:%d, ai_socktype:%d, ai_protocol:%d, ...", __func__, \
+               p,  p->ai_flags, p->ai_family, p->ai_socktype, p->ai_protocol);                   \
+                                                                                                 \
+    ALOGV("%s: p:%p->{... ai_addrlen:%d, ai_addr:%p, ai_canonname:%p, p->ai_next:%p);", __func__,\
+               p,      p->ai_addrlen, p->ai_addr, p->ai_canonname, p->ai_next);                  \
+}
+
+/*
+ * Returns a list of portable addrinfo structures that are
+ * later made free with a call to the portable version of
+ * freeaddrinfo(); which is written below this function.
+ */
+int WRAP(getaddrinfo)(const char *node, const char *service,
+                 struct addrinfo_portable *portable_hints,
+                 struct addrinfo_portable **portable_results)
+{
+    int rv;
+    struct addrinfo *native_hints;
+    struct addrinfo **native_results, *rp;
+    int saved_portable_socktype;
+
+    ALOGV(" ");
+    ALOGV("%s(node:%p, service:%p, portable_hints:%p, portable_results:%p) {", __func__,
+              node,    service,    portable_hints,    portable_results);
+
+    PRINT_ADDRINFO(portable_hints);
+
+    /*
+     * The only part of the addrinfo structure that needs to be modified
+     * between ARM and MIPS is the socktype;
+     */
+    ASSERT(sizeof(struct addrinfo_portable) == sizeof(struct addrinfo));
+    native_hints = ((struct addrinfo *) portable_hints);
+    if (native_hints != NULL) {
+        saved_portable_socktype = portable_hints->ai_socktype;
+        native_hints->ai_socktype = socktype_pton(saved_portable_socktype);
+    }
+    ASSERT(portable_results != NULL);
+    native_results = (struct addrinfo **) portable_results;
+
+    rv = REAL(getaddrinfo)(node, service, native_hints, native_results);
+
+    if (native_hints != NULL) {
+        portable_hints->ai_socktype = saved_portable_socktype;
+    }
+
+
+    /*
+     * Map socktypes in the return list of addrinfo structures from native to portable.
+     * Assuming getaddrinfo() has left structure writeable and the list is generated
+     * on each call. This seems to be true when looking at the man page and the code
+     * at:
+     *          ./bionic/libc/netbsd/net/getaddrinfo.c
+     */
+    for (rp = *native_results; rp != NULL; rp = rp->ai_next) {
+        PRINT_ADDRINFO(rp);
+        rp->ai_socktype = socktype_ntop(rp->ai_socktype);
+    }
+    ALOGV("%s: return(rv:%d); }", __func__, rv);
+    return rv;
+}
+
+
+/*
+ * Free the results list returned from a previous call
+ * to the portable version of getaddrinfo().
+ */
+void WRAP(freeaddrinfo)(struct addrinfo_portable *portable_results)
+{
+    struct addrinfo *native_results, *rp;
+
+    ALOGV(" ");
+    ALOGV("%s(portable_results:%p) {", __func__, portable_results);
+
+    PRINT_ADDRINFO(portable_results);
+
+    /*
+     * The only part of each addrinfo structure that needs to be modified
+     * between ARM and MIPS is the socktype;
+     *
+     * Map socktypes in the return list of iportable addrinfo structures back to native.
+     * Again, assuming getaddrinfo() has left structure writeable and the list is generated
+     * on each call. This seems to be true when looking at the man page and the code.
+     */
+    ASSERT(sizeof(struct addrinfo_portable) == sizeof(struct addrinfo));
+    native_results = ((struct addrinfo *) portable_results);
+    for (rp = native_results; rp != NULL; rp = rp->ai_next) {
+        PRINT_ADDRINFO(rp);
+        rp->ai_socktype = socktype_pton(rp->ai_socktype);       /* Likely not really necessary */
+    }
+    REAL(freeaddrinfo)(native_results);
+
+    ALOGV("%s: return; }", __func__);
+    return;
+}
diff --git a/ndk/sources/android/libportable/arch-mips64/stat.c b/ndk/sources/android/libportable/arch-mips64/stat.c
new file mode 100644
index 0000000..0bfd0ba
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-mips64/stat.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright 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 <portability.h>
+#include <stat_portable.h>
+#include <sys/stat.h>
+
+
+int WRAP(fstat)(int fd, struct stat_portable *s) {
+  struct stat mips64_stat;
+  int ret = REAL(fstat)(fd, &mips64_stat);
+  stat_ntop(&mips64_stat, s);
+  return ret;
+}
+static inline int WRAP(fstat64)(int fd, struct stat64_portable *s) {
+  return WRAP(fstat)(fd, (struct stat_portable*)s);
+}
+
+
+int WRAP(fstatat)(int dirfd, const char *path, struct stat_portable *s, int flags) {
+  struct stat mips64_stat;
+  int ret = REAL(fstatat)(dirfd, path, &mips64_stat, flags);
+  stat_ntop(&mips64_stat, s);
+  return ret;
+}
+static inline int WRAP(fstatat64)(int dirfd, const char *path,
+                                  struct stat64_portable *s, int flags) {
+  return WRAP(fstatat)(dirfd, path, (struct stat_portable*)s, flags);
+}
+
+
+int WRAP(lstat)(const char *path, struct stat_portable *s) {
+  struct stat mips64_stat;
+  int ret = REAL(lstat)(path, &mips64_stat);
+  stat_ntop(&mips64_stat, s);
+  return ret;
+}
+static inline int WRAP(lstat64)(const char *path, struct stat64_portable *s) {
+  return WRAP(lstat)(path, (struct stat_portable*)s);
+}
+
+
+int WRAP(stat)(const char* path, struct stat_portable* s) {
+  struct stat mips64_stat;
+  int ret = REAL(stat)(path, &mips64_stat);
+  stat_ntop(&mips64_stat, s);
+  return ret;
+}
+static inline int WRAP(stat64)(const char* path, struct stat64_portable *s) {
+  return WRAP(stat)(path, (struct stat_portable*)s);
+}
+
diff --git a/ndk/sources/android/libportable/arch-mips64/va_funcs.c b/ndk/sources/android/libportable/arch-mips64/va_funcs.c
new file mode 100644
index 0000000..d27aaa0
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-mips64/va_funcs.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright 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 <portability.h>
+#include <stdarg.h>
+#include <stdarg_portable.h>
+
+#include <stdio.h>
+
+int WRAP(vfprintf)(FILE *stream, const char *format, va_list_portable *arg) {
+  return REAL(vfprintf)(stream, format, arg);
+}
+
+int WRAP(vfscanf)(FILE *stream, const char *format, va_list_portable *arg) {
+  return REAL(vfscanf)(stream, format, arg);
+}
+
+int WRAP(vprintf)(const char *format, va_list_portable *arg) {
+  return REAL(vprintf)(format, arg);
+}
+
+int WRAP(vscanf)(const char *format, va_list_portable *arg) {
+  return REAL(vscanf)(format, arg);
+}
+
+int WRAP(vsnprintf)(char *s, size_t n, const char *format, va_list_portable *arg) {
+  return REAL(vsnprintf)(s, n, format, arg);
+}
+
+int WRAP(vsprintf)(char *s, const char *format, va_list_portable *arg) {
+  return REAL(vsprintf)(s, format, arg);
+}
+
+int WRAP(vsscanf)(const char *s, const char *format, va_list_portable *arg) {
+  return REAL(vsscanf)(s, format, arg);
+}
+
diff --git a/ndk/sources/android/libportable/arch-mips64/vfs.c b/ndk/sources/android/libportable/arch-mips64/vfs.c
new file mode 100644
index 0000000..94d6793
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-mips64/vfs.c
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2012, 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 <portability.h>
+#include <vfs_portable.h>
diff --git a/ndk/sources/android/libportable/arch-x86_64/fenv.c b/ndk/sources/android/libportable/arch-x86_64/fenv.c
new file mode 100644
index 0000000..f1a55da
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-x86_64/fenv.c
@@ -0,0 +1,182 @@
+/*
+ * Copyright 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 <portability.h>
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <fenv.h>
+#include <fenv_portable.h>
+
+#ifndef _ROUND_MASK
+#define FE_TONEAREST  0x0000
+#define FE_DOWNWARD   0x0400
+#define FE_UPWARD     0x0800
+#define FE_TOWARDZERO 0x0c00
+#define _ROUND_MASK   (FE_TONEAREST | FE_DOWNWARD | \
+                       FE_UPWARD | FE_TOWARDZERO)
+#endif
+
+static inline int x86_64_change_except(int flag) {
+  int x86_64flag = 0;
+  int exception = flag & FE_ALL_EXCEPT_PORTABLE;
+
+  if (exception & FE_INVALID_PORTABLE)
+      x86_64flag |= FE_INVALID;
+  if (exception & FE_DIVBYZERO_PORTABLE)
+      x86_64flag |= FE_DIVBYZERO;
+  if (exception & FE_OVERFLOW_PORTABLE)
+      x86_64flag |= FE_OVERFLOW;
+  if (exception & FE_UNDERFLOW_PORTABLE)
+      x86_64flag |= FE_UNDERFLOW;
+  if (exception & FE_INEXACT_PORTABLE)
+      x86_64flag |= FE_INEXACT;
+
+  return x86_64flag;
+}
+
+static inline int x86_64_change_rounding(int flag) {
+  int x86_64flag = 0;
+  int rounding = flag & 0x03;
+
+  switch(rounding) {
+    case FE_TONEAREST_PORTABLE: {
+      x86_64flag = FE_TONEAREST;
+      break;
+    }
+    case FE_DOWNWARD_PORTABLE: {
+      x86_64flag = FE_DOWNWARD;
+      break;
+    }
+    case FE_UPWARD_PORTABLE: {
+      x86_64flag = FE_UPWARD;
+      break;
+    }
+    case FE_TOWARDZERO_PORTABLE: {
+      x86_64flag = FE_TOWARDZERO;
+      break;
+    }
+  }
+
+  return x86_64flag;
+}
+
+static inline int x86_64_get_except(int x86_64flag) {
+  int flag = 0;
+  int exception = x86_64flag & FE_ALL_EXCEPT;
+
+  if (exception & FE_INVALID)
+      flag |= FE_INVALID_PORTABLE;
+  if (exception & FE_DIVBYZERO)
+      flag |= FE_DIVBYZERO_PORTABLE;
+  if (exception & FE_OVERFLOW)
+      flag |= FE_OVERFLOW_PORTABLE;
+  if (exception & FE_UNDERFLOW)
+      flag |= FE_UNDERFLOW_PORTABLE;
+  if (exception & FE_INEXACT)
+      flag |= FE_INEXACT_PORTABLE;
+
+  return flag;
+}
+
+static inline int x86_64_get_rounding(int x86_64flag) {
+  int flag = 0;
+  int rounding = x86_64flag & _ROUND_MASK;
+
+  switch(rounding) {
+    case FE_TONEAREST: {
+      flag = FE_TONEAREST_PORTABLE;
+      break;
+    }
+    case FE_DOWNWARD: {
+      flag = FE_DOWNWARD_PORTABLE;
+      break;
+    }
+    case FE_UPWARD: {
+      flag = FE_UPWARD_PORTABLE;
+      break;
+    }
+    case FE_TOWARDZERO: {
+      flag = FE_TOWARDZERO_PORTABLE;
+      break;
+    }
+  }
+
+  return flag;
+}
+
+
+int WRAP(feclearexcept)(int flag) {
+  return REAL(feclearexcept)(x86_64_change_except(flag));
+}
+
+#ifdef __LP64__
+int WRAP(fegetexceptflag)(fexcept_t_portable *obj, int flag) {
+  int ret = REAL(fegetexceptflag)((fexcept_t*)obj, x86_64_change_except(flag));
+  *obj = (fexcept_t_portable) x86_64_get_except(*obj);
+  return ret;
+}
+
+int WRAP(fesetexceptflag)(const fexcept_t_portable *obj, int flag) {
+  const fexcept_t x86_64obj = x86_64_change_except(*obj);
+  int x86_64flag = x86_64_change_except(flag);
+  return REAL(fesetexceptflag)(&x86_64obj, x86_64flag);
+}
+#endif
+
+int WRAP(feraiseexcept)(int flag) {
+  return REAL(feraiseexcept)(x86_64_change_except(flag));
+}
+
+
+int WRAP(fetestexcept)(int flag) {
+  int ret = REAL(fetestexcept)(x86_64_change_except(flag));
+  return x86_64_get_except(ret);
+}
+
+int WRAP(fegetround)(void) {
+  int round = REAL(fegetround)();
+  return x86_64_get_rounding(round);
+}
+
+int WRAP(fesetround)(int round) {
+  return REAL(fesetround)(x86_64_change_rounding(round));
+}
+
+#ifdef __LP64__
+int WRAP(fegetenv)(fenv_t_portable *obj) {
+  return REAL(fegetenv)((fenv_t*)obj);
+}
+
+int WRAP(feholdexcept)(fenv_t_portable *obj) {
+  return REAL(feholdexcept)((fenv_t*)obj);
+}
+
+int WRAP(fesetenv)(const fenv_t_portable *obj) {
+  return REAL(fesetenv)((const fenv_t*)obj);
+}
+
+int WRAP(feupdateenv)(const fenv_t_portable *obj) {
+  return REAL(feupdateenv)((const fenv_t*)obj);
+}
+#endif
+
+int WRAP(fegetexcept)(void) {
+  int flag = REAL(fegetexcept)();
+  return x86_64_get_except(flag);
+}
+
diff --git a/ndk/sources/android/libportable/arch-x86_64/stat.c b/ndk/sources/android/libportable/arch-x86_64/stat.c
new file mode 100644
index 0000000..dd128bc
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-x86_64/stat.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright 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 <portability.h>
+#include <stat_portable.h>
+#include <sys/stat.h>
+
+
+int WRAP(fstat)(int fd, struct stat_portable *s) {
+  struct stat x86_64_stat;
+  int ret = REAL(fstat)(fd, &x86_64_stat);
+  stat_ntop(&x86_64_stat, s);
+  return ret;
+}
+static inline int WRAP(fstat64)(int fd, struct stat64_portable *s) {
+  return WRAP(fstat)(fd, (struct stat_portable*)s);
+}
+
+
+int WRAP(fstatat)(int dirfd, const char *path, struct stat_portable *s, int flags) {
+  struct stat x86_64_stat;
+  int ret = REAL(fstatat)(dirfd, path, &x86_64_stat, flags);
+  stat_ntop(&x86_64_stat, s);
+  return ret;
+}
+static inline int WRAP(fstatat64)(int dirfd, const char *path,
+                                  struct stat64_portable *s, int flags) {
+  return WRAP(fstatat)(dirfd, path, (struct stat_portable*)s, flags);
+}
+
+
+int WRAP(lstat)(const char *path, struct stat_portable *s) {
+  struct stat x86_64_stat;
+  int ret = REAL(lstat)(path, &x86_64_stat);
+  stat_ntop(&x86_64_stat, s);
+  return ret;
+}
+static inline int WRAP(lstat64)(const char *path, struct stat64_portable *s) {
+  return WRAP(lstat)(path, (struct stat_portable*)s);
+}
+
+
+int WRAP(stat)(const char* path, struct stat_portable* s) {
+  struct stat x86_64_stat;
+  int ret = REAL(stat)(path, &x86_64_stat);
+  stat_ntop(&x86_64_stat, s);
+  return ret;
+}
+static inline int WRAP(stat64)(const char* path, struct stat64_portable *s) {
+  return WRAP(stat)(path, (struct stat_portable*)s);
+}
+
diff --git a/ndk/sources/android/libportable/arch-x86_64/va_funcs.c b/ndk/sources/android/libportable/arch-x86_64/va_funcs.c
new file mode 100644
index 0000000..d27aaa0
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-x86_64/va_funcs.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright 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 <portability.h>
+#include <stdarg.h>
+#include <stdarg_portable.h>
+
+#include <stdio.h>
+
+int WRAP(vfprintf)(FILE *stream, const char *format, va_list_portable *arg) {
+  return REAL(vfprintf)(stream, format, arg);
+}
+
+int WRAP(vfscanf)(FILE *stream, const char *format, va_list_portable *arg) {
+  return REAL(vfscanf)(stream, format, arg);
+}
+
+int WRAP(vprintf)(const char *format, va_list_portable *arg) {
+  return REAL(vprintf)(format, arg);
+}
+
+int WRAP(vscanf)(const char *format, va_list_portable *arg) {
+  return REAL(vscanf)(format, arg);
+}
+
+int WRAP(vsnprintf)(char *s, size_t n, const char *format, va_list_portable *arg) {
+  return REAL(vsnprintf)(s, n, format, arg);
+}
+
+int WRAP(vsprintf)(char *s, const char *format, va_list_portable *arg) {
+  return REAL(vsprintf)(s, format, arg);
+}
+
+int WRAP(vsscanf)(const char *s, const char *format, va_list_portable *arg) {
+  return REAL(vsscanf)(s, format, arg);
+}
+
diff --git a/ndk/sources/android/libportable/arch-x86_64/vfs.c b/ndk/sources/android/libportable/arch-x86_64/vfs.c
new file mode 100644
index 0000000..94d6793
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-x86_64/vfs.c
@@ -0,0 +1,18 @@
+/*
+ * Copyright 2012, 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 <portability.h>
+#include <vfs_portable.h>
diff --git a/ndk/sources/android/libportable/common/include/fenv_portable.h b/ndk/sources/android/libportable/common/include/fenv_portable.h
index 902c141..1d1e500 100644
--- a/ndk/sources/android/libportable/common/include/fenv_portable.h
+++ b/ndk/sources/android/libportable/common/include/fenv_portable.h
@@ -19,6 +19,13 @@
 
 #include <sys/types.h>
 
+#ifdef __LP64__
+typedef struct {
+  unsigned char a[28];  // x86_64 largest size
+} fenv_t_portable;
+typedef __uint32_t fexcept_t_portable;
+#endif
+
 /* Exception flags. */
 #define FE_INVALID_PORTABLE    0x01
 #define FE_DIVBYZERO_PORTABLE  0x02
diff --git a/ndk/sources/android/libportable/common/include/portability.h b/ndk/sources/android/libportable/common/include/portability.h
index 54f1406..b157645 100644
--- a/ndk/sources/android/libportable/common/include/portability.h
+++ b/ndk/sources/android/libportable/common/include/portability.h
@@ -17,6 +17,7 @@
 #ifndef _PORTABILITY_H_
 #define _PORTABILITY_H_
 
+#include <stdint.h>
 #include "asm-generic/portability.h"
 /*
  * Common portability helper routines
@@ -35,7 +36,7 @@
     return p == 0
         || p == (void *)-1
 #ifdef __mips__
-        || (int)p < 0
+        || (intptr_t)p < 0
 #endif
         ;
 }
diff --git a/ndk/sources/android/libportable/common/include/stat_portable.h b/ndk/sources/android/libportable/common/include/stat_portable.h
index d50291d..fb9671d 100644
--- a/ndk/sources/android/libportable/common/include/stat_portable.h
+++ b/ndk/sources/android/libportable/common/include/stat_portable.h
@@ -20,6 +20,59 @@
 #include <sys/stat.h>
 #include <string.h>
 
+#ifdef __LP64__
+
+#define __STAT64_BODY_PORTABLE \
+  unsigned long st_dev; \
+  unsigned long st_ino; \
+  unsigned int st_mode; \
+  unsigned int st_nlink; \
+  uid_t st_uid; \
+  gid_t st_gid; \
+  unsigned long st_rdev; \
+  unsigned long __pad1; \
+  long st_size; \
+  int st_blksize; \
+  int __pad2; \
+  long st_blocks; \
+  long st_atime; \
+  unsigned long st_atime_nsec; \
+  long st_mtime; \
+  unsigned long st_mtime_nsec; \
+  long st_ctime; \
+  unsigned long st_ctime_nsec; \
+  unsigned int __unused4; \
+  unsigned int __unused5; \
+  unsigned long __unused_for_largest_size; \
+
+
+struct stat_portable { __STAT64_BODY_PORTABLE };
+struct stat64_portable { __STAT64_BODY_PORTABLE };
+
+static inline
+void stat_ntop(struct stat *n_stat, struct stat_portable *p_stat)
+{
+    memset(p_stat, '\0', sizeof(struct stat_portable));
+    p_stat->st_dev        = n_stat->st_dev;
+    p_stat->st_ino        = n_stat->st_ino;
+    p_stat->st_mode       = n_stat->st_mode;
+    p_stat->st_nlink      = n_stat->st_nlink;
+    p_stat->st_uid        = n_stat->st_uid;
+    p_stat->st_gid        = n_stat->st_gid;
+    p_stat->st_rdev       = n_stat->st_rdev;
+    p_stat->st_size       = n_stat->st_size;
+    p_stat->st_blksize    = n_stat->st_blksize;
+    p_stat->st_blocks     = n_stat->st_blocks;
+    p_stat->st_atime      = n_stat->st_atime;
+    p_stat->st_atime_nsec = n_stat->st_atime_nsec;
+    p_stat->st_mtime      = n_stat->st_mtime;
+    p_stat->st_mtime_nsec = n_stat->st_mtime_nsec;
+    p_stat->st_ctime      = n_stat->st_ctime;
+    p_stat->st_ctime_nsec = n_stat->st_ctime_nsec;
+}
+
+#else // ! __LP64__
+
 /* It's easy to change kernel to support stat */
 struct stat_portable {
     unsigned long long  st_dev;
@@ -148,4 +201,6 @@
     p_stat->st_ino        = n_stat->st_ino;
 }
 
+#endif // __LP64__
+
 #endif /* _STAT_PORTABLE_H */
diff --git a/ndk/sources/android/libportable/common/include/stdarg_portable.h b/ndk/sources/android/libportable/common/include/stdarg_portable.h
new file mode 100644
index 0000000..726ed11
--- /dev/null
+++ b/ndk/sources/android/libportable/common/include/stdarg_portable.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright 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.
+ */
+
+#ifndef _STDARG_PORTABLE_H_
+#define _STDARG_PORTABLE_H_
+
+// The elements are not important. This struct should be interpreted
+// differently by all targets.
+typedef struct va_list_portable {
+  void *ptr1;
+  void *ptr2;
+  void *ptr3;
+  int offset1;
+  int offset2;
+} va_list_portable;
+
+#endif /* _STDARG_PORTABLE_H_ */
diff --git a/ndk/sources/android/libportable/common/include/vfs_portable.h b/ndk/sources/android/libportable/common/include/vfs_portable.h
new file mode 100644
index 0000000..762ce0c
--- /dev/null
+++ b/ndk/sources/android/libportable/common/include/vfs_portable.h
@@ -0,0 +1,96 @@
+/*
+ * Copyright 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.
+ */
+
+#ifndef _VFS_PORTABLE_H
+#define _VFS_PORTABLE_H
+
+#include <stdlib.h>
+#include <sys/vfs.h>
+
+/* The kernel's __kernel_fsid_t has a 'val' member but glibc uses '__val'. */
+typedef struct { int __val[2]; } __fsid_t_portable;
+typedef __fsid_t_portable fsid_t_portable;
+
+#define __STATFS64_BODY_PORTABLE \
+  uint64_t f_type; \
+  uint64_t f_bsize; \
+  uint64_t f_blocks; \
+  uint64_t f_bfree; \
+  uint64_t f_bavail; \
+  uint64_t f_files; \
+  uint64_t f_ffree; \
+  fsid_t_portable f_fsid; \
+  uint64_t f_namelen; \
+  uint64_t f_frsize; \
+  uint64_t f_flags; \
+  uint64_t f_spare[5]; \
+
+
+struct statfs_portable { __STATFS64_BODY_PORTABLE };
+struct statfs64_portable { __STATFS64_BODY_PORTABLE };
+
+static inline
+void statfs_ntop(struct statfs *n_statfs, struct statfs_portable *p_statfs) {
+  memset(p_statfs, 0, sizeof(struct statfs_portable));
+  p_statfs->f_type = n_statfs->f_type;
+  p_statfs->f_bsize = n_statfs->f_bsize;
+  p_statfs->f_blocks = n_statfs->f_blocks;
+  p_statfs->f_bfree = n_statfs->f_bfree;
+  p_statfs->f_bavail = n_statfs->f_bavail;
+  p_statfs->f_files = n_statfs->f_files;
+  p_statfs->f_ffree = n_statfs->f_ffree;
+  memcpy(&p_statfs->f_fsid, &n_statfs->f_fsid, sizeof(int)*2);
+  p_statfs->f_namelen = n_statfs->f_namelen;
+  p_statfs->f_frsize = n_statfs->f_frsize;
+  p_statfs->f_flags = n_statfs->f_flags;
+#ifdef __mips__
+  memcpy(&p_statfs->f_spare, &n_statfs->f_spare, 4);
+#else
+  memcpy(&p_statfs->f_spare, &n_statfs->f_spare, 5);
+#endif
+}
+
+
+static inline
+int WRAP(statfs)(const char* path, struct statfs_portable* stat) {
+  struct statfs native_stat;
+
+  int ret = REAL(statfs)(path, &native_stat);
+  statfs_ntop(&native_stat, stat);
+  return ret;
+}
+
+static inline
+int WRAP(statfs64)(const char* path, struct statfs64_portable* stat) {
+  return WRAP(statfs)(path, (struct statfs_portable*)stat);
+}
+
+
+static inline
+int WRAP(fstatfs)(int fd, struct statfs_portable* stat) {
+  struct statfs native_stat;
+
+  int ret = REAL(fstatfs)(fd, &native_stat);
+  statfs_ntop(&native_stat, stat);
+  return ret;
+}
+
+static inline
+int WRAP(fstatfs64)(int fd, struct statfs64_portable* stat) {
+  return WRAP(fstatfs)(fd, (struct statfs_portable*)stat);
+}
+
+#endif