Merge "Revert "Revert "Turn on XOM for libc"""
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..e4d3d5e
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "presubmit": [
+    {
+      "name": "CtsBionicTestCases"
+    }
+  ]
+}
diff --git a/build/bionic.go b/build/bionic.go
index 93c2494..54ad10b 100644
--- a/build/bionic.go
+++ b/build/bionic.go
@@ -89,6 +89,9 @@
 	// Symlinks to the mountpoints from the system and recovery partitions
 	// Symlinks names will have the same suffix as the mount point
 	Symlinks []string
+
+	// List of sanitizer names that this APEX is enabled for
+	SanitizerNames []string `blueprint:"mutated"`
 }
 
 type dependencyTag struct {
@@ -98,6 +101,31 @@
 
 var mountsourceTag = dependencyTag{name: "mountsource"}
 
+
+func (m *bionicMountpoint) EnableSanitizer(sanitizerName string) {
+	if !android.InList(sanitizerName, m.properties.SanitizerNames) {
+		m.properties.SanitizerNames = append(m.properties.SanitizerNames, sanitizerName)
+	}
+}
+
+func (m *bionicMountpoint) IsSanitizerEnabled(ctx android.BaseModuleContext, sanitizerName string) bool {
+	if android.InList(sanitizerName, m.properties.SanitizerNames) {
+		return true
+	}
+
+	// Then follow the global setting
+	globalSanitizerNames := []string{}
+	if m.Host() {
+		globalSanitizerNames = ctx.Config().SanitizeHost()
+	} else {
+		arches := ctx.Config().SanitizeDeviceArch()
+		if len(arches) == 0 || android.InList(m.Arch().ArchType.Name, arches) {
+			globalSanitizerNames = ctx.Config().SanitizeDevice()
+		}
+	}
+	return android.InList(sanitizerName, globalSanitizerNames)
+}
+
 func (m *bionicMountpoint) DepsMutator(ctx android.BottomUpMutatorContext) {
 	if Bool(m.properties.Library) == Bool(m.properties.Binary) {
 		ctx.ModuleErrorf("either binary or library must be set to true")
diff --git a/libc/async_safe/Android.bp b/libc/async_safe/Android.bp
index a54d3b0..fbaaad1 100644
--- a/libc/async_safe/Android.bp
+++ b/libc/async_safe/Android.bp
@@ -12,7 +12,20 @@
     recovery_available: true,
 
     include_dirs: ["bionic/libc"],
-    header_libs: ["libc_headers"],
+    header_libs: ["libc_headers", "liblog_headers"],
 
     export_include_dirs: ["include"],
+    export_header_lib_headers: ["liblog_headers"],
+    stl: "none",
+}
+
+cc_library_headers {
+    name: "libasync_safe_headers",
+    recovery_available: true,
+    defaults: ["linux_bionic_supported"],
+
+    export_include_dirs: ["include"],
+
+    system_shared_libs: [],
+    stl: "none",
 }
diff --git a/libc/async_safe/include/async_safe/CHECK.h b/libc/async_safe/include/async_safe/CHECK.h
new file mode 100644
index 0000000..bec89a3
--- /dev/null
+++ b/libc/async_safe/include/async_safe/CHECK.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#pragma once
+
+#include <sys/cdefs.h>
+
+#include <async_safe/log.h>
+
+__BEGIN_DECLS
+
+// TODO: replace this with something more like <android-base/logging.h>'s family of macros.
+
+#define CHECK(predicate) \
+  do { \
+    if (!(predicate)) { \
+      async_safe_fatal("%s:%d: %s CHECK '" #predicate "' failed", \
+          __FILE__, __LINE__, __FUNCTION__); \
+    } \
+  } while(0)
+
+__END_DECLS
diff --git a/libc/async_safe/include/async_safe/log.h b/libc/async_safe/include/async_safe/log.h
index df68062..0e02ea7 100644
--- a/libc/async_safe/include/async_safe/log.h
+++ b/libc/async_safe/include/async_safe/log.h
@@ -34,36 +34,14 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+// This file is an alternative to <android/log.h>, but reuses
+// `android_LogPriority` and should not have conflicting identifiers.
+#include <android/log.h>
+
 // These functions do not allocate memory to send data to the log.
 
 __BEGIN_DECLS
 
-enum {
-  ANDROID_LOG_UNKNOWN = 0,
-  ANDROID_LOG_DEFAULT,    /* only for SetMinPriority() */
-
-  ANDROID_LOG_VERBOSE,
-  ANDROID_LOG_DEBUG,
-  ANDROID_LOG_INFO,
-  ANDROID_LOG_WARN,
-  ANDROID_LOG_ERROR,
-  ANDROID_LOG_FATAL,
-
-  ANDROID_LOG_SILENT,     /* only for SetMinPriority(); must be last */
-};
-
-enum {
-  LOG_ID_MIN = 0,
-
-  LOG_ID_MAIN = 0,
-  LOG_ID_RADIO = 1,
-  LOG_ID_EVENTS = 2,
-  LOG_ID_SYSTEM = 3,
-  LOG_ID_CRASH = 4,
-
-  LOG_ID_MAX
-};
-
 // Formats a message to the log (priority 'fatal'), then aborts.
 // Implemented as a macro so that async_safe_fatal isn't on the stack when we crash:
 // we appear to go straight from the caller to abort, saving an uninteresting stack
@@ -91,16 +69,8 @@
 
 int async_safe_format_fd(int fd, const char* format , ...) __printflike(2, 3);
 int async_safe_format_fd_va_list(int fd, const char* format, va_list args);
-int async_safe_format_log(int pri, const char* tag, const char* fmt, ...) __printflike(3, 4);
-int async_safe_format_log_va_list(int pri, const char* tag, const char* fmt, va_list ap);
-int async_safe_write_log(int pri, const char* tag, const char* msg);
-
-#define CHECK(predicate) \
-  do { \
-    if (!(predicate)) { \
-      async_safe_fatal("%s:%d: %s CHECK '" #predicate "' failed", \
-          __FILE__, __LINE__, __FUNCTION__); \
-    } \
-  } while(0)
+int async_safe_format_log(int priority, const char* tag, const char* fmt, ...) __printflike(3, 4);
+int async_safe_format_log_va_list(int priority, const char* tag, const char* fmt, va_list ap);
+int async_safe_write_log(int priority, const char* tag, const char* msg);
 
 __END_DECLS
diff --git a/libc/bionic/bionic_allocator.cpp b/libc/bionic/bionic_allocator.cpp
index d9302ad..da0f7d0 100644
--- a/libc/bionic/bionic_allocator.cpp
+++ b/libc/bionic/bionic_allocator.cpp
@@ -38,6 +38,7 @@
 #include <new>
 
 #include <async_safe/log.h>
+#include <async_safe/CHECK.h>
 
 #include "private/bionic_macros.h"
 #include "private/bionic_page.h"
diff --git a/libc/bionic/libc_init_static.cpp b/libc/bionic/libc_init_static.cpp
index 3219239..41f100d 100644
--- a/libc/bionic/libc_init_static.cpp
+++ b/libc/bionic/libc_init_static.cpp
@@ -67,9 +67,20 @@
 }
 
 #if defined(__aarch64__) || defined(__x86_64__)
-extern __LIBC_HIDDEN__ ElfW(Rela) __rela_iplt_start[], __rela_iplt_end[];
+extern __LIBC_HIDDEN__ __attribute__((weak)) ElfW(Rela) __rela_iplt_start[], __rela_iplt_end[];
 
 static void call_ifunc_resolvers() {
+  if (__rela_iplt_start == nullptr || __rela_iplt_end == nullptr) {
+    // These symbols are not emitted by gold. Gold has code to do so, but for
+    // whatever reason it is not being run. In these cases ifuncs cannot be
+    // resolved, so we do not support using ifuncs in static executables linked
+    // with gold.
+    //
+    // Since they are weak, they will be non-null when linked with bfd/lld and
+    // null when linked with gold.
+    return;
+  }
+
   typedef ElfW(Addr) (*ifunc_resolver_t)(void);
   for (ElfW(Rela) *r = __rela_iplt_start; r != __rela_iplt_end; ++r) {
     ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(r->r_offset);
@@ -78,9 +89,20 @@
   }
 }
 #else
-extern __LIBC_HIDDEN__ ElfW(Rel) __rel_iplt_start[], __rel_iplt_end[];
+extern __LIBC_HIDDEN__ __attribute__((weak)) ElfW(Rel) __rel_iplt_start[], __rel_iplt_end[];
 
 static void call_ifunc_resolvers() {
+  if (__rel_iplt_start == nullptr || __rel_iplt_end == nullptr) {
+    // These symbols are not emitted by gold. Gold has code to do so, but for
+    // whatever reason it is not being run. In these cases ifuncs cannot be
+    // resolved, so we do not support using ifuncs in static executables linked
+    // with gold.
+    //
+    // Since they are weak, they will be non-null when linked with bfd/lld and
+    // null when linked with gold.
+    return;
+  }
+
   typedef ElfW(Addr) (*ifunc_resolver_t)(void);
   for (ElfW(Rel) *r = __rel_iplt_start; r != __rel_iplt_end; ++r) {
     ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(r->r_offset);
diff --git a/libc/bionic/system_property_set.cpp b/libc/bionic/system_property_set.cpp
index bc3ba76..c508db1 100644
--- a/libc/bionic/system_property_set.cpp
+++ b/libc/bionic/system_property_set.cpp
@@ -42,6 +42,7 @@
 #include <unistd.h>
 
 #include <async_safe/log.h>
+#include <async_safe/CHECK.h>
 
 #include "private/bionic_defs.h"
 #include "private/bionic_macros.h"
diff --git a/libc/include/android/dlext.h b/libc/include/android/dlext.h
index e78be39..f0b731c 100644
--- a/libc/include/android/dlext.h
+++ b/libc/include/android/dlext.h
@@ -114,6 +114,29 @@
    */
   ANDROID_DLEXT_USE_NAMESPACE = 0x200,
 
+  /**
+   * Instructs dlopen to apply `ANDROID_DLEXT_RESERVED_ADDRESS`,
+   * `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`, `ANDROID_DLEXT_WRITE_RELRO` and
+   * `ANDROID_DLEXT_USE_RELRO` to any libraries loaded as dependencies of the
+   * main library as well.
+   *
+   * This means that if the main library depends on one or more not-already-loaded libraries, they
+   * will be loaded consecutively into the region starting at `reserved_addr`, and `reserved_size`
+   * must be large enough to contain all of the libraries. The libraries will be loaded in the
+   * deterministic order constructed from the DT_NEEDED entries, rather than the more secure random
+   * order used by default.
+   *
+   * Each library's GNU RELRO sections will be written out to `relro_fd` in the same order they were
+   * loaded. This will mean that the resulting file is dependent on which of the libraries were
+   * already loaded, as only the newly loaded libraries will be included, not any already-loaded
+   * dependencies. The caller should ensure that the set of libraries newly loaded is consistent
+   * for this to be effective.
+   *
+   * This is mainly useful for the system WebView implementation.
+   */
+  ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE = 0x400,
+
+
   /** Mask of valid bits. */
   ANDROID_DLEXT_VALID_FLAG_BITS       = ANDROID_DLEXT_RESERVED_ADDRESS |
                                         ANDROID_DLEXT_RESERVED_ADDRESS_HINT |
@@ -122,7 +145,8 @@
                                         ANDROID_DLEXT_USE_LIBRARY_FD |
                                         ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET |
                                         ANDROID_DLEXT_FORCE_LOAD |
-                                        ANDROID_DLEXT_USE_NAMESPACE,
+                                        ANDROID_DLEXT_USE_NAMESPACE |
+                                        ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE,
 };
 
 struct android_namespace_t;
diff --git a/libc/system_properties/Android.bp b/libc/system_properties/Android.bp
index f94cda9..911afb1 100644
--- a/libc/system_properties/Android.bp
+++ b/libc/system_properties/Android.bp
@@ -12,8 +12,8 @@
     whole_static_libs: [
         "libpropertyinfoparser",
     ],
-    static_libs: [
-        "libasync_safe",
+    header_libs: [
+        "libasync_safe_headers",
     ],
 
     include_dirs: [
diff --git a/libdl/Android.bp b/libdl/Android.bp
index 43ddbfe..642cc7a 100644
--- a/libdl/Android.bp
+++ b/libdl/Android.bp
@@ -92,6 +92,8 @@
 
     nocrt: true,
     system_shared_libs: [],
+
+    // Opt out of native_coverage when opting out of system_shared_libs
     native_coverage: false,
 
     // This is placeholder library the actual implementation is (currently)
diff --git a/linker/Android.bp b/linker/Android.bp
index 06a942d..613be3d 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -375,3 +375,39 @@
         never: true,
     },
 }
+
+cc_test {
+    name: "linker-unit-tests",
+
+    cflags: [
+        "-g",
+        "-Wall",
+        "-Wextra",
+        "-Wunused",
+        "-Werror",
+    ],
+
+    // We need to access Bionic private headers in the linker.
+    include_dirs: ["bionic/libc"],
+
+    srcs: [
+        // Tests.
+        "linker_block_allocator_test.cpp",
+        "linker_config_test.cpp",
+        "linked_list_test.cpp",
+        "linker_sleb128_test.cpp",
+        "linker_utils_test.cpp",
+
+        // Parts of the linker that we're testing.
+        "linker_block_allocator.cpp",
+        "linker_config.cpp",
+        "linker_test_globals.cpp",
+        "linker_utils.cpp",
+    ],
+
+    static_libs: [
+        "libasync_safe",
+        "libbase",
+        "liblog",
+    ],
+}
diff --git a/linker/Android.mk b/linker/Android.mk
deleted file mode 100644
index ea7451c..0000000
--- a/linker/Android.mk
+++ /dev/null
@@ -1,3 +0,0 @@
-LOCAL_PATH := $(call my-dir)
-
-include $(call first-makefiles-under,$(LOCAL_PATH))
diff --git a/linker/tests/linked_list_test.cpp b/linker/linked_list_test.cpp
similarity index 99%
rename from linker/tests/linked_list_test.cpp
rename to linker/linked_list_test.cpp
index 2b88ed0..71a7d09 100644
--- a/linker/tests/linked_list_test.cpp
+++ b/linker/linked_list_test.cpp
@@ -32,7 +32,7 @@
 
 #include <gtest/gtest.h>
 
-#include "../linked_list.h"
+#include "linked_list.h"
 
 namespace {
 
diff --git a/linker/linker.cpp b/linker/linker.cpp
index ed84a46..61cd818 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -698,9 +698,9 @@
     return elf_reader.Read(realpath, fd_, file_offset_, file_size);
   }
 
-  bool load() {
+  bool load(address_space_params* address_space) {
     ElfReader& elf_reader = get_elf_reader();
-    if (!elf_reader.Load(extinfo_)) {
+    if (!elf_reader.Load(address_space)) {
       return false;
     }
 
@@ -1697,10 +1697,34 @@
       load_list.push_back(task);
     }
   }
-  shuffle(&load_list);
+  bool reserved_address_recursive = false;
+  if (extinfo) {
+    reserved_address_recursive = extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
+  }
+  if (!reserved_address_recursive) {
+    // Shuffle the load order in the normal case, but not if we are loading all
+    // the libraries to a reserved address range.
+    shuffle(&load_list);
+  }
+
+  // Set up address space parameters.
+  address_space_params extinfo_params, default_params;
+  size_t relro_fd_offset = 0;
+  if (extinfo) {
+    if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS) {
+      extinfo_params.start_addr = extinfo->reserved_addr;
+      extinfo_params.reserved_size = extinfo->reserved_size;
+      extinfo_params.must_use_address = true;
+    } else if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_HINT) {
+      extinfo_params.start_addr = extinfo->reserved_addr;
+      extinfo_params.reserved_size = extinfo->reserved_size;
+    }
+  }
 
   for (auto&& task : load_list) {
-    if (!task->load()) {
+    address_space_params* address_space =
+        (reserved_address_recursive || !task->is_dt_needed()) ? &extinfo_params : &default_params;
+    if (!task->load(address_space)) {
       return false;
     }
   }
@@ -1810,7 +1834,7 @@
       // we should avoid linking them (because if they are not linked -> they
       // are in the local_group_roots and will be linked later).
       if (!si->is_linked() && si->get_primary_namespace() == local_group_ns) {
-        if (!si->link_image(global_group, local_group, extinfo) ||
+        if (!si->link_image(global_group, local_group, extinfo, &relro_fd_offset) ||
             !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
           return false;
         }
@@ -2171,9 +2195,13 @@
            new_name);
     // Some APEXs could be optionally disabled. Only translate the path
     // when the old file is absent and the new file exists.
+    // TODO(b/124218500): Re-enable it once app compat issue is resolved
+    /*
     if (file_exists(name)) {
       LD_LOG(kLogDlopen, "dlopen %s exists, not translating", name);
-    } else if (!file_exists(new_name)) {
+    } else
+    */
+    if (!file_exists(new_name)) {
       LD_LOG(kLogDlopen, "dlopen %s does not exist, not translating",
              new_name);
     } else {
@@ -3781,7 +3809,7 @@
 }
 
 bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
-                        const android_dlextinfo* extinfo) {
+                        const android_dlextinfo* extinfo, size_t* relro_fd_offset) {
   if (is_image_linked()) {
     // already linked.
     return true;
@@ -3928,7 +3956,7 @@
     }
   } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
     if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
-                                 extinfo->relro_fd) < 0) {
+                                 extinfo->relro_fd, relro_fd_offset) < 0) {
       DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
              get_realpath(), strerror(errno));
       return false;
