Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1 | //===-- DisassemblerLLVMC.cpp -----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | 45a4014 | 2015-10-22 21:24:37 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Project includes |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 13 | #include "DisassemblerLLVMC.h" |
| 14 | |
| 15 | // Other libraries and framework includes |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 16 | #include "llvm-c/Disassembler.h" |
Benjamin Kramer | 79dad1d | 2016-01-26 16:45:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallString.h" |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
| 19 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | 79dad1d | 2016-01-26 16:45:00 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 21 | #include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h" |
| 22 | #include "llvm/MC/MCDisassembler/MCRelocationInfo.h" |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInst.h" |
| 24 | #include "llvm/MC/MCInstPrinter.h" |
| 25 | #include "llvm/MC/MCInstrInfo.h" |
| 26 | #include "llvm/MC/MCRegisterInfo.h" |
| 27 | #include "llvm/MC/MCSubtargetInfo.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ScopedPrinter.h" |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 30 | #include "llvm/Support/TargetRegistry.h" |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 31 | #include "llvm/Support/TargetSelect.h" |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 32 | |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 33 | #include "lldb/Core/Address.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 34 | #include "lldb/Core/Module.h" |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 35 | #include "lldb/Symbol/SymbolContext.h" |
| 36 | #include "lldb/Target/ExecutionContext.h" |
| 37 | #include "lldb/Target/Process.h" |
| 38 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | d5944cd | 2013-12-06 01:12:00 +0000 | [diff] [blame] | 39 | #include "lldb/Target/SectionLoadList.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 40 | #include "lldb/Target/StackFrame.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 42 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 43 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 44 | #include "lldb/Utility/RegularExpression.h" |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 45 | #include "lldb/Utility/Stream.h" |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 46 | |
| 47 | using namespace lldb; |
| 48 | using namespace lldb_private; |
| 49 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 50 | class DisassemblerLLVMC::MCDisasmInstance { |
| 51 | public: |
| 52 | static std::unique_ptr<MCDisasmInstance> |
| 53 | Create(const char *triple, const char *cpu, const char *features_str, |
| 54 | unsigned flavor, DisassemblerLLVMC &owner); |
| 55 | |
| 56 | ~MCDisasmInstance() = default; |
| 57 | |
| 58 | uint64_t GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len, |
| 59 | lldb::addr_t pc, llvm::MCInst &mc_inst) const; |
| 60 | void PrintMCInst(llvm::MCInst &mc_inst, std::string &inst_string, |
| 61 | std::string &comments_string); |
| 62 | void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style); |
| 63 | bool CanBranch(llvm::MCInst &mc_inst) const; |
| 64 | bool HasDelaySlot(llvm::MCInst &mc_inst) const; |
| 65 | bool IsCall(llvm::MCInst &mc_inst) const; |
| 66 | |
| 67 | private: |
| 68 | MCDisasmInstance(std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up, |
| 69 | std::unique_ptr<llvm::MCRegisterInfo> &®_info_up, |
| 70 | std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up, |
| 71 | std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up, |
| 72 | std::unique_ptr<llvm::MCContext> &&context_up, |
| 73 | std::unique_ptr<llvm::MCDisassembler> &&disasm_up, |
| 74 | std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up); |
| 75 | |
| 76 | std::unique_ptr<llvm::MCInstrInfo> m_instr_info_up; |
| 77 | std::unique_ptr<llvm::MCRegisterInfo> m_reg_info_up; |
| 78 | std::unique_ptr<llvm::MCSubtargetInfo> m_subtarget_info_up; |
| 79 | std::unique_ptr<llvm::MCAsmInfo> m_asm_info_up; |
| 80 | std::unique_ptr<llvm::MCContext> m_context_up; |
| 81 | std::unique_ptr<llvm::MCDisassembler> m_disasm_up; |
| 82 | std::unique_ptr<llvm::MCInstPrinter> m_instr_printer_up; |
| 83 | }; |
| 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | class InstructionLLVMC : public lldb_private::Instruction { |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 86 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | InstructionLLVMC(DisassemblerLLVMC &disasm, |
| 88 | const lldb_private::Address &address, |
| 89 | AddressClass addr_class) |
| 90 | : Instruction(address, addr_class), |
| 91 | m_disasm_wp(std::static_pointer_cast<DisassemblerLLVMC>( |
| 92 | disasm.shared_from_this())), |
| 93 | m_does_branch(eLazyBoolCalculate), m_has_delay_slot(eLazyBoolCalculate), |
| 94 | m_is_call(eLazyBoolCalculate), m_is_valid(false), |
| 95 | m_using_file_addr(false) {} |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | ~InstructionLLVMC() override = default; |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | bool DoesBranch() override { |
| 100 | if (m_does_branch == eLazyBoolCalculate) { |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 101 | DisassemblerScope disasm(*this); |
| 102 | if (disasm) { |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 103 | DataExtractor data; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | if (m_opcode.GetData(data)) { |
| 105 | bool is_alternate_isa; |
| 106 | lldb::addr_t pc = m_address.GetFileAddress(); |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 107 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 108 | DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr = |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 109 | GetDisasmToUse(is_alternate_isa, disasm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | const uint8_t *opcode_data = data.GetDataStart(); |
| 111 | const size_t opcode_data_len = data.GetByteSize(); |
| 112 | llvm::MCInst inst; |
| 113 | const size_t inst_size = |
| 114 | mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); |
| 115 | // Be conservative, if we didn't understand the instruction, say it |
| 116 | // might branch... |
| 117 | if (inst_size == 0) |
| 118 | m_does_branch = eLazyBoolYes; |
| 119 | else { |
| 120 | const bool can_branch = mc_disasm_ptr->CanBranch(inst); |
| 121 | if (can_branch) |
| 122 | m_does_branch = eLazyBoolYes; |
| 123 | else |
| 124 | m_does_branch = eLazyBoolNo; |
| 125 | } |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 126 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | } |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 128 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | return m_does_branch == eLazyBoolYes; |
| 130 | } |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | bool HasDelaySlot() override { |
| 133 | if (m_has_delay_slot == eLazyBoolCalculate) { |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 134 | DisassemblerScope disasm(*this); |
| 135 | if (disasm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | DataExtractor data; |
| 137 | if (m_opcode.GetData(data)) { |
| 138 | bool is_alternate_isa; |
| 139 | lldb::addr_t pc = m_address.GetFileAddress(); |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 140 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 141 | DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr = |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 142 | GetDisasmToUse(is_alternate_isa, disasm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | const uint8_t *opcode_data = data.GetDataStart(); |
| 144 | const size_t opcode_data_len = data.GetByteSize(); |
| 145 | llvm::MCInst inst; |
| 146 | const size_t inst_size = |
| 147 | mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); |
| 148 | // if we didn't understand the instruction, say it doesn't have a |
| 149 | // delay slot... |
| 150 | if (inst_size == 0) |
| 151 | m_has_delay_slot = eLazyBoolNo; |
| 152 | else { |
| 153 | const bool has_delay_slot = mc_disasm_ptr->HasDelaySlot(inst); |
| 154 | if (has_delay_slot) |
| 155 | m_has_delay_slot = eLazyBoolYes; |
| 156 | else |
| 157 | m_has_delay_slot = eLazyBoolNo; |
| 158 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 159 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | return m_has_delay_slot == eLazyBoolYes; |
| 163 | } |
| 164 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 165 | DisassemblerLLVMC::MCDisasmInstance *GetDisasmToUse(bool &is_alternate_isa) { |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 166 | DisassemblerScope disasm(*this); |
| 167 | return GetDisasmToUse(is_alternate_isa, disasm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | size_t Decode(const lldb_private::Disassembler &disassembler, |
| 171 | const lldb_private::DataExtractor &data, |
| 172 | lldb::offset_t data_offset) override { |
| 173 | // All we have to do is read the opcode which can be easy for some |
| 174 | // architectures |
| 175 | bool got_op = false; |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 176 | DisassemblerScope disasm(*this); |
| 177 | if (disasm) { |
| 178 | const ArchSpec &arch = disasm->GetArchitecture(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | const lldb::ByteOrder byte_order = data.GetByteOrder(); |
| 180 | |
| 181 | const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize(); |
| 182 | const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize(); |
| 183 | if (min_op_byte_size == max_op_byte_size) { |
| 184 | // Fixed size instructions, just read that amount of data. |
| 185 | if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size)) |
| 186 | return false; |
| 187 | |
| 188 | switch (min_op_byte_size) { |
| 189 | case 1: |
| 190 | m_opcode.SetOpcode8(data.GetU8(&data_offset), byte_order); |
| 191 | got_op = true; |
| 192 | break; |
| 193 | |
| 194 | case 2: |
| 195 | m_opcode.SetOpcode16(data.GetU16(&data_offset), byte_order); |
| 196 | got_op = true; |
| 197 | break; |
| 198 | |
| 199 | case 4: |
| 200 | m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order); |
| 201 | got_op = true; |
| 202 | break; |
| 203 | |
| 204 | case 8: |
| 205 | m_opcode.SetOpcode64(data.GetU64(&data_offset), byte_order); |
| 206 | got_op = true; |
| 207 | break; |
| 208 | |
| 209 | default: |
| 210 | m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size), |
| 211 | min_op_byte_size); |
| 212 | got_op = true; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | if (!got_op) { |
| 217 | bool is_alternate_isa = false; |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 218 | DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr = |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 219 | GetDisasmToUse(is_alternate_isa, disasm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | |
| 221 | const llvm::Triple::ArchType machine = arch.GetMachine(); |
| 222 | if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb) { |
| 223 | if (machine == llvm::Triple::thumb || is_alternate_isa) { |
| 224 | uint32_t thumb_opcode = data.GetU16(&data_offset); |
| 225 | if ((thumb_opcode & 0xe000) != 0xe000 || |
| 226 | ((thumb_opcode & 0x1800u) == 0)) { |
| 227 | m_opcode.SetOpcode16(thumb_opcode, byte_order); |
| 228 | m_is_valid = true; |
| 229 | } else { |
| 230 | thumb_opcode <<= 16; |
| 231 | thumb_opcode |= data.GetU16(&data_offset); |
| 232 | m_opcode.SetOpcode16_2(thumb_opcode, byte_order); |
| 233 | m_is_valid = true; |
| 234 | } |
| 235 | } else { |
| 236 | m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order); |
| 237 | m_is_valid = true; |
| 238 | } |
| 239 | } else { |
| 240 | // The opcode isn't evenly sized, so we need to actually use the llvm |
| 241 | // disassembler to parse it and get the size. |
| 242 | uint8_t *opcode_data = |
| 243 | const_cast<uint8_t *>(data.PeekData(data_offset, 1)); |
| 244 | const size_t opcode_data_len = data.BytesLeft(data_offset); |
| 245 | const addr_t pc = m_address.GetFileAddress(); |
| 246 | llvm::MCInst inst; |
| 247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | const size_t inst_size = |
| 249 | mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | if (inst_size == 0) |
| 251 | m_opcode.Clear(); |
| 252 | else { |
| 253 | m_opcode.SetOpcodeBytes(opcode_data, inst_size); |
| 254 | m_is_valid = true; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | return m_opcode.GetByteSize(); |
| 259 | } |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void AppendComment(std::string &description) { |
| 264 | if (m_comment.empty()) |
| 265 | m_comment.swap(description); |
| 266 | else { |
| 267 | m_comment.append(", "); |
| 268 | m_comment.append(description); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void CalculateMnemonicOperandsAndComment( |
| 273 | const lldb_private::ExecutionContext *exe_ctx) override { |
| 274 | DataExtractor data; |
| 275 | const AddressClass address_class = GetAddressClass(); |
| 276 | |
| 277 | if (m_opcode.GetData(data)) { |
| 278 | std::string out_string; |
| 279 | std::string comment_string; |
| 280 | |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 281 | DisassemblerScope disasm(*this, exe_ctx); |
| 282 | if (disasm) { |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 283 | DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 285 | if (address_class == AddressClass::eCodeAlternateISA) |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 286 | mc_disasm_ptr = disasm->m_alternate_disasm_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | else |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 288 | mc_disasm_ptr = disasm->m_disasm_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 289 | |
| 290 | lldb::addr_t pc = m_address.GetFileAddress(); |
| 291 | m_using_file_addr = true; |
| 292 | |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 293 | const bool data_from_file = disasm->m_data_from_file; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | bool use_hex_immediates = true; |
| 295 | Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC; |
| 296 | |
| 297 | if (exe_ctx) { |
| 298 | Target *target = exe_ctx->GetTargetPtr(); |
| 299 | if (target) { |
| 300 | use_hex_immediates = target->GetUseHexImmediates(); |
| 301 | hex_style = target->GetHexImmediateStyle(); |
| 302 | |
| 303 | if (!data_from_file) { |
| 304 | const lldb::addr_t load_addr = m_address.GetLoadAddress(target); |
| 305 | if (load_addr != LLDB_INVALID_ADDRESS) { |
| 306 | pc = load_addr; |
| 307 | m_using_file_addr = false; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | const uint8_t *opcode_data = data.GetDataStart(); |
| 314 | const size_t opcode_data_len = data.GetByteSize(); |
| 315 | llvm::MCInst inst; |
| 316 | size_t inst_size = |
| 317 | mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); |
| 318 | |
| 319 | if (inst_size > 0) { |
| 320 | mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style); |
| 321 | mc_disasm_ptr->PrintMCInst(inst, out_string, comment_string); |
| 322 | |
| 323 | if (!comment_string.empty()) { |
| 324 | AppendComment(comment_string); |
| 325 | } |
| 326 | } |
| 327 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 328 | if (inst_size == 0) { |
| 329 | m_comment.assign("unknown opcode"); |
| 330 | inst_size = m_opcode.GetByteSize(); |
| 331 | StreamString mnemonic_strm; |
| 332 | lldb::offset_t offset = 0; |
| 333 | lldb::ByteOrder byte_order = data.GetByteOrder(); |
| 334 | switch (inst_size) { |
| 335 | case 1: { |
| 336 | const uint8_t uval8 = data.GetU8(&offset); |
| 337 | m_opcode.SetOpcode8(uval8, byte_order); |
| 338 | m_opcode_name.assign(".byte"); |
| 339 | mnemonic_strm.Printf("0x%2.2x", uval8); |
| 340 | } break; |
| 341 | case 2: { |
| 342 | const uint16_t uval16 = data.GetU16(&offset); |
| 343 | m_opcode.SetOpcode16(uval16, byte_order); |
| 344 | m_opcode_name.assign(".short"); |
| 345 | mnemonic_strm.Printf("0x%4.4x", uval16); |
| 346 | } break; |
| 347 | case 4: { |
| 348 | const uint32_t uval32 = data.GetU32(&offset); |
| 349 | m_opcode.SetOpcode32(uval32, byte_order); |
| 350 | m_opcode_name.assign(".long"); |
| 351 | mnemonic_strm.Printf("0x%8.8x", uval32); |
| 352 | } break; |
| 353 | case 8: { |
| 354 | const uint64_t uval64 = data.GetU64(&offset); |
| 355 | m_opcode.SetOpcode64(uval64, byte_order); |
| 356 | m_opcode_name.assign(".quad"); |
| 357 | mnemonic_strm.Printf("0x%16.16" PRIx64, uval64); |
| 358 | } break; |
| 359 | default: |
| 360 | if (inst_size == 0) |
| 361 | return; |
| 362 | else { |
| 363 | const uint8_t *bytes = data.PeekData(offset, inst_size); |
| 364 | if (bytes == NULL) |
| 365 | return; |
| 366 | m_opcode_name.assign(".byte"); |
| 367 | m_opcode.SetOpcodeBytes(bytes, inst_size); |
| 368 | mnemonic_strm.Printf("0x%2.2x", bytes[0]); |
| 369 | for (uint32_t i = 1; i < inst_size; ++i) |
| 370 | mnemonic_strm.Printf(" 0x%2.2x", bytes[i]); |
| 371 | } |
| 372 | break; |
| 373 | } |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 374 | m_mnemonics = mnemonic_strm.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 375 | return; |
| 376 | } else { |
| 377 | if (m_does_branch == eLazyBoolCalculate) { |
| 378 | const bool can_branch = mc_disasm_ptr->CanBranch(inst); |
| 379 | if (can_branch) |
| 380 | m_does_branch = eLazyBoolYes; |
| 381 | else |
| 382 | m_does_branch = eLazyBoolNo; |
| 383 | } |
| 384 | } |
| 385 | |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 386 | static RegularExpression s_regex( |
| 387 | llvm::StringRef("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?")); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 388 | |
| 389 | RegularExpression::Match matches(3); |
| 390 | |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 391 | if (s_regex.Execute(out_string, &matches)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | matches.GetMatchAtIndex(out_string.c_str(), 1, m_opcode_name); |
| 393 | matches.GetMatchAtIndex(out_string.c_str(), 2, m_mnemonics); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | bool IsValid() const { return m_is_valid; } |
| 400 | |
| 401 | bool UsingFileAddress() const { return m_using_file_addr; } |
| 402 | size_t GetByteSize() const { return m_opcode.GetByteSize(); } |
| 403 | |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 404 | /// Grants exclusive access to the disassembler and initializes it with the |
| 405 | /// given InstructionLLVMC and an optional ExecutionContext. |
| 406 | class DisassemblerScope { |
| 407 | std::shared_ptr<DisassemblerLLVMC> m_disasm; |
| 408 | |
| 409 | public: |
| 410 | explicit DisassemblerScope( |
| 411 | InstructionLLVMC &i, |
| 412 | const lldb_private::ExecutionContext *exe_ctx = nullptr) |
| 413 | : m_disasm(i.m_disasm_wp.lock()) { |
| 414 | m_disasm->m_mutex.lock(); |
| 415 | m_disasm->m_inst = &i; |
| 416 | m_disasm->m_exe_ctx = exe_ctx; |
| 417 | } |
| 418 | ~DisassemblerScope() { m_disasm->m_mutex.unlock(); } |
| 419 | |
| 420 | /// Evaluates to true if this scope contains a valid disassembler. |
| 421 | operator bool() const { return static_cast<bool>(m_disasm); } |
| 422 | |
| 423 | std::shared_ptr<DisassemblerLLVMC> operator->() { return m_disasm; } |
| 424 | }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | |
| 426 | static llvm::StringRef::const_iterator |
| 427 | ConsumeWhitespace(llvm::StringRef::const_iterator osi, |
| 428 | llvm::StringRef::const_iterator ose) { |
| 429 | while (osi != ose) { |
| 430 | switch (*osi) { |
| 431 | default: |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 432 | return osi; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | case ' ': |
| 434 | case '\t': |
| 435 | break; |
| 436 | } |
| 437 | ++osi; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | return osi; |
| 441 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 442 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | static std::pair<bool, llvm::StringRef::const_iterator> |
| 444 | ConsumeChar(llvm::StringRef::const_iterator osi, const char c, |
| 445 | llvm::StringRef::const_iterator ose) { |
| 446 | bool found = false; |
| 447 | |
| 448 | osi = ConsumeWhitespace(osi, ose); |
| 449 | if (osi != ose && *osi == c) { |
| 450 | found = true; |
| 451 | ++osi; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 452 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | |
| 454 | return std::make_pair(found, osi); |
| 455 | } |
| 456 | |
| 457 | static std::pair<Operand, llvm::StringRef::const_iterator> |
| 458 | ParseRegisterName(llvm::StringRef::const_iterator osi, |
| 459 | llvm::StringRef::const_iterator ose) { |
| 460 | Operand ret; |
| 461 | ret.m_type = Operand::Type::Register; |
| 462 | std::string str; |
| 463 | |
| 464 | osi = ConsumeWhitespace(osi, ose); |
| 465 | |
| 466 | while (osi != ose) { |
| 467 | if (*osi >= '0' && *osi <= '9') { |
| 468 | if (str.empty()) { |
| 469 | return std::make_pair(Operand(), osi); |
| 470 | } else { |
| 471 | str.push_back(*osi); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 472 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | } else if (*osi >= 'a' && *osi <= 'z') { |
| 474 | str.push_back(*osi); |
| 475 | } else { |
| 476 | switch (*osi) { |
| 477 | default: |
| 478 | if (str.empty()) { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 479 | return std::make_pair(Operand(), osi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 480 | } else { |
| 481 | ret.m_register = ConstString(str); |
| 482 | return std::make_pair(ret, osi); |
| 483 | } |
| 484 | case '%': |
| 485 | if (!str.empty()) { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 486 | return std::make_pair(Operand(), osi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | } |
| 488 | break; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 489 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 490 | } |
| 491 | ++osi; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 492 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | |
| 494 | ret.m_register = ConstString(str); |
| 495 | return std::make_pair(ret, osi); |
| 496 | } |
| 497 | |
| 498 | static std::pair<Operand, llvm::StringRef::const_iterator> |
| 499 | ParseImmediate(llvm::StringRef::const_iterator osi, |
| 500 | llvm::StringRef::const_iterator ose) { |
| 501 | Operand ret; |
| 502 | ret.m_type = Operand::Type::Immediate; |
| 503 | std::string str; |
| 504 | bool is_hex = false; |
| 505 | |
| 506 | osi = ConsumeWhitespace(osi, ose); |
| 507 | |
| 508 | while (osi != ose) { |
| 509 | if (*osi >= '0' && *osi <= '9') { |
| 510 | str.push_back(*osi); |
| 511 | } else if (*osi >= 'a' && *osi <= 'f') { |
| 512 | if (is_hex) { |
| 513 | str.push_back(*osi); |
| 514 | } else { |
| 515 | return std::make_pair(Operand(), osi); |
| 516 | } |
| 517 | } else { |
| 518 | switch (*osi) { |
| 519 | default: |
| 520 | if (str.empty()) { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 521 | return std::make_pair(Operand(), osi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 522 | } else { |
| 523 | ret.m_immediate = strtoull(str.c_str(), nullptr, 0); |
| 524 | return std::make_pair(ret, osi); |
| 525 | } |
| 526 | case 'x': |
| 527 | if (!str.compare("0")) { |
| 528 | is_hex = true; |
| 529 | str.push_back(*osi); |
| 530 | } else { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 531 | return std::make_pair(Operand(), osi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | } |
| 533 | break; |
| 534 | case '#': |
| 535 | case '$': |
| 536 | if (!str.empty()) { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 537 | return std::make_pair(Operand(), osi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 538 | } |
| 539 | break; |
| 540 | case '-': |
| 541 | if (str.empty()) { |
| 542 | ret.m_negative = true; |
| 543 | } else { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 544 | return std::make_pair(Operand(), osi); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 546 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 547 | } |
| 548 | ++osi; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 549 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | |
| 551 | ret.m_immediate = strtoull(str.c_str(), nullptr, 0); |
| 552 | return std::make_pair(ret, osi); |
| 553 | } |
| 554 | |
| 555 | // -0x5(%rax,%rax,2) |
| 556 | static std::pair<Operand, llvm::StringRef::const_iterator> |
| 557 | ParseIntelIndexedAccess(llvm::StringRef::const_iterator osi, |
| 558 | llvm::StringRef::const_iterator ose) { |
| 559 | std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator = |
| 560 | ParseImmediate(osi, ose); |
| 561 | if (offset_and_iterator.first.IsValid()) { |
| 562 | osi = offset_and_iterator.second; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 563 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | |
| 565 | bool found = false; |
| 566 | std::tie(found, osi) = ConsumeChar(osi, '(', ose); |
| 567 | if (!found) { |
| 568 | return std::make_pair(Operand(), osi); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 569 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | |
| 571 | std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator = |
| 572 | ParseRegisterName(osi, ose); |
| 573 | if (base_and_iterator.first.IsValid()) { |
| 574 | osi = base_and_iterator.second; |
| 575 | } else { |
| 576 | return std::make_pair(Operand(), osi); |
| 577 | } |
| 578 | |
| 579 | std::tie(found, osi) = ConsumeChar(osi, ',', ose); |
| 580 | if (!found) { |
| 581 | return std::make_pair(Operand(), osi); |
| 582 | } |
| 583 | |
| 584 | std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator = |
| 585 | ParseRegisterName(osi, ose); |
| 586 | if (index_and_iterator.first.IsValid()) { |
| 587 | osi = index_and_iterator.second; |
| 588 | } else { |
| 589 | return std::make_pair(Operand(), osi); |
| 590 | } |
| 591 | |
| 592 | std::tie(found, osi) = ConsumeChar(osi, ',', ose); |
| 593 | if (!found) { |
| 594 | return std::make_pair(Operand(), osi); |
| 595 | } |
| 596 | |
| 597 | std::pair<Operand, llvm::StringRef::const_iterator> |
| 598 | multiplier_and_iterator = ParseImmediate(osi, ose); |
| 599 | if (index_and_iterator.first.IsValid()) { |
| 600 | osi = index_and_iterator.second; |
| 601 | } else { |
| 602 | return std::make_pair(Operand(), osi); |
| 603 | } |
| 604 | |
| 605 | std::tie(found, osi) = ConsumeChar(osi, ')', ose); |
| 606 | if (!found) { |
| 607 | return std::make_pair(Operand(), osi); |
| 608 | } |
| 609 | |
| 610 | Operand product; |
| 611 | product.m_type = Operand::Type::Product; |
| 612 | product.m_children.push_back(index_and_iterator.first); |
| 613 | product.m_children.push_back(multiplier_and_iterator.first); |
| 614 | |
| 615 | Operand index; |
| 616 | index.m_type = Operand::Type::Sum; |
| 617 | index.m_children.push_back(base_and_iterator.first); |
| 618 | index.m_children.push_back(product); |
| 619 | |
| 620 | if (offset_and_iterator.first.IsValid()) { |
| 621 | Operand offset; |
| 622 | offset.m_type = Operand::Type::Sum; |
| 623 | offset.m_children.push_back(offset_and_iterator.first); |
| 624 | offset.m_children.push_back(index); |
| 625 | |
| 626 | Operand deref; |
| 627 | deref.m_type = Operand::Type::Dereference; |
| 628 | deref.m_children.push_back(offset); |
| 629 | return std::make_pair(deref, osi); |
| 630 | } else { |
| 631 | Operand deref; |
| 632 | deref.m_type = Operand::Type::Dereference; |
| 633 | deref.m_children.push_back(index); |
| 634 | return std::make_pair(deref, osi); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // -0x10(%rbp) |
| 639 | static std::pair<Operand, llvm::StringRef::const_iterator> |
| 640 | ParseIntelDerefAccess(llvm::StringRef::const_iterator osi, |
| 641 | llvm::StringRef::const_iterator ose) { |
| 642 | std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator = |
| 643 | ParseImmediate(osi, ose); |
| 644 | if (offset_and_iterator.first.IsValid()) { |
| 645 | osi = offset_and_iterator.second; |
| 646 | } |
| 647 | |
| 648 | bool found = false; |
| 649 | std::tie(found, osi) = ConsumeChar(osi, '(', ose); |
| 650 | if (!found) { |
| 651 | return std::make_pair(Operand(), osi); |
| 652 | } |
| 653 | |
| 654 | std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator = |
| 655 | ParseRegisterName(osi, ose); |
| 656 | if (base_and_iterator.first.IsValid()) { |
| 657 | osi = base_and_iterator.second; |
| 658 | } else { |
| 659 | return std::make_pair(Operand(), osi); |
| 660 | } |
| 661 | |
| 662 | std::tie(found, osi) = ConsumeChar(osi, ')', ose); |
| 663 | if (!found) { |
| 664 | return std::make_pair(Operand(), osi); |
| 665 | } |
| 666 | |
| 667 | if (offset_and_iterator.first.IsValid()) { |
| 668 | Operand offset; |
| 669 | offset.m_type = Operand::Type::Sum; |
| 670 | offset.m_children.push_back(offset_and_iterator.first); |
| 671 | offset.m_children.push_back(base_and_iterator.first); |
| 672 | |
| 673 | Operand deref; |
| 674 | deref.m_type = Operand::Type::Dereference; |
| 675 | deref.m_children.push_back(offset); |
| 676 | return std::make_pair(deref, osi); |
| 677 | } else { |
| 678 | Operand deref; |
| 679 | deref.m_type = Operand::Type::Dereference; |
| 680 | deref.m_children.push_back(base_and_iterator.first); |
| 681 | return std::make_pair(deref, osi); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // [sp, #8]! |
| 686 | static std::pair<Operand, llvm::StringRef::const_iterator> |
| 687 | ParseARMOffsetAccess(llvm::StringRef::const_iterator osi, |
| 688 | llvm::StringRef::const_iterator ose) { |
| 689 | bool found = false; |
| 690 | std::tie(found, osi) = ConsumeChar(osi, '[', ose); |
| 691 | if (!found) { |
| 692 | return std::make_pair(Operand(), osi); |
| 693 | } |
| 694 | |
| 695 | std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator = |
| 696 | ParseRegisterName(osi, ose); |
| 697 | if (base_and_iterator.first.IsValid()) { |
| 698 | osi = base_and_iterator.second; |
| 699 | } else { |
| 700 | return std::make_pair(Operand(), osi); |
| 701 | } |
| 702 | |
| 703 | std::tie(found, osi) = ConsumeChar(osi, ',', ose); |
| 704 | if (!found) { |
| 705 | return std::make_pair(Operand(), osi); |
| 706 | } |
| 707 | |
| 708 | std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator = |
| 709 | ParseImmediate(osi, ose); |
| 710 | if (offset_and_iterator.first.IsValid()) { |
| 711 | osi = offset_and_iterator.second; |
| 712 | } |
| 713 | |
| 714 | std::tie(found, osi) = ConsumeChar(osi, ']', ose); |
| 715 | if (!found) { |
| 716 | return std::make_pair(Operand(), osi); |
| 717 | } |
| 718 | |
| 719 | Operand offset; |
| 720 | offset.m_type = Operand::Type::Sum; |
| 721 | offset.m_children.push_back(offset_and_iterator.first); |
| 722 | offset.m_children.push_back(base_and_iterator.first); |
| 723 | |
| 724 | Operand deref; |
| 725 | deref.m_type = Operand::Type::Dereference; |
| 726 | deref.m_children.push_back(offset); |
| 727 | return std::make_pair(deref, osi); |
| 728 | } |
| 729 | |
| 730 | // [sp] |
| 731 | static std::pair<Operand, llvm::StringRef::const_iterator> |
| 732 | ParseARMDerefAccess(llvm::StringRef::const_iterator osi, |
| 733 | llvm::StringRef::const_iterator ose) { |
| 734 | bool found = false; |
| 735 | std::tie(found, osi) = ConsumeChar(osi, '[', ose); |
| 736 | if (!found) { |
| 737 | return std::make_pair(Operand(), osi); |
| 738 | } |
| 739 | |
| 740 | std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator = |
| 741 | ParseRegisterName(osi, ose); |
| 742 | if (base_and_iterator.first.IsValid()) { |
| 743 | osi = base_and_iterator.second; |
| 744 | } else { |
| 745 | return std::make_pair(Operand(), osi); |
| 746 | } |
| 747 | |
| 748 | std::tie(found, osi) = ConsumeChar(osi, ']', ose); |
| 749 | if (!found) { |
| 750 | return std::make_pair(Operand(), osi); |
| 751 | } |
| 752 | |
| 753 | Operand deref; |
| 754 | deref.m_type = Operand::Type::Dereference; |
| 755 | deref.m_children.push_back(base_and_iterator.first); |
| 756 | return std::make_pair(deref, osi); |
| 757 | } |
| 758 | |
| 759 | static void DumpOperand(const Operand &op, Stream &s) { |
| 760 | switch (op.m_type) { |
| 761 | case Operand::Type::Dereference: |
| 762 | s.PutCString("*"); |
| 763 | DumpOperand(op.m_children[0], s); |
| 764 | break; |
| 765 | case Operand::Type::Immediate: |
| 766 | if (op.m_negative) { |
| 767 | s.PutCString("-"); |
| 768 | } |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 769 | s.PutCString(llvm::to_string(op.m_immediate)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 770 | break; |
| 771 | case Operand::Type::Invalid: |
| 772 | s.PutCString("Invalid"); |
| 773 | break; |
| 774 | case Operand::Type::Product: |
| 775 | s.PutCString("("); |
| 776 | DumpOperand(op.m_children[0], s); |
| 777 | s.PutCString("*"); |
| 778 | DumpOperand(op.m_children[1], s); |
| 779 | s.PutCString(")"); |
| 780 | break; |
| 781 | case Operand::Type::Register: |
| 782 | s.PutCString(op.m_register.AsCString()); |
| 783 | break; |
| 784 | case Operand::Type::Sum: |
| 785 | s.PutCString("("); |
| 786 | DumpOperand(op.m_children[0], s); |
| 787 | s.PutCString("+"); |
| 788 | DumpOperand(op.m_children[1], s); |
| 789 | s.PutCString(")"); |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | bool ParseOperands( |
| 795 | llvm::SmallVectorImpl<Instruction::Operand> &operands) override { |
| 796 | const char *operands_string = GetOperands(nullptr); |
| 797 | |
| 798 | if (!operands_string) { |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | llvm::StringRef operands_ref(operands_string); |
| 803 | |
| 804 | llvm::StringRef::const_iterator osi = operands_ref.begin(); |
| 805 | llvm::StringRef::const_iterator ose = operands_ref.end(); |
| 806 | |
| 807 | while (osi != ose) { |
| 808 | Operand operand; |
| 809 | llvm::StringRef::const_iterator iter; |
| 810 | |
| 811 | if ((std::tie(operand, iter) = ParseIntelIndexedAccess(osi, ose), |
| 812 | operand.IsValid()) || |
| 813 | (std::tie(operand, iter) = ParseIntelDerefAccess(osi, ose), |
| 814 | operand.IsValid()) || |
| 815 | (std::tie(operand, iter) = ParseARMOffsetAccess(osi, ose), |
| 816 | operand.IsValid()) || |
| 817 | (std::tie(operand, iter) = ParseARMDerefAccess(osi, ose), |
| 818 | operand.IsValid()) || |
| 819 | (std::tie(operand, iter) = ParseRegisterName(osi, ose), |
| 820 | operand.IsValid()) || |
| 821 | (std::tie(operand, iter) = ParseImmediate(osi, ose), |
| 822 | operand.IsValid())) { |
| 823 | osi = iter; |
| 824 | operands.push_back(operand); |
| 825 | } else { |
| 826 | return false; |
| 827 | } |
| 828 | |
| 829 | std::pair<bool, llvm::StringRef::const_iterator> found_and_iter = |
| 830 | ConsumeChar(osi, ',', ose); |
| 831 | if (found_and_iter.first) { |
| 832 | osi = found_and_iter.second; |
| 833 | } |
| 834 | |
| 835 | osi = ConsumeWhitespace(osi, ose); |
| 836 | } |
| 837 | |
| 838 | DisassemblerSP disasm_sp = m_disasm_wp.lock(); |
| 839 | |
| 840 | if (disasm_sp && operands.size() > 1) { |
| 841 | // TODO tie this into the MC Disassembler's notion of clobbers. |
| 842 | switch (disasm_sp->GetArchitecture().GetMachine()) { |
| 843 | default: |
| 844 | break; |
| 845 | case llvm::Triple::x86: |
| 846 | case llvm::Triple::x86_64: |
| 847 | operands[operands.size() - 1].m_clobbered = true; |
| 848 | break; |
| 849 | case llvm::Triple::arm: |
| 850 | operands[0].m_clobbered = true; |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if (Log *log = |
| 856 | lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)) { |
| 857 | StreamString ss; |
| 858 | |
| 859 | ss.Printf("[%s] expands to %zu operands:\n", operands_string, |
| 860 | operands.size()); |
| 861 | for (const Operand &operand : operands) { |
| 862 | ss.PutCString(" "); |
| 863 | DumpOperand(operand, ss); |
| 864 | ss.PutCString("\n"); |
| 865 | } |
| 866 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 867 | log->PutString(ss.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | return true; |
| 871 | } |
| 872 | |
| 873 | bool IsCall() override { |
| 874 | if (m_is_call == eLazyBoolCalculate) { |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 875 | DisassemblerScope disasm(*this); |
| 876 | if (disasm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | DataExtractor data; |
| 878 | if (m_opcode.GetData(data)) { |
| 879 | bool is_alternate_isa; |
| 880 | lldb::addr_t pc = m_address.GetFileAddress(); |
| 881 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 882 | DisassemblerLLVMC::MCDisasmInstance *mc_disasm_ptr = |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 883 | GetDisasmToUse(is_alternate_isa, disasm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 884 | const uint8_t *opcode_data = data.GetDataStart(); |
| 885 | const size_t opcode_data_len = data.GetByteSize(); |
| 886 | llvm::MCInst inst; |
| 887 | const size_t inst_size = |
| 888 | mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); |
| 889 | if (inst_size == 0) { |
| 890 | m_is_call = eLazyBoolNo; |
| 891 | } else { |
| 892 | if (mc_disasm_ptr->IsCall(inst)) |
| 893 | m_is_call = eLazyBoolYes; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 894 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 895 | m_is_call = eLazyBoolNo; |
| 896 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 897 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 898 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 899 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 900 | return m_is_call == eLazyBoolYes; |
| 901 | } |
| 902 | |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 903 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 904 | std::weak_ptr<DisassemblerLLVMC> m_disasm_wp; |
| 905 | LazyBool m_does_branch; |
| 906 | LazyBool m_has_delay_slot; |
| 907 | LazyBool m_is_call; |
| 908 | bool m_is_valid; |
| 909 | bool m_using_file_addr; |
Raphael Isemann | 4925421 | 2018-08-28 15:31:01 +0000 | [diff] [blame] | 910 | |
| 911 | private: |
| 912 | DisassemblerLLVMC::MCDisasmInstance * |
| 913 | GetDisasmToUse(bool &is_alternate_isa, DisassemblerScope &disasm) { |
| 914 | is_alternate_isa = false; |
| 915 | if (disasm) { |
| 916 | if (disasm->m_alternate_disasm_up) { |
| 917 | const AddressClass address_class = GetAddressClass(); |
| 918 | |
| 919 | if (address_class == AddressClass::eCodeAlternateISA) { |
| 920 | is_alternate_isa = true; |
| 921 | return disasm->m_alternate_disasm_up.get(); |
| 922 | } |
| 923 | } |
| 924 | return disasm->m_disasm_up.get(); |
| 925 | } |
| 926 | return nullptr; |
| 927 | } |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 928 | }; |
| 929 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 930 | std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance> |
| 931 | DisassemblerLLVMC::MCDisasmInstance::Create(const char *triple, const char *cpu, |
| 932 | const char *features_str, |
| 933 | unsigned flavor, |
| 934 | DisassemblerLLVMC &owner) { |
| 935 | using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>; |
| 936 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 937 | std::string Status; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 938 | const llvm::Target *curr_target = |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 939 | llvm::TargetRegistry::lookupTarget(triple, Status); |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 940 | if (!curr_target) |
| 941 | return Instance(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 942 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 943 | std::unique_ptr<llvm::MCInstrInfo> instr_info_up( |
| 944 | curr_target->createMCInstrInfo()); |
| 945 | if (!instr_info_up) |
| 946 | return Instance(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 947 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 948 | std::unique_ptr<llvm::MCRegisterInfo> reg_info_up( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 949 | curr_target->createMCRegInfo(triple)); |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 950 | if (!reg_info_up) |
| 951 | return Instance(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 952 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 953 | std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info_up( |
| 954 | curr_target->createMCSubtargetInfo(triple, cpu, features_str)); |
| 955 | if (!subtarget_info_up) |
| 956 | return Instance(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 957 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 958 | std::unique_ptr<llvm::MCAsmInfo> asm_info_up( |
| 959 | curr_target->createMCAsmInfo(*reg_info_up, triple)); |
| 960 | if (!asm_info_up) |
| 961 | return Instance(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 962 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 963 | std::unique_ptr<llvm::MCContext> context_up( |
| 964 | new llvm::MCContext(asm_info_up.get(), reg_info_up.get(), 0)); |
| 965 | if (!context_up) |
| 966 | return Instance(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 967 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 968 | std::unique_ptr<llvm::MCDisassembler> disasm_up( |
| 969 | curr_target->createMCDisassembler(*subtarget_info_up, *context_up)); |
| 970 | if (!disasm_up) |
| 971 | return Instance(); |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 972 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 973 | std::unique_ptr<llvm::MCRelocationInfo> rel_info_up( |
| 974 | curr_target->createMCRelocationInfo(triple, *context_up)); |
| 975 | if (!rel_info_up) |
| 976 | return Instance(); |
| 977 | |
| 978 | std::unique_ptr<llvm::MCSymbolizer> symbolizer_up( |
| 979 | curr_target->createMCSymbolizer( |
| 980 | triple, nullptr, DisassemblerLLVMC::SymbolLookupCallback, &owner, |
| 981 | context_up.get(), std::move(rel_info_up))); |
| 982 | disasm_up->setSymbolizer(std::move(symbolizer_up)); |
| 983 | |
| 984 | unsigned asm_printer_variant = |
| 985 | flavor == ~0U ? asm_info_up->getAssemblerDialect() : flavor; |
| 986 | |
| 987 | std::unique_ptr<llvm::MCInstPrinter> instr_printer_up( |
| 988 | curr_target->createMCInstPrinter(llvm::Triple{triple}, |
| 989 | asm_printer_variant, *asm_info_up, |
| 990 | *instr_info_up, *reg_info_up)); |
| 991 | if (!instr_printer_up) |
| 992 | return Instance(); |
| 993 | |
| 994 | return Instance( |
| 995 | new MCDisasmInstance(std::move(instr_info_up), std::move(reg_info_up), |
| 996 | std::move(subtarget_info_up), std::move(asm_info_up), |
| 997 | std::move(context_up), std::move(disasm_up), |
| 998 | std::move(instr_printer_up))); |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1001 | DisassemblerLLVMC::MCDisasmInstance::MCDisasmInstance( |
| 1002 | std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up, |
| 1003 | std::unique_ptr<llvm::MCRegisterInfo> &®_info_up, |
| 1004 | std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up, |
| 1005 | std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up, |
| 1006 | std::unique_ptr<llvm::MCContext> &&context_up, |
| 1007 | std::unique_ptr<llvm::MCDisassembler> &&disasm_up, |
| 1008 | std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up) |
| 1009 | : m_instr_info_up(std::move(instr_info_up)), |
| 1010 | m_reg_info_up(std::move(reg_info_up)), |
| 1011 | m_subtarget_info_up(std::move(subtarget_info_up)), |
| 1012 | m_asm_info_up(std::move(asm_info_up)), |
| 1013 | m_context_up(std::move(context_up)), m_disasm_up(std::move(disasm_up)), |
| 1014 | m_instr_printer_up(std::move(instr_printer_up)) { |
| 1015 | assert(m_instr_info_up && m_reg_info_up && m_subtarget_info_up && |
| 1016 | m_asm_info_up && m_context_up && m_disasm_up && m_instr_printer_up); |
| 1017 | } |
Eugene Zelenko | 8dd3fdb | 2015-10-21 01:42:15 +0000 | [diff] [blame] | 1018 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1019 | uint64_t DisassemblerLLVMC::MCDisasmInstance::GetMCInst( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1020 | const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1021 | llvm::MCInst &mc_inst) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1022 | llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len); |
| 1023 | llvm::MCDisassembler::DecodeStatus status; |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1024 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1025 | uint64_t new_inst_size; |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1026 | status = m_disasm_up->getInstruction(mc_inst, new_inst_size, data, pc, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1027 | llvm::nulls(), llvm::nulls()); |
| 1028 | if (status == llvm::MCDisassembler::Success) |
| 1029 | return new_inst_size; |
| 1030 | else |
| 1031 | return 0; |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1034 | void DisassemblerLLVMC::MCDisasmInstance::PrintMCInst( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1035 | llvm::MCInst &mc_inst, std::string &inst_string, |
| 1036 | std::string &comments_string) { |
| 1037 | llvm::raw_string_ostream inst_stream(inst_string); |
| 1038 | llvm::raw_string_ostream comments_stream(comments_string); |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1039 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1040 | m_instr_printer_up->setCommentStream(comments_stream); |
| 1041 | m_instr_printer_up->printInst(&mc_inst, inst_stream, llvm::StringRef(), |
| 1042 | *m_subtarget_info_up); |
| 1043 | m_instr_printer_up->setCommentStream(llvm::nulls()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1044 | comments_stream.flush(); |
| 1045 | |
| 1046 | static std::string g_newlines("\r\n"); |
| 1047 | |
| 1048 | for (size_t newline_pos = 0; |
| 1049 | (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) != |
| 1050 | comments_string.npos; |
| 1051 | /**/) { |
| 1052 | comments_string.replace(comments_string.begin() + newline_pos, |
| 1053 | comments_string.begin() + newline_pos + 1, 1, ' '); |
| 1054 | } |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1057 | void DisassemblerLLVMC::MCDisasmInstance::SetStyle( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1058 | bool use_hex_immed, HexImmediateStyle hex_style) { |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1059 | m_instr_printer_up->setPrintImmHex(use_hex_immed); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1060 | switch (hex_style) { |
| 1061 | case eHexStyleC: |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1062 | m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::C); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | case eHexStyleAsm: |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1065 | m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::Asm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1066 | break; |
| 1067 | } |
Daniel Malea | d79ae05 | 2013-08-07 21:54:09 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1070 | bool DisassemblerLLVMC::MCDisasmInstance::CanBranch( |
| 1071 | llvm::MCInst &mc_inst) const { |
| 1072 | return m_instr_info_up->get(mc_inst.getOpcode()) |
| 1073 | .mayAffectControlFlow(mc_inst, *m_reg_info_up); |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1076 | bool DisassemblerLLVMC::MCDisasmInstance::HasDelaySlot( |
| 1077 | llvm::MCInst &mc_inst) const { |
| 1078 | return m_instr_info_up->get(mc_inst.getOpcode()).hasDelaySlot(); |
Bhushan D. Attarde | 7f3daed | 2015-08-26 06:04:54 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1081 | bool DisassemblerLLVMC::MCDisasmInstance::IsCall(llvm::MCInst &mc_inst) const { |
| 1082 | return m_instr_info_up->get(mc_inst.getOpcode()).isCall(); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1085 | DisassemblerLLVMC::DisassemblerLLVMC(const ArchSpec &arch, |
| 1086 | const char *flavor_string) |
| 1087 | : Disassembler(arch, flavor_string), m_exe_ctx(NULL), m_inst(NULL), |
| 1088 | m_data_from_file(false) { |
| 1089 | if (!FlavorValidForArchSpec(arch, m_flavor.c_str())) { |
| 1090 | m_flavor.assign("default"); |
| 1091 | } |
| 1092 | |
| 1093 | unsigned flavor = ~0U; |
| 1094 | llvm::Triple triple = arch.GetTriple(); |
| 1095 | |
| 1096 | // So far the only supported flavor is "intel" on x86. The base class will |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1097 | // set this correctly coming in. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1098 | if (triple.getArch() == llvm::Triple::x86 || |
| 1099 | triple.getArch() == llvm::Triple::x86_64) { |
| 1100 | if (m_flavor == "intel") { |
| 1101 | flavor = 1; |
| 1102 | } else if (m_flavor == "att") { |
| 1103 | flavor = 0; |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1104 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1105 | } |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1107 | ArchSpec thumb_arch(arch); |
| 1108 | if (triple.getArch() == llvm::Triple::arm) { |
| 1109 | std::string thumb_arch_name(thumb_arch.GetTriple().getArchName().str()); |
| 1110 | // Replace "arm" with "thumb" so we get all thumb variants correct |
| 1111 | if (thumb_arch_name.size() > 3) { |
| 1112 | thumb_arch_name.erase(0, 3); |
| 1113 | thumb_arch_name.insert(0, "thumb"); |
| 1114 | } else { |
| 1115 | thumb_arch_name = "thumbv8.2a"; |
Jim Ingham | 0f063ba | 2013-03-02 00:26:47 +0000 | [diff] [blame] | 1116 | } |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 1117 | thumb_arch.GetTriple().setArchName(llvm::StringRef(thumb_arch_name)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1118 | } |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | // If no sub architecture specified then use the most recent arm architecture |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1121 | // so the disassembler will return all instruction. Without it we will see a |
| 1122 | // lot of unknow opcode in case the code uses instructions which are not |
| 1123 | // available in the oldest arm version (used when no sub architecture is |
| 1124 | // specified) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | if (triple.getArch() == llvm::Triple::arm && |
| 1126 | triple.getSubArch() == llvm::Triple::NoSubArch) |
| 1127 | triple.setArchName("armv8.2a"); |
| 1128 | |
Jason Molenda | 0dfb84c | 2018-09-07 01:28:48 +0000 | [diff] [blame^] | 1129 | std::string features_str = ""; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1130 | const char *triple_str = triple.getTriple().c_str(); |
| 1131 | |
| 1132 | // ARM Cortex M0-M7 devices only execute thumb instructions |
| 1133 | if (arch.IsAlwaysThumbInstructions()) { |
| 1134 | triple_str = thumb_arch.GetTriple().getTriple().c_str(); |
Jason Molenda | 0dfb84c | 2018-09-07 01:28:48 +0000 | [diff] [blame^] | 1135 | features_str += "+fp-armv8,"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | const char *cpu = ""; |
| 1139 | |
| 1140 | switch (arch.GetCore()) { |
| 1141 | case ArchSpec::eCore_mips32: |
| 1142 | case ArchSpec::eCore_mips32el: |
| 1143 | cpu = "mips32"; |
| 1144 | break; |
| 1145 | case ArchSpec::eCore_mips32r2: |
| 1146 | case ArchSpec::eCore_mips32r2el: |
| 1147 | cpu = "mips32r2"; |
| 1148 | break; |
| 1149 | case ArchSpec::eCore_mips32r3: |
| 1150 | case ArchSpec::eCore_mips32r3el: |
| 1151 | cpu = "mips32r3"; |
| 1152 | break; |
| 1153 | case ArchSpec::eCore_mips32r5: |
| 1154 | case ArchSpec::eCore_mips32r5el: |
| 1155 | cpu = "mips32r5"; |
| 1156 | break; |
| 1157 | case ArchSpec::eCore_mips32r6: |
| 1158 | case ArchSpec::eCore_mips32r6el: |
| 1159 | cpu = "mips32r6"; |
| 1160 | break; |
| 1161 | case ArchSpec::eCore_mips64: |
| 1162 | case ArchSpec::eCore_mips64el: |
| 1163 | cpu = "mips64"; |
| 1164 | break; |
| 1165 | case ArchSpec::eCore_mips64r2: |
| 1166 | case ArchSpec::eCore_mips64r2el: |
| 1167 | cpu = "mips64r2"; |
| 1168 | break; |
| 1169 | case ArchSpec::eCore_mips64r3: |
| 1170 | case ArchSpec::eCore_mips64r3el: |
| 1171 | cpu = "mips64r3"; |
| 1172 | break; |
| 1173 | case ArchSpec::eCore_mips64r5: |
| 1174 | case ArchSpec::eCore_mips64r5el: |
| 1175 | cpu = "mips64r5"; |
| 1176 | break; |
| 1177 | case ArchSpec::eCore_mips64r6: |
| 1178 | case ArchSpec::eCore_mips64r6el: |
| 1179 | cpu = "mips64r6"; |
| 1180 | break; |
| 1181 | default: |
| 1182 | cpu = ""; |
| 1183 | break; |
| 1184 | } |
| 1185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1186 | if (triple.getArch() == llvm::Triple::mips || |
| 1187 | triple.getArch() == llvm::Triple::mipsel || |
| 1188 | triple.getArch() == llvm::Triple::mips64 || |
| 1189 | triple.getArch() == llvm::Triple::mips64el) { |
| 1190 | uint32_t arch_flags = arch.GetFlags(); |
| 1191 | if (arch_flags & ArchSpec::eMIPSAse_msa) |
| 1192 | features_str += "+msa,"; |
| 1193 | if (arch_flags & ArchSpec::eMIPSAse_dsp) |
| 1194 | features_str += "+dsp,"; |
| 1195 | if (arch_flags & ArchSpec::eMIPSAse_dspr2) |
| 1196 | features_str += "+dspr2,"; |
| 1197 | } |
| 1198 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1199 | // If any AArch64 variant, enable the ARMv8.2 ISA extensions so we can |
| 1200 | // disassemble newer instructions. |
Jason Molenda | a22e923 | 2017-12-22 00:16:04 +0000 | [diff] [blame] | 1201 | if (triple.getArch() == llvm::Triple::aarch64) |
| 1202 | features_str += "+v8.2a"; |
| 1203 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1204 | // We use m_disasm_ap.get() to tell whether we are valid or not, so if this |
| 1205 | // isn't good for some reason, we won't be valid and FindPlugin will fail and |
| 1206 | // we won't get used. |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1207 | m_disasm_up = MCDisasmInstance::Create(triple_str, cpu, features_str.c_str(), |
| 1208 | flavor, *this); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1209 | |
| 1210 | llvm::Triple::ArchType llvm_arch = triple.getArch(); |
| 1211 | |
| 1212 | // For arm CPUs that can execute arm or thumb instructions, also create a |
| 1213 | // thumb instruction disassembler. |
| 1214 | if (llvm_arch == llvm::Triple::arm) { |
| 1215 | std::string thumb_triple(thumb_arch.GetTriple().getTriple()); |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1216 | m_alternate_disasm_up = |
Jason Molenda | 0dfb84c | 2018-09-07 01:28:48 +0000 | [diff] [blame^] | 1217 | MCDisasmInstance::Create(thumb_triple.c_str(), "", features_str.c_str(), |
| 1218 | flavor, *this); |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1219 | if (!m_alternate_disasm_up) |
| 1220 | m_disasm_up.reset(); |
| 1221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1222 | } else if (llvm_arch == llvm::Triple::mips || |
| 1223 | llvm_arch == llvm::Triple::mipsel || |
| 1224 | llvm_arch == llvm::Triple::mips64 || |
| 1225 | llvm_arch == llvm::Triple::mips64el) { |
| 1226 | /* Create alternate disassembler for MIPS16 and microMIPS */ |
| 1227 | uint32_t arch_flags = arch.GetFlags(); |
| 1228 | if (arch_flags & ArchSpec::eMIPSAse_mips16) |
| 1229 | features_str += "+mips16,"; |
| 1230 | else if (arch_flags & ArchSpec::eMIPSAse_micromips) |
| 1231 | features_str += "+micromips,"; |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1232 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1233 | m_alternate_disasm_up = MCDisasmInstance::Create( |
| 1234 | triple_str, cpu, features_str.c_str(), flavor, *this); |
| 1235 | if (!m_alternate_disasm_up) |
| 1236 | m_disasm_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1237 | } |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Eugene Zelenko | 45a4014 | 2015-10-22 21:24:37 +0000 | [diff] [blame] | 1240 | DisassemblerLLVMC::~DisassemblerLLVMC() = default; |
| 1241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1242 | Disassembler *DisassemblerLLVMC::CreateInstance(const ArchSpec &arch, |
| 1243 | const char *flavor) { |
| 1244 | if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch) { |
| 1245 | std::unique_ptr<DisassemblerLLVMC> disasm_ap( |
| 1246 | new DisassemblerLLVMC(arch, flavor)); |
Eugene Zelenko | 45a4014 | 2015-10-22 21:24:37 +0000 | [diff] [blame] | 1247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1248 | if (disasm_ap.get() && disasm_ap->IsValid()) |
| 1249 | return disasm_ap.release(); |
| 1250 | } |
| 1251 | return NULL; |
Eugene Zelenko | 8dd3fdb | 2015-10-21 01:42:15 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1254 | size_t DisassemblerLLVMC::DecodeInstructions(const Address &base_addr, |
| 1255 | const DataExtractor &data, |
| 1256 | lldb::offset_t data_offset, |
| 1257 | size_t num_instructions, |
| 1258 | bool append, bool data_from_file) { |
| 1259 | if (!append) |
| 1260 | m_instruction_list.Clear(); |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1262 | if (!IsValid()) |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1263 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1264 | |
| 1265 | m_data_from_file = data_from_file; |
| 1266 | uint32_t data_cursor = data_offset; |
| 1267 | const size_t data_byte_size = data.GetByteSize(); |
| 1268 | uint32_t instructions_parsed = 0; |
| 1269 | Address inst_addr(base_addr); |
| 1270 | |
| 1271 | while (data_cursor < data_byte_size && |
| 1272 | instructions_parsed < num_instructions) { |
| 1273 | |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame] | 1274 | AddressClass address_class = AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1275 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1276 | if (m_alternate_disasm_up) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1277 | address_class = inst_addr.GetAddressClass(); |
| 1278 | |
| 1279 | InstructionSP inst_sp( |
| 1280 | new InstructionLLVMC(*this, inst_addr, address_class)); |
| 1281 | |
| 1282 | if (!inst_sp) |
| 1283 | break; |
| 1284 | |
| 1285 | uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor); |
| 1286 | |
| 1287 | if (inst_size == 0) |
| 1288 | break; |
| 1289 | |
| 1290 | m_instruction_list.Append(inst_sp); |
| 1291 | data_cursor += inst_size; |
| 1292 | inst_addr.Slide(inst_size); |
| 1293 | instructions_parsed++; |
| 1294 | } |
| 1295 | |
| 1296 | return data_cursor - data_offset; |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1299 | void DisassemblerLLVMC::Initialize() { |
| 1300 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 1301 | "Disassembler that uses LLVM MC to disassemble " |
| 1302 | "i386, x86_64, ARM, and ARM64.", |
| 1303 | CreateInstance); |
Jason Molenda | c980fa9 | 2015-02-13 23:24:21 +0000 | [diff] [blame] | 1304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1305 | llvm::InitializeAllTargetInfos(); |
| 1306 | llvm::InitializeAllTargetMCs(); |
| 1307 | llvm::InitializeAllAsmParsers(); |
| 1308 | llvm::InitializeAllDisassemblers(); |
| 1309 | } |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1310 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1311 | void DisassemblerLLVMC::Terminate() { |
| 1312 | PluginManager::UnregisterPlugin(CreateInstance); |
| 1313 | } |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1315 | ConstString DisassemblerLLVMC::GetPluginNameStatic() { |
| 1316 | static ConstString g_name("llvm-mc"); |
| 1317 | return g_name; |
| 1318 | } |
Sylvestre Ledru | a3e4ceb | 2014-04-15 12:08:57 +0000 | [diff] [blame] | 1319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1320 | int DisassemblerLLVMC::OpInfoCallback(void *disassembler, uint64_t pc, |
| 1321 | uint64_t offset, uint64_t size, |
| 1322 | int tag_type, void *tag_bug) { |
| 1323 | return static_cast<DisassemblerLLVMC *>(disassembler) |
| 1324 | ->OpInfo(pc, offset, size, tag_type, tag_bug); |
| 1325 | } |
| 1326 | |
| 1327 | const char *DisassemblerLLVMC::SymbolLookupCallback(void *disassembler, |
| 1328 | uint64_t value, |
| 1329 | uint64_t *type, uint64_t pc, |
| 1330 | const char **name) { |
| 1331 | return static_cast<DisassemblerLLVMC *>(disassembler) |
| 1332 | ->SymbolLookup(value, type, pc, name); |
| 1333 | } |
| 1334 | |
| 1335 | bool DisassemblerLLVMC::FlavorValidForArchSpec( |
| 1336 | const lldb_private::ArchSpec &arch, const char *flavor) { |
| 1337 | llvm::Triple triple = arch.GetTriple(); |
| 1338 | if (flavor == NULL || strcmp(flavor, "default") == 0) |
| 1339 | return true; |
| 1340 | |
| 1341 | if (triple.getArch() == llvm::Triple::x86 || |
| 1342 | triple.getArch() == llvm::Triple::x86_64) { |
| 1343 | if (strcmp(flavor, "intel") == 0 || strcmp(flavor, "att") == 0) |
| 1344 | return true; |
| 1345 | else |
| 1346 | return false; |
| 1347 | } else |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
Tatyana Krasnukha | 6c2c08f | 2018-01-11 12:06:22 +0000 | [diff] [blame] | 1351 | bool DisassemblerLLVMC::IsValid() const { return m_disasm_up.operator bool(); } |
| 1352 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1353 | int DisassemblerLLVMC::OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size, |
| 1354 | int tag_type, void *tag_bug) { |
| 1355 | switch (tag_type) { |
| 1356 | default: |
| 1357 | break; |
| 1358 | case 1: |
| 1359 | memset(tag_bug, 0, sizeof(::LLVMOpInfo1)); |
| 1360 | break; |
| 1361 | } |
| 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | const char *DisassemblerLLVMC::SymbolLookup(uint64_t value, uint64_t *type_ptr, |
| 1366 | uint64_t pc, const char **name) { |
| 1367 | if (*type_ptr) { |
| 1368 | if (m_exe_ctx && m_inst) { |
| 1369 | // std::string remove_this_prior_to_checkin; |
| 1370 | Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : NULL; |
| 1371 | Address value_so_addr; |
| 1372 | Address pc_so_addr; |
| 1373 | if (m_inst->UsingFileAddress()) { |
| 1374 | ModuleSP module_sp(m_inst->GetAddress().GetModule()); |
| 1375 | if (module_sp) { |
| 1376 | module_sp->ResolveFileAddress(value, value_so_addr); |
| 1377 | module_sp->ResolveFileAddress(pc, pc_so_addr); |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1378 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1379 | } else if (target && !target->GetSectionLoadList().IsEmpty()) { |
| 1380 | target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr); |
| 1381 | target->GetSectionLoadList().ResolveLoadAddress(pc, pc_so_addr); |
| 1382 | } |
Greg Clayton | ba812f4 | 2012-05-10 02:52:23 +0000 | [diff] [blame] | 1383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1384 | SymbolContext sym_ctx; |
| 1385 | const uint32_t resolve_scope = |
| 1386 | eSymbolContextFunction | eSymbolContextSymbol; |
| 1387 | if (pc_so_addr.IsValid() && pc_so_addr.GetModule()) { |
| 1388 | pc_so_addr.GetModule()->ResolveSymbolContextForAddress( |
| 1389 | pc_so_addr, resolve_scope, sym_ctx); |
| 1390 | } |
| 1391 | |
| 1392 | if (value_so_addr.IsValid() && value_so_addr.GetSection()) { |
| 1393 | StreamString ss; |
| 1394 | |
| 1395 | bool format_omitting_current_func_name = false; |
| 1396 | if (sym_ctx.symbol || sym_ctx.function) { |
| 1397 | AddressRange range; |
| 1398 | if (sym_ctx.GetAddressRange(resolve_scope, 0, false, range) && |
| 1399 | range.GetBaseAddress().IsValid() && |
| 1400 | range.ContainsLoadAddress(value_so_addr, target)) { |
| 1401 | format_omitting_current_func_name = true; |
| 1402 | } |
| 1403 | } |
| 1404 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1405 | // If the "value" address (the target address we're symbolicating) is |
| 1406 | // inside the same SymbolContext as the current instruction pc |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1407 | // (pc_so_addr), don't print the full function name - just print it |
| 1408 | // with DumpStyleNoFunctionName style, e.g. "<+36>". |
| 1409 | if (format_omitting_current_func_name) { |
| 1410 | value_so_addr.Dump(&ss, target, Address::DumpStyleNoFunctionName, |
| 1411 | Address::DumpStyleSectionNameOffset); |
| 1412 | } else { |
| 1413 | value_so_addr.Dump( |
| 1414 | &ss, target, |
| 1415 | Address::DumpStyleResolvedDescriptionNoFunctionArguments, |
| 1416 | Address::DumpStyleSectionNameOffset); |
| 1417 | } |
| 1418 | |
| 1419 | if (!ss.GetString().empty()) { |
| 1420 | // If Address::Dump returned a multi-line description, most commonly |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1421 | // seen when we have multiple levels of inlined functions at an |
| 1422 | // address, only show the first line. |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 1423 | std::string str = ss.GetString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1424 | size_t first_eol_char = str.find_first_of("\r\n"); |
| 1425 | if (first_eol_char != std::string::npos) { |
| 1426 | str.erase(first_eol_char); |
| 1427 | } |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 1428 | m_inst->AppendComment(str); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | *type_ptr = LLVMDisassembler_ReferenceType_InOut_None; |
| 1435 | *name = NULL; |
| 1436 | return NULL; |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | //------------------------------------------------------------------ |
| 1440 | // PluginInterface protocol |
| 1441 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1442 | ConstString DisassemblerLLVMC::GetPluginName() { return GetPluginNameStatic(); } |
Sean Callanan | 95e5c63 | 2012-02-17 00:53:45 +0000 | [diff] [blame] | 1443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1444 | uint32_t DisassemblerLLVMC::GetPluginVersion() { return 1; } |