Merge "Fix comment typo."
diff --git a/libc/Android.mk b/libc/Android.mk
index 4a470b7..f56065a 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -70,7 +70,6 @@
     bionic/sigsetmask.c \
     bionic/system_properties_compat.c \
     bionic/unlockpt.c \
-    stdio/findfp.c \
     stdio/snprintf.c\
     stdio/sprintf.c \
     stdlib/atexit.c \
@@ -227,9 +226,7 @@
     upstream-freebsd/lib/libc/stdio/fclose.c \
     upstream-freebsd/lib/libc/stdio/flags.c \
     upstream-freebsd/lib/libc/stdio/fopen.c \
-    upstream-freebsd/lib/libc/stdio/makebuf.c \
     upstream-freebsd/lib/libc/stdio/mktemp.c \
-    upstream-freebsd/lib/libc/stdio/setvbuf.c \
     upstream-freebsd/lib/libc/stdlib/abs.c \
     upstream-freebsd/lib/libc/stdlib/getopt_long.c \
     upstream-freebsd/lib/libc/stdlib/imaxabs.c \
@@ -279,7 +276,6 @@
     upstream-netbsd/lib/libc/stdlib/div.c \
     upstream-netbsd/lib/libc/stdlib/drand48.c \
     upstream-netbsd/lib/libc/stdlib/erand48.c \
-    upstream-netbsd/lib/libc/stdlib/exit.c \
     upstream-netbsd/lib/libc/stdlib/jrand48.c \
     upstream-netbsd/lib/libc/stdlib/ldiv.c \
     upstream-netbsd/lib/libc/stdlib/lldiv.c \
@@ -381,6 +377,7 @@
     upstream-openbsd/lib/libc/stdio/fgetwc.c \
     upstream-openbsd/lib/libc/stdio/fgetws.c \
     upstream-openbsd/lib/libc/stdio/fileno.c \
+    upstream-openbsd/lib/libc/stdio/findfp.c \
     upstream-openbsd/lib/libc/stdio/fprintf.c \
     upstream-openbsd/lib/libc/stdio/fpurge.c \
     upstream-openbsd/lib/libc/stdio/fputc.c \
@@ -407,6 +404,7 @@
     upstream-openbsd/lib/libc/stdio/gets.c \
     upstream-openbsd/lib/libc/stdio/getwc.c \
     upstream-openbsd/lib/libc/stdio/getwchar.c \
+    upstream-openbsd/lib/libc/stdio/makebuf.c \
     upstream-openbsd/lib/libc/stdio/perror.c \
     upstream-openbsd/lib/libc/stdio/printf.c \
     upstream-openbsd/lib/libc/stdio/putc.c \
@@ -422,6 +420,7 @@
     upstream-openbsd/lib/libc/stdio/scanf.c \
     upstream-openbsd/lib/libc/stdio/setbuf.c \
     upstream-openbsd/lib/libc/stdio/setbuffer.c \
+    upstream-openbsd/lib/libc/stdio/setvbuf.c \
     upstream-openbsd/lib/libc/stdio/sscanf.c \
     upstream-openbsd/lib/libc/stdio/stdio.c \
     upstream-openbsd/lib/libc/stdio/swprintf.c \
@@ -451,6 +450,7 @@
     upstream-openbsd/lib/libc/stdlib/atoi.c \
     upstream-openbsd/lib/libc/stdlib/atol.c \
     upstream-openbsd/lib/libc/stdlib/atoll.c \
+    upstream-openbsd/lib/libc/stdlib/exit.c \
     upstream-openbsd/lib/libc/stdlib/getenv.c \
     upstream-openbsd/lib/libc/stdlib/setenv.c \
     upstream-openbsd/lib/libc/stdlib/strtoimax.c \
diff --git a/libc/bionic/abort.cpp b/libc/bionic/abort.cpp
index 6fcdfda..69ac0e5 100644
--- a/libc/bionic/abort.cpp
+++ b/libc/bionic/abort.cpp
@@ -32,8 +32,6 @@
 #include <unistd.h>
 #include "atexit.h"
 
-__LIBC_HIDDEN__ void (*__cleanup)();
-
 #ifdef __arm__
 extern "C" __LIBC_HIDDEN__ void __libc_android_abort()
 #else
@@ -47,11 +45,6 @@
   sigdelset(&mask, SIGABRT);
   sigprocmask(SIG_SETMASK, &mask, NULL);
 
-  // POSIX requires we flush stdio buffers on abort.
-  if (__cleanup) {
-    (*__cleanup)();
-  }
-
   raise(SIGABRT);
 
   // If SIGABRT ignored, or caught and the handler returns,
diff --git a/libc/bionic/dlmalloc.c b/libc/bionic/dlmalloc.c
index 66a825b..3a615d2 100644
--- a/libc/bionic/dlmalloc.c
+++ b/libc/bionic/dlmalloc.c
@@ -34,10 +34,7 @@
 // Ugly inclusion of C file so that bionic specific #defines configure dlmalloc.
 #include "../upstream-dlmalloc/malloc.c"
 
-extern void (*__cleanup)();
-
 static void __bionic_heap_corruption_error(const char* function) {
-  __cleanup = NULL; // The heap is corrupt. We can forget trying to shut down stdio.
   __libc_fatal("heap corruption detected by %s", function);
 }
 
diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp
index ec3d04b..97e1a03 100644
--- a/libc/bionic/system_properties.cpp
+++ b/libc/bionic/system_properties.cpp
@@ -655,23 +655,6 @@
     return 0;
 }
 
-int __system_property_wait(const prop_info *pi)
-{
-    if (pi == 0) {
-        prop_area *pa = __system_property_area__;
-        const uint32_t n = pa->serial;
-        do {
-            __futex_wait(&pa->serial, n, NULL);
-        } while (n == pa->serial);
-    } else {
-        const uint32_t n = pi->serial;
-        do {
-            __futex_wait((volatile void *)&pi->serial, n, NULL);
-        } while (n == pi->serial);
-    }
-    return 0;
-}
-
 int __system_property_update(prop_info *pi, const char *value, unsigned int len)
 {
     prop_area *pa = __system_property_area__;
diff --git a/libc/include/sys/ucontext.h b/libc/include/sys/ucontext.h
index 3737cbe..f150ac7 100644
--- a/libc/include/sys/ucontext.h
+++ b/libc/include/sys/ucontext.h
@@ -150,7 +150,8 @@
   stack_t uc_stack;
   mcontext_t uc_mcontext;
   sigset_t uc_sigmask;
-  /* TODO: __fpregs_mem? */
+  char __padding[128 - sizeof(sigset_t)];
+  struct _libc_fpstate __fpregs_mem;
 } ucontext_t;
 
 #elif defined(__mips__)
@@ -238,7 +239,31 @@
 typedef long greg_t;
 typedef greg_t gregset_t[NGREG];
 
-typedef struct user_i387_struct* fpregset_t;
+struct _libc_fpxreg {
+  unsigned short significand[4];
+  unsigned short exponent;
+  unsigned short padding[3];
+};
+
+struct _libc_xmmreg {
+  uint32_t element[4];
+};
+
+struct _libc_fpstate {
+  uint16_t cwd;
+  uint16_t swd;
+  uint16_t ftw;
+  uint16_t fop;
+  uint64_t rip;
+  uint64_t rdp;
+  uint32_t mxcsr;
+  uint32_t mxcr_mask;
+  struct _libc_fpxreg _st[8];
+  struct _libc_xmmreg _xmm[16];
+  uint32_t padding[24];
+};
+
+typedef struct _libc_fpstate* fpregset_t;
 
 typedef struct {
   gregset_t gregs;
@@ -252,7 +277,8 @@
   stack_t uc_stack;
   mcontext_t uc_mcontext;
   sigset_t uc_sigmask;
-  /* TODO: __fpregs_mem? */
+  char __padding[128 - sizeof(sigset_t)];
+  struct _libc_fpstate __fpregs_mem;
 } ucontext_t;
 
 #endif