diff --git a/linker/linker.h b/linker/linker.h
index 964c266..4c89ceb 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -188,3 +188,9 @@
 void decrement_dso_handle_reference_counter(void* dso_handle);
 
 void purge_unused_memory();
+
+struct address_space_params {
+  void* start_addr = nullptr;
+  size_t reserved_size = 0;
+  bool must_use_address = false;
+};
diff --git a/linker/tests/linker_block_allocator_test.cpp b/linker/linker_block_allocator_test.cpp
similarity index 98%
rename from linker/tests/linker_block_allocator_test.cpp
rename to linker/linker_block_allocator_test.cpp
index d5eb97c..359eefb 100644
--- a/linker/tests/linker_block_allocator_test.cpp
+++ b/linker/linker_block_allocator_test.cpp
@@ -32,7 +32,7 @@
 
 #include <gtest/gtest.h>
 
-#include "../linker_block_allocator.h"
+#include "linker_block_allocator.h"
 
 #include <unistd.h>
 
@@ -145,4 +145,3 @@
   testing::FLAGS_gtest_death_test_style = "threadsafe";
   ASSERT_EXIT(protect_all(), testing::KilledBySignal(SIGSEGV), "trying to access protected page");
 }
-
diff --git a/linker/tests/linker_config_test.cpp b/linker/linker_config_test.cpp
similarity index 99%
rename from linker/tests/linker_config_test.cpp
rename to linker/linker_config_test.cpp
index 14fd132..6a55bb2 100644
--- a/linker/tests/linker_config_test.cpp
+++ b/linker/linker_config_test.cpp
@@ -32,8 +32,8 @@
 
 #include <gtest/gtest.h>
 
