blob: 0d51cfdc1ab605d7ced52691349b651902aa4f4f [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
Zheng Xua34e7602015-02-03 12:03:15 +080021#include <sstream>
Serban Constantinescue6622be2014-02-27 15:36:47 +000022
Andreas Gampebda1d602016-08-29 17:43:45 -070023#include "android-base/logging.h"
24#include "android-base/stringprintf.h"
25
26using android::base::StringPrintf;
Serban Constantinescue6622be2014-02-27 15:36:47 +000027
Scott Wakeling97c72b72016-06-24 16:19:36 +010028using namespace vixl::aarch64; // NOLINT(build/namespaces)
29
Serban Constantinescue6622be2014-02-27 15:36:47 +000030namespace art {
31namespace arm64 {
32
Zheng Xua34e7602015-02-03 12:03:15 +080033// 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.
36enum {
Serban Constantinescu9bd88b02015-04-22 16:24:46 +010037 TR = 19,
Zheng Xua34e7602015-02-03 12:03:15 +080038 IP0 = 16,
39 IP1 = 17,
40 FP = 29,
41 LR = 30
42};
43
Scott Wakeling97c72b72016-06-24 16:19:36 +010044void CustomDisassembler::AppendRegisterNameToOutput(const Instruction* instr,
45 const CPURegister& reg) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +000046 USE(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +000047 if (reg.IsRegister() && reg.Is64Bits()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010048 if (reg.GetCode() == TR) {
Alexandre Ramesd737ab32015-03-06 09:11:12 +000049 AppendToOutput("tr");
50 return;
Scott Wakeling97c72b72016-06-24 16:19:36 +010051 } else if (reg.GetCode() == LR) {
Alexandre Ramesd737ab32015-03-06 09:11:12 +000052 AppendToOutput("lr");
53 return;
Alexandre Ramesa37d9252014-10-27 11:28:14 +000054 }
Alexandre Ramesd737ab32015-03-06 09:11:12 +000055 // Fall through.
Alexandre Ramesa37d9252014-10-27 11:28:14 +000056 }
57 // Print other register names as usual.
58 Disassembler::AppendRegisterNameToOutput(instr, reg);
59}
60
Scott Wakeling97c72b72016-06-24 16:19:36 +010061void CustomDisassembler::VisitLoadLiteral(const Instruction* instr) {
Alexandre Ramesa37d9252014-10-27 11:28:14 +000062 Disassembler::VisitLoadLiteral(instr);
63
64 if (!read_literals_) {
65 return;
66 }
67
Aart Bikd3059e72016-05-11 10:30:47 -070068 // 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 Wakeling97c72b72016-06-24 16:19:36 +010071 void* data_address = instr->GetLiteralAddress<void*>();
Aart Bikd3059e72016-05-11 10:30:47 -070072 if (data_address < base_address_ || data_address >= end_address_) {
73 AppendToOutput(" (?)");
74 return;
75 }
Alexandre Ramesa37d9252014-10-27 11:28:14 +000076
Aart Bikd3059e72016-05-11 10:30:47 -070077 // Output information on literal.
Scott Wakeling97c72b72016-06-24 16:19:36 +010078 Instr op = instr->Mask(LoadLiteralMask);
Alexandre Ramesa37d9252014-10-27 11:28:14 +000079 switch (op) {
Scott Wakeling97c72b72016-06-24 16:19:36 +010080 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 Xua34e7602015-02-03 12:03:15 +080085 AppendToOutput(" (0x%" PRIx64 " / %" PRId64 ")", data, data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +000086 break;
87 }
Scott Wakeling97c72b72016-06-24 16:19:36 +010088 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 Xua34e7602015-02-03 12:03:15 +080092 AppendToOutput(" (%g)", data);
Alexandre Ramesa37d9252014-10-27 11:28:14 +000093 break;
94 }
95 default:
96 break;
97 }
98}
99
Scott Wakeling97c72b72016-06-24 16:19:36 +0100100void CustomDisassembler::VisitLoadStoreUnsignedOffset(const Instruction* instr) {
Zheng Xua34e7602015-02-03 12:03:15 +0800101 Disassembler::VisitLoadStoreUnsignedOffset(instr);
102
Scott Wakeling97c72b72016-06-24 16:19:36 +0100103 if (instr->GetRn() == TR) {
Vladimir Marko8feddbc2020-09-03 09:59:45 +0100104 AppendThreadOfsetName(instr);
Zheng Xua34e7602015-02-03 12:03:15 +0800105 }
106}
107
Vladimir Marko8feddbc2020-09-03 09:59:45 +0100108void 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
126void 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 Constantinescue6622be2014-02-27 15:36:47 +0000133size_t DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100134 const Instruction* instr = reinterpret_cast<const Instruction*>(begin);
Alexandre Ramesfef019c2014-10-10 17:14:18 +0100135 decoder.Decode(instr);
Alexandre Ramesd737ab32015-03-06 09:11:12 +0000136 os << FormatInstructionPointer(begin)
Scott Wakeling97c72b72016-06-24 16:19:36 +0100137 << StringPrintf(": %08x\t%s\n", instr->GetInstructionBits(), disasm.GetOutput());
138 return kInstructionSize;
Serban Constantinescue6622be2014-02-27 15:36:47 +0000139}
140
141void DisassemblerArm64::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100142 for (const uint8_t* cur = begin; cur < end; cur += kInstructionSize) {
Serban Constantinescue6622be2014-02-27 15:36:47 +0000143 Dump(os, cur);
144 }
145}
146
147} // namespace arm64
148} // namespace art