Move art to use dalvik names for .oat files

Change-Id: Ia383c0a0847684a513cab7db44b4ecf8fe05e1c3
diff --git a/Android.mk b/Android.mk
index 9d446fc..94929c1 100644
--- a/Android.mk
+++ b/Android.mk
@@ -69,35 +69,44 @@
 
 .PHONY: clean-oat-host
 clean-oat-host:
+	rm -f $(ART_NATIVETEST_OUT)/*.dex
 	rm -f $(ART_NATIVETEST_OUT)/*.oat
 	rm -f $(ART_NATIVETEST_OUT)/*.art
+	rm -f $(ART_TEST_OUT)/*.dex
 	rm -f $(ART_TEST_OUT)/*.oat
 	rm -f $(ART_TEST_OUT)/*.art
+	rm -f $(DALVIK_CACHE_OUT)/*@classes.dex
 	rm -f $(DALVIK_CACHE_OUT)/*.oat
 	rm -f $(DALVIK_CACHE_OUT)/*.art
+	rm -f $(HOST_OUT_JAVA_LIBRARIES)/*.dex
 	rm -f $(HOST_OUT_JAVA_LIBRARIES)/*.oat
 	rm -f $(HOST_OUT_JAVA_LIBRARIES)/*.art
+	rm -f $(TARGET_OUT_JAVA_LIBRARIES)/*.dex
 	rm -f $(TARGET_OUT_JAVA_LIBRARIES)/*.oat
 	rm -f $(TARGET_OUT_JAVA_LIBRARIES)/*.art
+	rm -f $(TARGET_OUT_UNSTRIPPED)/system/framework/*.dex
 	rm -f $(TARGET_OUT_UNSTRIPPED)/system/framework/*.oat
-	rm -f $(TARGET_OUT_APPS)/*.oat
-	rm -f $(TARGET_OUT_INTERMEDIATES)/JAVA_LIBRARIES/*_intermediates/javalib.jar.oat
-	rm -f $(TARGET_OUT_INTERMEDIATES)/APPS/*_intermediates/*.apk.oat
-	rm -rf /tmp/test-*/dalvik-cache/*.oat
+	rm -f $(TARGET_OUT_APPS)/*.dex
+	rm -f $(TARGET_OUT_INTERMEDIATES)/JAVA_LIBRARIES/*_intermediates/javalib.odex
+	rm -f $(TARGET_OUT_INTERMEDIATES)/APPS/*_intermediates/*.odex
+	rm -rf /tmp/test-*/dalvik-cache/*@classes.dex
 
 .PHONY: clean-oat-target
 clean-oat-target:
 	adb remount
+	adb shell rm $(ART_NATIVETEST_DIR)/*.odex
 	adb shell rm $(ART_NATIVETEST_DIR)/*.oat
 	adb shell rm $(ART_NATIVETEST_DIR)/*.art
+	adb shell rm $(ART_TEST_DIR)/*.odex
 	adb shell rm $(ART_TEST_DIR)/*.oat
 	adb shell rm $(ART_TEST_DIR)/*.art
+	adb shell rm $(DALVIK_CACHE_DIR)/*.dex
 	adb shell rm $(DALVIK_CACHE_DIR)/*.oat
 	adb shell rm $(DALVIK_CACHE_DIR)/*.art
 	adb shell rm $(DEXPREOPT_BOOT_JAR_DIR)/*.oat
 	adb shell rm $(DEXPREOPT_BOOT_JAR_DIR)/*.art
-	adb shell rm system/app/*.oat
-	adb shell rm data/run-test/test-*/dalvik-cache/*.oat
+	adb shell rm system/app/*.odex
+	adb shell rm data/run-test/test-*/dalvik-cache/*@classes.dex
 
 # we aren't building most of art on darwin right now, but we do need to build new dalvikvm
 ifeq ($(HOST_OS)-$(HOST_ARCH),darwin-x86)
@@ -339,7 +348,7 @@
 
 .PHONY: dump-oat-Calculator
 ifeq ($(ART_BUILD_TARGET_NDEBUG),true)
-dump-oat-Calculator: $(TARGET_OUT_APPS)/Calculator.apk.oat $(TARGET_BOOT_IMG_OUT) $(OATDUMP)
+dump-oat-Calculator: $(TARGET_OUT_APPS)/Calculator.odex $(TARGET_BOOT_IMG_OUT) $(OATDUMP)
 	$(OATDUMP) --oat-file=$< --output=/tmp/Calculator.oatdump.txt
 	@echo Output in /tmp/Calculator.oatdump.txt
 endif
diff --git a/build/Android.oattest.mk b/build/Android.oattest.mk
index e2ee41a..36fd0fc 100644
--- a/build/Android.oattest.mk
+++ b/build/Android.oattest.mk
@@ -69,11 +69,11 @@
 	$(hide) (adb pull $(ART_TEST_DIR)/test-art-target-oat-$(1) /tmp/ && echo test-art-target-oat-$(1) PASSED) || (echo test-art-target-oat-$(1) FAILED && exit 1)
 	$(hide) rm /tmp/test-art-target-oat-$(1)
 
-$(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).jar.oat: $(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).jar $(HOST_CORE_IMG_OUT) | $(DEX2OAT)
+$(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).odex: $(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).jar $(HOST_CORE_IMG_OUT) | $(DEX2OAT)
 	$(DEX2OAT) --runtime-arg -Xms16m --runtime-arg -Xmx16m --boot-image=$(HOST_CORE_IMG_OUT) --dex-file=$$< --oat-file=$$@ --instruction-set=$(HOST_ARCH) --host --host-prefix="" --android-root=$(HOST_OUT)
 
 .PHONY: test-art-host-oat-$(1)
-test-art-host-oat-$(1): $(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).jar.oat test-art-host-dependencies
+test-art-host-oat-$(1): $(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).odex test-art-host-dependencies
 	mkdir -p /tmp/android-data/test-art-host-oat-$(1)
 	ANDROID_DATA=/tmp/android-data/test-art-host-oat-$(1) \
 	  ANDROID_ROOT=$(HOST_OUT) \
@@ -83,7 +83,7 @@
 	$(hide) rm -r /tmp/android-data/test-art-host-oat-$(1)
 
 .PHONY: test-art-host-interpreter-oat-$(1)
-test-art-host-interpreter-oat-$(1): $(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).jar.oat test-art-host-dependencies
+test-art-host-interpreter-oat-$(1): $(HOST_OUT_JAVA_LIBRARIES)/oat-test-dex-$(1).odex test-art-host-dependencies
 	mkdir -p /tmp/android-data/test-art-host-interpreter-oat-$(1)
 	ANDROID_DATA=/tmp/android-data/test-art-host-interpreter-oat-$(1) \
 	  ANDROID_ROOT=$(HOST_OUT) \
diff --git a/src/class_linker.cc b/src/class_linker.cc
index 85bd9f6..70c7ff3 100644
--- a/src/class_linker.cc
+++ b/src/class_linker.cc
@@ -810,12 +810,12 @@
 
   const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
   if (oat_dex_file == NULL) {
-    LOG(ERROR) << ".oat file " << oat_file->GetLocation()
+    LOG(ERROR) << "oat file " << oat_file->GetLocation()
                << " does not contain contents for " << dex_location;
     std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file->GetOatDexFiles();
     for (size_t i = 0; i < oat_dex_files.size(); i++) {
       const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
-      LOG(ERROR) << ".oat file " << oat_file->GetLocation()
+      LOG(ERROR) << "oat file " << oat_file->GetLocation()
                  << " contains contents for " << oat_dex_file->GetDexFileLocation();
     }
     return false;
@@ -829,14 +829,14 @@
   if (!image_check) {
     std::string image_file(image_header.GetImageRoot(
         ImageHeader::kOatLocation)->AsString()->ToModifiedUtf8());
-    LOG(WARNING) << ".oat file " << oat_file->GetLocation()
+    LOG(WARNING) << "oat file " << oat_file->GetLocation()
                  << " mismatch ( " << std::hex << oat_file->GetOatHeader().GetImageFileLocationOatChecksum()
                  << ", " << oat_file->GetOatHeader().GetImageFileLocationOatDataBegin()
                  << ") with " << image_file
                  << " (" << image_oat_checksum << ", " << std::hex << image_oat_data_begin << ")";
   }
   if (!dex_check) {
-    LOG(WARNING) << ".oat file " << oat_file->GetLocation()
+    LOG(WARNING) << "oat file " << oat_file->GetLocation()
                  << " mismatch ( " << std::hex << oat_dex_file->GetDexFileLocationChecksum()
                  << ") with " << dex_location
                  << " (" << std::hex << dex_location_checksum << ")";
@@ -864,16 +864,16 @@
   }
 
   // Look for an existing file next to dex. for example, for
-  // /foo/bar/baz.jar, look for /foo/bar/baz.jar.oat.
-  std::string oat_filename(OatFile::DexFilenameToOatFilename(dex_location));
-  const OatFile* oat_file = FindOatFileFromOatLocationLocked(oat_filename);
+  // /foo/bar/baz.jar, look for /foo/bar/baz.odex.
+  std::string odex_filename(OatFile::DexFilenameToOdexFilename(dex_location));
+  const OatFile* oat_file = FindOatFileFromOatLocationLocked(odex_filename);
   if (oat_file != NULL) {
     uint32_t dex_location_checksum;
     if (!DexFile::GetChecksum(dex_location, dex_location_checksum)) {
       // If no classes.dex found in dex_location, it has been stripped, assume oat is up-to-date.
       // This is the common case in user builds for jar's and apk's in the /system directory.
       const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_location);
-      CHECK(oat_dex_file != NULL) << oat_filename << " " << dex_location;
+      CHECK(oat_dex_file != NULL) << odex_filename << " " << dex_location;
       RegisterOatFileLocked(*oat_file);
       return oat_dex_file->OpenDexFile();
     }
@@ -885,8 +885,8 @@
     }
   }
   // Look for an existing file in the dalvik-cache, validating the result if found
-  // not found in /foo/bar/baz.oat? try /data/dalvik-cache/foo@bar@baz.oat
-  std::string cache_location(GetDalvikCacheFilenameOrDie(oat_filename));
+  // not found in /foo/bar/baz.odex? try /data/dalvik-cache/foo@bar@baz.jar@classes.dex
+  std::string cache_location(GetDalvikCacheFilenameOrDie(dex_location));
   oat_file = FindOatFileFromOatLocationLocked(cache_location);
   if (oat_file != NULL) {
     uint32_t dex_location_checksum;
@@ -901,13 +901,13 @@
       return dex_file;
     }
     if (TEMP_FAILURE_RETRY(unlink(oat_file->GetLocation().c_str())) != 0) {
-      PLOG(FATAL) << "Failed to remove obsolete .oat file " << oat_file->GetLocation();
+      PLOG(FATAL) << "Failed to remove obsolete oat file " << oat_file->GetLocation();
     }
   }
-  LOG(INFO) << "Failed to open oat file from " << oat_filename << " or " << cache_location << ".";
+  LOG(INFO) << "Failed to open oat file from " << odex_filename << " or " << cache_location << ".";
 
   // Try to generate oat file if it wasn't found or was obsolete.
-  std::string oat_cache_filename(GetDalvikCacheFilenameOrDie(oat_filename));
+  std::string oat_cache_filename(GetDalvikCacheFilenameOrDie(dex_location));
   return FindOrCreateOatFileForDexLocationLocked(dex_location, oat_cache_filename);
 }
 
diff --git a/src/native/dalvik_system_DexFile.cc b/src/native/dalvik_system_DexFile.cc
index c49e017..e07339c 100644
--- a/src/native/dalvik_system_DexFile.cc
+++ b/src/native/dalvik_system_DexFile.cc
@@ -209,9 +209,9 @@
     }
   }
 
-  // Check if we have an oat file next to the dex file.
-  std::string oat_filename(OatFile::DexFilenameToOatFilename(filename.c_str()));
-  UniquePtr<const OatFile> oat_file(OatFile::Open(oat_filename, oat_filename, NULL));
+  // Check if we have an odex file next to the dex file.
+  std::string odex_filename(OatFile::DexFilenameToOdexFilename(filename.c_str()));
+  UniquePtr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL));
   if (oat_file.get() != NULL) {
     ScopedObjectAccess soa(env);
     const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename.c_str());
@@ -231,7 +231,7 @@
       }
       if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename.c_str(), location_checksum)) {
         if (debug_logging) {
-          LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << oat_filename
+          LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
               << " is up-to-date checksum compared to " << filename.c_str();
         }
         return JNI_FALSE;
@@ -240,8 +240,8 @@
   }
 
   // Check if we have an oat file in the cache
-  std::string cache_location(GetDalvikCacheFilenameOrDie(oat_filename));
-  oat_file.reset(OatFile::Open(cache_location, oat_filename, NULL));
+  std::string cache_location(GetDalvikCacheFilenameOrDie(filename.c_str()));
+  oat_file.reset(OatFile::Open(cache_location, filename.c_str(), NULL));
   if (oat_file.get() == NULL) {
     LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
               << " does not exist for " << filename.c_str();
diff --git a/src/oat_file.cc b/src/oat_file.cc
index 7bbf94c..ebf92a7 100644
--- a/src/oat_file.cc
+++ b/src/oat_file.cc
@@ -31,11 +31,13 @@
 
 namespace art {
 
-std::string OatFile::DexFilenameToOatFilename(const std::string& location) {
+std::string OatFile::DexFilenameToOdexFilename(const std::string& location) {
   CHECK(IsValidDexFilename(location) || IsValidZipFilename(location));
-  std::string oat_location(location);
-  oat_location += ".oat";
-  return oat_location;
+  std::string odex_location(location);
+  odex_location.resize(odex_location.size() - 3);  // 3=dex or zip or apk
+  CHECK_EQ('.', odex_location[odex_location.size()-1]);
+  odex_location += "odex";
+  return odex_location;
 }
 
 void OatFile::CheckLocation(const std::string& location) {
@@ -59,17 +61,7 @@
                        const std::string& location,
                        byte* requested_base) {
   CHECK(!filename.empty()) << location;
-  CheckLocation(location);
-  /*
-   * TODO: Reenable dlopen when it works again on MIPS. It may have broken from this change:
-   * commit 818d98eb563ad5d7293b8b5c40f3dabf745e611f
-   * Author: Brian Carlstrom <bdc@google.com>
-   * Date:   Sun Feb 10 21:38:12 2013 -0800
-   *
-   *    Fix MIPS to use standard kPageSize=0x1000 section alignment for ELF sections
-   *
-   *    Change-Id: I905f0c5f75921a65bd7426a54d6258c780d85d0e
-   */
+  CheckLocation(filename);
   OatFile* result = OpenDlopen(filename, location, requested_base);
   if (result != NULL) {
     return result;
diff --git a/src/oat_file.h b/src/oat_file.h
index 46aad4f..0bf79a9 100644
--- a/src/oat_file.h
+++ b/src/oat_file.h
@@ -36,8 +36,9 @@
 
 class OatFile {
  public:
-  // Returns an OatFile name based on a DexFile location
-  static std::string DexFilenameToOatFilename(const std::string& location);
+  // Returns an .odex file name next adjacent to the dex location.
+  // For example, for "/foo/bar/baz.jar", return "/foo/bar/baz.odex".
+  static std::string DexFilenameToOdexFilename(const std::string& location);
 
   // Open an oat file. Returns NULL on failure.  Requested base can
   // optionally be used to request where the file should be loaded.
diff --git a/src/utils.cc b/src/utils.cc
index bb83802..723be92 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -1208,6 +1208,10 @@
     LOG(FATAL) << "Expected path in location to be absolute: "<< location;
   }
   std::string cache_file(location, 1); // skip leading slash
+  if (!IsValidDexFilename(location)) {
+    cache_file += "/";
+    cache_file += DexFile::kClassesDex;
+  }
   std::replace(cache_file.begin(), cache_file.end(), '/', '@');
   return dalvik_cache + "/" + cache_file;
 }
@@ -1225,7 +1229,9 @@
 }
 
 bool IsValidOatFilename(const std::string& filename) {
-  return EndsWith(filename, ".oat");
+  return (EndsWith(filename, ".odex") ||
+          EndsWith(filename, ".oat") ||
+          EndsWith(filename, DexFile::kClassesDex));
 }
 
 }  // namespace art