diff --git a/libc/kernel/tools/defaults.py b/libc/kernel/tools/defaults.py
index 8c41cc6..67ab702 100644
--- a/libc/kernel/tools/defaults.py
+++ b/libc/kernel/tools/defaults.py
@@ -42,9 +42,9 @@
 # maps an architecture to a set of default macros that would be provided by
 # toolchain preprocessor
 kernel_default_arch_macros = {
-    "arm": {},
+    "arm": {"__ARMEB__": kCppUndefinedMacro, "__ARM_EABI__": "1"},
     "arm64": {},
-    "mips": {"CONFIG_32BIT":"1"},
+    "mips": {"CONFIG_32BIT":"1", "__MIPSEB__": kCppUndefinedMacro, "__MIPSEL__": "1"},
     "x86": {},
     }
 
diff --git a/libc/kernel/uapi/asm-arm/asm/byteorder.h b/libc/kernel/uapi/asm-arm/asm/byteorder.h
index 0d2bdf3..6f17a48 100644
--- a/libc/kernel/uapi/asm-arm/asm/byteorder.h
+++ b/libc/kernel/uapi/asm-arm/asm/byteorder.h
@@ -18,11 +18,6 @@
  ****************************************************************************/
 #ifndef __ASM_ARM_BYTEORDER_H
 #define __ASM_ARM_BYTEORDER_H
-#ifdef __ARMEB__
-#include <linux/byteorder/big_endian.h>
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#else
 #include <linux/byteorder/little_endian.h>
 #endif
