From 6f6114140fbc09c5c7bec441922412635a7f7ff1 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Wed, 21 Jan 2015 22:25:24 -0800 Subject: [PATCH] ART: Refactor common ELF->InstructionSet code Move code into instruction_set.h/cc. Change-Id: I34d5c82791042c68629df84e0f4b9321231d51b9 --- patchoat/patchoat.cc | 26 +------------------------- runtime/arch/instruction_set.cc | 25 +++++++++++++++++++++++++ runtime/arch/instruction_set.h | 2 ++ runtime/elf_file.cc | 30 +----------------------------- 4 files changed, 29 insertions(+), 54 deletions(-) diff --git a/patchoat/patchoat.cc b/patchoat/patchoat.cc index 6c86c7b7a..2059a96c7 100644 --- a/patchoat/patchoat.cc +++ b/patchoat/patchoat.cc @@ -48,30 +48,6 @@ namespace art { -static InstructionSet ElfISAToInstructionSet(Elf32_Word isa, Elf32_Word e_flags) { - switch (isa) { - 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; - } else { - return kNone; - } - default: - return kNone; - } -} - static bool LocationToFilename(const std::string& location, InstructionSet isa, std::string* filename) { bool has_system = false; @@ -219,7 +195,7 @@ bool PatchOat::Patch(File* input_oat, const std::string& image_location, off_t d LOG(ERROR) << "unable to read elf header"; return false; } - isa = ElfISAToInstructionSet(elf_hdr.e_machine, elf_hdr.e_flags); + isa = GetInstructionSetFromELF(elf_hdr.e_machine, elf_hdr.e_flags); } const char* isa_name = GetInstructionSetString(isa); std::string image_filename; diff --git a/runtime/arch/instruction_set.cc b/runtime/arch/instruction_set.cc index 5ab461bc7..81ca01042 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 @@ InstructionSet GetInstructionSetFromString(const char* isa_str) { 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 9135e58c1..9cfd2eb2d 100644 --- a/runtime/arch/instruction_set.h +++ b/runtime/arch/instruction_set.h @@ -80,6 +80,8 @@ const char* GetInstructionSetString(InstructionSet isa); // 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: diff --git a/runtime/elf_file.cc b/runtime/elf_file.cc index 1b91aa64d..a22e2741c 100644 --- a/runtime/elf_file.cc +++ b/runtime/elf_file.cc @@ -1313,35 +1313,7 @@ bool ElfFileImplGetPath(); if (executable) { - InstructionSet elf_ISA = kNone; - switch (GetHeader().e_machine) { - case EM_ARM: { - elf_ISA = kArm; - break; - } - case EM_AARCH64: { - elf_ISA = kArm64; - break; - } - case EM_386: { - elf_ISA = kX86; - break; - } - case EM_X86_64: { - elf_ISA = kX86_64; - break; - } - case EM_MIPS: { - if ((GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R2 || - (GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_32R6) { - elf_ISA = kMips; - } else if ((GetHeader().e_flags & EF_MIPS_ARCH) == EF_MIPS_ARCH_64R6) { - elf_ISA = kMips64; - } - break; - } - } - + InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags); if (elf_ISA != kRuntimeISA) { std::ostringstream oss; oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; -- 2.11.0