Revert "Write dex files to oat file early."

This reverts commit 625a64aad13905d8a2454bf3cc0e874487b110d5.

Breaks the Mac build:

Undefined symbols for architecture i386:
  "_CloseArchive", referenced from:
      ... in oat_writer.o
ld: symbol(s) not found for architecture i386

Change-Id: I21608bc51437834e1e6abde9bcbe5e7d9998197e
diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc
index 9b93c13..bc8ba97 100644
--- a/runtime/dex_file.cc
+++ b/runtime/dex_file.cc
@@ -687,8 +687,8 @@
   return nullptr;
 }
 
-void DexFile::CreateTypeLookupTable(uint8_t* storage) const {
-  lookup_table_.reset(TypeLookupTable::Create(*this, storage));
+void DexFile::CreateTypeLookupTable() const {
+  lookup_table_.reset(TypeLookupTable::Create(*this));
 }
 
 // Given a signature place the type ids into the given vector
diff --git a/runtime/dex_file.h b/runtime/dex_file.h
index facb6fd..8a3db6c 100644
--- a/runtime/dex_file.h
+++ b/runtime/dex_file.h
@@ -1157,7 +1157,7 @@
     return lookup_table_.get();
   }
 
-  void CreateTypeLookupTable(uint8_t* storage = nullptr) const;
+  void CreateTypeLookupTable() const;
 
  private:
   // Opens a .dex file
diff --git a/runtime/mem_map.cc b/runtime/mem_map.cc
index 18c52e4..3571edb 100644
--- a/runtime/mem_map.cc
+++ b/runtime/mem_map.cc
@@ -583,10 +583,6 @@
   }
 }
 
-bool MemMap::Sync() {
-  return msync(BaseBegin(), BaseSize(), MS_SYNC) == 0;
-}
-
 bool MemMap::Protect(int prot) {
   if (base_begin_ == nullptr && base_size_ == 0) {
     prot_ = prot;
diff --git a/runtime/mem_map.h b/runtime/mem_map.h
index ebd550a..ed21365 100644
--- a/runtime/mem_map.h
+++ b/runtime/mem_map.h
@@ -126,8 +126,6 @@
     return name_;
   }
 
-  bool Sync();
-
   bool Protect(int prot);
 
   void MadviseDontNeedAndZero();
diff --git a/runtime/oat.h b/runtime/oat.h
index 989e3f9..13fd6a4 100644
--- a/runtime/oat.h
+++ b/runtime/oat.h
@@ -31,7 +31,7 @@
 class PACKED(4) OatHeader {
  public:
   static constexpr uint8_t kOatMagic[] = { 'o', 'a', 't', '\n' };
-  static constexpr uint8_t kOatVersion[] = { '0', '7', '5', '\0' };
+  static constexpr uint8_t kOatVersion[] = { '0', '7', '4', '\0' };
 
   static constexpr const char* kImageLocationKey = "image-location";
   static constexpr const char* kDex2OatCmdLineKey = "dex2oat-cmdline";
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 3babcf7..83e594b 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -46,7 +46,6 @@
 #include "oat_file_manager.h"
 #include "os.h"
 #include "runtime.h"
-#include "type_lookup_table.h"
 #include "utils.h"
 #include "utils/dex_cache_arrays_layout-inl.h"
 #include "vmap_table.h"
@@ -267,15 +266,16 @@
                                 i);
       return false;
     }
-    if (UNLIKELY(static_cast<size_t>(End() - oat) < dex_file_location_size)) {
+
+    const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
+    oat += dex_file_location_size;
+    if (UNLIKELY(oat > End())) {
       *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu with truncated dex file "
                                     "location",
                                 GetLocation().c_str(),
                                 i);
       return false;
     }
-    const char* dex_file_location_data = reinterpret_cast<const char*>(oat);
-    oat += dex_file_location_size;
 
     std::string dex_file_location = ResolveRelativeEncodedDexLocation(
         abs_dex_location,
@@ -318,17 +318,6 @@
                                 Size());
       return false;
     }