-#include "../linker_config.h"
-#include "../linker_utils.h"
+#include "linker_config.h"
+#include "linker_utils.h"
 
 #include <unistd.h>
 
diff --git a/linker/linker_debug.h b/linker/linker_debug.h
index 862ea12..7a1cb3c 100644
--- a/linker/linker_debug.h
+++ b/linker/linker_debug.h
@@ -56,6 +56,7 @@
 #include <unistd.h>
 
 #include <async_safe/log.h>
+#include <async_safe/CHECK.h>
 
 __LIBC_HIDDEN__ extern int g_ld_debug_verbosity;
 
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 9129a52..9de7f51 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -165,7 +165,7 @@
   si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
 
   si->prelink_image();
-  si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr);
+  si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr, nullptr);
   // prevents accidental unloads...
   si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_NODELETE);
   si->set_linked();
@@ -281,7 +281,8 @@
   if (!elf_reader.Read(result.path.c_str(), fd.get(), file_offset, result.file_stat.st_size)) {
     __linker_error("error: %s\n", linker_get_error_buffer());
   }
-  if (!elf_reader.Load(nullptr)) {
+  address_space_params address_space;
+  if (!elf_reader.Load(&address_space)) {
     __linker_error("error: %s\n", linker_get_error_buffer());
   }
 
@@ -448,7 +449,7 @@
                       &namespaces)) {
     __linker_cannot_link(g_argv[0]);
   } else if (needed_libraries_count == 0) {
-    if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr)) {
+    if (!si->link_image(g_empty_list, soinfo_list_t::make_list(si), nullptr, nullptr)) {
       __linker_cannot_link(g_argv[0]);
     }
     si->increment_ref_count();
@@ -623,7 +624,7 @@
   // itself without having to look into local_group and (2) allocators
   // are not yet initialized, and therefore we cannot use linked_list.push_*
   // functions at this point.
-  if (!tmp_linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link(args.argv[0]);
+  if (!tmp_linker_so.link_image(g_empty_list, g_empty_list, nullptr, nullptr)) __linker_cannot_link(args.argv[0]);
 
   return __linker_init_post_relocation(args, tmp_linker_so);
 }
diff --git a/linker/linker_phdr.cpp b/linker/linker_phdr.cpp
index 34ac606..1aaa17f 100644
--- a/linker/linker_phdr.cpp
+++ b/linker/linker_phdr.cpp
@@ -166,14 +166,12 @@
   return did_read_;
 }
 
