Merge "Make static ifunc resolvers optional."
diff --git a/libc/Android.bp b/libc/Android.bp
index 262d951..489918b 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -85,6 +85,8 @@
             no_libcrt: true,
         },
     },
+    // TODO(ivanlozano): Remove after b/118321713
+    xom: false,
 }
 
 // ========================================================
diff --git a/libc/async_safe/Android.bp b/libc/async_safe/Android.bp
index 6ae1c42..fbaaad1 100644
--- a/libc/async_safe/Android.bp
+++ b/libc/async_safe/Android.bp
@@ -22,6 +22,7 @@
 cc_library_headers {
     name: "libasync_safe_headers",
     recovery_available: true,
+    defaults: ["linux_bionic_supported"],
 
     export_include_dirs: ["include"],
 
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/linker/linker.cpp b/linker/linker.cpp
index 8a70ca9..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;
         }
@@ -3785,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;
@@ -3932,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/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/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/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();
+}