blob: 23472a8dcab2535fef244ba6abb2c817e50d1a57 [file] [log] [blame]
Serban Constantinescue6622be2014-02-27 15:36:47 +00001/*
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 Cawthornebb3ef5a2021-12-21 22:01:14 +000021#include <regex>
22
Zheng Xua34e7602015-02-03 12:03:15 +080023#include <sstream>
Serban Constantinescue6622be2014-02-27 15:36:47 +000024
Andreas Gampebda1d602016-08-29 17:43:45 -070025#include "android-base/logging.h"
26#include "android-base/stringprintf.h"
27
28using android::base::StringPrintf;
Serban Constantinescue6622be2014-02-27 15:36:47 +000029
Scott Wakeling97c72b72016-06-24 16:19:36 +010030using namespace vixl::aarch64; // NOLINT(build/namespaces)
31
Serban Constantinescue6622be2014-02-27 15:36:47 +000032namespace art {
33namespace arm64 {
34
Zheng Xua34e7602015-02-03 12:03:15 +080035// 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.
38enum {
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010039 TR = 19,
Zheng Xua34e7602015-02-03 12:03:15 +080040 IP0 = 16,
41 IP1 = 17,
42 FP = 29,
43 LR = 30
44};
45
Scott Wakeling97c72b72016-06-24 16:19:36 +010046void CustomDisassembler::AppendRegisterNameToOutput(const Instruction* instr,
47 const CPURegister& reg) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +000048 USE(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +000049 if (reg.IsRegister() && reg.Is64Bits()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010050 if (reg.GetCode() == TR) {
Alexandre Ramesd737ab32015-03-06 09:11:12 +000051 AppendToOutput("tr");
52 return;
Scott Wakeling97c72b72016-06-24 16:19:36 +010053 } else if (reg.GetCode() == LR) {
Alexandre Ramesd737ab32015-03-06 09:11:12 +000054 AppendToOutput("lr");
55 return;
Alexandre Ramesa37d9252014-10-27 11:28:14 +000056 }
Alexandre Ramesd737ab32015-03-06 09:11:12 +000057 // Fall through.
Alexandre Ramesa37d9252014-10-27 11:28:14 +000058 }
59 // Print other register names as usual.
60 Disassembler::AppendRegisterNameToOutput(instr, reg);
61}
62
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +000063void CustomDisassembler::Visit(vixl::aarch64::Metadata* metadata, const Instruction* instr) {
64 vixl::aarch64::Disassembler::Visit(metadata, instr);
65 const std::string& form = (*metadata)["form"];
Alexandre Ramesa37d9252014-10-27 11:28:14 +000066
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +000067 // 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
90void CustomDisassembler::VisitLoadLiteralInstr(const Instruction* instr) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +000091 if (!read_literals_) {
92 return;
93 }
94
Aart Bikd3059e72016-05-11 10:30:47 -070095 // 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 Wakeling97c72b72016-06-24 16:19:36 +010098 void* data_address = instr->GetLiteralAddress<void*>();
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +000099
Aart Bikd3059e72016-05-11 10:30:47 -0700100 if (data_address < base_address_ || data_address >= end_address_) {
101 AppendToOutput(" (?)");
102 return;
103 }
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000104
Aart Bikd3059e72016-05-11 10:30:47 -0700105 // Output information on literal.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100106 Instr op = instr->Mask(LoadLiteralMask);
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000107 switch (op) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100108 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 Xua34e7602015-02-03 12:03:15 +0800113 AppendToOutput(" (0x%" PRIx64 " / %" PRId64 ")", data, data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000114 break;
115 }
Scott Wakeling97c72b72016-06-24 16:19:36 +0100116 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 Xua34e7602015-02-03 12:03:15 +0800120 AppendToOutput(" (%g)", data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000121 break;
122 }
123 default:
124 break;
125 }
126}
127
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +0000128void CustomDisassembler::VisitLoadStoreUnsignedOffsetInstr(const Instruction* instr) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100129 if (instr->GetRn() == TR) {
Vladimir Marko8feddbc2020-09-03 09:59:45 +0100130 AppendThreadOfsetName(instr);
Zheng Xua34e7602015-02-03 12:03:15 +0800131 }
132}
133
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +0000134void CustomDisassembler::VisitUnconditionalBranchInstr(const Instruction* instr) {
Vladimir Marko8feddbc2020-09-03 09:59:45 +0100135 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
150void 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 Constantinescue6622be2014-02-27 15:36:47 +0000157size_t DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100158 const Instruction* instr = reinterpret_cast<const Instruction*>(begin);
Alexandre Ramesfef019c2014-10-10 17:14:18 +0100159 decoder.Decode(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +0000160 os << FormatInstructionPointer(begin)
Scott Wakeling97c72b72016-06-24 16:19:36 +0100161 << StringPrintf(": %08x\t%s\n", instr->GetInstructionBits(), disasm.GetOutput());
162 return kInstructionSize;
Serban Constantinescue6622be2014-02-27 15:36:47 +0000163}
164
165void DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100166 for (const uint8_t* cur = begin; cur < end; cur += kInstructionSize) {
Serban Constantinescue6622be2014-02-27 15:36:47 +0000167 Dump(os, cur);
168 }
169}
170
171} // namespace arm64
172} // namespace art