-#endif
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
diff --git a/libc/kernel/uapi/asm-arm/asm/stat.h b/libc/kernel/uapi/asm-arm/asm/stat.h
index d8188bf..b3425dd 100644
--- a/libc/kernel/uapi/asm-arm/asm/stat.h
+++ b/libc/kernel/uapi/asm-arm/asm/stat.h
@@ -37,68 +37,55 @@
 #define STAT_HAVE_NSEC
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct stat {
-#ifdef __ARMEB__
- unsigned short st_dev;
- unsigned short __pad1;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#else
  unsigned long st_dev;
-#endif
  unsigned long st_ino;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned short st_mode;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned short st_nlink;
  unsigned short st_uid;
  unsigned short st_gid;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#ifdef __ARMEB__
- unsigned short st_rdev;
- unsigned short __pad2;
-#else
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_rdev;
-#endif
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_size;
  unsigned long st_blksize;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_blocks;
  unsigned long st_atime;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_atime_nsec;
  unsigned long st_mtime;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_mtime_nsec;
  unsigned long st_ctime;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_ctime_nsec;
  unsigned long __unused4;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long __unused5;
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct stat64 {
  unsigned long long st_dev;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned char __pad0[4];
 #define STAT64_HAS_BROKEN_ST_INO 1
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long __st_ino;
  unsigned int st_mode;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned int st_nlink;
  unsigned long st_uid;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_gid;
  unsigned long long st_rdev;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned char __pad3[4];
  long long st_size;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_blksize;
  unsigned long long st_blocks;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_atime;
  unsigned long st_atime_nsec;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_mtime;
  unsigned long st_mtime_nsec;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long st_ctime;
  unsigned long st_ctime_nsec;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned long long st_ino;
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #endif
diff --git a/libc/kernel/uapi/asm-arm/asm/unistd.h b/libc/kernel/uapi/asm-arm/asm/unistd.h
index f99bfb1..4ce108c 100644
--- a/libc/kernel/uapi/asm-arm/asm/unistd.h
+++ b/libc/kernel/uapi/asm-arm/asm/unistd.h
@@ -19,12 +19,7 @@
 #ifndef _UAPI__ASM_ARM_UNISTD_H
 #define _UAPI__ASM_ARM_UNISTD_H
 #define __NR_OABI_SYSCALL_BASE 0x900000
-#if defined(__thumb__) || defined(__ARM_EABI__)
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define __NR_SYSCALL_BASE 0
-#else
-#define __NR_SYSCALL_BASE __NR_OABI_SYSCALL_BASE
-#endif
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define __NR_restart_syscall (__NR_SYSCALL_BASE+ 0)
 #define __NR_exit (__NR_SYSCALL_BASE+ 1)
@@ -468,22 +463,19 @@
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #define __ARM_NR_usr32 (__ARM_NR_BASE+4)
 #define __ARM_NR_set_tls (__ARM_NR_BASE+5)
-#ifdef __ARM_EABI__
 #undef __NR_time
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #undef __NR_umount
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #undef __NR_stime
 #undef __NR_alarm
 #undef __NR_utime
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #undef __NR_getrlimit
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #undef __NR_select
 #undef __NR_readdir
 #undef __NR_mmap
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #undef __NR_socketcall
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #undef __NR_syscall
 #undef __NR_ipc
 #endif
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#endif
diff --git a/libc/kernel/uapi/asm-mips/asm/byteorder.h b/libc/kernel/uapi/asm-mips/asm/byteorder.h
index 477c93d..965fd8f 100644
--- a/libc/kernel/uapi/asm-mips/asm/byteorder.h
+++ b/libc/kernel/uapi/asm-mips/asm/byteorder.h
@@ -18,13 +18,6 @@
  ****************************************************************************/
 #ifndef _ASM_BYTEORDER_H
 #define _ASM_BYTEORDER_H
-#ifdef __MIPSEB__
-#include <linux/byteorder/big_endian.h>
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#elif defined(__MIPSEL__)
 #include <linux/byteorder/little_endian.h>
-#else
-#error "MIPS, but neither __MIPSEB__, nor __MIPSEL__???"
+#endif
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#endif
-#endif
diff --git a/libc/kernel/uapi/asm-mips/asm/inst.h b/libc/kernel/uapi/asm-mips/asm/inst.h
index 379286c..43e9a50 100644
--- a/libc/kernel/uapi/asm-mips/asm/inst.h
+++ b/libc/kernel/uapi/asm-mips/asm/inst.h
@@ -434,453 +434,446 @@
  MIPS16e_swrasp_func = 02,
 };
 #define MM_NOP16 0x0c00
-#ifdef __MIPSEB__
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#define BITFIELD_FIELD(field, more)   field;   more
-#elif defined(__MIPSEL__)
 #define BITFIELD_FIELD(field, more)   more   field;
-#else
 /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
-#error "MIPS but neither __MIPSEL__ nor __MIPSEB__?"
-#endif
 struct j_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int target : 26,
  ;))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct i_format {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rs : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 5,
  BITFIELD_FIELD(signed int simmediate : 16,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct u_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rs : 5,
  BITFIELD_FIELD(unsigned int rt : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int uimmediate : 16,
  ;))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct c_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int c_op : 3,
  BITFIELD_FIELD(unsigned int cache : 2,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int simmediate : 16,
  ;)))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct r_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 5,
  BITFIELD_FIELD(unsigned int rd : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int re : 5,
  BITFIELD_FIELD(unsigned int func : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct p_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rs : 5,
  BITFIELD_FIELD(unsigned int rt : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rd : 5,
  BITFIELD_FIELD(unsigned int re : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;))))))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct f_format {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int : 1,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 4,
  BITFIELD_FIELD(unsigned int rt : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rd : 5,
  BITFIELD_FIELD(unsigned int re : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;)))))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct ma_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int fr : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int ft : 5,
  BITFIELD_FIELD(unsigned int fs : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fd : 5,
  BITFIELD_FIELD(unsigned int func : 4,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 2,
  ;)))))))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct b_format {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int code : 20,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;)))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct ps_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int ft : 5,
  BITFIELD_FIELD(unsigned int fs : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fd : 5,
  BITFIELD_FIELD(unsigned int func : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct v_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int sel : 4,
  BITFIELD_FIELD(unsigned int fmt : 1,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int vt : 5,
  BITFIELD_FIELD(unsigned int vs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int vd : 5,
  BITFIELD_FIELD(unsigned int func : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;)))))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct fb_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int bc : 5,
  BITFIELD_FIELD(unsigned int cc : 3,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int flag : 2,
  BITFIELD_FIELD(signed int simmediate : 16,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;)))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct fp0_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 5,
  BITFIELD_FIELD(unsigned int ft : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fs : 5,
  BITFIELD_FIELD(unsigned int fd : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;))))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_fp0_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int ft : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fs : 5,
  BITFIELD_FIELD(unsigned int fd : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 3,
  BITFIELD_FIELD(unsigned int op : 2,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;)))))))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct fp1_format {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int op : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 5,
  BITFIELD_FIELD(unsigned int fs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fd : 5,
  BITFIELD_FIELD(unsigned int func : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct mm_fp1_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 5,
  BITFIELD_FIELD(unsigned int fs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 2,
  BITFIELD_FIELD(unsigned int op : 8,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;))))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_fp2_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int fd : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fs : 5,
  BITFIELD_FIELD(unsigned int cc : 3,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int zero : 2,
  BITFIELD_FIELD(unsigned int fmt : 2,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int op : 3,
  BITFIELD_FIELD(unsigned int func : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct mm_fp3_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 5,
  BITFIELD_FIELD(unsigned int fs : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 3,
  BITFIELD_FIELD(unsigned int op : 7,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;))))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_fp4_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rt : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fs : 5,
  BITFIELD_FIELD(unsigned int cc : 3,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fmt : 3,
  BITFIELD_FIELD(unsigned int cond : 4,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;)))))))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_fp5_format {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int index : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int base : 5,
  BITFIELD_FIELD(unsigned int fd : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int op : 5,
  BITFIELD_FIELD(unsigned int func : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct fp6_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fr : 5,
  BITFIELD_FIELD(unsigned int ft : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fs : 5,
  BITFIELD_FIELD(unsigned int fd : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 6,
  ;))))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_fp6_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int ft : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fs : 5,
  BITFIELD_FIELD(unsigned int fd : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int fr : 5,
  BITFIELD_FIELD(unsigned int func : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct mm_i_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 5,
  BITFIELD_FIELD(unsigned int rs : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(signed int simmediate : 16,
  ;))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_m_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rd : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int base : 5,
  BITFIELD_FIELD(unsigned int func : 4,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(signed int simmediate : 12,
  ;)))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_x_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int index : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int base : 5,
  BITFIELD_FIELD(unsigned int rd : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 11,
  ;)))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_b0_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(signed int simmediate : 10,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int : 16,
  ;)))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm_b1_format {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rs : 3,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(signed int simmediate : 7,
  BITFIELD_FIELD(unsigned int : 16,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct mm16_m_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 4,
  BITFIELD_FIELD(unsigned int rlist : 2,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int imm : 4,
  BITFIELD_FIELD(unsigned int : 16,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;)))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct mm16_rb_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 3,
  BITFIELD_FIELD(unsigned int base : 3,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(signed int simmediate : 4,
  BITFIELD_FIELD(unsigned int : 16,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;)))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct mm16_r3_format {
  BITFIELD_FIELD(unsigned int opcode : 6,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rt : 3,
  BITFIELD_FIELD(signed int simmediate : 7,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int : 16,
  ;))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct mm16_r5_format {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 6,
  BITFIELD_FIELD(unsigned int rt : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(signed int simmediate : 5,
  BITFIELD_FIELD(unsigned int : 16,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct m16e_rr {
  BITFIELD_FIELD(unsigned int opcode : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int rx : 3,
  BITFIELD_FIELD(unsigned int nd : 1,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int l : 1,
  BITFIELD_FIELD(unsigned int ra : 1,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 5,
  ;))))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct m16e_jal {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 5,
  BITFIELD_FIELD(unsigned int x : 1,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int imm20_16 : 5,
  BITFIELD_FIELD(signed int imm25_21 : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct m16e_i64 {
  BITFIELD_FIELD(unsigned int opcode : 5,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 3,
  BITFIELD_FIELD(unsigned int imm : 8,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;)))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct m16e_ri64 {
  BITFIELD_FIELD(unsigned int opcode : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 3,
  BITFIELD_FIELD(unsigned int ry : 3,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int imm : 5,
  ;))))
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct m16e_ri {
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 5,
  BITFIELD_FIELD(unsigned int rx : 3,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int imm : 8,
  ;)))
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 };
 struct m16e_rri {
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int opcode : 5,
  BITFIELD_FIELD(unsigned int rx : 3,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int ry : 3,
  BITFIELD_FIELD(unsigned int imm : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;))))
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 struct m16e_i8 {
  BITFIELD_FIELD(unsigned int opcode : 5,
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  BITFIELD_FIELD(unsigned int func : 3,
  BITFIELD_FIELD(unsigned int imm : 8,
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  ;)))
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 union mips_instruction {
  unsigned int word;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  unsigned short halfword[2];
  unsigned char byte[4];
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct j_format j_format;
  struct i_format i_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct u_format u_format;
  struct c_format c_format;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct r_format r_format;
  struct p_format p_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct f_format f_format;
  struct ma_format ma_format;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct b_format b_format;
  struct ps_format ps_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct v_format v_format;
  struct fb_format fb_format;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct fp0_format fp0_format;
  struct mm_fp0_format mm_fp0_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct fp1_format fp1_format;
  struct mm_fp1_format mm_fp1_format;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm_fp2_format mm_fp2_format;
  struct mm_fp3_format mm_fp3_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm_fp4_format mm_fp4_format;
  struct mm_fp5_format mm_fp5_format;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct fp6_format fp6_format;
  struct mm_fp6_format mm_fp6_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm_i_format mm_i_format;
  struct mm_m_format mm_m_format;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm_x_format mm_x_format;
  struct mm_b0_format mm_b0_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm_b1_format mm_b1_format;
  struct mm16_m_format mm16_m_format ;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm16_rb_format mm16_rb_format;
  struct mm16_r3_format mm16_r3_format;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct mm16_r5_format mm16_r5_format;
 };
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 union mips16e_instruction {
  unsigned int full : 16;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct m16e_rr rr;
  struct m16e_jal jal;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct m16e_i64 i64;
  struct m16e_ri64 ri64;
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct m16e_ri ri;
  struct m16e_rri rri;
-/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  struct m16e_i8 i8;
 };
+/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
 #endif
diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
index b051e22..c607206 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -104,10 +104,10 @@
 {
 	struct atexit *p = __atexit;
 	struct atexit_fn *fnp;
-	int pgsize = getpagesize();
+	size_t pgsize = sysconf(_SC_PAGESIZE);
 	int ret = -1;
 
-	if (pgsize < (int)sizeof(*p))
+	if (pgsize < sizeof(*p))
 		return (-1);
 	_ATEXIT_LOCK();
 	p = __atexit;
@@ -216,3 +216,41 @@
 	}
 	_ATEXIT_UNLOCK();
 }
+
+/*
+ * Register the cleanup function
+ */
+void
+__atexit_register_cleanup(void (*func)(void))
+{
+        struct atexit *p;
+        size_t pgsize = sysconf(_SC_PAGESIZE);
+
+        if (pgsize < sizeof(*p))
+                return;
+        _ATEXIT_LOCK();
+        p = __atexit;
+        while (p != NULL && p->next != NULL)
+                p = p->next;
+        if (p == NULL) {
+                p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE,
+                    MAP_ANON | MAP_PRIVATE, -1, 0);
+                if (p == MAP_FAILED)
+                        goto unlock;
+                p->ind = 1;
+                p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) /
+                    sizeof(p->fns[0]);
+                p->next = NULL;
+                __atexit = p;
+        } else {
+                if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
+                        goto unlock;
+        }
+        p->fns[0].cxa_func = (void (*)(void*))func;
+        p->fns[0].fn_arg = NULL;
+        p->fns[0].fn_dso = NULL;
+        mprotect(p, pgsize, PROT_READ);
+unlock:
+        _ATEXIT_UNLOCK();
+}
+
diff --git a/libc/stdio/findfp.c b/libc/upstream-openbsd/lib/libc/stdio/findfp.c
similarity index 84%
rename from libc/stdio/findfp.c
rename to libc/upstream-openbsd/lib/libc/stdio/findfp.c
index 2dd32c9..b8c7dc1 100644
--- a/libc/stdio/findfp.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/findfp.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: findfp.c,v 1.9 2005/08/08 08:05:36 espie Exp $ */
+/*	$OpenBSD: findfp.c,v 1.15 2013/12/17 16:33:27 deraadt Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -54,7 +54,7 @@
 /*	 p r w flags file _bf z  cookie      close    read    seek    write
 	 ext */
 
-/* the usual - (stdin + stdout + stderr) */
+				/* the usual - (stdin + stdout + stderr) */
 static FILE usual[FOPEN_MAX - 3];
 static struct __sfileext usualext[FOPEN_MAX - 3];
 static struct glue uglue = { 0, FOPEN_MAX - 3, usual };
@@ -62,7 +62,6 @@
 _THREAD_PRIVATE_MUTEX(__sfp_mutex);
 
 static struct __sfileext __sFext[3];
-
 FILE __sF[3] = {
 	std(__SRD, STDIN_FILENO),		/* stdin */
 	std(__SWR, STDOUT_FILENO),		/* stdout */
@@ -144,35 +143,11 @@
 	return (fp);
 }
 
-#if 0
-#define getdtablesize()	sysconf(_SC_OPEN_MAX)
-
 /*
- * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
- * but documented historically for certain applications.  Bad applications.
- */
-void
-f_prealloc(void)
-{
-	struct glue *g;
-	int n;
-
-	n = getdtablesize() - FOPEN_MAX + 20;		/* 20 for slop. */
-	for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
-		/* void */;
-	if (n > 0 && ((g = moreglue(n)) != NULL)) {
-		_THREAD_PRIVATE_MUTEX_LOCK(__sfp_mutex);
-		lastglue->next = g;
-		lastglue = g;
-		_THREAD_PRIVATE_MUTEX_UNLOCK(__sfp_mutex);
-	}
-}
-#endif
-
-/*
- * exit() calls _cleanup() through *__cleanup, set whenever we
- * open or buffer a file.  This chicanery is done so that programs
- * that do not use stdio need not link it all in.
+ * exit() and abort() call _cleanup() through the callback registered
+ * with __atexit_register_cleanup(), set whenever we open or buffer a
+ * file. This chicanery is done so that programs that do not use stdio
+ * need not link it all in.
  *
  * The name `_cleanup' is, alas, fairly well known outside stdio.
  */
@@ -199,7 +174,7 @@
 		_FILEEXT_SETUP(usual+i, usualext+i);
 	}
 	/* make sure we clean up on exit */
-	__cleanup = _cleanup; /* conservative */
+	__atexit_register_cleanup(_cleanup); /* conservative */
 	__sdidinit = 1;
 out:
 	_THREAD_PRIVATE_MUTEX_UNLOCK(__sinit_mutex);
diff --git a/libc/upstream-freebsd/lib/libc/stdio/makebuf.c b/libc/upstream-openbsd/lib/libc/stdio/makebuf.c
similarity index 86%
rename from libc/upstream-freebsd/lib/libc/stdio/makebuf.c
rename to libc/upstream-openbsd/lib/libc/stdio/makebuf.c
index a92087e..d47e27c 100644
--- a/libc/upstream-freebsd/lib/libc/stdio/makebuf.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/makebuf.c
@@ -1,3 +1,4 @@
+/*	$OpenBSD: makebuf.c,v 1.8 2005/12/28 18:50:22 millert Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -30,21 +31,11 @@
  * SUCH DAMAGE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)makebuf.c	8.1 (Berkeley) 6/4/93";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "namespace.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "un-namespace.h"
-
-#include "libc_private.h"
 #include "local.h"
 
 /*
@@ -52,7 +43,7 @@
  * Per the ANSI C standard, ALL tty devices default to line buffered.
  *
  * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek
- * optimisation) right after the _fstat() that finds the buffer size.
+ * optimisation) right after the fstat() that finds the buffer size.
  */
 void
 __smakebuf(FILE *fp)
@@ -74,7 +65,7 @@
 		fp->_bf._size = 1;
 		return;
 	}
-	__cleanup = _cleanup;
+	__atexit_register_cleanup(_cleanup);
 	flags |= __SMBF;
 	fp->_bf._base = fp->_p = p;
 	fp->_bf._size = size;
@@ -91,15 +82,15 @@
 {
 	struct stat st;
 
-	if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) {
+	if (fp->_file < 0 || fstat(fp->_file, &st) < 0) {
 		*couldbetty = 0;
 		*bufsize = BUFSIZ;
 		return (__SNPT);
 	}
 
 	/* could be a tty iff it is a character device */
-	*couldbetty = (st.st_mode & S_IFMT) == S_IFCHR;
-	if (st.st_blksize <= 0) {
+	*couldbetty = S_ISCHR(st.st_mode);
+	if (st.st_blksize == 0) {
 		*bufsize = BUFSIZ;
 		return (__SNPT);
 	}
diff --git a/libc/upstream-freebsd/lib/libc/stdio/setvbuf.c b/libc/upstream-openbsd/lib/libc/stdio/setvbuf.c
similarity index 90%
rename from libc/upstream-freebsd/lib/libc/stdio/setvbuf.c
rename to libc/upstream-openbsd/lib/libc/stdio/setvbuf.c
index d396960..6c49f7a 100644
--- a/libc/upstream-freebsd/lib/libc/stdio/setvbuf.c
+++ b/libc/upstream-openbsd/lib/libc/stdio/setvbuf.c
@@ -1,3 +1,4 @@
+/*	$OpenBSD: setvbuf.c,v 1.11 2009/11/09 00:18:27 kurt Exp $ */
 /*-
  * Copyright (c) 1990, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -30,25 +31,16 @@
  * SUCH DAMAGE.
  */
 
-#if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)setvbuf.c	8.2 (Berkeley) 11/16/93";
-#endif /* LIBC_SCCS and not lint */
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "namespace.h"
 #include <stdio.h>
 #include <stdlib.h>
-#include "un-namespace.h"
 #include "local.h"
-#include "libc_private.h"
 
 /*
  * Set one of the three kinds of buffering, optionally including
  * a buffer.
  */
 int
-setvbuf(FILE * __restrict fp, char * __restrict buf, int mode, size_t size)
+setvbuf(FILE *fp, char *buf, int mode, size_t size)
 {
 	int ret, flags;
 	size_t iosize;
@@ -63,22 +55,23 @@
 		if ((mode != _IOFBF && mode != _IOLBF) || (int)size < 0)
 			return (EOF);
 
-	FLOCKFILE(fp);
 	/*
 	 * Write current buffer, if any.  Discard unread input (including
 	 * ungetc data), cancel line buffering, and free old buffer if
 	 * malloc()ed.  We also clear any eof condition, as if this were
 	 * a seek.
 	 */
+	FLOCKFILE(fp);
 	ret = 0;
 	(void)__sflush(fp);
 	if (HASUB(fp))
 		FREEUB(fp);
+	WCIO_FREE(fp);
 	fp->_r = fp->_lbfsize = 0;
 	flags = fp->_flags;
 	if (flags & __SMBF)
 		free((void *)fp->_bf._base);
-	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SOFF | __SNPT | __SEOF);
+	flags &= ~(__SLBF | __SNBF | __SMBF | __SOPT | __SNPT | __SEOF);
 
 	/* If setting unbuffered mode, skip all the hard work. */
 	if (mode == _IONBF)
@@ -154,8 +147,8 @@
 		/* begin/continue reading, or stay in intermediate state */
 		fp->_w = 0;
 	}
-	__cleanup = _cleanup;
-
 	FUNLOCKFILE(fp);
+	__atexit_register_cleanup(_cleanup);
+
 	return (ret);
 }
diff --git a/libc/upstream-netbsd/lib/libc/stdlib/exit.c b/libc/upstream-openbsd/lib/libc/stdlib/exit.c
similarity index 76%
rename from libc/upstream-netbsd/lib/libc/stdlib/exit.c
rename to libc/upstream-openbsd/lib/libc/stdlib/exit.c
index 67e6adf..ef8b335 100644
--- a/libc/upstream-netbsd/lib/libc/stdlib/exit.c
+++ b/libc/upstream-openbsd/lib/libc/stdlib/exit.c
@@ -1,8 +1,7 @@
-/*	$NetBSD: exit.c,v 1.15 2011/05/18 19:36:36 dsl Exp $	*/
-
+/*	$OpenBSD: exit.c,v 1.12 2007/09/03 14:40:16 millert Exp $ */
 /*-
- * Copyright (c) 1990, 1993
- *	The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,23 +28,11 @@
  * SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)exit.c	8.1 (Berkeley) 6/4/93";
-#else
-__RCSID("$NetBSD: exit.c,v 1.15 2011/05/18 19:36:36 dsl Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
-
+#include <sys/types.h>
+#include <sys/mman.h>
 #include <stdlib.h>
 #include <unistd.h>
-#ifdef _LIBC
-#include "reentrant.h"
 #include "atexit.h"
-#endif
-
-void (*__cleanup)(void);
 
 /*
  * Exit, flushing stdio buffers if necessary.
@@ -53,11 +40,10 @@
 void
 exit(int status)
 {
-
-#ifdef _LIBC
+	/*
+	 * Call functions registered by atexit() or _cxa_atexit()
+	 * (including the stdio cleanup routine) and then _exit().
+	 */
 	__cxa_finalize(NULL);
-#endif
-	if (__cleanup)
-		(*__cleanup)();
 	_exit(status);
 }
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index a3cad11..85e91c3 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -225,78 +225,26 @@
 static unsigned g_libdl_chains[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
 #endif
 
+// Defined as global because we do not yet have access
+// to synchronization functions __cxa_guard_* needed
+// to define statics inside functions.
+static soinfo __libdl_info;
+
 // This is used by the dynamic linker. Every process gets these symbols for free.
-soinfo libdl_info = {
-    "libdl.so",
+soinfo* get_libdl_info() {
+  if (__libdl_info.name[0] == '\0') {
+    // initialize
+    strncpy(__libdl_info.name, "libdl.so", sizeof(__libdl_info.name));
+    __libdl_info.flags = FLAG_LINKED | FLAG_NEW_SOINFO;
+    __libdl_info.strtab = ANDROID_LIBDL_STRTAB;
+    __libdl_info.symtab = g_libdl_symtab;
+    __libdl_info.nbucket = sizeof(g_libdl_buckets)/sizeof(unsigned);
+    __libdl_info.nchain = sizeof(g_libdl_chains)/sizeof(unsigned);
+    __libdl_info.bucket = g_libdl_buckets;
+    __libdl_info.chain = g_libdl_chains;
+    __libdl_info.has_DT_SYMBOLIC = true;
+  }
 
-    .phdr = 0,
-    .phnum = 0,
-    .entry = 0,
-    .base = 0,
-    .size = 0,
+  return &__libdl_info;
+}
 
-#if !defined(__LP64__)
-    .unused1 = 0,
-#endif
-
-    .dynamic = 0,
-
-#if !defined(__LP64__)
-    .unused2 = 0, .unused3 = 0,
-#endif
-
-    .next = 0,
-
-    .flags = FLAG_LINKED,
-
-    .strtab = ANDROID_LIBDL_STRTAB,
-    .symtab = g_libdl_symtab,
-
-    .nbucket = sizeof(g_libdl_buckets)/sizeof(unsigned),
-    .nchain = sizeof(g_libdl_chains)/sizeof(unsigned),
-    .bucket = g_libdl_buckets,
-    .chain = g_libdl_chains,
-
-#if defined(USE_RELA)
-    .plt_rela = 0,
-    .plt_rela_count = 0,
-    .rela = 0,
-    .rela_count = 0,
-#else
-    .plt_got = 0,
-    .plt_rel = 0,
-    .plt_rel_count = 0,
-    .rel = 0,
-    .rel_count = 0,
-#endif
-
-    .preinit_array = 0,
-    .preinit_array_count = 0,
-
-    .init_array = 0,
-    .init_array_count = 0,
-
-    .fini_array = 0,
-    .fini_array_count = 0,
-
-    .init_func = 0,
-    .fini_func = 0,
-
-#if defined(__arm__)
-    .ARM_exidx = 0,
-    .ARM_exidx_count = 0,
-#elif defined(__mips__)
-    .mips_symtabno = 0,
-    .mips_local_gotno = 0,
-    .mips_gotsym = 0,
-#endif
-
-    .ref_count = 0,
-    { .l_addr = 0, .l_name = 0, .l_ld = 0, .l_next = 0, .l_prev = 0, },
-    .constructors_called = false,
-    .load_bias = 0,
-#if !defined(__LP64__)
-    .has_text_relocations = false,
-#endif
-    .has_DT_SYMBOLIC = true,
-};
diff --git a/linker/linked_list.h b/linker/linked_list.h
new file mode 100644
index 0000000..a6bea6c
--- /dev/null
+++ b/linker/linked_list.h
@@ -0,0 +1,75 @@
+/*
+ * 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.
+ */
+
+#ifndef __LINKED_LIST_H
+#define __LINKED_LIST_H
+
+#include "private/bionic_macros.h"
+
+template<typename T>
+struct LinkedListEntry {
+  LinkedListEntry<T>* next;
+  T* element;
+};
+
+/*
+ * Represents linked list of objects of type T
+ */
+template<typename T, typename Allocator>
+class LinkedList {
+ public:
+  LinkedList() : head_(nullptr) {}
+
+  void push_front(T* const element) {
+    LinkedListEntry<T>* new_entry = Allocator::alloc();
+    new_entry->next = head_;
+    new_entry->element = element;
+    head_ = new_entry;
+  }
+
+  void clear() {
+    while (head_ != nullptr) {
+      LinkedListEntry<T>* p = head_;
+      head_ = head_->next;
+      Allocator::free(p);
+    }
+  }
+
+  template<typename F>
+  void for_each(F&& action) {
+    for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
+      if (e->element != nullptr) {
+        action(e->element);
+      }
+    }
+  }
+
+  template<typename F>
+  void remove_if(F&& predicate) {
+    LinkedListEntry<T>* e = head_;
+    for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
+      if (e->element != nullptr && predicate(e->element)) {
+        e->element = nullptr;
+      }
+    }
+  }
+
+ private:
+  LinkedListEntry<T>* head_;
+  DISALLOW_COPY_AND_ASSIGN(LinkedList);
+};
+
+#endif // __LINKED_LIST_H
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 86204de..c5006e0 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -68,13 +68,11 @@
 static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo);
 static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
 
-// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
-// maps, each a single page in size. The pages are broken up into as many struct soinfo
-// objects as will fit.
 static LinkerAllocator<soinfo> g_soinfo_allocator;
+static LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
 
-static soinfo* solist = &libdl_info;
-static soinfo* sonext = &libdl_info;
+static soinfo* solist;
+static soinfo* sonext;
 static soinfo* somain; /* main process, always the one after libdl_info */
 
 static const char* const kDefaultLdPaths[] = {
@@ -263,7 +261,20 @@
   rtld_db_dlactivity();
 }
 
-static soinfo* soinfo_alloc(const char* name) {
+LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
+  return g_soinfo_links_allocator.alloc();
+}
+
+void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
+  g_soinfo_links_allocator.free(entry);
+}
+
+static void protect_data(int protection) {
+  g_soinfo_allocator.protect_all(protection);
+  g_soinfo_links_allocator.protect_all(protection);
+}
+
+static soinfo* soinfo_alloc(const char* name, struct stat* file_stat) {
   if (strlen(name) >= SOINFO_NAME_LEN) {
     DL_ERR("library name \"%s\" too long", name);
     return NULL;
@@ -274,6 +285,13 @@
   // Initialize the new element.
   memset(si, 0, sizeof(soinfo));
   strlcpy(si->name, name, sizeof(si->name));
+  si->flags = FLAG_NEW_SOINFO;
+
+  if (file_stat != NULL) {
+    si->set_st_dev(file_stat->st_dev);
+    si->set_st_ino(file_stat->st_ino);
+  }
+
   sonext->next = si;
   sonext = si;
 
@@ -286,6 +304,10 @@
         return;
     }
 
+    if (si->base != 0 && si->size != 0) {
+      munmap(reinterpret_cast<void*>(si->base), si->size);
+    }
+
     soinfo *prev = NULL, *trav;
 
     TRACE("name %s: freeing soinfo @ %p", si->name, si);
@@ -301,6 +323,9 @@
         return;
     }
 
