blob: 8d6be243be8dc51159f552b183afd349c9e066c2 [file] [log] [blame]
Sean Callanan95e5c632012-02-17 00:53:45 +00001//===-- 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 Zelenko45a40142015-10-22 21:24:37 +000010// C Includes
11// C++ Includes
12// Project includes
Sean Callanan95e5c632012-02-17 00:53:45 +000013#include "llvm-c/Disassembler.h"
Benjamin Kramer79dad1d2016-01-26 16:45:00 +000014#include "llvm/ADT/SmallString.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000015#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCContext.h"
Benjamin Kramer79dad1d2016-01-26 16:45:00 +000017#include "llvm/MC/MCDisassembler/MCDisassembler.h"
18#include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h"
19#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000020#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstPrinter.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/Support/ErrorHandling.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000026#include "llvm/Support/ScopedPrinter.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000027#include "llvm/Support/TargetRegistry.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000028#include "llvm/Support/TargetSelect.h"
Jim Ingham0f063ba2013-03-02 00:26:47 +000029
Eugene Zelenko45a40142015-10-22 21:24:37 +000030// Other libraries and framework includes
31#include "DisassemblerLLVMC.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000032
33#include "lldb/Core/Address.h"
34#include "lldb/Core/DataExtractor.h"
Sean Callanan4740a732016-09-06 04:48:36 +000035#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000036#include "lldb/Core/Module.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000037#include "lldb/Core/Stream.h"
38#include "lldb/Symbol/SymbolContext.h"
39#include "lldb/Target/ExecutionContext.h"
40#include "lldb/Target/Process.h"
41#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000042#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000043#include "lldb/Target/StackFrame.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000044#include "lldb/Target/Target.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000045
Virgile Bellob2f1fb22013-08-23 12:44:05 +000046#include "lldb/Core/RegularExpression.h"
Sean Callanan95e5c632012-02-17 00:53:45 +000047
48using namespace lldb;
49using namespace lldb_private;
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051class InstructionLLVMC : public lldb_private::Instruction {
Sean Callanan95e5c632012-02-17 00:53:45 +000052public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 InstructionLLVMC(DisassemblerLLVMC &disasm,
54 const lldb_private::Address &address,
55 AddressClass addr_class)
56 : Instruction(address, addr_class),
57 m_disasm_wp(std::static_pointer_cast<DisassemblerLLVMC>(
58 disasm.shared_from_this())),
59 m_does_branch(eLazyBoolCalculate), m_has_delay_slot(eLazyBoolCalculate),
60 m_is_call(eLazyBoolCalculate), m_is_valid(false),
61 m_using_file_addr(false) {}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 ~InstructionLLVMC() override = default;
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 bool DoesBranch() override {
66 if (m_does_branch == eLazyBoolCalculate) {
67 std::shared_ptr<DisassemblerLLVMC> disasm_sp(GetDisassembler());
68 if (disasm_sp) {
69 disasm_sp->Lock(this, NULL);
Greg Claytonba812f42012-05-10 02:52:23 +000070 DataExtractor data;
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 if (m_opcode.GetData(data)) {
72 bool is_alternate_isa;
73 lldb::addr_t pc = m_address.GetFileAddress();
Greg Claytonba812f42012-05-10 02:52:23 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr =
76 GetDisasmToUse(is_alternate_isa);
77 const uint8_t *opcode_data = data.GetDataStart();
78 const size_t opcode_data_len = data.GetByteSize();
79 llvm::MCInst inst;
80 const size_t inst_size =
81 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
82 // Be conservative, if we didn't understand the instruction, say it
83 // might branch...
84 if (inst_size == 0)
85 m_does_branch = eLazyBoolYes;
86 else {
87 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
88 if (can_branch)
89 m_does_branch = eLazyBoolYes;
90 else
91 m_does_branch = eLazyBoolNo;
92 }
Greg Claytonba812f42012-05-10 02:52:23 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 disasm_sp->Unlock();
95 }
Sean Callanan95e5c632012-02-17 00:53:45 +000096 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 return m_does_branch == eLazyBoolYes;
98 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 bool HasDelaySlot() override {
101 if (m_has_delay_slot == eLazyBoolCalculate) {
102 std::shared_ptr<DisassemblerLLVMC> disasm_sp(GetDisassembler());
103 if (disasm_sp) {
104 disasm_sp->Lock(this, NULL);
105 DataExtractor data;
106 if (m_opcode.GetData(data)) {
107 bool is_alternate_isa;
108 lldb::addr_t pc = m_address.GetFileAddress();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr =
111 GetDisasmToUse(is_alternate_isa);
112 const uint8_t *opcode_data = data.GetDataStart();
113 const size_t opcode_data_len = data.GetByteSize();
114 llvm::MCInst inst;
115 const size_t inst_size =
116 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
117 // if we didn't understand the instruction, say it doesn't have a
118 // delay slot...
119 if (inst_size == 0)
120 m_has_delay_slot = eLazyBoolNo;
121 else {
122 const bool has_delay_slot = mc_disasm_ptr->HasDelaySlot(inst);
123 if (has_delay_slot)
124 m_has_delay_slot = eLazyBoolYes;
125 else
126 m_has_delay_slot = eLazyBoolNo;
127 }
Sean Callanan4740a732016-09-06 04:48:36 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 disasm_sp->Unlock();
130 }
131 }
132 return m_has_delay_slot == eLazyBoolYes;
133 }
134
135 DisassemblerLLVMC::LLVMCDisassembler *GetDisasmToUse(bool &is_alternate_isa) {
136 is_alternate_isa = false;
137 std::shared_ptr<DisassemblerLLVMC> disasm_sp(GetDisassembler());
138 if (disasm_sp) {
139 if (disasm_sp->m_alternate_disasm_ap.get() != NULL) {
140 const AddressClass address_class = GetAddressClass();
141
142 if (address_class == eAddressClassCodeAlternateISA) {
143 is_alternate_isa = true;
144 return disasm_sp->m_alternate_disasm_ap.get();
145 }
146 }
147 return disasm_sp->m_disasm_ap.get();
148 }
149 return nullptr;
150 }
151
152 size_t Decode(const lldb_private::Disassembler &disassembler,
153 const lldb_private::DataExtractor &data,
154 lldb::offset_t data_offset) override {
155 // All we have to do is read the opcode which can be easy for some
156 // architectures
157 bool got_op = false;
158 std::shared_ptr<DisassemblerLLVMC> disasm_sp(GetDisassembler());
159 if (disasm_sp) {
160 const ArchSpec &arch = disasm_sp->GetArchitecture();
161 const lldb::ByteOrder byte_order = data.GetByteOrder();
162
163 const uint32_t min_op_byte_size = arch.GetMinimumOpcodeByteSize();
164 const uint32_t max_op_byte_size = arch.GetMaximumOpcodeByteSize();
165 if (min_op_byte_size == max_op_byte_size) {
166 // Fixed size instructions, just read that amount of data.
167 if (!data.ValidOffsetForDataOfSize(data_offset, min_op_byte_size))
168 return false;
169
170 switch (min_op_byte_size) {
171 case 1:
172 m_opcode.SetOpcode8(data.GetU8(&data_offset), byte_order);
173 got_op = true;
174 break;
175
176 case 2:
177 m_opcode.SetOpcode16(data.GetU16(&data_offset), byte_order);
178 got_op = true;
179 break;
180
181 case 4:
182 m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order);
183 got_op = true;
184 break;
185
186 case 8:
187 m_opcode.SetOpcode64(data.GetU64(&data_offset), byte_order);
188 got_op = true;
189 break;
190
191 default:
192 m_opcode.SetOpcodeBytes(data.PeekData(data_offset, min_op_byte_size),
193 min_op_byte_size);
194 got_op = true;
195 break;
196 }
197 }
198 if (!got_op) {
199 bool is_alternate_isa = false;
200 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr =
201 GetDisasmToUse(is_alternate_isa);
202
203 const llvm::Triple::ArchType machine = arch.GetMachine();
204 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb) {
205 if (machine == llvm::Triple::thumb || is_alternate_isa) {
206 uint32_t thumb_opcode = data.GetU16(&data_offset);
207 if ((thumb_opcode & 0xe000) != 0xe000 ||
208 ((thumb_opcode & 0x1800u) == 0)) {
209 m_opcode.SetOpcode16(thumb_opcode, byte_order);
210 m_is_valid = true;
211 } else {
212 thumb_opcode <<= 16;
213 thumb_opcode |= data.GetU16(&data_offset);
214 m_opcode.SetOpcode16_2(thumb_opcode, byte_order);
215 m_is_valid = true;
216 }
217 } else {
218 m_opcode.SetOpcode32(data.GetU32(&data_offset), byte_order);
219 m_is_valid = true;
220 }
221 } else {
222 // The opcode isn't evenly sized, so we need to actually use the llvm
223 // disassembler to parse it and get the size.
224 uint8_t *opcode_data =
225 const_cast<uint8_t *>(data.PeekData(data_offset, 1));
226 const size_t opcode_data_len = data.BytesLeft(data_offset);
227 const addr_t pc = m_address.GetFileAddress();
228 llvm::MCInst inst;
229
230 disasm_sp->Lock(this, NULL);
231 const size_t inst_size =
232 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
233 disasm_sp->Unlock();
234 if (inst_size == 0)
235 m_opcode.Clear();
236 else {
237 m_opcode.SetOpcodeBytes(opcode_data, inst_size);
238 m_is_valid = true;
239 }
240 }
241 }
242 return m_opcode.GetByteSize();
243 }
244 return 0;
245 }
246
247 void AppendComment(std::string &description) {
248 if (m_comment.empty())
249 m_comment.swap(description);
250 else {
251 m_comment.append(", ");
252 m_comment.append(description);
253 }
254 }
255
256 void CalculateMnemonicOperandsAndComment(
257 const lldb_private::ExecutionContext *exe_ctx) override {
258 DataExtractor data;
259 const AddressClass address_class = GetAddressClass();
260
261 if (m_opcode.GetData(data)) {
262 std::string out_string;
263 std::string comment_string;
264
265 std::shared_ptr<DisassemblerLLVMC> disasm_sp(GetDisassembler());
266 if (disasm_sp) {
267 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr;
268
269 if (address_class == eAddressClassCodeAlternateISA)
270 mc_disasm_ptr = disasm_sp->m_alternate_disasm_ap.get();
271 else
272 mc_disasm_ptr = disasm_sp->m_disasm_ap.get();
273
274 lldb::addr_t pc = m_address.GetFileAddress();
275 m_using_file_addr = true;
276
277 const bool data_from_file = disasm_sp->m_data_from_file;
278 bool use_hex_immediates = true;
279 Disassembler::HexImmediateStyle hex_style = Disassembler::eHexStyleC;
280
281 if (exe_ctx) {
282 Target *target = exe_ctx->GetTargetPtr();
283 if (target) {
284 use_hex_immediates = target->GetUseHexImmediates();
285 hex_style = target->GetHexImmediateStyle();
286
287 if (!data_from_file) {
288 const lldb::addr_t load_addr = m_address.GetLoadAddress(target);
289 if (load_addr != LLDB_INVALID_ADDRESS) {
290 pc = load_addr;
291 m_using_file_addr = false;
292 }
293 }
294 }
295 }
296
297 disasm_sp->Lock(this, exe_ctx);
298
299 const uint8_t *opcode_data = data.GetDataStart();
300 const size_t opcode_data_len = data.GetByteSize();
301 llvm::MCInst inst;
302 size_t inst_size =
303 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
304
305 if (inst_size > 0) {
306 mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style);
307 mc_disasm_ptr->PrintMCInst(inst, out_string, comment_string);
308
309 if (!comment_string.empty()) {
310 AppendComment(comment_string);
311 }
312 }
313
314 disasm_sp->Unlock();
315
316 if (inst_size == 0) {
317 m_comment.assign("unknown opcode");
318 inst_size = m_opcode.GetByteSize();
319 StreamString mnemonic_strm;
320 lldb::offset_t offset = 0;
321 lldb::ByteOrder byte_order = data.GetByteOrder();
322 switch (inst_size) {
323 case 1: {
324 const uint8_t uval8 = data.GetU8(&offset);
325 m_opcode.SetOpcode8(uval8, byte_order);
326 m_opcode_name.assign(".byte");
327 mnemonic_strm.Printf("0x%2.2x", uval8);
328 } break;
329 case 2: {
330 const uint16_t uval16 = data.GetU16(&offset);
331 m_opcode.SetOpcode16(uval16, byte_order);
332 m_opcode_name.assign(".short");
333 mnemonic_strm.Printf("0x%4.4x", uval16);
334 } break;
335 case 4: {
336 const uint32_t uval32 = data.GetU32(&offset);
337 m_opcode.SetOpcode32(uval32, byte_order);
338 m_opcode_name.assign(".long");
339 mnemonic_strm.Printf("0x%8.8x", uval32);
340 } break;
341 case 8: {
342 const uint64_t uval64 = data.GetU64(&offset);
343 m_opcode.SetOpcode64(uval64, byte_order);
344 m_opcode_name.assign(".quad");
345 mnemonic_strm.Printf("0x%16.16" PRIx64, uval64);
346 } break;
347 default:
348 if (inst_size == 0)
349 return;
350 else {
351 const uint8_t *bytes = data.PeekData(offset, inst_size);
352 if (bytes == NULL)
353 return;
354 m_opcode_name.assign(".byte");
355 m_opcode.SetOpcodeBytes(bytes, inst_size);
356 mnemonic_strm.Printf("0x%2.2x", bytes[0]);
357 for (uint32_t i = 1; i < inst_size; ++i)
358 mnemonic_strm.Printf(" 0x%2.2x", bytes[i]);
359 }
360 break;
361 }
362 m_mnemonics.swap(mnemonic_strm.GetString());
363 return;
364 } else {
365 if (m_does_branch == eLazyBoolCalculate) {
366 const bool can_branch = mc_disasm_ptr->CanBranch(inst);
367 if (can_branch)
368 m_does_branch = eLazyBoolYes;
369 else
370 m_does_branch = eLazyBoolNo;
371 }
372 }
373
374 static RegularExpression s_regex("[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?");
375
376 RegularExpression::Match matches(3);
377
378 if (s_regex.Execute(out_string.c_str(), &matches)) {
379 matches.GetMatchAtIndex(out_string.c_str(), 1, m_opcode_name);
380 matches.GetMatchAtIndex(out_string.c_str(), 2, m_mnemonics);
381 }
382 }
383 }
384 }
385
386 bool IsValid() const { return m_is_valid; }
387
388 bool UsingFileAddress() const { return m_using_file_addr; }
389 size_t GetByteSize() const { return m_opcode.GetByteSize(); }
390
391 std::shared_ptr<DisassemblerLLVMC> GetDisassembler() {
392 return m_disasm_wp.lock();
393 }
394
395 static llvm::StringRef::const_iterator
396 ConsumeWhitespace(llvm::StringRef::const_iterator osi,
397 llvm::StringRef::const_iterator ose) {
398 while (osi != ose) {
399 switch (*osi) {
400 default:
Sean Callanan4740a732016-09-06 04:48:36 +0000401 return osi;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000402 case ' ':
403 case '\t':
404 break;
405 }
406 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000407 }
408
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 return osi;
410 }
Sean Callanan4740a732016-09-06 04:48:36 +0000411
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 static std::pair<bool, llvm::StringRef::const_iterator>
413 ConsumeChar(llvm::StringRef::const_iterator osi, const char c,
414 llvm::StringRef::const_iterator ose) {
415 bool found = false;
416
417 osi = ConsumeWhitespace(osi, ose);
418 if (osi != ose && *osi == c) {
419 found = true;
420 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422
423 return std::make_pair(found, osi);
424 }
425
426 static std::pair<Operand, llvm::StringRef::const_iterator>
427 ParseRegisterName(llvm::StringRef::const_iterator osi,
428 llvm::StringRef::const_iterator ose) {
429 Operand ret;
430 ret.m_type = Operand::Type::Register;
431 std::string str;
432
433 osi = ConsumeWhitespace(osi, ose);
434
435 while (osi != ose) {
436 if (*osi >= '0' && *osi <= '9') {
437 if (str.empty()) {
438 return std::make_pair(Operand(), osi);
439 } else {
440 str.push_back(*osi);
Sean Callanan4740a732016-09-06 04:48:36 +0000441 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442 } else if (*osi >= 'a' && *osi <= 'z') {
443 str.push_back(*osi);
444 } else {
445 switch (*osi) {
446 default:
447 if (str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000448 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 } else {
450 ret.m_register = ConstString(str);
451 return std::make_pair(ret, osi);
452 }
453 case '%':
454 if (!str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000455 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456 }
457 break;
Sean Callanan4740a732016-09-06 04:48:36 +0000458 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 }
460 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462
463 ret.m_register = ConstString(str);
464 return std::make_pair(ret, osi);
465 }
466
467 static std::pair<Operand, llvm::StringRef::const_iterator>
468 ParseImmediate(llvm::StringRef::const_iterator osi,
469 llvm::StringRef::const_iterator ose) {
470 Operand ret;
471 ret.m_type = Operand::Type::Immediate;
472 std::string str;
473 bool is_hex = false;
474
475 osi = ConsumeWhitespace(osi, ose);
476
477 while (osi != ose) {
478 if (*osi >= '0' && *osi <= '9') {
479 str.push_back(*osi);
480 } else if (*osi >= 'a' && *osi <= 'f') {
481 if (is_hex) {
482 str.push_back(*osi);
483 } else {
484 return std::make_pair(Operand(), osi);
485 }
486 } else {
487 switch (*osi) {
488 default:
489 if (str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000490 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 } else {
492 ret.m_immediate = strtoull(str.c_str(), nullptr, 0);
493 return std::make_pair(ret, osi);
494 }
495 case 'x':
496 if (!str.compare("0")) {
497 is_hex = true;
498 str.push_back(*osi);
499 } else {
Sean Callanan4740a732016-09-06 04:48:36 +0000500 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 }
502 break;
503 case '#':
504 case '$':
505 if (!str.empty()) {
Sean Callanan4740a732016-09-06 04:48:36 +0000506 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 }
508 break;
509 case '-':
510 if (str.empty()) {
511 ret.m_negative = true;
512 } else {
Sean Callanan4740a732016-09-06 04:48:36 +0000513 return std::make_pair(Operand(), osi);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
Sean Callanan4740a732016-09-06 04:48:36 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 }
517 ++osi;
Sean Callanan4740a732016-09-06 04:48:36 +0000518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519
520 ret.m_immediate = strtoull(str.c_str(), nullptr, 0);
521 return std::make_pair(ret, osi);
522 }
523
524 // -0x5(%rax,%rax,2)
525 static std::pair<Operand, llvm::StringRef::const_iterator>
526 ParseIntelIndexedAccess(llvm::StringRef::const_iterator osi,
527 llvm::StringRef::const_iterator ose) {
528 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
529 ParseImmediate(osi, ose);
530 if (offset_and_iterator.first.IsValid()) {
531 osi = offset_and_iterator.second;
Sean Callanan4740a732016-09-06 04:48:36 +0000532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533
534 bool found = false;
535 std::tie(found, osi) = ConsumeChar(osi, '(', ose);
536 if (!found) {
537 return std::make_pair(Operand(), osi);
Sean Callanan4740a732016-09-06 04:48:36 +0000538 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539
540 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
541 ParseRegisterName(osi, ose);
542 if (base_and_iterator.first.IsValid()) {
543 osi = base_and_iterator.second;
544 } else {
545 return std::make_pair(Operand(), osi);
546 }
547
548 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
549 if (!found) {
550 return std::make_pair(Operand(), osi);
551 }
552
553 std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator =
554 ParseRegisterName(osi, ose);
555 if (index_and_iterator.first.IsValid()) {
556 osi = index_and_iterator.second;
557 } else {
558 return std::make_pair(Operand(), osi);
559 }
560
561 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
562 if (!found) {
563 return std::make_pair(Operand(), osi);
564 }
565
566 std::pair<Operand, llvm::StringRef::const_iterator>
567 multiplier_and_iterator = ParseImmediate(osi, ose);
568 if (index_and_iterator.first.IsValid()) {
569 osi = index_and_iterator.second;
570 } else {
571 return std::make_pair(Operand(), osi);
572 }
573
574 std::tie(found, osi) = ConsumeChar(osi, ')', ose);
575 if (!found) {
576 return std::make_pair(Operand(), osi);
577 }
578
579 Operand product;
580 product.m_type = Operand::Type::Product;
581 product.m_children.push_back(index_and_iterator.first);
582 product.m_children.push_back(multiplier_and_iterator.first);
583
584 Operand index;
585 index.m_type = Operand::Type::Sum;
586 index.m_children.push_back(base_and_iterator.first);
587 index.m_children.push_back(product);
588
589 if (offset_and_iterator.first.IsValid()) {
590 Operand offset;
591 offset.m_type = Operand::Type::Sum;
592 offset.m_children.push_back(offset_and_iterator.first);
593 offset.m_children.push_back(index);
594
595 Operand deref;
596 deref.m_type = Operand::Type::Dereference;
597 deref.m_children.push_back(offset);
598 return std::make_pair(deref, osi);
599 } else {
600 Operand deref;
601 deref.m_type = Operand::Type::Dereference;
602 deref.m_children.push_back(index);
603 return std::make_pair(deref, osi);
604 }
605 }
606
607 // -0x10(%rbp)
608 static std::pair<Operand, llvm::StringRef::const_iterator>
609 ParseIntelDerefAccess(llvm::StringRef::const_iterator osi,
610 llvm::StringRef::const_iterator ose) {
611 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
612 ParseImmediate(osi, ose);
613 if (offset_and_iterator.first.IsValid()) {
614 osi = offset_and_iterator.second;
615 }
616
617 bool found = false;
618 std::tie(found, osi) = ConsumeChar(osi, '(', ose);
619 if (!found) {
620 return std::make_pair(Operand(), osi);
621 }
622
623 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
624 ParseRegisterName(osi, ose);
625 if (base_and_iterator.first.IsValid()) {
626 osi = base_and_iterator.second;
627 } else {
628 return std::make_pair(Operand(), osi);
629 }
630
631 std::tie(found, osi) = ConsumeChar(osi, ')', ose);
632 if (!found) {
633 return std::make_pair(Operand(), osi);
634 }
635
636 if (offset_and_iterator.first.IsValid()) {
637 Operand offset;
638 offset.m_type = Operand::Type::Sum;
639 offset.m_children.push_back(offset_and_iterator.first);
640 offset.m_children.push_back(base_and_iterator.first);
641
642 Operand deref;
643 deref.m_type = Operand::Type::Dereference;
644 deref.m_children.push_back(offset);
645 return std::make_pair(deref, osi);
646 } else {
647 Operand deref;
648 deref.m_type = Operand::Type::Dereference;
649 deref.m_children.push_back(base_and_iterator.first);
650 return std::make_pair(deref, osi);
651 }
652 }
653
654 // [sp, #8]!
655 static std::pair<Operand, llvm::StringRef::const_iterator>
656 ParseARMOffsetAccess(llvm::StringRef::const_iterator osi,
657 llvm::StringRef::const_iterator ose) {
658 bool found = false;
659 std::tie(found, osi) = ConsumeChar(osi, '[', ose);
660 if (!found) {
661 return std::make_pair(Operand(), osi);
662 }
663
664 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
665 ParseRegisterName(osi, ose);
666 if (base_and_iterator.first.IsValid()) {
667 osi = base_and_iterator.second;
668 } else {
669 return std::make_pair(Operand(), osi);
670 }
671
672 std::tie(found, osi) = ConsumeChar(osi, ',', ose);
673 if (!found) {
674 return std::make_pair(Operand(), osi);
675 }
676
677 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
678 ParseImmediate(osi, ose);
679 if (offset_and_iterator.first.IsValid()) {
680 osi = offset_and_iterator.second;
681 }
682
683 std::tie(found, osi) = ConsumeChar(osi, ']', ose);
684 if (!found) {
685 return std::make_pair(Operand(), osi);
686 }
687
688 Operand offset;
689 offset.m_type = Operand::Type::Sum;
690 offset.m_children.push_back(offset_and_iterator.first);
691 offset.m_children.push_back(base_and_iterator.first);
692
693 Operand deref;
694 deref.m_type = Operand::Type::Dereference;
695 deref.m_children.push_back(offset);
696 return std::make_pair(deref, osi);
697 }
698
699 // [sp]
700 static std::pair<Operand, llvm::StringRef::const_iterator>
701 ParseARMDerefAccess(llvm::StringRef::const_iterator osi,
702 llvm::StringRef::const_iterator ose) {
703 bool found = false;
704 std::tie(found, osi) = ConsumeChar(osi, '[', ose);
705 if (!found) {
706 return std::make_pair(Operand(), osi);
707 }
708
709 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
710 ParseRegisterName(osi, ose);
711 if (base_and_iterator.first.IsValid()) {
712 osi = base_and_iterator.second;
713 } else {
714 return std::make_pair(Operand(), osi);
715 }
716
717 std::tie(found, osi) = ConsumeChar(osi, ']', ose);
718 if (!found) {
719 return std::make_pair(Operand(), osi);
720 }
721
722 Operand deref;
723 deref.m_type = Operand::Type::Dereference;
724 deref.m_children.push_back(base_and_iterator.first);
725 return std::make_pair(deref, osi);
726 }
727
728 static void DumpOperand(const Operand &op, Stream &s) {
729 switch (op.m_type) {
730 case Operand::Type::Dereference:
731 s.PutCString("*");
732 DumpOperand(op.m_children[0], s);
733 break;
734 case Operand::Type::Immediate:
735 if (op.m_negative) {
736 s.PutCString("-");
737 }
738 s.PutCString(llvm::to_string(op.m_immediate).c_str());
739 break;
740 case Operand::Type::Invalid:
741 s.PutCString("Invalid");
742 break;
743 case Operand::Type::Product:
744 s.PutCString("(");
745 DumpOperand(op.m_children[0], s);
746 s.PutCString("*");
747 DumpOperand(op.m_children[1], s);
748 s.PutCString(")");
749 break;
750 case Operand::Type::Register:
751 s.PutCString(op.m_register.AsCString());
752 break;
753 case Operand::Type::Sum:
754 s.PutCString("(");
755 DumpOperand(op.m_children[0], s);
756 s.PutCString("+");
757 DumpOperand(op.m_children[1], s);
758 s.PutCString(")");
759 break;
760 }
761 }
762
763 bool ParseOperands(
764 llvm::SmallVectorImpl<Instruction::Operand> &operands) override {
765 const char *operands_string = GetOperands(nullptr);
766
767 if (!operands_string) {
768 return false;
769 }
770
771 llvm::StringRef operands_ref(operands_string);
772
773 llvm::StringRef::const_iterator osi = operands_ref.begin();
774 llvm::StringRef::const_iterator ose = operands_ref.end();
775
776 while (osi != ose) {
777 Operand operand;
778 llvm::StringRef::const_iterator iter;
779
780 if ((std::tie(operand, iter) = ParseIntelIndexedAccess(osi, ose),
781 operand.IsValid()) ||
782 (std::tie(operand, iter) = ParseIntelDerefAccess(osi, ose),
783 operand.IsValid()) ||
784 (std::tie(operand, iter) = ParseARMOffsetAccess(osi, ose),
785 operand.IsValid()) ||
786 (std::tie(operand, iter) = ParseARMDerefAccess(osi, ose),
787 operand.IsValid()) ||
788 (std::tie(operand, iter) = ParseRegisterName(osi, ose),
789 operand.IsValid()) ||
790 (std::tie(operand, iter) = ParseImmediate(osi, ose),
791 operand.IsValid())) {
792 osi = iter;
793 operands.push_back(operand);
794 } else {
795 return false;
796 }
797
798 std::pair<bool, llvm::StringRef::const_iterator> found_and_iter =
799 ConsumeChar(osi, ',', ose);
800 if (found_and_iter.first) {
801 osi = found_and_iter.second;
802 }
803
804 osi = ConsumeWhitespace(osi, ose);
805 }
806
807 DisassemblerSP disasm_sp = m_disasm_wp.lock();
808
809 if (disasm_sp && operands.size() > 1) {
810 // TODO tie this into the MC Disassembler's notion of clobbers.
811 switch (disasm_sp->GetArchitecture().GetMachine()) {
812 default:
813 break;
814 case llvm::Triple::x86:
815 case llvm::Triple::x86_64:
816 operands[operands.size() - 1].m_clobbered = true;
817 break;
818 case llvm::Triple::arm:
819 operands[0].m_clobbered = true;
820 break;
821 }
822 }
823
824 if (Log *log =
825 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS)) {
826 StreamString ss;
827
828 ss.Printf("[%s] expands to %zu operands:\n", operands_string,
829 operands.size());
830 for (const Operand &operand : operands) {
831 ss.PutCString(" ");
832 DumpOperand(operand, ss);
833 ss.PutCString("\n");
834 }
835
836 log->PutCString(ss.GetData());
837 }
838
839 return true;
840 }
841
842 bool IsCall() override {
843 if (m_is_call == eLazyBoolCalculate) {
844 std::shared_ptr<DisassemblerLLVMC> disasm_sp(GetDisassembler());
845 if (disasm_sp) {
846 disasm_sp->Lock(this, NULL);
847 DataExtractor data;
848 if (m_opcode.GetData(data)) {
849 bool is_alternate_isa;
850 lldb::addr_t pc = m_address.GetFileAddress();
851
852 DisassemblerLLVMC::LLVMCDisassembler *mc_disasm_ptr =
853 GetDisasmToUse(is_alternate_isa);
854 const uint8_t *opcode_data = data.GetDataStart();
855 const size_t opcode_data_len = data.GetByteSize();
856 llvm::MCInst inst;
857 const size_t inst_size =
858 mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst);
859 if (inst_size == 0) {
860 m_is_call = eLazyBoolNo;
861 } else {
862 if (mc_disasm_ptr->IsCall(inst))
863 m_is_call = eLazyBoolYes;
Sean Callanan4740a732016-09-06 04:48:36 +0000864 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865 m_is_call = eLazyBoolNo;
866 }
Sean Callanan4740a732016-09-06 04:48:36 +0000867 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 disasm_sp->Unlock();
869 }
Sean Callanan4740a732016-09-06 04:48:36 +0000870 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000871 return m_is_call == eLazyBoolYes;
872 }
873
Sean Callanan95e5c632012-02-17 00:53:45 +0000874protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000875 std::weak_ptr<DisassemblerLLVMC> m_disasm_wp;
876 LazyBool m_does_branch;
877 LazyBool m_has_delay_slot;
878 LazyBool m_is_call;
879 bool m_is_valid;
880 bool m_using_file_addr;
Sean Callanan95e5c632012-02-17 00:53:45 +0000881};
882
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883DisassemblerLLVMC::LLVMCDisassembler::LLVMCDisassembler(
884 const char *triple, const char *cpu, const char *features_str,
885 unsigned flavor, DisassemblerLLVMC &owner)
886 : m_is_valid(true) {
887 std::string Error;
888 const llvm::Target *curr_target =
889 llvm::TargetRegistry::lookupTarget(triple, Error);
890 if (!curr_target) {
891 m_is_valid = false;
892 return;
893 }
894
895 m_instr_info_ap.reset(curr_target->createMCInstrInfo());
896 m_reg_info_ap.reset(curr_target->createMCRegInfo(triple));
897
898 m_subtarget_info_ap.reset(
899 curr_target->createMCSubtargetInfo(triple, cpu, features_str));
900
901 std::unique_ptr<llvm::MCRegisterInfo> reg_info(
902 curr_target->createMCRegInfo(triple));
903 m_asm_info_ap.reset(curr_target->createMCAsmInfo(*reg_info, triple));
904
905 if (m_instr_info_ap.get() == NULL || m_reg_info_ap.get() == NULL ||
906 m_subtarget_info_ap.get() == NULL || m_asm_info_ap.get() == NULL) {
907 m_is_valid = false;
908 return;
909 }
910
911 m_context_ap.reset(
912 new llvm::MCContext(m_asm_info_ap.get(), m_reg_info_ap.get(), 0));
913
914 m_disasm_ap.reset(curr_target->createMCDisassembler(
915 *m_subtarget_info_ap.get(), *m_context_ap.get()));
916 if (m_disasm_ap.get() && m_context_ap.get()) {
917 std::unique_ptr<llvm::MCRelocationInfo> RelInfo(
918 curr_target->createMCRelocationInfo(triple, *m_context_ap.get()));
919 if (!RelInfo) {
920 m_is_valid = false;
921 return;
922 }
923 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(
924 curr_target->createMCSymbolizer(
925 triple, NULL, DisassemblerLLVMC::SymbolLookupCallback,
926 (void *)&owner, m_context_ap.get(), std::move(RelInfo)));
927 m_disasm_ap->setSymbolizer(std::move(symbolizer_up));
928
929 unsigned asm_printer_variant;
930 if (flavor == ~0U)
931 asm_printer_variant = m_asm_info_ap->getAssemblerDialect();
932 else {
933 asm_printer_variant = flavor;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000934 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000935
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 m_instr_printer_ap.reset(curr_target->createMCInstPrinter(
937 llvm::Triple{triple}, asm_printer_variant, *m_asm_info_ap.get(),
938 *m_instr_info_ap.get(), *m_reg_info_ap.get()));
939 if (m_instr_printer_ap.get() == NULL) {
940 m_disasm_ap.reset();
941 m_is_valid = false;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000942 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000943 } else
944 m_is_valid = false;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000945}
946
Eugene Zelenko45a40142015-10-22 21:24:37 +0000947DisassemblerLLVMC::LLVMCDisassembler::~LLVMCDisassembler() = default;
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +0000948
Kate Stoneb9c1b512016-09-06 20:57:50 +0000949uint64_t DisassemblerLLVMC::LLVMCDisassembler::GetMCInst(
950 const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc,
951 llvm::MCInst &mc_inst) {
952 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
953 llvm::MCDisassembler::DecodeStatus status;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000954
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955 uint64_t new_inst_size;
956 status = m_disasm_ap->getInstruction(mc_inst, new_inst_size, data, pc,
957 llvm::nulls(), llvm::nulls());
958 if (status == llvm::MCDisassembler::Success)
959 return new_inst_size;
960 else
961 return 0;
Jim Ingham0f063ba2013-03-02 00:26:47 +0000962}
963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964void DisassemblerLLVMC::LLVMCDisassembler::PrintMCInst(
965 llvm::MCInst &mc_inst, std::string &inst_string,
966 std::string &comments_string) {
967 llvm::raw_string_ostream inst_stream(inst_string);
968 llvm::raw_string_ostream comments_stream(comments_string);
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +0000969
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 m_instr_printer_ap->setCommentStream(comments_stream);
971 m_instr_printer_ap->printInst(&mc_inst, inst_stream, llvm::StringRef(),
972 *m_subtarget_info_ap);
973 m_instr_printer_ap->setCommentStream(llvm::nulls());
974 comments_stream.flush();
975
976 static std::string g_newlines("\r\n");
977
978 for (size_t newline_pos = 0;
979 (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) !=
980 comments_string.npos;
981 /**/) {
982 comments_string.replace(comments_string.begin() + newline_pos,
983 comments_string.begin() + newline_pos + 1, 1, ' ');
984 }
Jim Ingham0f063ba2013-03-02 00:26:47 +0000985}
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987void DisassemblerLLVMC::LLVMCDisassembler::SetStyle(
988 bool use_hex_immed, HexImmediateStyle hex_style) {
989 m_instr_printer_ap->setPrintImmHex(use_hex_immed);
990 switch (hex_style) {
991 case eHexStyleC:
992 m_instr_printer_ap->setPrintHexStyle(llvm::HexStyle::C);
993 break;
994 case eHexStyleAsm:
995 m_instr_printer_ap->setPrintHexStyle(llvm::HexStyle::Asm);
996 break;
997 }
Daniel Malead79ae052013-08-07 21:54:09 +0000998}
999
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000bool DisassemblerLLVMC::LLVMCDisassembler::CanBranch(llvm::MCInst &mc_inst) {
1001 return m_instr_info_ap->get(mc_inst.getOpcode())
1002 .mayAffectControlFlow(mc_inst, *m_reg_info_ap.get());
Jim Ingham0f063ba2013-03-02 00:26:47 +00001003}
1004
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005bool DisassemblerLLVMC::LLVMCDisassembler::HasDelaySlot(llvm::MCInst &mc_inst) {
1006 return m_instr_info_ap->get(mc_inst.getOpcode()).hasDelaySlot();
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00001007}
1008
Kate Stoneb9c1b512016-09-06 20:57:50 +00001009bool DisassemblerLLVMC::LLVMCDisassembler::IsCall(llvm::MCInst &mc_inst) {
1010 return m_instr_info_ap->get(mc_inst.getOpcode()).isCall();
Sean Callanan4740a732016-09-06 04:48:36 +00001011}
1012
Kate Stoneb9c1b512016-09-06 20:57:50 +00001013DisassemblerLLVMC::DisassemblerLLVMC(const ArchSpec &arch,
1014 const char *flavor_string)
1015 : Disassembler(arch, flavor_string), m_exe_ctx(NULL), m_inst(NULL),
1016 m_data_from_file(false) {
1017 if (!FlavorValidForArchSpec(arch, m_flavor.c_str())) {
1018 m_flavor.assign("default");
1019 }
1020
1021 unsigned flavor = ~0U;
1022 llvm::Triple triple = arch.GetTriple();
1023
1024 // So far the only supported flavor is "intel" on x86. The base class will
1025 // set this
1026 // correctly coming in.
1027 if (triple.getArch() == llvm::Triple::x86 ||
1028 triple.getArch() == llvm::Triple::x86_64) {
1029 if (m_flavor == "intel") {
1030 flavor = 1;
1031 } else if (m_flavor == "att") {
1032 flavor = 0;
Jim Ingham0f063ba2013-03-02 00:26:47 +00001033 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001034 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 ArchSpec thumb_arch(arch);
1037 if (triple.getArch() == llvm::Triple::arm) {
1038 std::string thumb_arch_name(thumb_arch.GetTriple().getArchName().str());
1039 // Replace "arm" with "thumb" so we get all thumb variants correct
1040 if (thumb_arch_name.size() > 3) {
1041 thumb_arch_name.erase(0, 3);
1042 thumb_arch_name.insert(0, "thumb");
1043 } else {
1044 thumb_arch_name = "thumbv8.2a";
Jim Ingham0f063ba2013-03-02 00:26:47 +00001045 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 thumb_arch.GetTriple().setArchName(
1047 llvm::StringRef(thumb_arch_name.c_str()));
1048 }
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 // If no sub architecture specified then use the most recent arm architecture
1051 // so the
1052 // disassembler will return all instruction. Without it we will see a lot of
1053 // unknow opcode
1054 // in case the code uses instructions which are not available in the oldest
1055 // arm version
1056 // (used when no sub architecture is specified)
1057 if (triple.getArch() == llvm::Triple::arm &&
1058 triple.getSubArch() == llvm::Triple::NoSubArch)
1059 triple.setArchName("armv8.2a");
1060
1061 const char *triple_str = triple.getTriple().c_str();
1062
1063 // ARM Cortex M0-M7 devices only execute thumb instructions
1064 if (arch.IsAlwaysThumbInstructions()) {
1065 triple_str = thumb_arch.GetTriple().getTriple().c_str();
1066 }
1067
1068 const char *cpu = "";
1069
1070 switch (arch.GetCore()) {
1071 case ArchSpec::eCore_mips32:
1072 case ArchSpec::eCore_mips32el:
1073 cpu = "mips32";
1074 break;
1075 case ArchSpec::eCore_mips32r2:
1076 case ArchSpec::eCore_mips32r2el:
1077 cpu = "mips32r2";
1078 break;
1079 case ArchSpec::eCore_mips32r3:
1080 case ArchSpec::eCore_mips32r3el:
1081 cpu = "mips32r3";
1082 break;
1083 case ArchSpec::eCore_mips32r5:
1084 case ArchSpec::eCore_mips32r5el:
1085 cpu = "mips32r5";
1086 break;
1087 case ArchSpec::eCore_mips32r6:
1088 case ArchSpec::eCore_mips32r6el:
1089 cpu = "mips32r6";
1090 break;
1091 case ArchSpec::eCore_mips64:
1092 case ArchSpec::eCore_mips64el:
1093 cpu = "mips64";
1094 break;
1095 case ArchSpec::eCore_mips64r2:
1096 case ArchSpec::eCore_mips64r2el:
1097 cpu = "mips64r2";
1098 break;
1099 case ArchSpec::eCore_mips64r3:
1100 case ArchSpec::eCore_mips64r3el:
1101 cpu = "mips64r3";
1102 break;
1103 case ArchSpec::eCore_mips64r5:
1104 case ArchSpec::eCore_mips64r5el:
1105 cpu = "mips64r5";
1106 break;
1107 case ArchSpec::eCore_mips64r6:
1108 case ArchSpec::eCore_mips64r6el:
1109 cpu = "mips64r6";
1110 break;
1111 default:
1112 cpu = "";
1113 break;
1114 }
1115
1116 std::string features_str = "";
1117 if (triple.getArch() == llvm::Triple::mips ||
1118 triple.getArch() == llvm::Triple::mipsel ||
1119 triple.getArch() == llvm::Triple::mips64 ||
1120 triple.getArch() == llvm::Triple::mips64el) {
1121 uint32_t arch_flags = arch.GetFlags();
1122 if (arch_flags & ArchSpec::eMIPSAse_msa)
1123 features_str += "+msa,";
1124 if (arch_flags & ArchSpec::eMIPSAse_dsp)
1125 features_str += "+dsp,";
1126 if (arch_flags & ArchSpec::eMIPSAse_dspr2)
1127 features_str += "+dspr2,";
1128 }
1129
1130 m_disasm_ap.reset(new LLVMCDisassembler(triple_str, cpu, features_str.c_str(),
1131 flavor, *this));
1132 if (!m_disasm_ap->IsValid()) {
1133 // We use m_disasm_ap.get() to tell whether we are valid or not, so if this
1134 // isn't good for some reason,
1135 // we reset it, and then we won't be valid and FindPlugin will fail and we
1136 // won't get used.
1137 m_disasm_ap.reset();
1138 }
1139
1140 llvm::Triple::ArchType llvm_arch = triple.getArch();
1141
1142 // For arm CPUs that can execute arm or thumb instructions, also create a
1143 // thumb instruction disassembler.
1144 if (llvm_arch == llvm::Triple::arm) {
1145 std::string thumb_triple(thumb_arch.GetTriple().getTriple());
1146 m_alternate_disasm_ap.reset(
1147 new LLVMCDisassembler(thumb_triple.c_str(), "", "", flavor, *this));
1148 if (!m_alternate_disasm_ap->IsValid()) {
1149 m_disasm_ap.reset();
1150 m_alternate_disasm_ap.reset();
Jason Molenda01aa5342013-07-22 22:11:53 +00001151 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152 } else if (llvm_arch == llvm::Triple::mips ||
1153 llvm_arch == llvm::Triple::mipsel ||
1154 llvm_arch == llvm::Triple::mips64 ||
1155 llvm_arch == llvm::Triple::mips64el) {
1156 /* Create alternate disassembler for MIPS16 and microMIPS */
1157 uint32_t arch_flags = arch.GetFlags();
1158 if (arch_flags & ArchSpec::eMIPSAse_mips16)
1159 features_str += "+mips16,";
1160 else if (arch_flags & ArchSpec::eMIPSAse_micromips)
1161 features_str += "+micromips,";
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001162
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 m_alternate_disasm_ap.reset(new LLVMCDisassembler(
1164 triple_str, cpu, features_str.c_str(), flavor, *this));
1165 if (!m_alternate_disasm_ap->IsValid()) {
1166 m_disasm_ap.reset();
1167 m_alternate_disasm_ap.reset();
Jason Molenda01aa5342013-07-22 22:11:53 +00001168 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169 }
Sean Callanan95e5c632012-02-17 00:53:45 +00001170}
1171
Eugene Zelenko45a40142015-10-22 21:24:37 +00001172DisassemblerLLVMC::~DisassemblerLLVMC() = default;
1173
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174Disassembler *DisassemblerLLVMC::CreateInstance(const ArchSpec &arch,
1175 const char *flavor) {
1176 if (arch.GetTriple().getArch() != llvm::Triple::UnknownArch) {
1177 std::unique_ptr<DisassemblerLLVMC> disasm_ap(
1178 new DisassemblerLLVMC(arch, flavor));
Eugene Zelenko45a40142015-10-22 21:24:37 +00001179
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 if (disasm_ap.get() && disasm_ap->IsValid())
1181 return disasm_ap.release();
1182 }
1183 return NULL;
Eugene Zelenko8dd3fdb2015-10-21 01:42:15 +00001184}
1185
Kate Stoneb9c1b512016-09-06 20:57:50 +00001186size_t DisassemblerLLVMC::DecodeInstructions(const Address &base_addr,
1187 const DataExtractor &data,
1188 lldb::offset_t data_offset,
1189 size_t num_instructions,
1190 bool append, bool data_from_file) {
1191 if (!append)
1192 m_instruction_list.Clear();
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001193
Kate Stoneb9c1b512016-09-06 20:57:50 +00001194 if (!IsValid())
Sean Callanan95e5c632012-02-17 00:53:45 +00001195 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001196
1197 m_data_from_file = data_from_file;
1198 uint32_t data_cursor = data_offset;
1199 const size_t data_byte_size = data.GetByteSize();
1200 uint32_t instructions_parsed = 0;
1201 Address inst_addr(base_addr);
1202
1203 while (data_cursor < data_byte_size &&
1204 instructions_parsed < num_instructions) {
1205
1206 AddressClass address_class = eAddressClassCode;
1207
1208 if (m_alternate_disasm_ap.get() != NULL)
1209 address_class = inst_addr.GetAddressClass();
1210
1211 InstructionSP inst_sp(
1212 new InstructionLLVMC(*this, inst_addr, address_class));
1213
1214 if (!inst_sp)
1215 break;
1216
1217 uint32_t inst_size = inst_sp->Decode(*this, data, data_cursor);
1218
1219 if (inst_size == 0)
1220 break;
1221
1222 m_instruction_list.Append(inst_sp);
1223 data_cursor += inst_size;
1224 inst_addr.Slide(inst_size);
1225 instructions_parsed++;
1226 }
1227
1228 return data_cursor - data_offset;
Sean Callanan95e5c632012-02-17 00:53:45 +00001229}
1230
Kate Stoneb9c1b512016-09-06 20:57:50 +00001231void DisassemblerLLVMC::Initialize() {
1232 PluginManager::RegisterPlugin(GetPluginNameStatic(),
1233 "Disassembler that uses LLVM MC to disassemble "
1234 "i386, x86_64, ARM, and ARM64.",
1235 CreateInstance);
Jason Molendac980fa92015-02-13 23:24:21 +00001236
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237 llvm::InitializeAllTargetInfos();
1238 llvm::InitializeAllTargetMCs();
1239 llvm::InitializeAllAsmParsers();
1240 llvm::InitializeAllDisassemblers();
1241}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001242
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243void DisassemblerLLVMC::Terminate() {
1244 PluginManager::UnregisterPlugin(CreateInstance);
1245}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001246
Kate Stoneb9c1b512016-09-06 20:57:50 +00001247ConstString DisassemblerLLVMC::GetPluginNameStatic() {
1248 static ConstString g_name("llvm-mc");
1249 return g_name;
1250}
Sylvestre Ledrua3e4ceb2014-04-15 12:08:57 +00001251
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252int DisassemblerLLVMC::OpInfoCallback(void *disassembler, uint64_t pc,
1253 uint64_t offset, uint64_t size,
1254 int tag_type, void *tag_bug) {
1255 return static_cast<DisassemblerLLVMC *>(disassembler)
1256 ->OpInfo(pc, offset, size, tag_type, tag_bug);
1257}
1258
1259const char *DisassemblerLLVMC::SymbolLookupCallback(void *disassembler,
1260 uint64_t value,
1261 uint64_t *type, uint64_t pc,
1262 const char **name) {
1263 return static_cast<DisassemblerLLVMC *>(disassembler)
1264 ->SymbolLookup(value, type, pc, name);
1265}
1266
1267bool DisassemblerLLVMC::FlavorValidForArchSpec(
1268 const lldb_private::ArchSpec &arch, const char *flavor) {
1269 llvm::Triple triple = arch.GetTriple();
1270 if (flavor == NULL || strcmp(flavor, "default") == 0)
1271 return true;
1272
1273 if (triple.getArch() == llvm::Triple::x86 ||
1274 triple.getArch() == llvm::Triple::x86_64) {
1275 if (strcmp(flavor, "intel") == 0 || strcmp(flavor, "att") == 0)
1276 return true;
1277 else
1278 return false;
1279 } else
1280 return false;
1281}
1282
1283int DisassemblerLLVMC::OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size,
1284 int tag_type, void *tag_bug) {
1285 switch (tag_type) {
1286 default:
1287 break;
1288 case 1:
1289 memset(tag_bug, 0, sizeof(::LLVMOpInfo1));
1290 break;
1291 }
1292 return 0;
1293}
1294
1295const char *DisassemblerLLVMC::SymbolLookup(uint64_t value, uint64_t *type_ptr,
1296 uint64_t pc, const char **name) {
1297 if (*type_ptr) {
1298 if (m_exe_ctx && m_inst) {
1299 // std::string remove_this_prior_to_checkin;
1300 Target *target = m_exe_ctx ? m_exe_ctx->GetTargetPtr() : NULL;
1301 Address value_so_addr;
1302 Address pc_so_addr;
1303 if (m_inst->UsingFileAddress()) {
1304 ModuleSP module_sp(m_inst->GetAddress().GetModule());
1305 if (module_sp) {
1306 module_sp->ResolveFileAddress(value, value_so_addr);
1307 module_sp->ResolveFileAddress(pc, pc_so_addr);
Sean Callanan95e5c632012-02-17 00:53:45 +00001308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 } else if (target && !target->GetSectionLoadList().IsEmpty()) {
1310 target->GetSectionLoadList().ResolveLoadAddress(value, value_so_addr);
1311 target->GetSectionLoadList().ResolveLoadAddress(pc, pc_so_addr);
1312 }
Greg Claytonba812f42012-05-10 02:52:23 +00001313
Kate Stoneb9c1b512016-09-06 20:57:50 +00001314 SymbolContext sym_ctx;
1315 const uint32_t resolve_scope =
1316 eSymbolContextFunction | eSymbolContextSymbol;
1317 if (pc_so_addr.IsValid() && pc_so_addr.GetModule()) {
1318 pc_so_addr.GetModule()->ResolveSymbolContextForAddress(
1319 pc_so_addr, resolve_scope, sym_ctx);
1320 }
1321
1322 if (value_so_addr.IsValid() && value_so_addr.GetSection()) {
1323 StreamString ss;
1324
1325 bool format_omitting_current_func_name = false;
1326 if (sym_ctx.symbol || sym_ctx.function) {
1327 AddressRange range;
1328 if (sym_ctx.GetAddressRange(resolve_scope, 0, false, range) &&
1329 range.GetBaseAddress().IsValid() &&
1330 range.ContainsLoadAddress(value_so_addr, target)) {
1331 format_omitting_current_func_name = true;
1332 }
1333 }
1334
1335 // If the "value" address (the target address we're symbolicating)
1336 // is inside the same SymbolContext as the current instruction pc
1337 // (pc_so_addr), don't print the full function name - just print it
1338 // with DumpStyleNoFunctionName style, e.g. "<+36>".
1339 if (format_omitting_current_func_name) {
1340 value_so_addr.Dump(&ss, target, Address::DumpStyleNoFunctionName,
1341 Address::DumpStyleSectionNameOffset);
1342 } else {
1343 value_so_addr.Dump(
1344 &ss, target,
1345 Address::DumpStyleResolvedDescriptionNoFunctionArguments,
1346 Address::DumpStyleSectionNameOffset);
1347 }
1348
1349 if (!ss.GetString().empty()) {
1350 // If Address::Dump returned a multi-line description, most commonly
1351 // seen when we
1352 // have multiple levels of inlined functions at an address, only show
1353 // the first line.
1354 std::string &str(ss.GetString());
1355 size_t first_eol_char = str.find_first_of("\r\n");
1356 if (first_eol_char != std::string::npos) {
1357 str.erase(first_eol_char);
1358 }
1359 m_inst->AppendComment(ss.GetString());
1360 }
1361 }
1362 }
1363 }
1364
1365 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
1366 *name = NULL;
1367 return NULL;
Sean Callanan95e5c632012-02-17 00:53:45 +00001368}
1369
1370//------------------------------------------------------------------
1371// PluginInterface protocol
1372//------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373ConstString DisassemblerLLVMC::GetPluginName() { return GetPluginNameStatic(); }
Sean Callanan95e5c632012-02-17 00:53:45 +00001374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375uint32_t DisassemblerLLVMC::GetPluginVersion() { return 1; }