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