Use mmapped boot image intern table for PIC app HLoadString.

Implement new HLoadString load kind for boot image strings
referenced by PIC-compiled apps (i.e. prebuilts) that uses
PC-relative load from a boot image InternTable mmapped into
the apps .bss. This reduces the size of the PIC prebuilts
that reference boot image strings compared to the kBssEntry
as we can completely avoid the slow path and stack map.

We separate the InternedStrings and ClassTable sections of
the boot image (.art) file from the rest, aligning the
start of the InternedStrings section to a page boundary.
This may actually increase the size of the boot image file
by a page but it also allows mprotecting() these tables as
read-only. The ClassTable section is included in
anticipation of a similar load kind for HLoadClass.

Prebuilt services.odex for aosp_angler-userdebug (arm64):
  - before: 20862776
  - after: 20308512 (-541KiB)
Note that 92KiB savings could have been achieved by simply
avoiding the read barrier, similar to the HLoadClass flag
IsInBootImage(). Such flag is now unnecessary.

Test: m test-art-host-gtest
Test: testrunner.py --host
Test: testrunner.py --host --pictest
Test: testrunner.py --target on Nexus 6P.
Test: testrunner.py --target --pictest on Nexus 6P.
Test: Nexus 6P boots.
Bug: 31951624
Change-Id: I5f2bf1fc0bb36a8483244317cfdfa69e192ef6c5
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 200681e..6515cfa 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -44,6 +44,7 @@
 #include "elf_file.h"
 #include "elf_utils.h"
 #include "gc_root.h"
+#include "gc/space/image_space.h"
 #include "mem_map.h"
 #include "mirror/class.h"
 #include "mirror/object-inl.h"
@@ -278,6 +279,37 @@
   return true;
 }
 
+static inline bool MapConstantTables(const gc::space::ImageSpace* space,
+                                     uint8_t* address) {
+  // If MREMAP_DUP is ever merged to Linux kernel, use it to avoid the unnecessary open()/close().
+  // Note: The current approach relies on the filename still referencing the same inode.
+
+  File file(space->GetImageFilename(), O_RDONLY, /* checkUsage */ false);
+  if (!file.IsOpened()) {
+    LOG(ERROR) << "Failed to open boot image file " << space->GetImageFilename();
+    return false;
+  }
+
+  uint32_t offset = space->GetImageHeader().GetBootImageConstantTablesOffset();
+  uint32_t size = space->GetImageHeader().GetBootImageConstantTablesSize();
+  std::string error_msg;
+  std::unique_ptr<MemMap> mem_map(MemMap::MapFileAtAddress(address,
+                                                           size,
+                                                           PROT_READ,
+                                                           MAP_PRIVATE,
+                                                           file.Fd(),
+                                                           offset,
+                                                           /* low_4gb */ false,
+                                                           /* reuse */ true,
+                                                           file.GetPath().c_str(),
+                                                           &error_msg));
+  if (mem_map == nullptr) {
+    LOG(ERROR) << "Failed to mmap boot image tables from file " << space->GetImageFilename();
+    return false;
+  }
+  return true;
+}
+
 bool OatFileBase::Setup(const char* abs_dex_location, std::string* error_msg) {
   if (!GetOatHeader().IsValid()) {
     std::string cause = GetOatHeader().GetValidationErrorMessage();
@@ -339,15 +371,12 @@
     return false;
   }
 
-  if (bss_methods_ != nullptr && bss_methods_ != bss_begin_) {
-    *error_msg = StringPrintf("In oat file '%s' found unexpected .bss gap before 'oatbssmethods': "
-                                  "begin = %p, methods = %p",
-                              GetLocation().c_str(),
-                              bss_begin_,
-                              bss_methods_);
-    return false;
-  }
-
+  uint8_t* after_tables =
+      (bss_methods_ != nullptr) ? bss_methods_ : bss_roots_;  // May be null.
+  uint8_t* boot_image_tables = (bss_begin_ == after_tables) ? nullptr : bss_begin_;
+  uint8_t* boot_image_tables_end =
+      (bss_begin_ == after_tables) ? nullptr : (after_tables != nullptr) ? after_tables : bss_end_;
+  DCHECK_EQ(boot_image_tables != nullptr, boot_image_tables_end != nullptr);
   uint32_t dex_file_count = GetOatHeader().GetDexFileCount();
   oat_dex_files_storage_.reserve(dex_file_count);
   for (size_t i = 0; i < dex_file_count; i++) {
@@ -605,6 +634,31 @@
     }
   }
 
+  if (boot_image_tables != nullptr) {
+    // Map boot image tables into the .bss. The reserved size must match size of the tables.
+    size_t reserved_size = static_cast<size_t>(boot_image_tables_end - boot_image_tables);
+    size_t tables_size = 0u;
+    for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
+      tables_size += space->GetImageHeader().GetBootImageConstantTablesSize();
+      DCHECK_ALIGNED(tables_size, kPageSize);
+    }
+    if (tables_size != reserved_size) {
+      *error_msg = StringPrintf("In oat file '%s' found unexpected boot image table sizes, "
+                                    " %zu bytes, should be %zu.",
+                                GetLocation().c_str(),
+                                reserved_size,
+                                tables_size);
+      return false;
+    }
+    for (gc::space::ImageSpace* space : Runtime::Current()->GetHeap()->GetBootImageSpaces()) {
+      uint32_t current_tables_size = space->GetImageHeader().GetBootImageConstantTablesSize();
+      if (current_tables_size != 0u && !MapConstantTables(space, boot_image_tables)) {
+        return false;
+      }
+      boot_image_tables += current_tables_size;
+    }
+    DCHECK(boot_image_tables == boot_image_tables_end);
+  }
   return true;
 }