+    // clear links to/from si
+    si->remove_all_links();
+
     /* prev will never be NULL, because the first entry in solist is
        always the static libdl_info.
     */
@@ -651,25 +676,52 @@
         return NULL;
     }
 
-    // Read the ELF header and load the segments.
     ElfReader elf_reader(name, fd);
+
+    struct stat file_stat;
+    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
+      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
+      return NULL;
+    }
+
+    // Check for symlink and other situations where
+    // file can have different names.
+    for (soinfo* si = solist; si != NULL; si = si->next) {
+      if (si->get_st_dev() != 0 &&
+          si->get_st_ino() != 0 &&
+          si->get_st_dev() == file_stat.st_dev &&
+          si->get_st_ino() == file_stat.st_ino) {
+        TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
+        return si;
+      }
+    }
+
+    // Read the ELF header and load the segments.
     if (!elf_reader.Load(extinfo)) {
         return NULL;
     }
 
     const char* bname = strrchr(name, '/');
-    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
+    soinfo* si = soinfo_alloc(bname ? bname + 1 : name, &file_stat);
     if (si == NULL) {
         return NULL;
     }
     si->base = elf_reader.load_start();
     si->size = elf_reader.load_size();
     si->load_bias = elf_reader.load_bias();
-    si->flags = 0;
-    si->entry = 0;
-    si->dynamic = NULL;
     si->phnum = elf_reader.phdr_count();
     si->phdr = elf_reader.loaded_phdr();
