blob: 262e8152fb8ade2a18ab3ce7f43ebbaf20823ef3 [file] [log] [blame]
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001/*
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
Ian Rogerscf7f1912014-10-22 22:06:39 -070019#include <ostream>
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080020
Andreas Gampebda1d602016-08-29 17:43:45 -070021#include "android-base/logging.h"
22#include "android-base/stringprintf.h"
23
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080024#include "disassembler_arm.h"
Serban Constantinescue6622be2014-02-27 15:36:47 +000025#include "disassembler_arm64.h"
Elliott Hughes60454e82012-04-25 12:56:03 -070026#include "disassembler_mips.h"
Ian Rogers706a10e2012-03-23 17:00:55 -070027#include "disassembler_x86.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080028
Andreas Gampebda1d602016-08-29 17:43:45 -070029using android::base::StringPrintf;
30
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080031namespace art {
32
Andreas Gampebda1d602016-08-29 17:43:45 -070033Disassembler::Disassembler(DisassemblerOptions* disassembler_options)
34 : disassembler_options_(disassembler_options) {
35 CHECK(disassembler_options_ != nullptr);
36}
37
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070038Disassembler* Disassembler::Create(InstructionSet instruction_set, DisassemblerOptions* options) {
Vladimir Marko33bff252017-11-01 14:35:42 +000039 if (instruction_set == InstructionSet::kArm || instruction_set == InstructionSet::kThumb2) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070040 return new arm::DisassemblerArm(options);
Vladimir Marko33bff252017-11-01 14:35:42 +000041 } else if (instruction_set == InstructionSet::kArm64) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070042 return new arm64::DisassemblerArm64(options);
Vladimir Marko33bff252017-11-01 14:35:42 +000043 } else if (instruction_set == InstructionSet::kMips) {
Andreas Gampe9b031f72018-10-04 11:03:34 -070044 return new mips::DisassemblerMips(options, /* is_o32_abi= */ true);
Vladimir Marko33bff252017-11-01 14:35:42 +000045 } else if (instruction_set == InstructionSet::kMips64) {
Andreas Gampe9b031f72018-10-04 11:03:34 -070046 return new mips::DisassemblerMips(options, /* is_o32_abi= */ false);
Vladimir Marko33bff252017-11-01 14:35:42 +000047 } else if (instruction_set == InstructionSet::kX86) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070048 return new x86::DisassemblerX86(options, false);
Vladimir Marko33bff252017-11-01 14:35:42 +000049 } else if (instruction_set == InstructionSet::kX86_64) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070050 return new x86::DisassemblerX86(options, true);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080051 } else {
Andreas Gampebda1d602016-08-29 17:43:45 -070052 UNIMPLEMENTED(FATAL) << static_cast<uint32_t>(instruction_set);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080054 }
55}
56
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -070057std::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
Alexandre Rameseb7b7392015-06-19 14:47:01 +010066Disassembler* create_disassembler(InstructionSet instruction_set, DisassemblerOptions* options) {
67 return Disassembler::Create(instruction_set, options);
68}
69
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080070} // namespace art