-bool ElfReader::Load(const android_dlextinfo* extinfo) {
+bool ElfReader::Load(address_space_params* address_space) {
   CHECK(did_read_);
   if (did_load_) {
     return true;
   }
-  if (ReserveAddressSpace(extinfo) &&
-      LoadSegments() &&
-      FindPhdr()) {
+  if (ReserveAddressSpace(address_space) && LoadSegments() && FindPhdr()) {
     did_load_ = true;
   }
 
@@ -559,7 +557,7 @@
 // Reserve a virtual address range big enough to hold all loadable
 // segments of a program header table. This is done by creating a
 // private anonymous mmap() with PROT_NONE.
-bool ElfReader::ReserveAddressSpace(const android_dlextinfo* extinfo) {
+bool ElfReader::ReserveAddressSpace(address_space_params* address_space) {
   ElfW(Addr) min_vaddr;
   load_size_ = phdr_table_get_load_size(phdr_table_, phdr_num_, &min_vaddr);
   if (load_size_ == 0) {
@@ -569,22 +567,11 @@
 
   uint8_t* addr = reinterpret_cast<uint8_t*>(min_vaddr);
   void* start;
-  size_t reserved_size = 0;
-  bool reserved_hint = true;
 
-  if (extinfo != nullptr) {
-    if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS) {
-      reserved_size = extinfo->reserved_size;
-      reserved_hint = false;
-    } else if (extinfo->flags & ANDROID_DLEXT_RESERVED_ADDRESS_HINT) {
-      reserved_size = extinfo->reserved_size;
-    }
-  }
-
-  if (load_size_ > reserved_size) {
-    if (!reserved_hint) {
+  if (load_size_ > address_space->reserved_size) {
+    if (address_space->must_use_address) {
       DL_ERR("reserved address space %zd smaller than %zd bytes needed for \"%s\"",
-             reserved_size - load_size_, load_size_, name_.c_str());
+             load_size_ - address_space->reserved_size, load_size_, name_.c_str());
       return false;
     }
     start = ReserveAligned(load_size_, kLibraryAlignment);
@@ -593,8 +580,12 @@
       return false;
     }
   } else {
-    start = extinfo->reserved_addr;
+    start = address_space->start_addr;
     mapped_by_caller_ = true;
+
+    // Update the reserved address space to subtract the space used by this library.
+    address_space->start_addr = reinterpret_cast<uint8_t*>(address_space->start_addr) + load_size_;
+    address_space->reserved_size -= load_size_;
   }
 
   load_start_ = start;
@@ -887,13 +878,15 @@
  *   phdr_count  -> number of entries in tables
  *   load_bias   -> load bias
  *   fd          -> readable file descriptor to use
+ *   file_offset -> pointer to offset into file descriptor to use/update
  * Return:
  *   0 on error, -1 on failure (error code in errno).
  */
 int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table,
                              size_t phdr_count,
                              ElfW(Addr) load_bias,
-                             int fd) {
+                             int fd,
+                             size_t* file_offset) {
   // Map the file at a temporary location so we can compare its contents.
   struct stat file_stat;
   if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
@@ -907,7 +900,6 @@
       return -1;
     }
   }
-  size_t file_offset = 0;
 
   // Iterate over the relro segments and compare/remap the pages.
   const ElfW(Phdr)* phdr = phdr_table;
@@ -921,12 +913,12 @@
     ElfW(Addr) seg_page_start = PAGE_START(phdr->p_vaddr) + load_bias;
     ElfW(Addr) seg_page_end   = PAGE_END(phdr->p_vaddr + phdr->p_memsz) + load_bias;
 
-    char* file_base = static_cast<char*>(temp_mapping) + file_offset;
+    char* file_base = static_cast<char*>(temp_mapping) + *file_offset;
     char* mem_base = reinterpret_cast<char*>(seg_page_start);
     size_t match_offset = 0;
     size_t size = seg_page_end - seg_page_start;
 
-    if (file_size - file_offset < size) {
+    if (file_size - *file_offset < size) {
       // File is too short to compare to this segment. The contents are likely
       // different as well (it's probably for a different library version) so
       // just don't bother checking.
@@ -950,7 +942,7 @@
       // Map over similar pages.
       if (mismatch_offset > match_offset) {
         void* map = mmap(mem_base + match_offset, mismatch_offset - match_offset,
-                         PROT_READ, MAP_PRIVATE|MAP_FIXED, fd, match_offset);
+                         PROT_READ, MAP_PRIVATE|MAP_FIXED, fd, *file_offset + match_offset);
         if (map == MAP_FAILED) {
           munmap(temp_mapping, file_size);
           return -1;
@@ -961,7 +953,7 @@
     }
 
     // Add to the base file offset in case there are multiple relro segments.
-    file_offset += size;
+    *file_offset += size;
   }
   munmap(temp_mapping, file_size);
   return 0;
diff --git a/linker/linker_phdr.h b/linker/linker_phdr.h
index 0eee089..9761bc8 100644
--- a/linker/linker_phdr.h
+++ b/linker/linker_phdr.h
@@ -43,7 +43,7 @@
   ElfReader();
 
   bool Read(const char* name, int fd, off64_t file_offset, off64_t file_size);
-  bool Load(const android_dlextinfo* extinfo);
+  bool Load(address_space_params* address_space);
 
   const char* name() const { return name_.c_str(); }
   size_t phdr_count() const { return phdr_num_; }
@@ -62,7 +62,7 @@
   bool ReadProgramHeaders();
   bool ReadSectionHeaders();
   bool ReadDynamicSection();
-  bool ReserveAddressSpace(const android_dlextinfo* extinfo);
+  bool ReserveAddressSpace(address_space_params* address_space);
   bool LoadSegments();
   bool FindPhdr();
   bool CheckPhdr(ElfW(Addr));
@@ -122,7 +122,7 @@
                                    ElfW(Addr) load_bias, int fd);
 
 int phdr_table_map_gnu_relro(const ElfW(Phdr)* phdr_table, size_t phdr_count,
-                             ElfW(Addr) load_bias, int fd);
+                             ElfW(Addr) load_bias, int fd, size_t* file_offset);
 
 #if defined(__arm__)
 int phdr_table_get_arm_exidx(const ElfW(Phdr)* phdr_table, size_t phdr_count, ElfW(Addr) load_bias,
diff --git a/linker/tests/linker_sleb128_test.cpp b/linker/linker_sleb128_test.cpp
similarity index 98%
rename from linker/tests/linker_sleb128_test.cpp
rename to linker/linker_sleb128_test.cpp
index 551faf2..9e819c6 100644
--- a/linker/tests/linker_sleb128_test.cpp
+++ b/linker/linker_sleb128_test.cpp
@@ -32,7 +32,7 @@
 
 #include <gtest/gtest.h>
 
-#include "../linker_sleb128.h"
+#include "linker_sleb128.h"
 
 TEST(linker_sleb128, smoke) {
   std::vector<uint8_t> encoding;
diff --git a/linker/linker_soinfo.h b/linker/linker_soinfo.h
index dd9c6aa..80c51af 100644
--- a/linker/linker_soinfo.h
+++ b/linker/linker_soinfo.h
@@ -223,7 +223,7 @@
   void call_pre_init_constructors();
   bool prelink_image();
   bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
-                  const android_dlextinfo* extinfo);
+                  const android_dlextinfo* extinfo, size_t* relro_fd_offset);
   bool protect_relro();
 
   void add_child(soinfo* child);
diff --git a/linker/tests/linker_globals.cpp b/linker/linker_test_globals.cpp
similarity index 100%
rename from linker/tests/linker_globals.cpp
rename to linker/linker_test_globals.cpp
diff --git a/linker/linker_tls.cpp b/linker/linker_tls.cpp
index a3aa9bf..d2edbb3 100644
--- a/linker/linker_tls.cpp
+++ b/linker/linker_tls.cpp
@@ -30,6 +30,7 @@
 
 #include <vector>
 
+#include "async_safe/CHECK.h"
 #include "private/ScopedRWLock.h"
 #include "private/ScopedSignalBlocker.h"
 #include "private/bionic_defs.h"
diff --git a/linker/tests/linker_utils_test.cpp b/linker/linker_utils_test.cpp
similarity index 99%
rename from linker/tests/linker_utils_test.cpp
rename to linker/linker_utils_test.cpp
index e406af5..44907da 100644
--- a/linker/tests/linker_utils_test.cpp
+++ b/linker/linker_utils_test.cpp
@@ -32,7 +32,7 @@
 
 #include <gtest/gtest.h>
 
-#include "../linker_utils.h"
+#include "linker_utils.h"
 
 TEST(linker_utils, format_string) {
   std::vector<std::pair<std::string, std::string>> params = {{ "LIB", "lib32"}, { "SDKVER", "42"}};
diff --git a/linker/tests/Android.mk b/linker/tests/Android.mk
deleted file mode 100644
index 63e0555..0000000
--- a/linker/tests/Android.mk
+++ /dev/null
@@ -1,54 +0,0 @@
-#
-# Copyright (C) 2012 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.
-#
-
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-LOCAL_MODULE := linker-unit-tests
-LOCAL_MODULE_STEM_32 := $(LOCAL_MODULE)32
-LOCAL_MODULE_STEM_64 := $(LOCAL_MODULE)64
-
-LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
-
-LOCAL_CFLAGS += -g -Wall -Wextra -Wunused -Werror
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../libc/
-
-LOCAL_SRC_FILES := \
-  linker_block_allocator_test.cpp \
-  linker_config_test.cpp \
-  linker_globals.cpp \
-  linked_list_test.cpp \
-  linker_sleb128_test.cpp \
-  linker_utils_test.cpp \
-  ../linker_block_allocator.cpp \
-  ../linker_config.cpp \
-  ../linker_utils.cpp \
-
-LOCAL_STATIC_LIBRARIES += libasync_safe libbase liblog
-
-include $(BUILD_NATIVE_TEST)
diff --git a/tests/Android.bp b/tests/Android.bp
index b039f00..75c6f9f 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -386,6 +386,7 @@
             ],
             static_libs: [
                 "libmeminfo",
+                "libprocinfo",
                 "libziparchive",
                 "libLLVMObject",
                 "libLLVMBitReader",
@@ -528,6 +529,7 @@
         "libdlext_test_different_soname",
         "libdlext_test_fd",
         "libdlext_test_norelro",
+        "libdlext_test_recursive",
         "libdlext_test_runpath_zip_zipaligned",
         "libdlext_test",
         "libdlext_test_zip",
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index aff5e37..cace36c 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -37,6 +37,7 @@
 #include <sys/wait.h>
 
 #include <meminfo/procmeminfo.h>
+#include <procinfo/process_map.h>
 #include <ziparchive/zip_archive.h>
 
 #include "gtest_globals.h"
@@ -59,6 +60,7 @@
 
 typedef int (*fn)(void);
 constexpr const char* kLibName = "libdlext_test.so";
+constexpr const char* kLibNameRecursive = "libdlext_test_recursive.so";
 constexpr const char* kLibNameNoRelro = "libdlext_test_norelro.so";
 constexpr const char* kLibZipSimpleZip = "libdir/libatest_simple_zip.so";
 constexpr auto kLibSize = 1024 * 1024; // how much address space to reserve for it
@@ -338,6 +340,48 @@
   EXPECT_EQ(nullptr, handle_);
 }
 
+TEST_F(DlExtTest, ReservedRecursive) {
+  void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  ASSERT_TRUE(start != MAP_FAILED);
+  android_dlextinfo extinfo;
+  extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
+  extinfo.reserved_addr = start;
+  extinfo.reserved_size = kLibSize;
+  handle_ = android_dlopen_ext(kLibNameRecursive, RTLD_NOW, &extinfo);
+  ASSERT_DL_NOTNULL(handle_);
+
+  fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
+  ASSERT_DL_NOTNULL(f);
+  EXPECT_GE(reinterpret_cast<void*>(f), start);
+  EXPECT_LT(reinterpret_cast<void*>(f),
+            reinterpret_cast<char*>(start) + kLibSize);
+  EXPECT_EQ(4, f());
+
+  f = reinterpret_cast<fn>(dlsym(handle_, "getBiggerRandomNumber"));
+  ASSERT_DL_NOTNULL(f);
+  EXPECT_GE(reinterpret_cast<void*>(f), start);
+  EXPECT_LT(reinterpret_cast<void*>(f),
+            reinterpret_cast<char*>(start) + kLibSize);
+  EXPECT_EQ(8, f());
+
+  uint32_t* taxicab_number = reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
+  ASSERT_DL_NOTNULL(taxicab_number);
+  EXPECT_GE(reinterpret_cast<void*>(taxicab_number), start);
+  EXPECT_LT(reinterpret_cast<void*>(taxicab_number), reinterpret_cast<char*>(start) + kLibSize);
+  EXPECT_EQ(1729U, *taxicab_number);
+}
+
+TEST_F(DlExtTest, ReservedRecursiveTooSmall) {
+  void* start = mmap(nullptr, kLibSize/2, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+  ASSERT_TRUE(start != MAP_FAILED);
+  android_dlextinfo extinfo;
+  extinfo.flags = ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
+  extinfo.reserved_addr = start;
+  extinfo.reserved_size = kLibSize/2;
+  handle_ = android_dlopen_ext(kLibNameRecursive, RTLD_NOW, &extinfo);
+  EXPECT_EQ(nullptr, handle_);
+}
+
 TEST_F(DlExtTest, ReservedHint) {
   void* start = mmap(nullptr, kLibSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
   ASSERT_TRUE(start != MAP_FAILED);
@@ -388,10 +432,14 @@
     DlExtTest::TearDown();
   }
 
-  void CreateRelroFile(const char* lib, const char* relro_file) {
+  void CreateRelroFile(const char* lib, const char* relro_file, bool recursive) {
     int relro_fd = open(relro_file, O_RDWR | O_TRUNC | O_CLOEXEC);
     ASSERT_NOERROR(relro_fd);
 
+    if (recursive) {
+      extinfo_.flags |= ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE;
+    }
+
     pid_t pid = fork();
     if (pid == 0) {
       // child process
@@ -417,13 +465,19 @@
     extinfo_.relro_fd = relro_fd;
   }
 
-  void TryUsingRelro(const char* lib) {
+  void TryUsingRelro(const char* lib, bool recursive) {
     handle_ = android_dlopen_ext(lib, RTLD_NOW, &extinfo_);
     ASSERT_DL_NOTNULL(handle_);
     fn f = reinterpret_cast<fn>(dlsym(handle_, "getRandomNumber"));
     ASSERT_DL_NOTNULL(f);
     EXPECT_EQ(4, f());
 
+    if (recursive) {
+      fn f = reinterpret_cast<fn>(dlsym(handle_, "getBiggerRandomNumber"));
+      ASSERT_DL_NOTNULL(f);
+      EXPECT_EQ(8, f());
+    }
+
     uint32_t* taxicab_number =
             reinterpret_cast<uint32_t*>(dlsym(handle_, "dlopen_testlib_taxicab_number"));
     ASSERT_DL_NOTNULL(taxicab_number);
@@ -433,6 +487,8 @@
   void SpawnChildrenAndMeasurePss(const char* lib, const char* relro_file, bool share_relro,
                                   size_t* pss_out);
 
+  std::string FindMappingName(void* ptr);
+
   android_dlextinfo extinfo_;
 };
 
@@ -440,8 +496,29 @@
   TemporaryFile tf; // Use tf to get an unique filename.
   ASSERT_NOERROR(close(tf.fd));
 
-  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.path));
-  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.path, false));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName, false));
+  void* relro_data = dlsym(handle_, "lots_of_relro");
+  ASSERT_DL_NOTNULL(relro_data);
+  EXPECT_EQ(tf.path, FindMappingName(relro_data));
+
+  // Use destructor of tf to close and unlink the file.
+  tf.fd = extinfo_.relro_fd;
+}
+
+TEST_F(DlExtRelroSharingTest, ChildWritesGoodDataRecursive) {
+  TemporaryFile tf; // Use tf to get an unique filename.
+  ASSERT_NOERROR(close(tf.fd));
+
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameRecursive, tf.path, true));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameRecursive, true));
+  void* relro_data = dlsym(handle_, "lots_of_relro");
+  ASSERT_DL_NOTNULL(relro_data);
+  EXPECT_EQ(tf.path, FindMappingName(relro_data));
+  void* recursive_relro_data = dlsym(handle_, "lots_more_relro");
+  ASSERT_DL_NOTNULL(recursive_relro_data);
+  EXPECT_EQ(tf.path, FindMappingName(recursive_relro_data));
+
 
   // Use destructor of tf to close and unlink the file.
   tf.fd = extinfo_.relro_fd;
