Merge "Accuracy tests for libm"
diff --git a/libc/Android.mk b/libc/Android.mk
index 75bb616..fdaa8ee 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -1179,8 +1179,10 @@
 # special for arm
 LOCAL_NO_CRT_arm := true
 LOCAL_CFLAGS_arm += -DCRT_LEGACY_WORKAROUND
+LOCAL_ASFLAGS_arm += $(libc_crt_target_cflags)
 LOCAL_SRC_FILES_arm += \
     arch-common/bionic/crtbegin_so.c \
+    arch-common/bionic/crtbrand.S \
     arch-arm/bionic/atexit_legacy.c \
     arch-common/bionic/crtend_so.S
 LOCAL_ADDRESS_SANITIZER := false
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index 60600e5..d72a160 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -5,8 +5,6 @@
 #
 
 libc_bionic_src_files_arm += \
-    bionic/memchr.c \
-    bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
@@ -22,6 +20,8 @@
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
 libc_openbsd_src_files_arm += \
+    upstream-openbsd/lib/libc/string/memchr.c \
+    upstream-openbsd/lib/libc/string/memrchr.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strlcat.c \
     upstream-openbsd/lib/libc/string/strlcpy.c \
diff --git a/libc/arch-arm64/arm64.mk b/libc/arch-arm64/arm64.mk
index 8418993..470a038 100644
--- a/libc/arch-arm64/arm64.mk
+++ b/libc/arch-arm64/arm64.mk
@@ -8,7 +8,6 @@
     bionic/__memset_chk.cpp \
     bionic/__strcpy_chk.cpp \
     bionic/__strcat_chk.cpp \
-    bionic/memrchr.c \
     bionic/strrchr.cpp \
 
 libc_freebsd_src_files_arm64 += \
@@ -21,6 +20,7 @@
     upstream-freebsd/lib/libc/string/wmemcmp.c \
 
 libc_openbsd_src_files_arm64 += \
+    upstream-openbsd/lib/libc/string/memrchr.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
     upstream-openbsd/lib/libc/string/strlcat.c \
diff --git a/libc/bionic/memchr.c b/libc/arch-common/bionic/crtbrand.S
similarity index 72%
rename from libc/bionic/memchr.c
rename to libc/arch-common/bionic/crtbrand.S
index b14167a..11bbfbe 100644
--- a/libc/bionic/memchr.c
+++ b/libc/arch-common/bionic/crtbrand.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2015 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,22 +25,15 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-#include <stddef.h>
-#include <string.h>
 
-void *memchr(const void *s, int c, size_t n)
-{
-    const unsigned char*  p   = s;
-    const unsigned char*  end = p + n;
-
-    for (;;) {
-        if (p >= end || p[0] == c) break; p++;
-        if (p >= end || p[0] == c) break; p++;
-        if (p >= end || p[0] == c) break; p++;
-        if (p >= end || p[0] == c) break; p++;
-    }
-    if (p >= end)
-        return NULL;
-    else
-        return (void*) p;
-}
+	.section	.note.android.ident,"a",%note
+	.align	2
+	.type	abitag, %object
+abitag:
+	.long	2f-1f			/* int32_t namesz */
+	.long	3f-2f			/* int32_t descsz */
+	.long	1			/* int32_t type */
+1:	.ascii	"Android\0"		/* char name[] */
+2:	.long	PLATFORM_SDK_VERSION	/* int32_t android_api */
+3:
+	.size	abitag, .-abitag
diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk
index 7e3fe25..33eceab 100644
--- a/libc/arch-mips/mips.mk
+++ b/libc/arch-mips/mips.mk
@@ -10,8 +10,6 @@
     bionic/__memset_chk.cpp \
     bionic/__strcpy_chk.cpp \
     bionic/__strcat_chk.cpp \
-    bionic/memchr.c \
-    bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
@@ -27,7 +25,9 @@
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
 libc_openbsd_src_files_mips += \
+    upstream-openbsd/lib/libc/string/memchr.c \
     upstream-openbsd/lib/libc/string/memmove.c \
