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