blob: c2156ca5e127676cbd8b34cb6c9f6c16d9c75f1e [file] [log] [blame]
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001/*
2 * Copyright (C) 2012 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_arm.h"
18
Anton Kirilov29b0cde2016-09-06 13:01:03 +010019#include <memory>
20#include <string>
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080021
Andreas Gampebda1d602016-08-29 17:43:45 -070022#include "android-base/logging.h"
Andreas Gampebda1d602016-08-29 17:43:45 -070023
Vladimir Marko55d7c182015-01-05 15:17:01 +000024#include "arch/arm/registers_arm.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070025#include "base/bit_utils.h"
Andreas Gampebda1d602016-08-29 17:43:45 -070026
Anton Kirilov29b0cde2016-09-06 13:01:03 +010027#pragma GCC diagnostic push
28#pragma GCC diagnostic ignored "-Wshadow"
Anton Kirilov29b0cde2016-09-06 13:01:03 +010029#include "aarch32/disasm-aarch32.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070030#include "aarch32/instructions-aarch32.h"
Anton Kirilov29b0cde2016-09-06 13:01:03 +010031#pragma GCC diagnostic pop
Elliott Hughes0f3c5532012-03-30 14:51:51 -070032
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080033namespace art {
34namespace arm {
35
Anton Kirilov29b0cde2016-09-06 13:01:03 +010036using vixl::aarch32::MemOperand;
37using vixl::aarch32::PrintDisassembler;
38using vixl::aarch32::pc;
Ian Rogersb23a7722012-10-09 16:54:26 -070039
Anton Kirilov29b0cde2016-09-06 13:01:03 +010040static const vixl::aarch32::Register tr(TR);
41
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010042class DisassemblerArm::CustomDisassembler final : public PrintDisassembler {
43 class CustomDisassemblerStream final : public DisassemblerStream {
Anton Kirilov29b0cde2016-09-06 13:01:03 +010044 public:
45 CustomDisassemblerStream(std::ostream& os,
46 const CustomDisassembler* disasm,
47 const DisassemblerOptions* options)
48 : DisassemblerStream(os), disasm_(disasm), options_(options) {}
49
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010050 DisassemblerStream& operator<<(const PrintLabel& label) override {
Anton Kirilov29b0cde2016-09-06 13:01:03 +010051 const LocationType type = label.GetLocationType();
52
53 switch (type) {
54 case kLoadByteLocation:
55 case kLoadHalfWordLocation:
56 case kLoadWordLocation:
57 case kLoadDoubleWordLocation:
58 case kLoadSignedByteLocation:
59 case kLoadSignedHalfWordLocation:
60 case kLoadSinglePrecisionLocation:
61 case kLoadDoublePrecisionLocation:
62 case kVld1Location:
63 case kVld2Location:
64 case kVld3Location:
65 case kVld4Location: {
Artem Serov672b9c12017-12-05 18:04:07 +000066 const int32_t offset = label.GetImmediate();
67 os() << "[pc, #" << offset << "]";
Anton Kirilov29b0cde2016-09-06 13:01:03 +010068 PrintLiteral(type, offset);
69 return *this;
70 }
Vladimir Marko8feddbc2020-09-03 09:59:45 +010071 case kCodeLocation:
72 DisassemblerStream::operator<<(label);
73 // Improve the disassembly of branch to thunk jumping to pointer from thread entrypoint.
74 if (disasm_->GetIsT32() && GetCurrentInstructionType() == vixl::aarch32::kBl) {
75 const uintptr_t begin = reinterpret_cast<uintptr_t>(options_->base_address_);
76 const uintptr_t end = reinterpret_cast<uintptr_t>(options_->end_address_);
77 uintptr_t address = label.GetLocation() + (options_->absolute_addresses_ ? 0u : begin);
78 if ((address >= begin && address < end && end - address >= 4u) &&
79 reinterpret_cast<const uint16_t*>(address)[0] == 0xf8d9 && // LDR Rt, [tr, #imm12]
80 (reinterpret_cast<const uint16_t*>(address)[1] >> 12) == 0xf) { // Rt == PC
81 uint32_t imm12 = reinterpret_cast<const uint16_t*>(address)[1] & 0xfffu;
82 os() << " ; ";
83 options_->thread_offset_name_function_(os(), imm12);
84 }
85 }
86 return *this;
Anton Kirilov29b0cde2016-09-06 13:01:03 +010087 default:
88 return DisassemblerStream::operator<<(label);
89 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080090 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 DisassemblerStream& operator<<(vixl::aarch32::Register reg) override {
Anton Kirilov29b0cde2016-09-06 13:01:03 +010093 if (reg.Is(tr)) {
94 os() << "tr";
95 return *this;
Elliott Hughes77405792012-03-15 15:22:12 -070096 } else {
Anton Kirilov29b0cde2016-09-06 13:01:03 +010097 return DisassemblerStream::operator<<(reg);
Elliott Hughes77405792012-03-15 15:22:12 -070098 }
Elliott Hughes77405792012-03-15 15:22:12 -070099 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100101 DisassemblerStream& operator<<(const MemOperand& operand) override {
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100102 // VIXL must use a PrintLabel object whenever the base register is PC;
103 // the following check verifies this invariant, and guards against bugs.
104 DCHECK(!operand.GetBaseRegister().Is(pc));
105 DisassemblerStream::operator<<(operand);
Vladimir Markodd577a32013-11-07 19:25:24 +0000106
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100107 if (operand.GetBaseRegister().Is(tr) && operand.IsImmediate()) {
108 os() << " ; ";
109 options_->thread_offset_name_function_(os(), operand.GetOffsetImmediate());
110 }
Vladimir Markodd577a32013-11-07 19:25:24 +0000111
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100112 return *this;
Elliott Hughes77405792012-03-15 15:22:12 -0700113 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100115 DisassemblerStream& operator<<(const vixl::aarch32::AlignedMemOperand& operand) override {
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100116 // VIXL must use a PrintLabel object whenever the base register is PC;
117 // the following check verifies this invariant, and guards against bugs.
118 DCHECK(!operand.GetBaseRegister().Is(pc));
119 return DisassemblerStream::operator<<(operand);
Ian Rogersa9650dd2013-10-04 08:23:32 -0700120 }
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100121
122 private:
123 void PrintLiteral(LocationType type, int32_t offset);
124
125 const CustomDisassembler* disasm_;
126 const DisassemblerOptions* options_;
127 };
128
129 public:
130 CustomDisassembler(std::ostream& os, const DisassemblerOptions* options)
Artem Serov672b9c12017-12-05 18:04:07 +0000131 : PrintDisassembler(&disassembler_stream_),
132 disassembler_stream_(os, this, options),
133 is_t32_(true) {}
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100135 void PrintCodeAddress(uint32_t prog_ctr) override {
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100136 os() << "0x" << std::hex << std::setw(8) << std::setfill('0') << prog_ctr << ": ";
Ian Rogersa9650dd2013-10-04 08:23:32 -0700137 }
Ian Rogersa9650dd2013-10-04 08:23:32 -0700138
Artem Serov672b9c12017-12-05 18:04:07 +0000139 void SetIsT32(bool is_t32) {
140 is_t32_ = is_t32;
141 }
142
143 bool GetIsT32() const {
144 return is_t32_;
145 }
146
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100147 private:
Alexandre Rames8872cad2016-09-26 14:14:01 +0100148 CustomDisassemblerStream disassembler_stream_;
Artem Serov672b9c12017-12-05 18:04:07 +0000149 // Whether T32 stream is decoded.
150 bool is_t32_;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000151};
Vladimir Marko55d7c182015-01-05 15:17:01 +0000152
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100153void DisassemblerArm::CustomDisassembler::CustomDisassemblerStream::PrintLiteral(LocationType type,
154 int32_t offset) {
155 // Literal offsets are not required to be aligned, so we may need unaligned access.
Andreas Gampec55bb392018-09-21 00:02:02 +0000156 using unaligned_int16_t __attribute__((__aligned__(1))) = const int16_t;
157 using unaligned_uint16_t __attribute__((__aligned__(1))) = const uint16_t;
158 using unaligned_int32_t __attribute__((__aligned__(1))) = const int32_t;
159 using unaligned_int64_t __attribute__((__aligned__(1))) = const int64_t;
160 using unaligned_float __attribute__((__aligned__(1))) = const float;
161 using unaligned_double __attribute__((__aligned__(1))) = const double;
Vladimir Marko55d7c182015-01-05 15:17:01 +0000162
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100163 // Zeros are used for the LocationType values this function does not care about.
164 const size_t literal_size[kVst4Location + 1] = {
165 0, 0, 0, 0, sizeof(uint8_t), sizeof(unaligned_uint16_t), sizeof(unaligned_int32_t),
166 sizeof(unaligned_int64_t), sizeof(int8_t), sizeof(unaligned_int16_t),
167 sizeof(unaligned_float), sizeof(unaligned_double)};
168 const uintptr_t begin = reinterpret_cast<uintptr_t>(options_->base_address_);
169 const uintptr_t end = reinterpret_cast<uintptr_t>(options_->end_address_);
Artem Serov672b9c12017-12-05 18:04:07 +0000170 uintptr_t literal_addr =
171 RoundDown(disasm_->GetCodeAddress(), vixl::aarch32::kRegSizeInBytes) + offset;
172 literal_addr += disasm_->GetIsT32() ? vixl::aarch32::kT32PcDelta : vixl::aarch32::kA32PcDelta;
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100173
174 if (!options_->absolute_addresses_) {
175 literal_addr += begin;
Aart Bikd3059e72016-05-11 10:30:47 -0700176 }
177
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100178 os() << " ; ";
179
180 // Bail out if not within expected buffer range to avoid trying to fetch invalid literals
181 // (we can encounter them when interpreting raw data as instructions).
182 if (literal_addr < begin || literal_addr > end - literal_size[type]) {
183 os() << "(?)";
184 } else {
185 switch (type) {
186 case kLoadByteLocation:
187 os() << *reinterpret_cast<const uint8_t*>(literal_addr);
188 break;
189 case kLoadHalfWordLocation:
190 os() << *reinterpret_cast<unaligned_uint16_t*>(literal_addr);
191 break;
192 case kLoadWordLocation: {
193 const int32_t value = *reinterpret_cast<unaligned_int32_t*>(literal_addr);
194 os() << "0x" << std::hex << std::setw(8) << std::setfill('0') << value;
195 break;
196 }
197 case kLoadDoubleWordLocation: {
198 const int64_t value = *reinterpret_cast<unaligned_int64_t*>(literal_addr);
199 os() << "0x" << std::hex << std::setw(16) << std::setfill('0') << value;
200 break;
201 }
202 case kLoadSignedByteLocation:
203 os() << *reinterpret_cast<const int8_t*>(literal_addr);
204 break;
205 case kLoadSignedHalfWordLocation:
206 os() << *reinterpret_cast<unaligned_int16_t*>(literal_addr);
207 break;
208 case kLoadSinglePrecisionLocation:
209 os() << *reinterpret_cast<unaligned_float*>(literal_addr);
210 break;
211 case kLoadDoublePrecisionLocation:
212 os() << *reinterpret_cast<unaligned_double*>(literal_addr);
213 break;
214 default:
215 UNIMPLEMENTED(FATAL) << "Unexpected literal type: " << type;
216 }
Vladimir Marko55d7c182015-01-05 15:17:01 +0000217 }
Vladimir Markoc777e0d2014-04-03 17:59:02 +0100218}
219
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100220DisassemblerArm::DisassemblerArm(DisassemblerOptions* options)
221 : Disassembler(options), disasm_(std::make_unique<CustomDisassembler>(output_, options)) {}
Elliott Hughes77405792012-03-15 15:22:12 -0700222
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100223size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) {
224 uintptr_t next;
225 // Remove the Thumb specifier bit; no effect if begin does not point to T32 code.
226 const uintptr_t instr_ptr = reinterpret_cast<uintptr_t>(begin) & ~1;
Aart Bikd3059e72016-05-11 10:30:47 -0700227
Scott Wakelingb77051e2016-11-21 19:46:00 +0000228 const bool is_t32 = (reinterpret_cast<uintptr_t>(begin) & 1) != 0;
229 disasm_->SetCodeAddress(GetPc(instr_ptr));
Artem Serov672b9c12017-12-05 18:04:07 +0000230 disasm_->SetIsT32(is_t32);
Dave Allison70202782013-10-22 17:52:19 -0700231
Scott Wakelingb77051e2016-11-21 19:46:00 +0000232 if (is_t32) {
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100233 const uint16_t* const ip = reinterpret_cast<const uint16_t*>(instr_ptr);
Scott Wakelingb77051e2016-11-21 19:46:00 +0000234 const uint16_t* const end_address = reinterpret_cast<const uint16_t*>(
235 GetDisassemblerOptions()->end_address_);
236 next = reinterpret_cast<uintptr_t>(disasm_->DecodeT32At(ip, end_address));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800237 } else {
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100238 const uint32_t* const ip = reinterpret_cast<const uint32_t*>(instr_ptr);
239 next = reinterpret_cast<uintptr_t>(disasm_->DecodeA32At(ip));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800240 }
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100241
242 os << output_.str();
243 output_.str(std::string());
244 return next - instr_ptr;
245}
246
247void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
248 DCHECK_LE(begin, end);
249
250 // Remove the Thumb specifier bit; no effect if begin does not point to T32 code.
251 const uintptr_t base = reinterpret_cast<uintptr_t>(begin) & ~1;
252
Scott Wakelingb77051e2016-11-21 19:46:00 +0000253 const bool is_t32 = (reinterpret_cast<uintptr_t>(begin) & 1) != 0;
254 disasm_->SetCodeAddress(GetPc(base));
Artem Serov672b9c12017-12-05 18:04:07 +0000255 disasm_->SetIsT32(is_t32);
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100256
Scott Wakelingb77051e2016-11-21 19:46:00 +0000257 if (is_t32) {
Anton Kirilov29b0cde2016-09-06 13:01:03 +0100258 // The Thumb specifier bits cancel each other.
259 disasm_->DisassembleT32Buffer(reinterpret_cast<const uint16_t*>(base), end - begin);
260 } else {
261 disasm_->DisassembleA32Buffer(reinterpret_cast<const uint32_t*>(base), end - begin);
262 }
263
264 os << output_.str();
265 output_.str(std::string());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800266}
267
268} // namespace arm
269} // namespace art