+
+    // At this point we know that whatever is loaded @ base is a valid ELF
+    // shared library whose segments are properly mapped in.
+    TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
+          reinterpret_cast<void*>(si->base), si->size, si->name);
+
+    if (!soinfo_link_image(si, extinfo)) {
+      soinfo_free(si);
+      return NULL;
+    }
+
     return si;
 }
 
@@ -703,23 +755,7 @@
   }
 
   TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
-  si = load_library(name, extinfo);
-  if (si == NULL) {
-    return NULL;
-  }
-
-  // At this point we know that whatever is loaded @ base is a valid ELF
-  // shared library whose segments are properly mapped in.
-  TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
-        reinterpret_cast<void*>(si->base), si->size, si->name);
-
-  if (!soinfo_link_image(si, extinfo)) {
-    munmap(reinterpret_cast<void*>(si->base), si->size);
-    soinfo_free(si);
-    return NULL;
-  }
-
-  return si;
+  return load_library(name, extinfo);
 }
 
 static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
@@ -735,15 +771,21 @@
     TRACE("unloading '%s'", si->name);
     si->CallDestructors();
 
-    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
-      if (d->d_tag == DT_NEEDED) {
-        const char* library_name = si->strtab + d->d_un.d_val;
-        TRACE("%s needs to unload %s", si->name, library_name);
-        soinfo_unload(find_loaded_library(library_name));
+    if ((si->flags | FLAG_NEW_SOINFO) != 0) {
+      si->get_children().for_each([&] (soinfo* child) {
+        TRACE("%s needs to unload %s", si->name, child->name);
+        soinfo_unload(child);
+      });
+    } else {
+      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
+        if (d->d_tag == DT_NEEDED) {
+          const char* library_name = si->strtab + d->d_un.d_val;
+          TRACE("%s needs to unload %s", si->name, library_name);
+          soinfo_unload(find_loaded_library(library_name));
+        }
       }
     }
 
-    munmap(reinterpret_cast<void*>(si->base), si->size);
     notify_gdb_of_unload(si);
     si->ref_count = 0;
     soinfo_free(si);
@@ -773,19 +815,20 @@
     DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
     return NULL;
   }
-  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
   soinfo* si = find_library(name, extinfo);
   if (si != NULL) {
     si->CallConstructors();
+    somain->add_child(si);
   }
-  g_soinfo_allocator.protect_all(PROT_READ);
+  protect_data(PROT_READ);
   return si;
 }
 
 int do_dlclose(soinfo* si) {
-  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
   int result = soinfo_unload(si);
-  g_soinfo_allocator.protect_all(PROT_READ);
+  protect_data(PROT_READ);
   return result;
 }
 
@@ -1333,7 +1376,7 @@
 
   // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
   // are still writable. This happens with our debug malloc (see http://b/7941716).
-  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
+  protect_data(PROT_READ | PROT_WRITE);
 }
 
 void soinfo::CallPreInitConstructors() {
@@ -1365,15 +1408,9 @@
           name, preinit_array_count);
   }
 
-  if (dynamic != NULL) {
-    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
-      if (d->d_tag == DT_NEEDED) {
-        const char* library_name = strtab + d->d_un.d_val;
-        TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
-        find_loaded_library(library_name)->CallConstructors();
-      }
-    }
-  }
+  get_children().for_each([] (soinfo* si) {
+    si->CallConstructors();
+  });
 
   TRACE("\"%s\": calling constructors", name);
 
@@ -1392,6 +1429,82 @@
   CallFunction("DT_FINI", fini_func);
 }
 
