blob: ef27e75c8c9a69c691b2d41b8218b8457e31f381 [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
Santiago Aboy Solanes44e21722024-09-19 13:00:26 +010063void CustomDisassembler::AppendCodeRelativeAddressToOutput(const Instruction* instr,
64 const void* addr) {
65 USE(instr);
66 int64_t rel_addr = CodeRelativeAddress(addr);
67 if (rel_addr >= 0) {
68 AppendToOutput("(addr 0x%08" PRIx64 ")", rel_addr);
69 } else {
70 AppendToOutput("(addr -0x%08" PRIx64 ")", -rel_addr);
71 }
72}
73
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +000074void CustomDisassembler::Visit(vixl::aarch64::Metadata* metadata, const Instruction* instr) {
75 vixl::aarch64::Disassembler::Visit(metadata, instr);
76 const std::string& form = (*metadata)["form"];
Alexandre Ramesa37d9252014-10-27 11:28:14 +000077
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +000078 // These regexs are long, but it is an attempt to match the mapping entry keys in the
79 // #define DEFAULT_FORM_TO_VISITOR_MAP(VISITORCLASS) in the file
80 // external/vixl/src/aarch64/decoder-visitor-map-aarch64.h
81 // for the ::VisitLoadLiteralInstr, ::VisitLoadStoreUnsignedOffset or ::VisitUnconditionalBranch
82 // function addresess key values.
83 // N.B. the mapping are many to one.
84 if (std::regex_match(form, std::regex("(ldrsw|ldr|prfm)_(32|64|d|b|h|q|s)_loadlit"))) {
85 VisitLoadLiteralInstr(instr);
86 return;
87 }
88
89 if (std::regex_match(form, std::regex(
90 "(ldrb|ldrh|ldrsb|ldrsh|ldrsw|ldr|prfm|strb|strh|str)_(32|64|d|b|h|q|s)_ldst_pos"))) {
91 VisitLoadStoreUnsignedOffsetInstr(instr);
92 return;
93 }
94
95 if (std::regex_match(form, std::regex("(bl|b)_only_branch_imm"))) {
96 VisitUnconditionalBranchInstr(instr);
97 return;
98 }
99}
100
101void CustomDisassembler::VisitLoadLiteralInstr(const Instruction* instr) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000102 if (!read_literals_) {
103 return;
104 }
105
Aart Bikd3059e72016-05-11 10:30:47 -0700106 // Get address of literal. Bail if not within expected buffer range to
107 // avoid trying to fetch invalid literals (we can encounter this when
108 // interpreting raw data as instructions).
Scott Wakeling97c72b72016-06-24 16:19:36 +0100109 void* data_address = instr->GetLiteralAddress<void*>();
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +0000110
Aart Bikd3059e72016-05-11 10:30:47 -0700111 if (data_address < base_address_ || data_address >= end_address_) {
112 AppendToOutput(" (?)");
113 return;
114 }
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000115
Aart Bikd3059e72016-05-11 10:30:47 -0700116 // Output information on literal.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100117 Instr op = instr->Mask(LoadLiteralMask);
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000118 switch (op) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100119 case LDR_w_lit:
120 case LDR_x_lit:
121 case LDRSW_x_lit: {
122 int64_t data = op == LDR_x_lit ? *reinterpret_cast<int64_t*>(data_address)
123 : *reinterpret_cast<int32_t*>(data_address);
Zheng Xua34e7602015-02-03 12:03:15 +0800124 AppendToOutput(" (0x%" PRIx64 " / %" PRId64 ")", data, data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000125 break;
126 }
Scott Wakeling97c72b72016-06-24 16:19:36 +0100127 case LDR_s_lit:
128 case LDR_d_lit: {
129 double data = (op == LDR_s_lit) ? *reinterpret_cast<float*>(data_address)
130 : *reinterpret_cast<double*>(data_address);
Zheng Xua34e7602015-02-03 12:03:15 +0800131 AppendToOutput(" (%g)", data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000132 break;
133 }
134 default:
135 break;
136 }
137}
138
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +0000139void CustomDisassembler::VisitLoadStoreUnsignedOffsetInstr(const Instruction* instr) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100140 if (instr->GetRn() == TR) {
Vladimir Marko8feddbc2020-09-03 09:59:45 +0100141 AppendThreadOfsetName(instr);
Zheng Xua34e7602015-02-03 12:03:15 +0800142 }
143}
144
Greg Cawthornebb3ef5a2021-12-21 22:01:14 +0000145void CustomDisassembler::VisitUnconditionalBranchInstr(const Instruction* instr) {
Vladimir Marko8feddbc2020-09-03 09:59:45 +0100146 if (instr->Mask(UnconditionalBranchMask) == BL) {
147 const Instruction* target = instr->GetImmPCOffsetTarget();
148 if (target >= base_address_ &&
149 target < end_address_ &&
150 target->Mask(LoadStoreMask) == LDR_x &&
151 target->GetRn() == TR &&
152 target->GetRt() == IP0 &&
153 target->GetNextInstruction() < end_address_ &&
154 target->GetNextInstruction()->Mask(UnconditionalBranchToRegisterMask) == BR &&
155 target->GetNextInstruction()->GetRn() == IP0) {
156 AppendThreadOfsetName(target);
157 }
158 }
159}
160
161void CustomDisassembler::AppendThreadOfsetName(const vixl::aarch64::Instruction* instr) {
162 int64_t offset = instr->GetImmLSUnsigned() << instr->GetSizeLS();
163 std::ostringstream tmp_stream;
164 options_->thread_offset_name_function_(tmp_stream, static_cast<uint32_t>(offset));
165 AppendToOutput(" ; %s", tmp_stream.str().c_str());
166}
167
Serban Constantinescue6622be2014-02-27 15:36:47 +0000168size_t DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100169 const Instruction* instr = reinterpret_cast<const Instruction*>(begin);
Alexandre Ramesfef019c2014-10-10 17:14:18 +0100170 decoder.Decode(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +0000171 os << FormatInstructionPointer(begin)
Scott Wakeling97c72b72016-06-24 16:19:36 +0100172 << StringPrintf(": %08x\t%s\n", instr->GetInstructionBits(), disasm.GetOutput());
173 return kInstructionSize;
Serban Constantinescue6622be2014-02-27 15:36:47 +0000174}
175
176void DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100177 for (const uint8_t* cur = begin; cur < end; cur += kInstructionSize) {
Serban Constantinescue6622be2014-02-27 15:36:47 +0000178 Dump(os, cur);
179 }
180}
181
182} // namespace arm64
183} // namespace art