Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1 | /* |
| 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 Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <string> |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 21 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 22 | #include "android-base/logging.h" |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 23 | |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 24 | #include "arch/arm/registers_arm.h" |
Andreas Gampe | 2a5c468 | 2015-08-14 08:22:54 -0700 | [diff] [blame] | 25 | #include "base/bit_utils.h" |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 26 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 27 | #pragma GCC diagnostic push |
| 28 | #pragma GCC diagnostic ignored "-Wshadow" |
| 29 | #include "aarch32/instructions-aarch32.h" |
| 30 | #include "aarch32/disasm-aarch32.h" |
| 31 | #pragma GCC diagnostic pop |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 32 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 33 | namespace art { |
| 34 | namespace arm { |
| 35 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 36 | using vixl::aarch32::MemOperand; |
| 37 | using vixl::aarch32::PrintDisassembler; |
| 38 | using vixl::aarch32::pc; |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 39 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 40 | static const vixl::aarch32::Register tr(TR); |
| 41 | |
| 42 | class DisassemblerArm::CustomDisassembler FINAL : public PrintDisassembler { |
| 43 | class CustomDisassemblerStream FINAL : public DisassemblerStream { |
| 44 | public: |
| 45 | CustomDisassemblerStream(std::ostream& os, |
| 46 | const CustomDisassembler* disasm, |
| 47 | const DisassemblerOptions* options) |
| 48 | : DisassemblerStream(os), disasm_(disasm), options_(options) {} |
| 49 | |
| 50 | DisassemblerStream& operator<<(const PrintLabel& label) OVERRIDE { |
| 51 | 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: { |
| 66 | const uintptr_t pc_delta = disasm_->IsT32() |
| 67 | ? vixl::aarch32::kT32PcDelta |
| 68 | : vixl::aarch32::kA32PcDelta; |
| 69 | const int32_t offset = label.GetLabel()->GetLocation(); |
| 70 | |
| 71 | os() << "[pc, #" << offset - pc_delta << "]"; |
| 72 | PrintLiteral(type, offset); |
| 73 | return *this; |
| 74 | } |
| 75 | default: |
| 76 | return DisassemblerStream::operator<<(label); |
| 77 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 78 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 79 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 80 | DisassemblerStream& operator<<(const vixl::aarch32::Register reg) OVERRIDE { |
| 81 | if (reg.Is(tr)) { |
| 82 | os() << "tr"; |
| 83 | return *this; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 84 | } else { |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 85 | return DisassemblerStream::operator<<(reg); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 86 | } |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 87 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 88 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 89 | DisassemblerStream& operator<<(const MemOperand& operand) OVERRIDE { |
| 90 | // VIXL must use a PrintLabel object whenever the base register is PC; |
| 91 | // the following check verifies this invariant, and guards against bugs. |
| 92 | DCHECK(!operand.GetBaseRegister().Is(pc)); |
| 93 | DisassemblerStream::operator<<(operand); |
Vladimir Marko | dd577a3 | 2013-11-07 19:25:24 +0000 | [diff] [blame] | 94 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 95 | if (operand.GetBaseRegister().Is(tr) && operand.IsImmediate()) { |
| 96 | os() << " ; "; |
| 97 | options_->thread_offset_name_function_(os(), operand.GetOffsetImmediate()); |
| 98 | } |
Vladimir Marko | dd577a3 | 2013-11-07 19:25:24 +0000 | [diff] [blame] | 99 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 100 | return *this; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 101 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 102 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 103 | DisassemblerStream& operator<<(const vixl::aarch32::AlignedMemOperand& operand) OVERRIDE { |
| 104 | // VIXL must use a PrintLabel object whenever the base register is PC; |
| 105 | // the following check verifies this invariant, and guards against bugs. |
| 106 | DCHECK(!operand.GetBaseRegister().Is(pc)); |
| 107 | return DisassemblerStream::operator<<(operand); |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 108 | } |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 109 | |
| 110 | private: |
| 111 | void PrintLiteral(LocationType type, int32_t offset); |
| 112 | |
| 113 | const CustomDisassembler* disasm_; |
| 114 | const DisassemblerOptions* options_; |
| 115 | }; |
| 116 | |
| 117 | public: |
| 118 | CustomDisassembler(std::ostream& os, const DisassemblerOptions* options) |
Alexandre Rames | 8872cad | 2016-09-26 14:14:01 +0100 | [diff] [blame] | 119 | : PrintDisassembler(&disassembler_stream_), disassembler_stream_(os, this, options) {} |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 120 | |
| 121 | void PrintPc(uint32_t prog_ctr) OVERRIDE { |
| 122 | os() << "0x" << std::hex << std::setw(8) << std::setfill('0') << prog_ctr << ": "; |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 123 | } |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 124 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 125 | bool IsT32() const { |
| 126 | return is_t32_; |
| 127 | } |
Vladimir Marko | c777e0d | 2014-04-03 17:59:02 +0100 | [diff] [blame] | 128 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 129 | void SetT32(bool is_t32) { |
| 130 | is_t32_ = is_t32; |
| 131 | } |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 132 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 133 | private: |
| 134 | bool is_t32_; |
Alexandre Rames | 8872cad | 2016-09-26 14:14:01 +0100 | [diff] [blame] | 135 | CustomDisassemblerStream disassembler_stream_; |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 136 | }; |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 137 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 138 | void DisassemblerArm::CustomDisassembler::CustomDisassemblerStream::PrintLiteral(LocationType type, |
| 139 | int32_t offset) { |
| 140 | // Literal offsets are not required to be aligned, so we may need unaligned access. |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 141 | typedef const int16_t unaligned_int16_t __attribute__ ((aligned (1))); |
| 142 | typedef const uint16_t unaligned_uint16_t __attribute__ ((aligned (1))); |
| 143 | typedef const int32_t unaligned_int32_t __attribute__ ((aligned (1))); |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 144 | typedef const int64_t unaligned_int64_t __attribute__ ((aligned (1))); |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 145 | typedef const float unaligned_float __attribute__ ((aligned (1))); |
| 146 | typedef const double unaligned_double __attribute__ ((aligned (1))); |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 147 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 148 | // Zeros are used for the LocationType values this function does not care about. |
| 149 | const size_t literal_size[kVst4Location + 1] = { |
| 150 | 0, 0, 0, 0, sizeof(uint8_t), sizeof(unaligned_uint16_t), sizeof(unaligned_int32_t), |
| 151 | sizeof(unaligned_int64_t), sizeof(int8_t), sizeof(unaligned_int16_t), |
| 152 | sizeof(unaligned_float), sizeof(unaligned_double)}; |
| 153 | const uintptr_t begin = reinterpret_cast<uintptr_t>(options_->base_address_); |
| 154 | const uintptr_t end = reinterpret_cast<uintptr_t>(options_->end_address_); |
| 155 | uintptr_t literal_addr = RoundDown(disasm_->GetPc(), vixl::aarch32::kRegSizeInBytes) + offset; |
| 156 | |
| 157 | if (!options_->absolute_addresses_) { |
| 158 | literal_addr += begin; |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 161 | os() << " ; "; |
| 162 | |
| 163 | // Bail out if not within expected buffer range to avoid trying to fetch invalid literals |
| 164 | // (we can encounter them when interpreting raw data as instructions). |
| 165 | if (literal_addr < begin || literal_addr > end - literal_size[type]) { |
| 166 | os() << "(?)"; |
| 167 | } else { |
| 168 | switch (type) { |
| 169 | case kLoadByteLocation: |
| 170 | os() << *reinterpret_cast<const uint8_t*>(literal_addr); |
| 171 | break; |
| 172 | case kLoadHalfWordLocation: |
| 173 | os() << *reinterpret_cast<unaligned_uint16_t*>(literal_addr); |
| 174 | break; |
| 175 | case kLoadWordLocation: { |
| 176 | const int32_t value = *reinterpret_cast<unaligned_int32_t*>(literal_addr); |
| 177 | os() << "0x" << std::hex << std::setw(8) << std::setfill('0') << value; |
| 178 | break; |
| 179 | } |
| 180 | case kLoadDoubleWordLocation: { |
| 181 | const int64_t value = *reinterpret_cast<unaligned_int64_t*>(literal_addr); |
| 182 | os() << "0x" << std::hex << std::setw(16) << std::setfill('0') << value; |
| 183 | break; |
| 184 | } |
| 185 | case kLoadSignedByteLocation: |
| 186 | os() << *reinterpret_cast<const int8_t*>(literal_addr); |
| 187 | break; |
| 188 | case kLoadSignedHalfWordLocation: |
| 189 | os() << *reinterpret_cast<unaligned_int16_t*>(literal_addr); |
| 190 | break; |
| 191 | case kLoadSinglePrecisionLocation: |
| 192 | os() << *reinterpret_cast<unaligned_float*>(literal_addr); |
| 193 | break; |
| 194 | case kLoadDoublePrecisionLocation: |
| 195 | os() << *reinterpret_cast<unaligned_double*>(literal_addr); |
| 196 | break; |
| 197 | default: |
| 198 | UNIMPLEMENTED(FATAL) << "Unexpected literal type: " << type; |
| 199 | } |
Vladimir Marko | 55d7c18 | 2015-01-05 15:17:01 +0000 | [diff] [blame] | 200 | } |
Vladimir Marko | c777e0d | 2014-04-03 17:59:02 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 203 | DisassemblerArm::DisassemblerArm(DisassemblerOptions* options) |
| 204 | : Disassembler(options), disasm_(std::make_unique<CustomDisassembler>(output_, options)) {} |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 205 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 206 | size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) { |
| 207 | uintptr_t next; |
| 208 | // Remove the Thumb specifier bit; no effect if begin does not point to T32 code. |
| 209 | const uintptr_t instr_ptr = reinterpret_cast<uintptr_t>(begin) & ~1; |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 210 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 211 | disasm_->SetT32((reinterpret_cast<uintptr_t>(begin) & 1) != 0); |
| 212 | disasm_->JumpToPc(GetPc(instr_ptr)); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 213 | |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 214 | if (disasm_->IsT32()) { |
| 215 | const uint16_t* const ip = reinterpret_cast<const uint16_t*>(instr_ptr); |
| 216 | next = reinterpret_cast<uintptr_t>(disasm_->DecodeT32At(ip)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 217 | } else { |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 218 | const uint32_t* const ip = reinterpret_cast<const uint32_t*>(instr_ptr); |
| 219 | next = reinterpret_cast<uintptr_t>(disasm_->DecodeA32At(ip)); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 220 | } |
Anton Kirilov | 29b0cde | 2016-09-06 13:01:03 +0100 | [diff] [blame] | 221 | |
| 222 | os << output_.str(); |
| 223 | output_.str(std::string()); |
| 224 | return next - instr_ptr; |
| 225 | } |
| 226 | |
| 227 | void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
| 228 | DCHECK_LE(begin, end); |
| 229 | |
| 230 | // Remove the Thumb specifier bit; no effect if begin does not point to T32 code. |
| 231 | const uintptr_t base = reinterpret_cast<uintptr_t>(begin) & ~1; |
| 232 | |
| 233 | disasm_->SetT32((reinterpret_cast<uintptr_t>(begin) & 1) != 0); |
| 234 | disasm_->JumpToPc(GetPc(base)); |
| 235 | |
| 236 | if (disasm_->IsT32()) { |
| 237 | // The Thumb specifier bits cancel each other. |
| 238 | disasm_->DisassembleT32Buffer(reinterpret_cast<const uint16_t*>(base), end - begin); |
| 239 | } else { |
| 240 | disasm_->DisassembleA32Buffer(reinterpret_cast<const uint32_t*>(base), end - begin); |
| 241 | } |
| 242 | |
| 243 | os << output_.str(); |
| 244 | output_.str(std::string()); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | } // namespace arm |
| 248 | } // namespace art |