Merge "Remove unused elf_machdep.h cruft."
diff --git a/android-changes-for-ndk-developers.md b/android-changes-for-ndk-developers.md
index 5279118..c301857 100644
--- a/android-changes-for-ndk-developers.md
+++ b/android-changes-for-ndk-developers.md
@@ -316,6 +316,16 @@
 the -soname linker option).
 
 
+## DT_RUNPATH support (Available in API level >= 24)
+
+If an ELF file contains a DT_RUNPATH entry, the directories listed there
+will be searched to resolve DT_NEEDED entries. The string `${ORIGIN}` will
+be rewritten at runtime to the directory containing the ELF file. This
+allows the use of relative paths. The `${LIB}` and `${PLATFORM}`
+substitutions supported on some systems are not currently implemented on
+Android.
+
+
 ## Writable and Executable Segments (Enforced for API level >= 26)
 
 Each segment in an ELF file has associated flags that tell the
diff --git a/libc/bionic/fortify.cpp b/libc/bionic/fortify.cpp
index cfbfbc5..144e133 100644
--- a/libc/bionic/fortify.cpp
+++ b/libc/bionic/fortify.cpp
@@ -71,15 +71,16 @@
 
 #include "private/bionic_fortify.h"
 
-struct __bionic_zero_size_is_okay_t __bionic_zero_size_is_okay;
-
 //
-// For more details see:
+// For more about FORTIFY see:
+//
+//   https://android-developers.googleblog.com/2017/04/fortify-in-android.html
+//
 //   http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
 //   http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
 //
-// TODO: add a link to similar clang documentation?
-//
+
+struct __bionic_zero_size_is_okay_t __bionic_zero_size_is_okay;
 
 int __FD_ISSET_chk(int fd, fd_set* set, size_t set_size) {
   __check_fd_set("FD_ISSET", fd, set_size);
diff --git a/libc/bionic/system_properties.cpp b/libc/bionic/system_properties.cpp
index e5947c8..9fb03ac 100644
--- a/libc/bionic/system_properties.cpp
+++ b/libc/bionic/system_properties.cpp
@@ -200,15 +200,6 @@
   DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
 };
 
-struct find_nth_cookie {
-  uint32_t count;
-  const uint32_t n;
-  const prop_info* pi;
-
-  explicit find_nth_cookie(uint32_t n) : count(0), n(n), pi(nullptr) {
-  }
-};
-
 // This is public because it was exposed in the NDK. As of 2017-01, ~60 apps reference this symbol.
 prop_area* __system_property_area__ = nullptr;
 
@@ -659,14 +650,6 @@
   return result;
 }
 
-static void find_nth_fn(const prop_info* pi, void* ptr) {
-  find_nth_cookie* cookie = reinterpret_cast<find_nth_cookie*>(ptr);
-
-  if (cookie->n == cookie->count) cookie->pi = pi;
-
-  cookie->count++;
-}
-
 bool prop_area::foreach_property(prop_bt* const trie,
                                  void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
   if (!trie) return false;
@@ -1456,20 +1439,19 @@
 }
 
 const prop_info* __system_property_find_nth(unsigned n) {
-  if (bionic_get_application_target_sdk_version() >= __ANDROID_API_O__) {
-    __libc_fatal(
-        "__system_property_find_nth is not supported since Android O,"
-        " please use __system_property_foreach instead.");
-  }
+  struct find_nth {
+    const uint32_t sought;
+    uint32_t current;
+    const prop_info* result;
 
-  find_nth_cookie cookie(n);
-
-  const int err = __system_property_foreach(find_nth_fn, &cookie);
-  if (err < 0) {
-    return nullptr;
-  }
-
-  return cookie.pi;
+    explicit find_nth(uint32_t n) : sought(n), current(0), result(nullptr) {}
+    static void fn(const prop_info* pi, void* ptr) {
+      find_nth* self = reinterpret_cast<find_nth*>(ptr);
+      if (self->current++ == self->sought) self->result = pi;
+    }
+  } state(n);
+  __system_property_foreach(find_nth::fn, &state);
+  return state.result;
 }
 
 int __system_property_foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie) {
@@ -1479,7 +1461,7 @@
 
   list_foreach(contexts, [propfn, cookie](context_node* l) {
     if (l->check_access_and_open()) {
-      l->pa()->foreach (propfn, cookie);
+      l->pa()->foreach(propfn, cookie);
     }
   });
   return 0;
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index aa93c78..004c6b5 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -256,8 +256,7 @@
  * added to commonly used libc functions. If a buffer overrun is
  * detected, the program is safely aborted.
  *
- * See
- * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html for details.
+ * https://android-developers.googleblog.com/2017/04/fortify-in-android.html
  */
 
 #define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
diff --git a/libc/include/sys/system_properties.h b/libc/include/sys/system_properties.h
index b55566e..c0bd445 100644
--- a/libc/include/sys/system_properties.h
+++ b/libc/include/sys/system_properties.h
@@ -91,11 +91,11 @@
 /* Deprecated. In Android O and above, there's no limit on property name length. */
 #define PROP_NAME_MAX   32
 /* Deprecated. Use __system_property_read_callback instead. */
-int __system_property_read(const prop_info *pi, char *name, char *value);
+int __system_property_read(const prop_info* pi, char* name, char* value);
 /* Deprecated. Use __system_property_read_callback instead. */
-int __system_property_get(const char *name, char *value);
-/* Deprecated. Use __system_property_foreach instead. Aborts in Android O and above. */
-const prop_info *__system_property_find_nth(unsigned n) __REMOVED_IN(26);
+int __system_property_get(const char* name, char* value);
+/* Deprecated. Use __system_property_foreach instead. */
+const prop_info* __system_property_find_nth(unsigned n);
 
 __END_DECLS
 
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 96dd477..0bc5a31 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -48,7 +48,7 @@
 
   char* old_value = *dlerror_slot;
   *dlerror_slot = new_value;
-  LD_LOG(kLogErrors, "%s\n", new_value);
+  if (new_value != nullptr) LD_LOG(kLogErrors, "dlerror set to \"%s\"", new_value);
   return old_value;
 }
 
diff --git a/tests/system_properties_test.cpp b/tests/system_properties_test.cpp
index 23d0cad..7415b3c 100644
--- a/tests/system_properties_test.cpp
+++ b/tests/system_properties_test.cpp
@@ -253,10 +253,21 @@
     ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6));
     ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6));
 
-    // This method is no longer supported and should result in abort
-    ASSERT_EXIT(__system_property_find_nth(0), testing::KilledBySignal(SIGABRT),
-                "__system_property_find_nth is not supported since Android O,"
-                " please use __system_property_foreach instead.");
+    char name[PROP_NAME_MAX];
+    char value[PROP_VALUE_MAX];
+    EXPECT_EQ(6, __system_property_read(__system_property_find_nth(0), name, value));
+    EXPECT_STREQ("property", name);
+    EXPECT_STREQ("value1", value);
+    EXPECT_EQ(6, __system_property_read(__system_property_find_nth(1), name, value));
+    EXPECT_STREQ("other_property", name);
+    EXPECT_STREQ("value2", value);
+    EXPECT_EQ(6, __system_property_read(__system_property_find_nth(2), name, value));
+    EXPECT_STREQ("property_other", name);
+    EXPECT_STREQ("value3", value);
+
+    for (unsigned i = 3; i < 1024; ++i) {
+      ASSERT_TRUE(__system_property_find_nth(i) == nullptr);
+    }
 #else // __BIONIC__
     GTEST_LOG_(INFO) << "This test does nothing.\n";
 #endif // __BIONIC__