linker: allow using reserved space for multiple libraries.

Introduce a new flag ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE which
instructs the linker to use the reserved address space to load all of
the newly-loaded libraries required by a dlopen() call instead of only
the main library. They will be loaded consecutively into that region if
they fit. The RELRO sections of all the loaded libraries will also be
considered for reading/writing shared RELRO data.

This will allow the WebView implementation to potentially consist of
more than one .so file while still benefiting from the RELRO sharing
optimisation, which would otherwise only apply to the "root" .so file.

Test: bionic-unit-tests (existing and newly added)
Bug: 110790153
Change-Id: I61da775c29fd5017d9a1e2b6b3757c3d20a355b3
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;