1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "disassembler_arm64.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include <inttypes.h>
20*795d594fSAndroid Build Coastguard Worker
21*795d594fSAndroid Build Coastguard Worker #include <regex>
22*795d594fSAndroid Build Coastguard Worker
23*795d594fSAndroid Build Coastguard Worker #include <sstream>
24*795d594fSAndroid Build Coastguard Worker
25*795d594fSAndroid Build Coastguard Worker #include "android-base/logging.h"
26*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
29*795d594fSAndroid Build Coastguard Worker
30*795d594fSAndroid Build Coastguard Worker using namespace vixl::aarch64; // NOLINT(build/namespaces)
31*795d594fSAndroid Build Coastguard Worker
32*795d594fSAndroid Build Coastguard Worker namespace art {
33*795d594fSAndroid Build Coastguard Worker namespace arm64 {
34*795d594fSAndroid Build Coastguard Worker
35*795d594fSAndroid Build Coastguard Worker // This enumeration should mirror the declarations in
36*795d594fSAndroid Build Coastguard Worker // runtime/arch/arm64/registers_arm64.h. We do not include that file to
37*795d594fSAndroid Build Coastguard Worker // avoid a dependency on libart.
38*795d594fSAndroid Build Coastguard Worker enum {
39*795d594fSAndroid Build Coastguard Worker TR = 19,
40*795d594fSAndroid Build Coastguard Worker IP0 = 16,
41*795d594fSAndroid Build Coastguard Worker IP1 = 17,
42*795d594fSAndroid Build Coastguard Worker FP = 29,
43*795d594fSAndroid Build Coastguard Worker LR = 30
44*795d594fSAndroid Build Coastguard Worker };
45*795d594fSAndroid Build Coastguard Worker
AppendRegisterNameToOutput(const Instruction * instr,const CPURegister & reg)46*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::AppendRegisterNameToOutput(const Instruction* instr,
47*795d594fSAndroid Build Coastguard Worker const CPURegister& reg) {
48*795d594fSAndroid Build Coastguard Worker USE(instr);
49*795d594fSAndroid Build Coastguard Worker if (reg.IsRegister() && reg.Is64Bits()) {
50*795d594fSAndroid Build Coastguard Worker if (reg.GetCode() == TR) {
51*795d594fSAndroid Build Coastguard Worker AppendToOutput("tr");
52*795d594fSAndroid Build Coastguard Worker return;
53*795d594fSAndroid Build Coastguard Worker } else if (reg.GetCode() == LR) {
54*795d594fSAndroid Build Coastguard Worker AppendToOutput("lr");
55*795d594fSAndroid Build Coastguard Worker return;
56*795d594fSAndroid Build Coastguard Worker }
57*795d594fSAndroid Build Coastguard Worker // Fall through.
58*795d594fSAndroid Build Coastguard Worker }
59*795d594fSAndroid Build Coastguard Worker // Print other register names as usual.
60*795d594fSAndroid Build Coastguard Worker Disassembler::AppendRegisterNameToOutput(instr, reg);
61*795d594fSAndroid Build Coastguard Worker }
62*795d594fSAndroid Build Coastguard Worker
AppendCodeRelativeAddressToOutput(const Instruction * instr,const void * addr)63*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::AppendCodeRelativeAddressToOutput(const Instruction* instr,
64*795d594fSAndroid Build Coastguard Worker const void* addr) {
65*795d594fSAndroid Build Coastguard Worker USE(instr);
66*795d594fSAndroid Build Coastguard Worker int64_t rel_addr = CodeRelativeAddress(addr);
67*795d594fSAndroid Build Coastguard Worker if (rel_addr >= 0) {
68*795d594fSAndroid Build Coastguard Worker AppendToOutput("(addr 0x%08" PRIx64 ")", rel_addr);
69*795d594fSAndroid Build Coastguard Worker } else {
70*795d594fSAndroid Build Coastguard Worker AppendToOutput("(addr -0x%08" PRIx64 ")", -rel_addr);
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker }
73*795d594fSAndroid Build Coastguard Worker
Visit(vixl::aarch64::Metadata * metadata,const Instruction * instr)74*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::Visit(vixl::aarch64::Metadata* metadata, const Instruction* instr) {
75*795d594fSAndroid Build Coastguard Worker vixl::aarch64::Disassembler::Visit(metadata, instr);
76*795d594fSAndroid Build Coastguard Worker const std::string& form = (*metadata)["form"];
77*795d594fSAndroid Build Coastguard Worker
78*795d594fSAndroid Build Coastguard Worker // These regexs are long, but it is an attempt to match the mapping entry keys in the
79*795d594fSAndroid Build Coastguard Worker // #define DEFAULT_FORM_TO_VISITOR_MAP(VISITORCLASS) in the file
80*795d594fSAndroid Build Coastguard Worker // external/vixl/src/aarch64/decoder-visitor-map-aarch64.h
81*795d594fSAndroid Build Coastguard Worker // for the ::VisitLoadLiteralInstr, ::VisitLoadStoreUnsignedOffset or ::VisitUnconditionalBranch
82*795d594fSAndroid Build Coastguard Worker // function addresess key values.
83*795d594fSAndroid Build Coastguard Worker // N.B. the mapping are many to one.
84*795d594fSAndroid Build Coastguard Worker if (std::regex_match(form, std::regex("(ldrsw|ldr|prfm)_(32|64|d|b|h|q|s)_loadlit"))) {
85*795d594fSAndroid Build Coastguard Worker VisitLoadLiteralInstr(instr);
86*795d594fSAndroid Build Coastguard Worker return;
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker
89*795d594fSAndroid Build Coastguard Worker if (std::regex_match(form, std::regex(
90*795d594fSAndroid Build Coastguard Worker "(ldrb|ldrh|ldrsb|ldrsh|ldrsw|ldr|prfm|strb|strh|str)_(32|64|d|b|h|q|s)_ldst_pos"))) {
91*795d594fSAndroid Build Coastguard Worker VisitLoadStoreUnsignedOffsetInstr(instr);
92*795d594fSAndroid Build Coastguard Worker return;
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
95*795d594fSAndroid Build Coastguard Worker if (std::regex_match(form, std::regex("(bl|b)_only_branch_imm"))) {
96*795d594fSAndroid Build Coastguard Worker VisitUnconditionalBranchInstr(instr);
97*795d594fSAndroid Build Coastguard Worker return;
98*795d594fSAndroid Build Coastguard Worker }
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker
VisitLoadLiteralInstr(const Instruction * instr)101*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::VisitLoadLiteralInstr(const Instruction* instr) {
102*795d594fSAndroid Build Coastguard Worker if (!read_literals_) {
103*795d594fSAndroid Build Coastguard Worker return;
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker
106*795d594fSAndroid Build Coastguard Worker // Get address of literal. Bail if not within expected buffer range to
107*795d594fSAndroid Build Coastguard Worker // avoid trying to fetch invalid literals (we can encounter this when
108*795d594fSAndroid Build Coastguard Worker // interpreting raw data as instructions).
109*795d594fSAndroid Build Coastguard Worker void* data_address = instr->GetLiteralAddress<void*>();
110*795d594fSAndroid Build Coastguard Worker
111*795d594fSAndroid Build Coastguard Worker if (data_address < base_address_ || data_address >= end_address_) {
112*795d594fSAndroid Build Coastguard Worker AppendToOutput(" (?)");
113*795d594fSAndroid Build Coastguard Worker return;
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker
116*795d594fSAndroid Build Coastguard Worker // Output information on literal.
117*795d594fSAndroid Build Coastguard Worker Instr op = instr->Mask(LoadLiteralMask);
118*795d594fSAndroid Build Coastguard Worker switch (op) {
119*795d594fSAndroid Build Coastguard Worker case LDR_w_lit:
120*795d594fSAndroid Build Coastguard Worker case LDR_x_lit:
121*795d594fSAndroid Build Coastguard Worker case LDRSW_x_lit: {
122*795d594fSAndroid Build Coastguard Worker int64_t data = op == LDR_x_lit ? *reinterpret_cast<int64_t*>(data_address)
123*795d594fSAndroid Build Coastguard Worker : *reinterpret_cast<int32_t*>(data_address);
124*795d594fSAndroid Build Coastguard Worker AppendToOutput(" (0x%" PRIx64 " / %" PRId64 ")", data, data);
125*795d594fSAndroid Build Coastguard Worker break;
126*795d594fSAndroid Build Coastguard Worker }
127*795d594fSAndroid Build Coastguard Worker case LDR_s_lit:
128*795d594fSAndroid Build Coastguard Worker case LDR_d_lit: {
129*795d594fSAndroid Build Coastguard Worker double data = (op == LDR_s_lit) ? *reinterpret_cast<float*>(data_address)
130*795d594fSAndroid Build Coastguard Worker : *reinterpret_cast<double*>(data_address);
131*795d594fSAndroid Build Coastguard Worker AppendToOutput(" (%g)", data);
132*795d594fSAndroid Build Coastguard Worker break;
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker default:
135*795d594fSAndroid Build Coastguard Worker break;
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker
VisitLoadStoreUnsignedOffsetInstr(const Instruction * instr)139*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::VisitLoadStoreUnsignedOffsetInstr(const Instruction* instr) {
140*795d594fSAndroid Build Coastguard Worker if (instr->GetRn() == TR) {
141*795d594fSAndroid Build Coastguard Worker AppendThreadOfsetName(instr);
142*795d594fSAndroid Build Coastguard Worker }
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker
VisitUnconditionalBranchInstr(const Instruction * instr)145*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::VisitUnconditionalBranchInstr(const Instruction* instr) {
146*795d594fSAndroid Build Coastguard Worker if (instr->Mask(UnconditionalBranchMask) == BL) {
147*795d594fSAndroid Build Coastguard Worker const Instruction* target = instr->GetImmPCOffsetTarget();
148*795d594fSAndroid Build Coastguard Worker if (target >= base_address_ &&
149*795d594fSAndroid Build Coastguard Worker target < end_address_ &&
150*795d594fSAndroid Build Coastguard Worker target->Mask(LoadStoreMask) == LDR_x &&
151*795d594fSAndroid Build Coastguard Worker target->GetRn() == TR &&
152*795d594fSAndroid Build Coastguard Worker target->GetRt() == IP0 &&
153*795d594fSAndroid Build Coastguard Worker target->GetNextInstruction() < end_address_ &&
154*795d594fSAndroid Build Coastguard Worker target->GetNextInstruction()->Mask(UnconditionalBranchToRegisterMask) == BR &&
155*795d594fSAndroid Build Coastguard Worker target->GetNextInstruction()->GetRn() == IP0) {
156*795d594fSAndroid Build Coastguard Worker AppendThreadOfsetName(target);
157*795d594fSAndroid Build Coastguard Worker }
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker }
160*795d594fSAndroid Build Coastguard Worker
AppendThreadOfsetName(const vixl::aarch64::Instruction * instr)161*795d594fSAndroid Build Coastguard Worker void CustomDisassembler::AppendThreadOfsetName(const vixl::aarch64::Instruction* instr) {
162*795d594fSAndroid Build Coastguard Worker int64_t offset = instr->GetImmLSUnsigned() << instr->GetSizeLS();
163*795d594fSAndroid Build Coastguard Worker std::ostringstream tmp_stream;
164*795d594fSAndroid Build Coastguard Worker options_->thread_offset_name_function_(tmp_stream, static_cast<uint32_t>(offset));
165*795d594fSAndroid Build Coastguard Worker AppendToOutput(" ; %s", tmp_stream.str().c_str());
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker
Dump(std::ostream & os,const uint8_t * begin)168*795d594fSAndroid Build Coastguard Worker size_t DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin) {
169*795d594fSAndroid Build Coastguard Worker const Instruction* instr = reinterpret_cast<const Instruction*>(begin);
170*795d594fSAndroid Build Coastguard Worker decoder.Decode(instr);
171*795d594fSAndroid Build Coastguard Worker os << FormatInstructionPointer(begin)
172*795d594fSAndroid Build Coastguard Worker << StringPrintf(": %08x\t%s\n", instr->GetInstructionBits(), disasm.GetOutput());
173*795d594fSAndroid Build Coastguard Worker return kInstructionSize;
174*795d594fSAndroid Build Coastguard Worker }
175*795d594fSAndroid Build Coastguard Worker
Dump(std::ostream & os,const uint8_t * begin,const uint8_t * end)176*795d594fSAndroid Build Coastguard Worker void DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
177*795d594fSAndroid Build Coastguard Worker for (const uint8_t* cur = begin; cur < end; cur += kInstructionSize) {
178*795d594fSAndroid Build Coastguard Worker Dump(os, cur);
179*795d594fSAndroid Build Coastguard Worker }
180*795d594fSAndroid Build Coastguard Worker }
181*795d594fSAndroid Build Coastguard Worker
182*795d594fSAndroid Build Coastguard Worker } // namespace arm64
183*795d594fSAndroid Build Coastguard Worker } // namespace art
184