@@ -451,15 +528,15 @@
   TemporaryFile tf; // // Use tf to get an unique filename.
   ASSERT_NOERROR(close(tf.fd));
 
-  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.path));
-  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro));
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibNameNoRelro, tf.path, false));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibNameNoRelro, false));
 
   // Use destructor of tf to close and unlink the file.
   tf.fd = extinfo_.relro_fd;
 }
 
 TEST_F(DlExtRelroSharingTest, RelroFileEmpty) {
-  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName));
+  ASSERT_NO_FATAL_FAILURE(TryUsingRelro(kLibName, false));
 }
 
 TEST_F(DlExtRelroSharingTest, VerifyMemorySaving) {
@@ -471,7 +548,7 @@
   TemporaryFile tf; // Use tf to get an unique filename.
   ASSERT_NOERROR(close(tf.fd));
 
-  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.path));
+  ASSERT_NO_FATAL_FAILURE(CreateRelroFile(kLibName, tf.path, false));
 
   int pipefd[2];
   ASSERT_NOERROR(pipe(pipefd));
@@ -586,6 +663,21 @@
   }
 }
 
+std::string DlExtRelroSharingTest::FindMappingName(void* ptr) {
+  uint64_t addr = reinterpret_cast<uint64_t>(ptr);
+  std::string found_name = "<not found>";
+
+  EXPECT_TRUE(android::procinfo::ReadMapFile(
+      "/proc/self/maps",
+      [&](uint64_t start, uint64_t end, uint16_t, uint16_t, ino_t, const char* name) {
+        if (addr >= start && addr < end) {
+          found_name = name;
+        }
+      }));
+
+  return found_name;
+}
+
 // Testing namespaces
 static const char* g_public_lib = "libnstest_public.so";
 