-    if (UNLIKELY(Size() - dex_file_offset < sizeof(DexFile::Header))) {
-      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
-                                    "offset %u of %zu but the size of dex file header is %zu",
-                                GetLocation().c_str(),
-                                i,
-                                dex_file_location.c_str(),
-                                dex_file_offset,
-                                Size(),
-                                sizeof(DexFile::Header));
-      return false;
-    }
 
     const uint8_t* dex_file_pointer = Begin() + dex_file_offset;
     if (UNLIKELY(!DexFile::IsMagicValid(dex_file_pointer))) {
@@ -350,75 +339,34 @@
       return false;
     }
     const DexFile::Header* header = reinterpret_cast<const DexFile::Header*>(dex_file_pointer);
-    if (Size() - dex_file_offset < header->file_size_) {
-      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with dex file "
-                                    "offset %u and size %u truncated at %zu",
-                                GetLocation().c_str(),
-                                i,
-                                dex_file_location.c_str(),
-                                dex_file_offset,
-                                header->file_size_,
-                                Size());
-      return false;
-    }
 
-    uint32_t class_offsets_offset;
-    if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &class_offsets_offset))) {
-      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' truncated "
-                                    "after class offsets offset",
-                                GetLocation().c_str(),
-                                i,
+    if (UNLIKELY(oat > End())) {
+      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
+                                "lookup table offset", GetLocation().c_str(), i,
                                 dex_file_location.c_str());
       return false;
     }
-    if (UNLIKELY(class_offsets_offset > Size()) ||
-        UNLIKELY((Size() - class_offsets_offset) / sizeof(uint32_t) < header->class_defs_size_)) {
-      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
-                                    "class offsets, offset %u of %zu, class defs %u",
-                                GetLocation().c_str(),
-                                i,
-                                dex_file_location.c_str(),
-                                class_offsets_offset,
-                                Size(),
-                                header->class_defs_size_);
-      return false;
-    }
-    if (UNLIKELY(!IsAligned<alignof(uint32_t)>(class_offsets_offset))) {
-      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with unaligned "
-                                    "class offsets, offset %u",
-                                GetLocation().c_str(),
-                                i,
-                                dex_file_location.c_str(),
-                                class_offsets_offset);
-      return false;
-    }
-    const uint32_t* class_offsets_pointer =
-        reinterpret_cast<const uint32_t*>(Begin() + class_offsets_offset);
-
-    uint32_t lookup_table_offset;
-    if (UNLIKELY(!ReadOatDexFileData(*this, &oat, &lookup_table_offset))) {
-      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' truncated "
-                                    "after lookup table offset",
-                                GetLocation().c_str(),
-                                i,
+    uint32_t lookup_table_offset = *reinterpret_cast<const uint32_t*>(oat);
+    oat += sizeof(lookup_table_offset);
+    if (Begin() + lookup_table_offset > End()) {
+      *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zd for '%s' with truncated "
+                                "lookup table", GetLocation().c_str(), i,
                                 dex_file_location.c_str());
       return false;
     }
     const uint8_t* lookup_table_data = lookup_table_offset != 0u
         ? Begin() + lookup_table_offset
         : nullptr;
-    if (lookup_table_offset != 0u &&
-        (UNLIKELY(lookup_table_offset > Size()) ||
-            UNLIKELY(Size() - lookup_table_offset <
-                     TypeLookupTable::RawDataLength(header->class_defs_size_)))) {
+
+    const uint32_t* methods_offsets_pointer = reinterpret_cast<const uint32_t*>(oat);
+
+    oat += (sizeof(*methods_offsets_pointer) * header->class_defs_size_);
+    if (UNLIKELY(oat > End())) {
       *error_msg = StringPrintf("In oat file '%s' found OatDexFile #%zu for '%s' with truncated "
-                                    "type lookup table, offset %u of %zu, class defs %u",
+                                    "method offsets",
                                 GetLocation().c_str(),
                                 i,
-                                dex_file_location.c_str(),
-                                lookup_table_offset,
-                                Size(),
-                                header->class_defs_size_);
+                                dex_file_location.c_str());
       return false;
     }
 
@@ -450,7 +398,7 @@
                                               dex_file_checksum,
                                               dex_file_pointer,
                                               lookup_table_data,
-                                              class_offsets_pointer,
+                                              methods_offsets_pointer,
                                               current_dex_cache_arrays);
     oat_dex_files_storage_.push_back(oat_dex_file);
 
