Serban Constantinescu | e6622be | 2014-02-27 15:36:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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_arm64.h" |
| 18 | |
| 19 | #include <inttypes.h> |
| 20 | |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 21 | #include <sstream> |
Serban Constantinescu | e6622be | 2014-02-27 15:36:47 +0000 | [diff] [blame] | 22 | |
| 23 | #include "base/logging.h" |
| 24 | #include "base/stringprintf.h" |
| 25 | #include "thread.h" |
| 26 | |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 27 | using namespace vixl::aarch64; // NOLINT(build/namespaces) |
| 28 | |
Serban Constantinescu | e6622be | 2014-02-27 15:36:47 +0000 | [diff] [blame] | 29 | namespace art { |
| 30 | namespace arm64 { |
| 31 | |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 32 | // This enumeration should mirror the declarations in |
| 33 | // runtime/arch/arm64/registers_arm64.h. We do not include that file to |
| 34 | // avoid a dependency on libart. |
| 35 | enum { |
Serban Constantinescu | 9bd88b0 | 2015-04-22 16:24:46 +0100 | [diff] [blame] | 36 | TR = 19, |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 37 | IP0 = 16, |
| 38 | IP1 = 17, |
| 39 | FP = 29, |
| 40 | LR = 30 |
| 41 | }; |
| 42 | |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 43 | void CustomDisassembler::AppendRegisterNameToOutput(const Instruction* instr, |
| 44 | const CPURegister& reg) { |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 45 | USE(instr); |
Alexandre Rames | d737ab3 | 2015-03-06 09:11:12 +0000 | [diff] [blame] | 46 | if (reg.IsRegister() && reg.Is64Bits()) { |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 47 | if (reg.GetCode() == TR) { |
Alexandre Rames | d737ab3 | 2015-03-06 09:11:12 +0000 | [diff] [blame] | 48 | AppendToOutput("tr"); |
| 49 | return; |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 50 | } else if (reg.GetCode() == LR) { |
Alexandre Rames | d737ab3 | 2015-03-06 09:11:12 +0000 | [diff] [blame] | 51 | AppendToOutput("lr"); |
| 52 | return; |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 53 | } |
Alexandre Rames | d737ab3 | 2015-03-06 09:11:12 +0000 | [diff] [blame] | 54 | // Fall through. |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 55 | } |
| 56 | // Print other register names as usual. |
| 57 | Disassembler::AppendRegisterNameToOutput(instr, reg); |
| 58 | } |
| 59 | |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 60 | void CustomDisassembler::VisitLoadLiteral(const Instruction* instr) { |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 61 | Disassembler::VisitLoadLiteral(instr); |
| 62 | |
| 63 | if (!read_literals_) { |
| 64 | return; |
| 65 | } |
| 66 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 67 | // Get address of literal. Bail if not within expected buffer range to |
| 68 | // avoid trying to fetch invalid literals (we can encounter this when |
| 69 | // interpreting raw data as instructions). |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 70 | void* data_address = instr->GetLiteralAddress<void*>(); |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 71 | if (data_address < base_address_ || data_address >= end_address_) { |
| 72 | AppendToOutput(" (?)"); |
| 73 | return; |
| 74 | } |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 75 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 76 | // Output information on literal. |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 77 | Instr op = instr->Mask(LoadLiteralMask); |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 78 | switch (op) { |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 79 | case LDR_w_lit: |
| 80 | case LDR_x_lit: |
| 81 | case LDRSW_x_lit: { |
| 82 | int64_t data = op == LDR_x_lit ? *reinterpret_cast<int64_t*>(data_address) |
| 83 | : *reinterpret_cast<int32_t*>(data_address); |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 84 | AppendToOutput(" (0x%" PRIx64 " / %" PRId64 ")", data, data); |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 85 | break; |
| 86 | } |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 87 | case LDR_s_lit: |
| 88 | case LDR_d_lit: { |
| 89 | double data = (op == LDR_s_lit) ? *reinterpret_cast<float*>(data_address) |
| 90 | : *reinterpret_cast<double*>(data_address); |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 91 | AppendToOutput(" (%g)", data); |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 99 | void CustomDisassembler::VisitLoadStoreUnsignedOffset(const Instruction* instr) { |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 100 | Disassembler::VisitLoadStoreUnsignedOffset(instr); |
| 101 | |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 102 | if (instr->GetRn() == TR) { |
| 103 | int64_t offset = instr->GetImmLSUnsigned() << instr->GetSizeLS(); |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 104 | std::ostringstream tmp_stream; |
| 105 | Thread::DumpThreadOffset<8>(tmp_stream, static_cast<uint32_t>(offset)); |
Alexandre Rames | 5e2c8d3 | 2015-08-06 14:49:28 +0100 | [diff] [blame] | 106 | AppendToOutput(" ; %s", tmp_stream.str().c_str()); |
Zheng Xu | a34e760 | 2015-02-03 12:03:15 +0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Serban Constantinescu | e6622be | 2014-02-27 15:36:47 +0000 | [diff] [blame] | 110 | size_t DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin) { |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 111 | const Instruction* instr = reinterpret_cast<const Instruction*>(begin); |
Alexandre Rames | fef019c | 2014-10-10 17:14:18 +0100 | [diff] [blame] | 112 | decoder.Decode(instr); |
Alexandre Rames | d737ab3 | 2015-03-06 09:11:12 +0000 | [diff] [blame] | 113 | os << FormatInstructionPointer(begin) |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 114 | << StringPrintf(": %08x\t%s\n", instr->GetInstructionBits(), disasm.GetOutput()); |
| 115 | return kInstructionSize; |
Serban Constantinescu | e6622be | 2014-02-27 15:36:47 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
Scott Wakeling | 97c72b7 | 2016-06-24 16:19:36 +0100 | [diff] [blame] | 119 | for (const uint8_t* cur = begin; cur < end; cur += kInstructionSize) { |
Serban Constantinescu | e6622be | 2014-02-27 15:36:47 +0000 | [diff] [blame] | 120 | Dump(os, cur); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | } // namespace arm64 |
| 125 | } // namespace art |