diff --git a/tests/grp_pwd_test.cpp b/tests/grp_pwd_test.cpp
index 4b207b6..eb8fe2a 100644
--- a/tests/grp_pwd_test.cpp
+++ b/tests/grp_pwd_test.cpp
@@ -34,6 +34,7 @@
 #include <private/android_filesystem_config.h>
 
 #if defined(__BIONIC__)
+#include <android/api-level.h>
 #include <android-base/properties.h>
 #endif
 
@@ -247,11 +248,9 @@
   expect_range(AID_SHARED_GID_START, AID_SHARED_GID_END);
   expect_range(AID_ISOLATED_START, AID_ISOLATED_END);
 
-  // Upgrading devices launched before API level 28 may not comply with the below check.
-  // Due to the difficulty in changing uids after launch, it is waived for these devices.
-  // Also grant this check for device launched with 28(P) to give the vendor time to
-  // adopt the AID scheme.
-  if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= 28) {
+  // TODO(73062966): We still don't have a good way to create vendor AIDs in the system or other
+  // non-vendor partitions, therefore we keep this check disabled.
+  if (android::base::GetIntProperty("ro.product.first_api_level", 0) <= __ANDROID_API_Q__) {
     return;
   }
 
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index 5bd028d..c40277d 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -49,6 +49,21 @@
 include $(LOCAL_PATH)/Android.build.testlib.mk
 
 # -----------------------------------------------------------------------------
+# Library used by dlext tests - recursive use of RELRO sharing
+# -----------------------------------------------------------------------------
+libdlext_test_recursive_src_files := \
+    dlext_test_recursive_library.cpp \
+
+libdlext_test_recursive_ldflags := \
+    -Wl,-z,relro \
+
+libdlext_test_recursive_shared_libraries := libdlext_test
+
+module := libdlext_test_recursive
+module_tag := optional
+include $(LOCAL_PATH)/Android.build.testlib.mk
+
+# -----------------------------------------------------------------------------
 # Library used by dlext tests - different name non-default location
 # -----------------------------------------------------------------------------
 module := libdlext_test_fd
diff --git a/tests/libs/dlext_test_recursive_library.cpp b/tests/libs/dlext_test_recursive_library.cpp
new file mode 100644
index 0000000..b37bd70
--- /dev/null
+++ b/tests/libs/dlext_test_recursive_library.cpp
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+extern "C" int getRandomNumber();
+
+class B {
+public:
+  virtual int getBiggerRandomNumber() {
+    // Call to the other library.
+    return getRandomNumber() * 2;
+  }
+
+  virtual ~B() {}
+};
+
+B b;
+
+// nested macros to make it easy to define a large amount of read-only data
+// which will require relocation.
+#define B_16 &b, &b, &b, &b, &b, &b, &b, &b, &b, &b, &b, &b, &b, &b, &b, &b,
+#define B_128 B_16 B_16 B_16 B_16 B_16 B_16 B_16 B_16
+#define B_1024 B_128 B_128 B_128 B_128 B_128 B_128 B_128 B_128
+
+extern "C" B* const lots_more_relro[] = {
+  B_1024 B_1024 B_1024 B_1024 B_1024 B_1024 B_1024 B_1024
+};
+
+extern "C" int getBiggerRandomNumber() {
+  // access the relro section (twice, in fact, once for the pointer, and once
+  // for the vtable of B) to check it's actually there.
+  return lots_more_relro[0]->getBiggerRandomNumber();
+}
diff --git a/tests/malloc_iterate_test.cpp b/tests/malloc_iterate_test.cpp
index 5e60a6d..76583eb 100644
--- a/tests/malloc_iterate_test.cpp
+++ b/tests/malloc_iterate_test.cpp
@@ -92,14 +92,15 @@
   test_data->total_allocated_bytes = 0;
 
   // Find all of the maps that are [anon:libc_malloc].
-  ASSERT_TRUE(android::procinfo::ReadMapFile("/proc/self/maps",
-    [&](uint64_t start, uint64_t end, uint16_t, uint64_t, const char* name) {
-    if (std::string(name) == "[anon:libc_malloc]") {
-      malloc_disable();
-      malloc_iterate(start, end - start, SavePointers, test_data);
-      malloc_enable();
-    }
-  }));
+  ASSERT_TRUE(android::procinfo::ReadMapFile(
+      "/proc/self/maps",
+      [&](uint64_t start, uint64_t end, uint16_t, uint64_t, ino_t, const char* name) {
+        if (std::string(name) == "[anon:libc_malloc]") {
+          malloc_disable();
+          malloc_iterate(start, end - start, SavePointers, test_data);
+          malloc_enable();
+        }
+      }));
 
   for (size_t i = 0; i < test_data->allocs.size(); i++) {
     EXPECT_EQ(1UL, test_data->allocs[i].count) << "Failed on size " << test_data->allocs[i].size;
@@ -180,14 +181,15 @@
   TestDataType test_data = {};
 
   // Find all of the maps that are not [anon:libc_malloc].
-  ASSERT_TRUE(android::procinfo::ReadMapFile("/proc/self/maps",
-    [&](uint64_t start, uint64_t end, uint16_t, uint64_t, const char* name) {
-    if (std::string(name) != "[anon:libc_malloc]") {
-      malloc_disable();
-      malloc_iterate(start, end - start, SavePointers, &test_data);
-      malloc_enable();
-    }
-  }));
+  ASSERT_TRUE(android::procinfo::ReadMapFile(
+      "/proc/self/maps",
+      [&](uint64_t start, uint64_t end, uint16_t, uint64_t, ino_t, const char* name) {
+        if (std::string(name) != "[anon:libc_malloc]") {
+          malloc_disable();
+          malloc_iterate(start, end - start, SavePointers, &test_data);
+          malloc_enable();
+        }
+      }));
 
   ASSERT_EQ(0UL, test_data.total_allocated_bytes);
 #else
diff --git a/tools/Android.mk b/tools/Android.mk
deleted file mode 100644
index 4dd66fe..0000000
--- a/tools/Android.mk
+++ /dev/null
@@ -1,19 +0,0 @@
-#
-# Copyright (C) 2015 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.
-#
-
-LOCAL_PATH := $(call my-dir)
-
-include $(call all-subdir-makefiles)