+void soinfo::add_child(soinfo* child) {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  this->children.push_front(child);
+  child->parents.push_front(this);
+}
+
+void soinfo::remove_all_links() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  // 1. Untie connected soinfos from 'this'.
+  children.for_each([&] (soinfo* child) {
+    child->parents.remove_if([&] (const soinfo* parent) {
+      return parent == this;
+    });
+  });
+
+  parents.for_each([&] (soinfo* parent) {
+    parent->children.for_each([&] (const soinfo* child) {
+      return child == this;
+    });
+  });
+
+  // 2. Once everything untied - clear local lists.
+  parents.clear();
+  children.clear();
+}
+
+void soinfo::set_st_dev(dev_t dev) {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  st_dev = dev;
+}
+
+void soinfo::set_st_ino(ino_t ino) {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return;
+  }
+
+  st_ino = ino;
+}
+
+dev_t soinfo::get_st_dev() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return 0;
+  }
+
+  return st_dev;
+};
+
+ino_t soinfo::get_st_ino() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return 0;
+  }
+
+  return st_ino;
+}
+
+// This is a return on get_children() in case
+// 'this->flags' does not have FLAG_NEW_SOINFO set.
+static soinfo::soinfo_list_t g_empty_list;
+
+soinfo::soinfo_list_t& soinfo::get_children() {
+  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
+    return g_empty_list;
+  }
+
+  return this->children;
+}
+
 /* Force any of the closed stdin, stdout and stderr to be associated with
    /dev/null. */
 static int nullify_closed_stdio() {
@@ -1715,6 +1828,8 @@
                        library_name, si->name, tmp_err_buf);
                 return false;
             }
+
+            si->add_child(lsi);
             *pneeded++ = lsi;
         }
     }
@@ -1824,13 +1939,12 @@
     return;
   }
 
-  soinfo* si = soinfo_alloc("[vdso]");
+  soinfo* si = soinfo_alloc("[vdso]", NULL);
 
   si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
   si->phnum = ehdr_vdso->e_phnum;
   si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
   si->size = phdr_table_get_load_size(si->phdr, si->phnum);
-  si->flags = 0;
   si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
 
   soinfo_link_image(si, NULL);
@@ -1838,6 +1952,40 @@
 }
 
 /*
+ * This is linker soinfo for GDB. See details below.
+ */
+static soinfo linker_soinfo_for_gdb;
+
+/* gdb expects the linker to be in the debug shared object list.
+ * Without this, gdb has trouble locating the linker's ".text"
+ * and ".plt" sections. Gdb could also potentially use this to
+ * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
+ * Don't use soinfo_alloc(), because the linker shouldn't
+ * be on the soinfo list.
+ */
+static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
+#if defined(__LP64__)
+  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker64", sizeof(linker_soinfo_for_gdb.name));
+#else
+  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker", sizeof(linker_soinfo_for_gdb.name));
+#endif
+  linker_soinfo_for_gdb.flags = FLAG_NEW_SOINFO;
+  linker_soinfo_for_gdb.base = linker_base;
+
+  /*
+   * Set the dynamic field in the link map otherwise gdb will complain with
+   * the following:
+   *   warning: .dynamic section for "/system/bin/linker" is not at the
+   *   expected address (wrong library or version mismatch?)
+   */
+  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
+  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
+  phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
+                                 &linker_soinfo_for_gdb.dynamic, NULL, NULL);
+  insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
+}
+
+/*
  * This code is called after the linker has linked itself and
  * fixed it's own GOT. It is safe to make references to externs
  * and other non-local data at this point.
@@ -1886,12 +2034,13 @@
 
     // Linker does not call constructors for its own
     // global variables so we need to initialize
-    // the allocator explicitly.
+    // the allocators explicitly.
     g_soinfo_allocator.init();
+    g_soinfo_links_allocator.init();
 
     INFO("[ android linker & debugger ]");
 
-    soinfo* si = soinfo_alloc(args.argv[0]);
+    soinfo* si = soinfo_alloc(args.argv[0], NULL);
     if (si == NULL) {
         exit(EXIT_FAILURE);
     }
@@ -1908,35 +2057,7 @@
     _r_debug.r_map = map;
     r_debug_tail = map;
 
-    /* gdb expects the linker to be in the debug shared object list.
-     * Without this, gdb has trouble locating the linker's ".text"
-     * and ".plt" sections. Gdb could also potentially use this to
-     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
-     * Don't use soinfo_alloc(), because the linker shouldn't
-     * be on the soinfo list.
-     */
-    {
-        static soinfo linker_soinfo;
-#if defined(__LP64__)
-        strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
-#else
-        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
-#endif
-        linker_soinfo.flags = 0;
-        linker_soinfo.base = linker_base;
-
-        /*
-         * Set the dynamic field in the link map otherwise gdb will complain with
-         * the following:
-         *   warning: .dynamic section for "/system/bin/linker" is not at the
-         *   expected address (wrong library or version mismatch?)
-         */
-        ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
-        ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
-        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
-                                       &linker_soinfo.dynamic, NULL, NULL);
-        insert_soinfo_into_debug_map(&linker_soinfo);
-    }
+    init_linker_info_for_gdb(linker_base);
 
     // Extract information passed from the kernel.
     si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
@@ -2071,6 +2192,10 @@
  * function, or other GOT reference will generate a segfault.
  */
 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
+  // Initialize static variables.
+  solist = get_libdl_info();
+  sonext = get_libdl_info();
+
   KernelArgumentBlock args(raw_args);
 
   ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
@@ -2106,7 +2231,7 @@
   args.abort_message_ptr = &g_abort_message;
   ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
 
-  g_soinfo_allocator.protect_all(PROT_READ);
+  protect_data(PROT_READ);
 
   // Return the address that the calling assembly stub should jump to.
   return start_address;
diff --git a/linker/linker.h b/linker/linker.h
index 645498a..e5aca6e 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -33,8 +33,10 @@
 #include <link.h>
 #include <unistd.h>
 #include <android/dlext.h>
+#include <sys/stat.h>
 
 #include "private/libc_logging.h"
+#include "linked_list.h"
 
 #define DL_ERR(fmt, x...) \
     do { \
@@ -84,6 +86,7 @@
 #define FLAG_LINKED     0x00000001
 #define FLAG_EXE        0x00000004 // The main executable
 #define FLAG_LINKER     0x00000010 // The linker itself
+#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
 
 #define SOINFO_NAME_LEN 128
 
@@ -94,8 +97,21 @@
 #define USE_RELA 1
 #endif
 
+struct soinfo;
+
+class SoinfoListAllocator {
+public:
+  static LinkedListEntry<soinfo>* alloc();
+  static void free(LinkedListEntry<soinfo>* entry);
+private:
+  // unconstructable
+  DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
+};
+
 struct soinfo {
  public:
+  typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
+ public:
   char name[SOINFO_NAME_LEN];
   const ElfW(Phdr)* phdr;
   size_t phnum;
@@ -179,17 +195,39 @@
   bool has_text_relocations;
 #endif
   bool has_DT_SYMBOLIC;
-
   void CallConstructors();
   void CallDestructors();
   void CallPreInitConstructors();
 
+  void add_child(soinfo* child);
+  void remove_all_links();
+
+  void set_st_dev(dev_t st_dev);
+  void set_st_ino(ino_t st_ino);
+  ino_t get_st_ino();
+  dev_t get_st_dev();
+
+  soinfo_list_t& get_children();
+
  private:
   void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
   void CallFunction(const char* function_name, linker_function_t function);
+
+ private:
+  // This part of the structure is only available
+  // when FLAG_NEW_SOINFO is set in this->flags.
+  unsigned int version;
+
+  dev_t st_dev;
+  ino_t st_ino;
+
+  // dependency graph
+  soinfo_list_t children;
+  soinfo_list_t parents;
+
 };
 
-extern soinfo libdl_info;
+extern soinfo* get_libdl_info();
 
 void do_android_get_LD_LIBRARY_PATH(char*, size_t);
 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
diff --git a/linker/linker_allocator.cpp b/linker/linker_allocator.cpp
index 5738090..c8b97b1 100644
--- a/linker/linker_allocator.cpp
+++ b/linker/linker_allocator.cpp
@@ -55,8 +55,7 @@
     free_block_list_ = block_info->next_block;
   }
 