diff --git a/runtime/parsed_options.cc b/runtime/parsed_options.cc
index 341be9a..2b92303 100644
--- a/runtime/parsed_options.cc
+++ b/runtime/parsed_options.cc
@@ -309,8 +309,8 @@
     const std::string option(options[i].first);
       // TODO: support -Djava.class.path
     if (option == "bootclasspath") {
-      auto boot_class_path = static_cast<std::vector<std::unique_ptr<const DexFile>>*>(
-          const_cast<void*>(options[i].second));
+      auto boot_class_path
+          = reinterpret_cast<const std::vector<const DexFile*>*>(options[i].second);
 
       if (runtime_options != nullptr) {
         runtime_options->Set(M::BootClassPathDexList, boot_class_path);
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 5b075b7..c4694ee 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1131,14 +1131,10 @@
     }
 
     std::vector<std::unique_ptr<const DexFile>> boot_class_path;
-    if (runtime_options.Exists(Opt::BootClassPathDexList)) {
-      boot_class_path.swap(*runtime_options.GetOrDefault(Opt::BootClassPathDexList));
-    } else {
-      OpenDexFiles(dex_filenames,
-                   dex_locations,
-                   runtime_options.GetOrDefault(Opt::Image),
-                   &boot_class_path);
-    }
+    OpenDexFiles(dex_filenames,
+                 dex_locations,
+                 runtime_options.GetOrDefault(Opt::Image),
+                 &boot_class_path);
     instruction_set_ = runtime_options.GetOrDefault(Opt::ImageInstructionSet);
     std::string error_msg;
     if (!class_linker_->InitWithoutImage(std::move(boot_class_path), &error_msg)) {
diff --git a/runtime/runtime_options.cc b/runtime/runtime_options.cc
index e75481c..c54461e 100644
--- a/runtime/runtime_options.cc
+++ b/runtime/runtime_options.cc
@@ -13,11 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 #include "runtime_options.h"
 
-#include <memory>
-
 #include "gc/heap.h"
 #include "monitor.h"
 #include "runtime.h"
diff --git a/runtime/runtime_options.def b/runtime/runtime_options.def
index c5b009d..5624285 100644
--- a/runtime/runtime_options.def
+++ b/runtime/runtime_options.def
@@ -117,8 +117,8 @@
 
 // Not parse-able from command line, but can be provided explicitly.
 // (Do not add anything here that is defined in ParsedOptions::MakeParser)
-RUNTIME_OPTIONS_KEY (std::vector<std::unique_ptr<const DexFile>>*, \
-                                          BootClassPathDexList)
+RUNTIME_OPTIONS_KEY (const std::vector<const DexFile*>*, \
+                                          BootClassPathDexList)  // TODO: make unique_ptr
 RUNTIME_OPTIONS_KEY (InstructionSet,      ImageInstructionSet,            kRuntimeISA)
 RUNTIME_OPTIONS_KEY (CompilerCallbacks*,  CompilerCallbacksPtr)  // TDOO: make unique_ptr
 RUNTIME_OPTIONS_KEY (bool (*)(),          HookIsSensitiveThread)
diff --git a/runtime/type_lookup_table.cc b/runtime/type_lookup_table.cc
index fc9faec..0d40bb7 100644
--- a/runtime/type_lookup_table.cc
+++ b/runtime/type_lookup_table.cc
@@ -16,7 +16,6 @@
 
 #include "type_lookup_table.h"
 
-#include "base/bit_utils.h"
 #include "dex_file-inl.h"
 #include "utf-inl.h"
 #include "utils.h"
@@ -43,39 +42,25 @@
 }
 
 uint32_t TypeLookupTable::RawDataLength(const DexFile& dex_file) {
-  return RawDataLength(dex_file.NumClassDefs());
+  return RoundUpToPowerOfTwo(dex_file.NumClassDefs()) * sizeof(Entry);
 }
 
-uint32_t TypeLookupTable::RawDataLength(uint32_t num_class_defs) {
-  return SupportedSize(num_class_defs) ? RoundUpToPowerOfTwo(num_class_defs) * sizeof(Entry) : 0u;
-}
-
-uint32_t TypeLookupTable::CalculateMask(uint32_t num_class_defs) {
-  return SupportedSize(num_class_defs) ? RoundUpToPowerOfTwo(num_class_defs) - 1u : 0u;
-}
-
-bool TypeLookupTable::SupportedSize(uint32_t num_class_defs) {
-  return num_class_defs != 0u && num_class_defs <= std::numeric_limits<uint16_t>::max();
-}
-
-TypeLookupTable* TypeLookupTable::Create(const DexFile& dex_file, uint8_t* storage) {
+TypeLookupTable* TypeLookupTable::Create(const DexFile& dex_file) {
   const uint32_t num_class_defs = dex_file.NumClassDefs();
-  return SupportedSize(num_class_defs)
-      ? new TypeLookupTable(dex_file, storage)
-      : nullptr;
+  return (num_class_defs == 0 || num_class_defs > std::numeric_limits<uint16_t>::max())
+      ? nullptr
+      : new TypeLookupTable(dex_file);
 }
 
 TypeLookupTable* TypeLookupTable::Open(const uint8_t* raw_data, const DexFile& dex_file) {
   return new TypeLookupTable(raw_data, dex_file);
 }
 
-TypeLookupTable::TypeLookupTable(const DexFile& dex_file, uint8_t* storage)
+TypeLookupTable::TypeLookupTable(const DexFile& dex_file)
     : dex_file_(dex_file),
-      mask_(CalculateMask(dex_file.NumClassDefs())),
-      entries_(storage != nullptr ? reinterpret_cast<Entry*>(storage) : new Entry[mask_ + 1]),
-      owns_entries_(storage == nullptr) {
-  static_assert(alignof(Entry) == 4u, "Expecting Entry to be 4-byte aligned.");
-  DCHECK_ALIGNED(storage, alignof(Entry));
+      mask_(RoundUpToPowerOfTwo(dex_file.NumClassDefs()) - 1),
+      entries_(new Entry[mask_ + 1]),
+      owns_entries_(true) {
   std::vector<uint16_t> conflict_class_defs;
   // The first stage. Put elements on their initial positions. If an initial position is already
   // occupied then delay the insertion of the element to the second stage to reduce probing
@@ -108,7 +93,7 @@
 
 TypeLookupTable::TypeLookupTable(const uint8_t* raw_data, const DexFile& dex_file)
     : dex_file_(dex_file),
-      mask_(CalculateMask(dex_file.NumClassDefs())),
+      mask_(RoundUpToPowerOfTwo(dex_file.NumClassDefs()) - 1),
       entries_(reinterpret_cast<Entry*>(const_cast<uint8_t*>(raw_data))),
       owns_entries_(false) {}
 
diff --git a/runtime/type_lookup_table.h b/runtime/type_lookup_table.h
index d74d01d..3c2295c 100644
--- a/runtime/type_lookup_table.h
+++ b/runtime/type_lookup_table.h
@@ -60,7 +60,7 @@
   }
 
   // Method creates lookup table for dex file
-  static TypeLookupTable* Create(const DexFile& dex_file, uint8_t* storage = nullptr);
+  static TypeLookupTable* Create(const DexFile& dex_file);
 
   // Method opens lookup table from binary data. Lookup table does not owns binary data.
   static TypeLookupTable* Open(const uint8_t* raw_data, const DexFile& dex_file);
@@ -76,9 +76,6 @@
   // Method returns length of binary data for the specified dex file.
   static uint32_t RawDataLength(const DexFile& dex_file);
 
-  // Method returns length of binary data for the specified number of class definitions.
-  static uint32_t RawDataLength(uint32_t num_class_defs);
-
  private:
    /**
     * To find element we need to compare strings.
@@ -112,11 +109,8 @@
     }
   };
 
-  static uint32_t CalculateMask(uint32_t num_class_defs);
-  static bool SupportedSize(uint32_t num_class_defs);
-
   // Construct from a dex file.
-  explicit TypeLookupTable(const DexFile& dex_file, uint8_t* storage);
+  explicit TypeLookupTable(const DexFile& dex_file);
 
   // Construct from a dex file with existing data.
   TypeLookupTable(const uint8_t* raw_data, const DexFile& dex_file);