+    upstream-openbsd/lib/libc/string/memrchr.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
diff --git a/libc/arch-mips64/mips64.mk b/libc/arch-mips64/mips64.mk
index 1376395..2b81e63 100644
--- a/libc/arch-mips64/mips64.mk
+++ b/libc/arch-mips64/mips64.mk
@@ -9,8 +9,6 @@
     bionic/__memset_chk.cpp \
     bionic/__strcpy_chk.cpp \
     bionic/__strcat_chk.cpp \
-    bionic/memchr.c \
-    bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
@@ -30,7 +28,9 @@
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
 libc_openbsd_src_files_mips64 += \
+    upstream-openbsd/lib/libc/string/memchr.c \
     upstream-openbsd/lib/libc/string/memmove.c \
+    upstream-openbsd/lib/libc/string/memrchr.c \
     upstream-openbsd/lib/libc/string/stpcpy.c \
     upstream-openbsd/lib/libc/string/stpncpy.c \
     upstream-openbsd/lib/libc/string/strcat.c \
diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk
index ae01d2a..06d3185 100644
--- a/libc/arch-x86_64/x86_64.mk
+++ b/libc/arch-x86_64/x86_64.mk
@@ -9,8 +9,6 @@
     bionic/__memset_chk.cpp \
     bionic/__strcpy_chk.cpp \
     bionic/__strcat_chk.cpp \
-    bionic/memchr.c \
-    bionic/memrchr.c \
     bionic/strchr.cpp \
     bionic/strnlen.c \
     bionic/strrchr.cpp \
@@ -25,6 +23,10 @@
     upstream-freebsd/lib/libc/string/wmemcmp.c \
     upstream-freebsd/lib/libc/string/wmemmove.c \
 
+libc_openbsd_src_files_x86_64 += \
+    upstream-openbsd/lib/libc/string/memchr.c \
+    upstream-openbsd/lib/libc/string/memrchr.c \
+
 #
 # Inherently architecture-specific code.
 #
diff --git a/libc/bionic/crtbrand.c b/libc/bionic/crtbrand.c
deleted file mode 100644
index a726c51..0000000
--- a/libc/bionic/crtbrand.c
+++ /dev/null
@@ -1,48 +0,0 @@
-/*-
- * Copyright 2000 David E. O'Brien, John D. Polstra.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <stdint.h>
-
-#define ABI_VENDOR	"Android"
-#define ABI_SECTION	".note.android.ident"
-#define ABI_NOTETYPE	1
-#define ABI_ANDROID_API	PLATFORM_SDK_VERSION
-
-/*
- * Special ".note" entry to tag an Android binary and specify the ABI version.
- */
-static const struct {
-    int32_t	namesz;
-    int32_t	descsz;
-    int32_t	type;
-    char	name[sizeof ABI_VENDOR];
-    int32_t	android_api;
-} abitag __attribute__ ((section (ABI_SECTION), aligned(4), used)) = {
-    sizeof ABI_VENDOR,
-    sizeof(int32_t),
-    ABI_NOTETYPE,
-    ABI_VENDOR,
-    ABI_ANDROID_API,
-};
diff --git a/libc/bionic/memrchr.c b/libc/bionic/memrchr.c
deleted file mode 100644
index aeb5643..0000000
--- a/libc/bionic/memrchr.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-#include <stddef.h>
-#include <string.h>
-
-void *memrchr(const void *s, int c, size_t n)
-{
-    if (n > 0) {
-        const char*  p = (const char*) s;
-        const char*  q = p + n;
-
-        while (1) {
-            q--; if (q < p || q[0] == (char) c) break;
-            q--; if (q < p || q[0] == (char) c) break;
-            q--; if (q < p || q[0] == (char) c) break;
-            q--; if (q < p || q[0] == (char) c) break;
-        }
-        if (q >= p)
-            return (void*)q;
-    }
-    return NULL;
-}
diff --git a/libc/bionic/sysconf.cpp b/libc/bionic/sysconf.cpp
index 411c23a..c301b27 100644
--- a/libc/bionic/sysconf.cpp
+++ b/libc/bionic/sysconf.cpp
@@ -33,6 +33,7 @@
 #include <pthread.h>
 #include <stdio.h>  // For FOPEN_MAX.
 #include <sys/auxv.h>
+#include <sys/resource.h>
 #include <sys/sysconf.h>
 #include <sys/sysinfo.h>
 #include <time.h>