-  block_info->next_block = nullptr;
-  block_info->num_free_blocks = 0;
+  memset(block_info, 0, block_size_);
 
   return block_info;
 }
@@ -78,6 +77,8 @@
     abort();
   }
 
+  memset(block, 0, block_size_);
+
   FreeBlockInfo* block_info = reinterpret_cast<FreeBlockInfo*>(block);
 
   block_info->next_block = free_block_list_;
@@ -100,6 +101,7 @@
   if (page == MAP_FAILED) {
     abort(); // oom
   }
+  memset(page, 0, PAGE_SIZE);
 
   FreeBlockInfo* first_block = reinterpret_cast<FreeBlockInfo*>(page->bytes);
   first_block->next_block = free_block_list_;
diff --git a/linker/linker_allocator.h b/linker/linker_allocator.h
index 3af99da..fbf58fe 100644
--- a/linker/linker_allocator.h
+++ b/linker/linker_allocator.h
@@ -51,6 +51,8 @@
 };
 
 /*
+ * We can't use malloc(3) in the dynamic linker.
+ *
  * A simple allocator for the dynamic linker. An allocator allocates instances
  * of a single fixed-size type. Allocations are backed by page-sized private
  * anonymous mmaps.
diff --git a/linker/tests/Android.mk b/linker/tests/Android.mk
index 600fe69..831cfcb 100644
--- a/linker/tests/Android.mk
+++ b/linker/tests/Android.mk
@@ -30,6 +30,7 @@
 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../libc/
 
 LOCAL_SRC_FILES := \
+  linked_list_test.cpp \
   linker_allocator_test.cpp \
   ../linker_allocator.cpp
 
diff --git a/linker/tests/linked_list_test.cpp b/linker/tests/linked_list_test.cpp
new file mode 100644
index 0000000..31ec7d5
--- /dev/null
+++ b/linker/tests/linked_list_test.cpp
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 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 <stdlib.h>
+#include <string>
+#include <sstream>
+
+#include <gtest/gtest.h>
+
+#include "../linked_list.h"
+
+namespace {
+
+bool alloc_called = false;
+bool free_called = false;
+
+class LinkedListTestAllocator {
+ public:
+  typedef LinkedListEntry<const char> entry_t;
+
+  static entry_t* alloc() {
+    alloc_called = true;
+    return reinterpret_cast<entry_t*>(::malloc(sizeof(entry_t)));
+  }
+
+  static void free(entry_t* p) {
+    free_called = true;
+    ::free(p);
+  }
+ private:
+  DISALLOW_IMPLICIT_CONSTRUCTORS(LinkedListTestAllocator);
+};
+
+typedef LinkedList<const char, LinkedListTestAllocator> test_list_t;
+
+std::string test_list_to_string(test_list_t& list) {
+  std::stringstream ss;
+  list.for_each([&] (const char* c) {
+    ss << c;
+  });
+
+  return ss.str();
+}
+
+};
+
+TEST(linked_list, simple) {
+  alloc_called = free_called = false;
+  test_list_t list;
+  ASSERT_EQ("", test_list_to_string(list));
+  ASSERT_TRUE(!alloc_called);
+  ASSERT_TRUE(!free_called);
+  list.push_front("a");
+  ASSERT_TRUE(alloc_called);
+  ASSERT_TRUE(!free_called);
+  ASSERT_EQ("a", test_list_to_string(list));
+  list.push_front("b");
+  ASSERT_EQ("ba", test_list_to_string(list));
+  list.push_front("c");
+  list.push_front("d");
+  ASSERT_EQ("dcba", test_list_to_string(list));
+  ASSERT_TRUE(alloc_called);
+  ASSERT_TRUE(!free_called);
+  alloc_called = free_called = false;
+  list.remove_if([] (const char* c) {
+    return *c == 'c';
+  });
+
+  ASSERT_TRUE(!alloc_called);
+  ASSERT_TRUE(!free_called);
+
+  ASSERT_EQ("dba", test_list_to_string(list));
+  alloc_called = free_called = false;
+  list.remove_if([] (const char* c) {
+    return *c == '2';
+  });
+  ASSERT_TRUE(!alloc_called);
+  ASSERT_TRUE(!free_called);
+  ASSERT_EQ("dba", test_list_to_string(list));
+  list.clear();
+  ASSERT_TRUE(!alloc_called);
+  ASSERT_TRUE(free_called);
+  ASSERT_EQ("", test_list_to_string(list));
+}
diff --git a/tests/Android.mk b/tests/Android.mk
index 4ad07ba..1e4900f 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -227,6 +227,19 @@
 ALL_MODULES := \
   $(ALL_MODULES) $(libdlext_sym)
 
+ifneq ($(TARGET_2ND_ARCH),)
+# link 64 bit .so
+libdlext_origin := $(TARGET_OUT)/lib64/libdlext_test.so
+libdlext_sym := $(subst libdlext_test,libdlext_test_v2,$(libdlext_origin))
+$(libdlext_sym): $(libdlext_origin)
+	@echo "Symlink: $@ -> $(notdir $<)"
+	@mkdir -p $(dir $@)
+	$(hide) ln -sf $(notdir $<) $@
+
+ALL_MODULES := \
+  $(ALL_MODULES) $(libdlext_sym)
+endif
+
 libdlext_test_norelro_src_files := \
     dlext_test_library.cpp \
 
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 2a0549a..4990582 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -17,6 +17,7 @@
 #include <gtest/gtest.h>
 
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <math.h>
 #include <stdio.h>
@@ -407,6 +408,28 @@
   EXPECT_STREQ("-0.000000", buf);
 }
 
+TEST(stdio, fprintf_failures_7229520) {
+  FILE* fp;
+
+  // Unbuffered case where the fprintf(3) itself fails.
+  ASSERT_NE(nullptr, fp = tmpfile());
+  setbuf(fp, NULL);
+  ASSERT_EQ(4, fprintf(fp, "epic"));
+  ASSERT_EQ(0, close(fileno(fp)));
+  ASSERT_EQ(-1, fprintf(fp, "fail"));
+  ASSERT_EQ(-1, fclose(fp));
+
+  // Buffered case where we won't notice until the fclose(3).
+  // It's likely this is what was actually seen in http://b/7229520,
+  // and that expecting fprintf to fail is setting yourself up for
+  // disappointment. Remember to check fclose(3)'s return value, kids!
+  ASSERT_NE(nullptr, fp = tmpfile());
+  ASSERT_EQ(4, fprintf(fp, "epic"));
+  ASSERT_EQ(0, close(fileno(fp)));
+  ASSERT_EQ(4, fprintf(fp, "fail"));
+  ASSERT_EQ(-1, fclose(fp));
+}
+
 TEST(stdio, popen) {
   FILE* fp = popen("cat /proc/version", "r");
   ASSERT_TRUE(fp != NULL);
@@ -560,9 +583,11 @@
   ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
   uselocale(LC_GLOBAL_LOCALE);
 
-  // For glibc we need to close and re-open the file in order for fseek to work
-  // after using setlocale(LC_CTYPE, "C.UTF-8") and fputwc.
-  // TODO: find out if this is expected or a bug in glibc.
+  // In glibc-2.16 fseek doesn't work properly in wide mode
+  // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
+  // to close and re-open the file. We do it in order to make the test pass
+  // with all glibcs.
+
   TemporaryFile tf;
   FILE* fp = fdopen(tf.fd, "w+");
   ASSERT_TRUE(fp != NULL);