ART: More ELF and oat file safety measures

In an ELF file, look for a shstrtab section when loading in
program-header-only mode. If the section is outside the file size,
it strongly indicates a broken compile.

When compiling oat files in the class linker, explicitly unlink
on failure. This should catch cases when dex2oat is killed or
crashes and doesn't have a chance to delete its (partial) output.

Bug: 15567083

(cherry picked from commit ad00fed942a9a04cf3f46784bbd04a5f00dd4ab8)

Change-Id: Ia0c75f151d91c6f26a71696967255d6d409ca882
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index 1f4cf8f..a225f60 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -21,6 +21,7 @@
 #include <memory>
 #include <queue>
 #include <string>
+#include <unistd.h>
 #include <utility>
 #include <vector>
 
@@ -704,7 +705,14 @@
     argv.push_back(compiler_options[i].c_str());
   }
 
-  return Exec(argv, error_msg);
+  if (!Exec(argv, error_msg)) {
+    // Manually delete the file. Ensures there is no garbage left over if the process unexpectedly
+    // died. Ignore unlink failure, propagate the original error.
+    TEMP_FAILURE_RETRY(unlink(oat_cache_filename));
+    return false;
+  }
+
+  return true;
 }
 
 const OatFile* ClassLinker::RegisterOatFile(const OatFile* oat_file) {