@@ -50,6 +51,12 @@
   return clock_getres(clock_id, NULL) == 0;
 }
 
+static long __sysconf_rlimit(int resource) {
+  rlimit rl;
+  getrlimit(resource, &rl);
+  return rl.rlim_cur;
+}
+
 long sysconf(int name) {
   switch (name) {
     case _SC_ARG_MAX:           return ARG_MAX;
@@ -57,13 +64,13 @@
     case _SC_BC_DIM_MAX:        return _POSIX2_BC_DIM_MAX;    // Minimum requirement.
     case _SC_BC_SCALE_MAX:      return _POSIX2_BC_SCALE_MAX;  // Minimum requirement.
     case _SC_BC_STRING_MAX:     return _POSIX2_BC_STRING_MAX; // Minimum requirement.
-    case _SC_CHILD_MAX:         return CHILD_MAX;
+    case _SC_CHILD_MAX:         return __sysconf_rlimit(RLIMIT_NPROC);
     case _SC_CLK_TCK:           return static_cast<long>(getauxval(AT_CLKTCK));
     case _SC_COLL_WEIGHTS_MAX:  return _POSIX2_COLL_WEIGHTS_MAX;  // Minimum requirement.
     case _SC_EXPR_NEST_MAX:     return _POSIX2_EXPR_NEST_MAX;     // Minimum requirement.
     case _SC_LINE_MAX:          return _POSIX2_LINE_MAX;          // Minimum requirement.
     case _SC_NGROUPS_MAX:       return NGROUPS_MAX;
-    case _SC_OPEN_MAX:          return OPEN_MAX;
+    case _SC_OPEN_MAX:          return __sysconf_rlimit(RLIMIT_NOFILE);
     case _SC_PASS_MAX:          return PASS_MAX;
     case _SC_2_C_BIND:          return _POSIX2_C_BIND;
     case _SC_2_C_DEV:           return _POSIX2_C_DEV;
diff --git a/libc/crt.mk b/libc/crt.mk
index 410f229..6365332 100644
--- a/libc/crt.mk
+++ b/libc/crt.mk
@@ -31,18 +31,19 @@
 
 my_libc_crt_target_ldflags := $(libc_crt_target_ldflags_$(my_arch))
 
-
+# crtbrand.S -> crtbrand.o
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
 $(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
 $(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
-$(GEN): $(LOCAL_PATH)/bionic/crtbrand.c
+$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtbrand.S
+	@mkdir -p $(dir $@)
 	$(hide) $(PRIVATE_CC) $(PRIVATE_CFLAGS) \
 		-MD -MF $(@:%.o=%.d) -o $@ -c $<
 	$(transform-d-to-p)
 -include $(GEN:%.o=%.P)
 
-
-GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
+# crtbegin_so.c -> crtbegin_so1.o
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so1.o
 $(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
 $(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
 $(GEN): $(my_libc_crt_target_crtbegin_so_file)
@@ -52,7 +53,16 @@
 	$(transform-d-to-p)
 -include $(GEN:%.o=%.P)
 
+# crtbegin_so1.o + crtbrand.o -> crtbegin_so.o
+GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
+$(GEN): PRIVATE_LD := $($(my_2nd_arch_prefix)TARGET_LD)
+$(GEN): PRIVATE_LDFLAGS := $(my_libc_crt_target_ldflags)
+$(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so1.o \
+    $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
+	@mkdir -p $(dir $@)
+	$(hide) $(PRIVATE_LD) $(PRIVATE_LDFLAGS) -r -o $@ $^
 
+# crtend_so.S -> crtend_so.o
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
 $(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
 $(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_so_cflags)
@@ -63,8 +73,7 @@
 	$(transform-d-to-p)
 -include $(GEN:%.o=%.P)
 
-
-# The following two are installed to device
+# crtbegin_so.o and crtend_so.o are installed to device
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_SHARED_LIBRARIES)/crtbegin_so.o
 $(GEN): $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
 	$(hide) mkdir -p $(dir $@) && cp -f $< $@
@@ -75,7 +84,7 @@
 	$(hide) mkdir -p $(dir $@) && cp -f $< $@
 ALL_GENERATED_SOURCES += $(GEN)
 
-
+# crtbegin.c -> crtbegin_static1.o
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o
 $(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
 $(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_cflags)
@@ -86,7 +95,7 @@
 	$(transform-d-to-p)
 -include $(GEN:%.o=%.P)
 
-
+# crtbegin_static1.o + crtbrand.o -> crtbegin_static.o
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static.o
 $(GEN): PRIVATE_LD := $($(my_2nd_arch_prefix)TARGET_LD)
 $(GEN): PRIVATE_LDFLAGS := $(my_libc_crt_target_ldflags)
@@ -95,7 +104,7 @@
 	@mkdir -p $(dir $@)
 	$(hide) $(PRIVATE_LD) $(PRIVATE_LDFLAGS) -r -o $@ $^
 
-
+# crtbegin.c -> crtbegin_dynamic1.o
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o
 $(GEN): PRIVATE_CC := $($(my_2nd_arch_prefix)TARGET_CC)
 $(GEN): PRIVATE_CFLAGS := $(my_libc_crt_target_cflags)
@@ -106,7 +115,7 @@
 	$(transform-d-to-p)
 -include $(GEN:%.o=%.P)
 
-
+# crtbegin_dynamic1.o + crtbrand.o -> crtbegin_dynamic.o
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic.o
 $(GEN): PRIVATE_LD := $($(my_2nd_arch_prefix)TARGET_LD)
 $(GEN): PRIVATE_LDFLAGS := $(my_libc_crt_target_ldflags)
@@ -115,7 +124,7 @@
 	@mkdir -p $(dir $@)
 	$(hide) $(PRIVATE_LD) $(PRIVATE_LDFLAGS) -r -o $@ $^
 
-
+# crtend.S -> crtend_android.o
 # We rename crtend.o to crtend_android.o to avoid a
 # name clash between gcc and bionic.
 GEN := $($(my_2nd_arch_prefix)TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_android.o
diff --git a/libc/include/elf.h b/libc/include/elf.h
index 2039cc0..a41a244 100644
--- a/libc/include/elf.h
+++ b/libc/include/elf.h
@@ -106,6 +106,9 @@
 #define STB_LOPROC    13
 #define STB_HIPROC    15
 
+#define SHT_LOOS    0x60000000
+#define SHT_HIOS    0x6fffffff
+
 #define STT_GNU_IFUNC 10
 #define STT_LOOS      10
 #define STT_HIOS      12
diff --git a/libc/include/grp.h b/libc/include/grp.h
index fc4d520..7b3e32b 100644
--- a/libc/include/grp.h
+++ b/libc/include/grp.h
@@ -54,9 +54,9 @@
 struct group	*getgrgid(gid_t);
 struct group	*getgrnam(const char *);
 #if __POSIX_VISIBLE >= 200112 || __XPG_VISIBLE
-struct group	*getgrent(void);
-void		 setgrent(void);
-void		 endgrent(void);
+struct group	*getgrent(void) __errorattr("getgrent is meaningless on Android");
+__errordecl(setgrent, "setgrent is meaningless on Android");
+__errordecl(endgrent, "endgrent is meaningless on Android");
 int		 getgrgid_r(gid_t, struct group *, char *,
 		    size_t, struct group **);
 int		 getgrnam_r(const char *, struct group *, char *,
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index 48763d7..1d33895 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -335,13 +335,15 @@
 #endif
 
 #if __GNUC_PREREQ(4, 3)
-#define __errordecl(name, msg) extern void name(void) __attribute__((__error__(msg)))
+#define __errorattr(msg) __attribute__((__error__(msg)))
 #define __warnattr(msg) __attribute__((__warning__(msg)))
 #else
-#define __errordecl(name, msg) extern void name(void)
+#define __errorattr(msg)
 #define __warnattr(msg)
 #endif
 
+#define __errordecl(name, msg) extern void name(void) __errorattr(msg)
+
 /*
  * Some BSD source needs these macros.
  * Originally they embedded the rcs versions of each source file
diff --git a/libc/include/sys/file.h b/libc/include/sys/file.h
index cf2f4b1..f414d34 100644
--- a/libc/include/sys/file.h
+++ b/libc/include/sys/file.h
@@ -25,14 +25,19 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 #ifndef _SYS_FILE_H_
 #define _SYS_FILE_H_
 
 #include <sys/cdefs.h>
 #include <sys/types.h>
 
-/* ANDROID: needed for flock() */
-#include <unistd.h>
 #include <fcntl.h>
 
+__BEGIN_DECLS
+
+int flock(int, int);
+
+__END_DECLS
+
 #endif /* _SYS_FILE_H_ */
diff --git a/libc/include/unistd.h b/libc/include/unistd.h
index 6403d4a..16d413a 100644
--- a/libc/include/unistd.h
+++ b/libc/include/unistd.h
@@ -168,7 +168,6 @@
 extern int dup3(int, int, int);
 extern int fcntl(int, int, ...);
 extern int ioctl(int, int, ...);
-extern int flock(int, int);
 extern int fsync(int);
 extern int fdatasync(int);
 extern int ftruncate(int, off_t);
diff --git a/libc/upstream-openbsd/lib/libc/string/memchr.c b/libc/upstream-openbsd/lib/libc/string/memchr.c
new file mode 100644
index 0000000..4573e3c
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/string/memchr.c
@@ -0,0 +1,48 @@
+/*	$OpenBSD: memchr.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by
+ * Chris Torek.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <string.h>
+
+void *
+memchr(const void *s, int c, size_t n)
+{
+	if (n != 0) {
+		const unsigned char *p = s;
+
+		do {
+			if (*p++ == (unsigned char)c)
+				return ((void *)(p - 1));
+		} while (--n != 0);
+	}
+	return (NULL);
+}
diff --git a/libc/upstream-openbsd/lib/libc/string/memrchr.c b/libc/upstream-openbsd/lib/libc/string/memrchr.c
new file mode 100644
index 0000000..bd27ebc
--- /dev/null
+++ b/libc/upstream-openbsd/lib/libc/string/memrchr.c
@@ -0,0 +1,38 @@
+/*	$OpenBSD: memrchr.c,v 1.2 2007/11/27 16:22:12 martynas Exp $	*/
+
+/*
+ * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <string.h>
+
+/*
+ * Reverse memchr()
+ * Find the last occurrence of 'c' in the buffer 's' of size 'n'.
+ */
+void *
+memrchr(const void *s, int c, size_t n)
+{
+	const unsigned char *cp;
+
+	if (n != 0) {
+		cp = (unsigned char *)s + n;
+		do {
+			if (*(--cp) == (unsigned char)c)
+				return((void *)cp);
+		} while (--n != 0);
+	}
+	return(NULL);
+}
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 3934484..5d2425f 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -2040,8 +2040,8 @@
         if ((dynamic_flags & PF_W) != 0) {
           d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
         }
-        break;
 #endif
+        break;
 #if defined(USE_RELA)
       case DT_RELA:
         rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
diff --git a/tests/string_test.cpp b/tests/string_test.cpp
index 137565e..66cf848 100644
--- a/tests/string_test.cpp
+++ b/tests/string_test.cpp
@@ -1385,3 +1385,13 @@
   TestBasename("///", "");
   TestBasename("//usr//lib//", "");
 }
+
+TEST(string, strnlen_147048) {
+  // https://code.google.com/p/android/issues/detail?id=147048
+  char stack_src[64] = {0};
+  EXPECT_EQ(0U, strnlen(stack_src, 1024*1024*1024));
+  char* heap_src = new char[1];
+  *heap_src = '\0';
+  EXPECT_EQ(0U, strnlen(heap_src, 1024*1024*1024));
+  delete[] heap_src;
+}
diff --git a/tests/wchar_test.cpp b/tests/wchar_test.cpp
index 63f3760..a1d1501 100644
--- a/tests/wchar_test.cpp
+++ b/tests/wchar_test.cpp
@@ -234,6 +234,11 @@
   ASSERT_EQ(NULL, wcsstr(haystack, bad_needle));
 }
 
+TEST(wchar, wcsstr_80199) {
+  // https://code.google.com/p/android/issues/detail?id=80199
+  ASSERT_TRUE(wcsstr(L"romrom", L"rom") != NULL);
+}
+
 TEST(wchar, mbtowc) {
   wchar_t out[8];