1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "disassembler.h"
18 
19 #include <ostream>
20 
21 #include "android-base/logging.h"
22 #include "android-base/stringprintf.h"
23 
24 #include "disassembler_arm.h"
25 #include "disassembler_arm64.h"
26 #include "disassembler_mips.h"
27 #include "disassembler_x86.h"
28 
29 using android::base::StringPrintf;
30 
31 namespace art {
32 
Disassembler(DisassemblerOptions * disassembler_options)33 Disassembler::Disassembler(DisassemblerOptions* disassembler_options)
34     : disassembler_options_(disassembler_options) {
35   CHECK(disassembler_options_ != nullptr);
36 }
37 
Create(InstructionSet instruction_set,DisassemblerOptions * options)38 Disassembler* Disassembler::Create(InstructionSet instruction_set, DisassemblerOptions* options) {
39   if (instruction_set == InstructionSet::kArm || instruction_set == InstructionSet::kThumb2) {
40     return new arm::DisassemblerArm(options);
41   } else if (instruction_set == InstructionSet::kArm64) {
42     return new arm64::DisassemblerArm64(options);
43   } else if (instruction_set == InstructionSet::kMips) {
44     return new mips::DisassemblerMips(options, /* is_o32_abi */ true);
45   } else if (instruction_set == InstructionSet::kMips64) {
46     return new mips::DisassemblerMips(options, /* is_o32_abi */ false);
47   } else if (instruction_set == InstructionSet::kX86) {
48     return new x86::DisassemblerX86(options, false);
49   } else if (instruction_set == InstructionSet::kX86_64) {
50     return new x86::DisassemblerX86(options, true);
51   } else {
52     UNIMPLEMENTED(FATAL) << static_cast<uint32_t>(instruction_set);
53     return nullptr;
54   }
55 }
56 
FormatInstructionPointer(const uint8_t * begin)57 std::string Disassembler::FormatInstructionPointer(const uint8_t* begin) {
58   if (disassembler_options_->absolute_addresses_) {
59     return StringPrintf("%p", begin);
60   } else {
61     size_t offset = begin - disassembler_options_->base_address_;
62     return StringPrintf("0x%08zx", offset);
63   }
64 }
65 
create_disassembler(InstructionSet instruction_set,DisassemblerOptions * options)66 Disassembler* create_disassembler(InstructionSet instruction_set, DisassemblerOptions* options) {
67   return Disassembler::Create(instruction_set, options);
68 }
69 
70 }  // namespace art
71