ART: Refactor common ELF->InstructionSet code

Move code into instruction_set.h/cc.

Change-Id: I34d5c82791042c68629df84e0f4b9321231d51b9
diff --git a/runtime/arch/instruction_set.cc b/runtime/arch/instruction_set.cc
index 5ab461b..81ca010 100644
--- a/runtime/arch/instruction_set.cc
+++ b/runtime/arch/instruction_set.cc
@@ -16,6 +16,8 @@
 
 #include "instruction_set.h"
 
+// Explicitly include our own elf.h to avoid Linux and other dependencies.
+#include "../elf.h"
 #include "globals.h"
 
 namespace art {
@@ -63,6 +65,29 @@
   return kNone;
 }
 
+InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags) {
+  switch (e_machine) {
+    case EM_ARM:
+      return kArm;
+    case EM_AARCH64:
+      return kArm64;
+    case EM_386:
+      return kX86;
+    case EM_X86_64:
+      return kX86_64;
+    case EM_MIPS: {
+      if ((e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2 ||
+          (e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6) {
+        return kMips;
+      } else if ((e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6) {
+        return kMips64;
+      }
+      break;
+    }
+  }
+  return kNone;
+}
+
 size_t GetInstructionSetAlignment(InstructionSet isa) {
   switch (isa) {
     case kArm:
diff --git a/runtime/arch/instruction_set.h b/runtime/arch/instruction_set.h
index 9135e58..9cfd2eb 100644
--- a/runtime/arch/instruction_set.h
+++ b/runtime/arch/instruction_set.h
@@ -80,6 +80,8 @@
 // Note: Returns kNone when the string cannot be parsed to a known value.
 InstructionSet GetInstructionSetFromString(const char* instruction_set);
 
+InstructionSet GetInstructionSetFromELF(uint16_t e_machine, uint32_t e_flags);
+
 static inline size_t GetInstructionSetPointerSize(InstructionSet isa) {
   switch (isa) {
     case kArm: