11#include "llvm-c/Disassembler.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDisassembler/MCDisassembler.h"
17#include "llvm/MC/MCDisassembler/MCExternalSymbolizer.h"
18#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstPrinter.h"
21#include "llvm/MC/MCInstrAnalysis.h"
22#include "llvm/MC/MCInstrInfo.h"
23#include "llvm/MC/MCRegisterInfo.h"
24#include "llvm/MC/MCSubtargetInfo.h"
25#include "llvm/MC/MCTargetOptions.h"
26#include "llvm/MC/TargetRegistry.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/ScopedPrinter.h"
29#include "llvm/Support/TargetSelect.h"
30#include "llvm/TargetParser/AArch64TargetParser.h"
55 static std::unique_ptr<MCDisasmInstance>
56 Create(
const char *triple,
const char *cpu,
const char *features_str,
61 uint64_t GetMCInst(
const uint8_t *opcode_data,
size_t opcode_data_len,
64 std::string &inst_string, std::string &comments_string);
66 void SetUseColor(
bool use_color);
67 bool GetUseColor()
const;
68 bool CanBranch(llvm::MCInst &mc_inst)
const;
69 bool HasDelaySlot(llvm::MCInst &mc_inst)
const;
70 bool IsCall(llvm::MCInst &mc_inst)
const;
71 bool IsLoad(llvm::MCInst &mc_inst)
const;
72 bool IsAuthenticated(llvm::MCInst &mc_inst)
const;
76 std::unique_ptr<llvm::MCRegisterInfo> &®_info_up,
77 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
78 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
79 std::unique_ptr<llvm::MCContext> &&context_up,
80 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
81 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up,
82 std::unique_ptr<llvm::MCInstrAnalysis> &&instr_analysis_up);
132 uint8_t opcode_len = opcode_and_modrm.
opcode_len;
133 uint8_t modrm = opcode_and_modrm.
modrm;
138 if (opcode >= 0x70 && opcode <= 0x7F) {
145 if (opcode >= 0x80 && opcode <= 0x8F) {
158 if (opcode_len == 1) {
159 uint8_t modrm_reg = (modrm >> 3) & 7;
162 else if (modrm_reg == 3)
164 else if (modrm_reg == 4)
166 else if (modrm_reg == 5)
222 if (opcode_len == 2) {
262std::optional<InstructionOpcodeAndModrm>
264 bool is_exec_mode_64b) {
266 bool prefix_done =
false;
273 while (!prefix_done) {
274 if (op_idx >= bytes_len)
313 if (is_exec_mode_64b)
321 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
328 ret.
modrm = inst_bytes[op_idx + 3];
332 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
336 ret.
opcode_len = inst_bytes[op_idx + 1] & 0x1f;
338 ret.
modrm = inst_bytes[op_idx + 4];
343 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
347 ret.
opcode_len = inst_bytes[op_idx + 1] & 0x03;
349 ret.
modrm = inst_bytes[op_idx + 5];
359 ret.
modrm = inst_bytes[op_idx + 1];
369 ret.
modrm = inst_bytes[op_idx + 1];
373 ret.
modrm = inst_bytes[op_idx + 1];
377 ret.
modrm = inst_bytes[op_idx + 1];
381 ret.
modrm = inst_bytes[op_idx + 1];
384 ret.
modrm = inst_bytes[op_idx + 1];
393 std::optional<InstructionOpcodeAndModrm> ret;
419 disasm.shared_from_this())) {}
456 const ArchSpec &arch = disasm->GetArchitecture();
461 if (min_op_byte_size == max_op_byte_size) {
466 switch (min_op_byte_size) {
495 bool is_alternate_isa =
false;
499 const llvm::Triple::ArchType machine = arch.
GetMachine();
500 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb) {
501 if (machine == llvm::Triple::thumb || is_alternate_isa) {
502 uint32_t thumb_opcode = data.
GetU16(&data_offset);
503 if ((thumb_opcode & 0xe000) != 0xe000 ||
504 ((thumb_opcode & 0x1800u) == 0)) {
509 thumb_opcode |= data.
GetU16(&data_offset);
520 uint8_t *opcode_data =
521 const_cast<uint8_t *
>(data.
PeekData(data_offset, 1));
522 const size_t opcode_data_len = data.
BytesLeft(data_offset);
526 const size_t inst_size =
527 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
554 if (disasm->GetArchitecture().GetMachine() == llvm::Triple::x86)
556 else if (disasm->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
569 std::string out_string;
570 std::string markup_out_string;
571 std::string comment_string;
572 std::string markup_comment_string;
578 if (address_class == AddressClass::eCodeAlternateISA)
579 mc_disasm_ptr = disasm->m_alternate_disasm_up.get();
581 mc_disasm_ptr = disasm->m_disasm_up.get();
586 const bool data_from_file = disasm->m_data_from_file;
587 bool use_hex_immediates =
true;
596 if (!data_from_file) {
610 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
613 mc_disasm_ptr->
SetStyle(use_hex_immediates, hex_style);
615 const bool saved_use_color = mc_disasm_ptr->
GetUseColor();
617 mc_disasm_ptr->
PrintMCInst(inst,
pc, out_string, comment_string);
620 markup_comment_string);
623 if (!comment_string.empty()) {
628 if (inst_size == 0) {
636 const uint8_t uval8 = data.
GetU8(&offset);
639 mnemonic_strm.
Printf(
"0x%2.2x", uval8);
642 const uint16_t uval16 = data.
GetU16(&offset);
645 mnemonic_strm.
Printf(
"0x%4.4x", uval16);
648 const uint32_t uval32 = data.
GetU32(&offset);
651 mnemonic_strm.
Printf(
"0x%8.8x", uval32);
654 const uint64_t uval64 = data.
GetU64(&offset);
657 mnemonic_strm.
Printf(
"0x%16.16" PRIx64, uval64);
663 const uint8_t *bytes = data.
PeekData(offset, inst_size);
664 if (bytes ==
nullptr)
668 mnemonic_strm.
Printf(
"0x%2.2x", bytes[0]);
669 for (uint32_t i = 1; i < inst_size; ++i)
670 mnemonic_strm.
Printf(
" 0x%2.2x", bytes[i]);
679 llvm::StringRef(
"[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?"));
681 llvm::SmallVector<llvm::StringRef, 4> matches;
682 if (s_regex.
Execute(out_string, &matches)) {
687 if (s_regex.
Execute(markup_out_string, &matches)) {
717 operator bool()
const {
return static_cast<bool>(
m_disasm); }
722 static llvm::StringRef::const_iterator
724 llvm::StringRef::const_iterator ose) {
739 static std::pair<bool, llvm::StringRef::const_iterator>
741 llvm::StringRef::const_iterator ose) {
745 if (osi != ose && *osi == c) {
750 return std::make_pair(found, osi);
753 static std::pair<Operand, llvm::StringRef::const_iterator>
755 llvm::StringRef::const_iterator ose) {
757 ret.
m_type = Operand::Type::Register;
763 if (*osi >=
'0' && *osi <=
'9') {
765 return std::make_pair(
Operand(), osi);
769 }
else if (*osi >=
'a' && *osi <=
'z') {
775 return std::make_pair(
Operand(), osi);
778 return std::make_pair(ret, osi);
782 return std::make_pair(
Operand(), osi);
791 return std::make_pair(ret, osi);
794 static std::pair<Operand, llvm::StringRef::const_iterator>
796 llvm::StringRef::const_iterator ose) {
798 ret.
m_type = Operand::Type::Immediate;
805 if (*osi >=
'0' && *osi <=
'9') {
807 }
else if (*osi >=
'a' && *osi <=
'f') {
811 return std::make_pair(
Operand(), osi);
817 return std::make_pair(
Operand(), osi);
819 ret.
m_immediate = strtoull(str.c_str(),
nullptr, 0);
820 return std::make_pair(ret, osi);
823 if (!str.compare(
"0")) {
827 return std::make_pair(
Operand(), osi);
833 return std::make_pair(
Operand(), osi);
840 return std::make_pair(
Operand(), osi);
847 ret.
m_immediate = strtoull(str.c_str(),
nullptr, 0);
848 return std::make_pair(ret, osi);
852 static std::pair<Operand, llvm::StringRef::const_iterator>
854 llvm::StringRef::const_iterator ose) {
855 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
857 if (offset_and_iterator.first.IsValid()) {
858 osi = offset_and_iterator.second;
864 return std::make_pair(
Operand(), osi);
867 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
869 if (base_and_iterator.first.IsValid()) {
870 osi = base_and_iterator.second;
872 return std::make_pair(
Operand(), osi);
877 return std::make_pair(
Operand(), osi);
880 std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator =
882 if (index_and_iterator.first.IsValid()) {
883 osi = index_and_iterator.second;
885 return std::make_pair(
Operand(), osi);
890 return std::make_pair(
Operand(), osi);
893 std::pair<Operand, llvm::StringRef::const_iterator>
895 if (index_and_iterator.first.IsValid()) {
896 osi = index_and_iterator.second;
898 return std::make_pair(
Operand(), osi);
903 return std::make_pair(
Operand(), osi);
907 product.
m_type = Operand::Type::Product;
908 product.
m_children.push_back(index_and_iterator.first);
909 product.
m_children.push_back(multiplier_and_iterator.first);
912 index.
m_type = Operand::Type::Sum;
913 index.
m_children.push_back(base_and_iterator.first);
916 if (offset_and_iterator.first.IsValid()) {
918 offset.
m_type = Operand::Type::Sum;
919 offset.
m_children.push_back(offset_and_iterator.first);
923 deref.
m_type = Operand::Type::Dereference;
925 return std::make_pair(deref, osi);
928 deref.
m_type = Operand::Type::Dereference;
930 return std::make_pair(deref, osi);
935 static std::pair<Operand, llvm::StringRef::const_iterator>
937 llvm::StringRef::const_iterator ose) {
938 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
940 if (offset_and_iterator.first.IsValid()) {
941 osi = offset_and_iterator.second;
947 return std::make_pair(
Operand(), osi);
950 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
952 if (base_and_iterator.first.IsValid()) {
953 osi = base_and_iterator.second;
955 return std::make_pair(
Operand(), osi);
960 return std::make_pair(
Operand(), osi);
963 if (offset_and_iterator.first.IsValid()) {
965 offset.
m_type = Operand::Type::Sum;
966 offset.
m_children.push_back(offset_and_iterator.first);
967 offset.
m_children.push_back(base_and_iterator.first);
970 deref.
m_type = Operand::Type::Dereference;
972 return std::make_pair(deref, osi);
975 deref.
m_type = Operand::Type::Dereference;
976 deref.
m_children.push_back(base_and_iterator.first);
977 return std::make_pair(deref, osi);
982 static std::pair<Operand, llvm::StringRef::const_iterator>
984 llvm::StringRef::const_iterator ose) {
988 return std::make_pair(
Operand(), osi);
991 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
993 if (base_and_iterator.first.IsValid()) {
994 osi = base_and_iterator.second;
996 return std::make_pair(
Operand(), osi);
1001 return std::make_pair(
Operand(), osi);
1004 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
1006 if (offset_and_iterator.first.IsValid()) {
1007 osi = offset_and_iterator.second;
1010 std::tie(found, osi) =
ConsumeChar(osi,
']', ose);
1012 return std::make_pair(
Operand(), osi);
1016 offset.
m_type = Operand::Type::Sum;
1017 offset.
m_children.push_back(offset_and_iterator.first);
1018 offset.
m_children.push_back(base_and_iterator.first);
1021 deref.
m_type = Operand::Type::Dereference;
1023 return std::make_pair(deref, osi);
1027 static std::pair<Operand, llvm::StringRef::const_iterator>
1029 llvm::StringRef::const_iterator ose) {
1031 std::tie(found, osi) =
ConsumeChar(osi,
'[', ose);
1033 return std::make_pair(
Operand(), osi);
1036 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
1038 if (base_and_iterator.first.IsValid()) {
1039 osi = base_and_iterator.second;
1041 return std::make_pair(
Operand(), osi);
1044 std::tie(found, osi) =
ConsumeChar(osi,
']', ose);
1046 return std::make_pair(
Operand(), osi);
1050 deref.
m_type = Operand::Type::Dereference;
1051 deref.
m_children.push_back(base_and_iterator.first);
1052 return std::make_pair(deref, osi);
1057 case Operand::Type::Dereference:
1061 case Operand::Type::Immediate:
1067 case Operand::Type::Invalid:
1070 case Operand::Type::Product:
1077 case Operand::Type::Register:
1080 case Operand::Type::Sum:
1092 const char *operands_string =
GetOperands(
nullptr);
1094 if (!operands_string) {
1098 llvm::StringRef operands_ref(operands_string);
1100 llvm::StringRef::const_iterator osi = operands_ref.begin();
1101 llvm::StringRef::const_iterator ose = operands_ref.end();
1103 while (osi != ose) {
1105 llvm::StringRef::const_iterator iter;
1120 operands.push_back(operand);
1125 std::pair<bool, llvm::StringRef::const_iterator> found_and_iter =
1127 if (found_and_iter.first) {
1128 osi = found_and_iter.second;
1136 if (disasm_sp && operands.size() > 1) {
1138 switch (disasm_sp->GetArchitecture().GetMachine()) {
1141 case llvm::Triple::x86:
1142 case llvm::Triple::x86_64:
1143 operands[operands.size() - 1].m_clobbered =
true;
1145 case llvm::Triple::arm:
1146 operands[0].m_clobbered =
true;
1151 if (
Log *log =
GetLog(LLDBLog::Process)) {
1154 ss.
Printf(
"[%s] expands to %zu operands:\n", operands_string,
1156 for (
const Operand &operand : operands) {
1204 bool is_alternate_isa;
1209 const size_t opcode_data_len = data.
GetByteSize();
1211 const size_t inst_size =
1212 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
1227 is_alternate_isa =
false;
1229 if (disasm->m_alternate_disasm_up) {
1232 if (address_class == AddressClass::eCodeAlternateISA) {
1233 is_alternate_isa =
true;
1234 return disasm->m_alternate_disasm_up.get();
1237 return disasm->m_disasm_up.get();
1243std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>
1245 const char *features_str,
1248 using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>;
1251 const llvm::Target *curr_target =
1252 llvm::TargetRegistry::lookupTarget(triple,
Status);
1256 std::unique_ptr<llvm::MCInstrInfo> instr_info_up(
1257 curr_target->createMCInstrInfo());
1261 std::unique_ptr<llvm::MCRegisterInfo> reg_info_up(
1262 curr_target->createMCRegInfo(triple));
1266 std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info_up(
1267 curr_target->createMCSubtargetInfo(triple, cpu, features_str));
1268 if (!subtarget_info_up)
1271 llvm::MCTargetOptions MCOptions;
1272 std::unique_ptr<llvm::MCAsmInfo> asm_info_up(
1273 curr_target->createMCAsmInfo(*reg_info_up, triple, MCOptions));
1277 std::unique_ptr<llvm::MCContext> context_up(
1278 new llvm::MCContext(llvm::Triple(triple), asm_info_up.get(),
1279 reg_info_up.get(), subtarget_info_up.get()));
1283 std::unique_ptr<llvm::MCDisassembler> disasm_up(
1284 curr_target->createMCDisassembler(*subtarget_info_up, *context_up));
1288 std::unique_ptr<llvm::MCRelocationInfo> rel_info_up(
1289 curr_target->createMCRelocationInfo(triple, *context_up));
1293 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(
1294 curr_target->createMCSymbolizer(
1296 context_up.get(), std::move(rel_info_up)));
1297 disasm_up->setSymbolizer(std::move(symbolizer_up));
1299 unsigned asm_printer_variant =
1300 flavor == ~0U ? asm_info_up->getAssemblerDialect() : flavor;
1302 std::unique_ptr<llvm::MCInstPrinter> instr_printer_up(
1303 curr_target->createMCInstPrinter(llvm::Triple{triple},
1304 asm_printer_variant, *asm_info_up,
1305 *instr_info_up, *reg_info_up));
1306 if (!instr_printer_up)
1309 instr_printer_up->setPrintBranchImmAsAddress(
true);
1312 std::unique_ptr<llvm::MCInstrAnalysis> instr_analysis_up(
1313 curr_target->createMCInstrAnalysis(instr_info_up.get()));
1316 std::move(instr_info_up), std::move(reg_info_up),
1317 std::move(subtarget_info_up), std::move(asm_info_up),
1318 std::move(context_up), std::move(disasm_up), std::move(instr_printer_up),
1319 std::move(instr_analysis_up)));
1323 std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
1324 std::unique_ptr<llvm::MCRegisterInfo> &®_info_up,
1325 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
1326 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
1327 std::unique_ptr<llvm::MCContext> &&context_up,
1328 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
1329 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up,
1330 std::unique_ptr<llvm::MCInstrAnalysis> &&instr_analysis_up)
1331 : m_instr_info_up(std::move(instr_info_up)),
1332 m_reg_info_up(std::move(reg_info_up)),
1333 m_subtarget_info_up(std::move(subtarget_info_up)),
1334 m_asm_info_up(std::move(asm_info_up)),
1335 m_context_up(std::move(context_up)),
m_disasm_up(std::move(disasm_up)),
1336 m_instr_printer_up(std::move(instr_printer_up)),
1337 m_instr_analysis_up(std::move(instr_analysis_up)) {
1343 const uint8_t *opcode_data,
size_t opcode_data_len,
lldb::addr_t pc,
1344 llvm::MCInst &mc_inst)
const {
1345 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
1346 llvm::MCDisassembler::DecodeStatus status;
1348 uint64_t new_inst_size;
1349 status =
m_disasm_up->getInstruction(mc_inst, new_inst_size, data,
pc,
1351 if (status == llvm::MCDisassembler::Success)
1352 return new_inst_size;
1358 llvm::MCInst &mc_inst,
lldb::addr_t pc, std::string &inst_string,
1359 std::string &comments_string) {
1360 llvm::raw_string_ostream inst_stream(inst_string);
1361 llvm::raw_string_ostream comments_stream(comments_string);
1363 inst_stream.enable_colors(m_instr_printer_up->getUseColor());
1364 m_instr_printer_up->setCommentStream(comments_stream);
1365 m_instr_printer_up->printInst(&mc_inst,
pc, llvm::StringRef(),
1366 *m_subtarget_info_up, inst_stream);
1367 m_instr_printer_up->setCommentStream(llvm::nulls());
1369 comments_stream.flush();
1371 static std::string g_newlines(
"\r\n");
1373 for (
size_t newline_pos = 0;
1374 (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) !=
1375 comments_string.npos;
1377 comments_string.replace(comments_string.begin() + newline_pos,
1378 comments_string.begin() + newline_pos + 1, 1,
' ');
1384 m_instr_printer_up->setPrintImmHex(use_hex_immed);
1385 switch (hex_style) {
1387 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::C);
1390 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::Asm);
1396 m_instr_printer_up->setUseColor(use_color);
1400 return m_instr_printer_up->getUseColor();
1404 llvm::MCInst &mc_inst)
const {
1405 if (m_instr_analysis_up)
1406 return m_instr_analysis_up->mayAffectControlFlow(mc_inst, *m_reg_info_up);
1407 return m_instr_info_up->get(mc_inst.getOpcode())
1408 .mayAffectControlFlow(mc_inst, *m_reg_info_up);
1412 llvm::MCInst &mc_inst)
const {
1413 return m_instr_info_up->get(mc_inst.getOpcode()).hasDelaySlot();
1417 if (m_instr_analysis_up)
1418 return m_instr_analysis_up->isCall(mc_inst);
1419 return m_instr_info_up->get(mc_inst.getOpcode()).isCall();
1423 return m_instr_info_up->get(mc_inst.getOpcode()).mayLoad();
1427 llvm::MCInst &mc_inst)
const {
1428 const auto &InstrDesc = m_instr_info_up->get(mc_inst.getOpcode());
1433 bool IsBrkC47x =
false;
1434 if (InstrDesc.isTrap() && mc_inst.getNumOperands() == 1) {
1435 const llvm::MCOperand &Op0 = mc_inst.getOperand(0);
1436 if (Op0.isImm() && Op0.getImm() >= 0xc470 && Op0.getImm() <= 0xc474)
1440 return InstrDesc.isAuthenticated() || IsBrkC47x;
1444 const char *flavor_string)
1452 unsigned flavor = ~0U;
1457 if (triple.getArch() == llvm::Triple::x86 ||
1458 triple.getArch() == llvm::Triple::x86_64) {
1467 if (triple.getArch() == llvm::Triple::arm) {
1468 std::string thumb_arch_name(thumb_arch.
GetTriple().getArchName().str());
1470 if (thumb_arch_name.size() > 3) {
1471 thumb_arch_name.erase(0, 3);
1472 thumb_arch_name.insert(0,
"thumb");
1474 thumb_arch_name =
"thumbv9.3a";
1476 thumb_arch.
GetTriple().setArchName(llvm::StringRef(thumb_arch_name));
1484 if (triple.getArch() == llvm::Triple::arm &&
1485 triple.getSubArch() == llvm::Triple::NoSubArch)
1486 triple.setArchName(
"armv9.3a");
1488 std::string features_str;
1489 const char *triple_str = triple.getTriple().c_str();
1493 triple_str = thumb_arch.
GetTriple().getTriple().c_str();
1494 features_str +=
"+fp-armv8,";
1497 const char *cpu =
"";
1546 uint32_t arch_flags = arch.
GetFlags();
1548 features_str +=
"+msa,";
1550 features_str +=
"+dsp,";
1552 features_str +=
"+dspr2,";
1556 if (triple.isAArch64()) {
1557 features_str +=
"+all,";
1559 if (triple.getVendor() == llvm::Triple::Apple)
1560 cpu =
"apple-latest";
1563 if (triple.isRISCV()) {
1564 uint32_t arch_flags = arch.
GetFlags();
1566 features_str +=
"+c,";
1568 features_str +=
"+e,";
1571 features_str +=
"+f,";
1574 features_str +=
"+f,+d,";
1577 features_str +=
"+f,+d,+q,";
1580 features_str +=
"+a,+m,";
1589 llvm::Triple::ArchType llvm_arch = triple.getArch();
1593 if (llvm_arch == llvm::Triple::arm) {
1594 std::string thumb_triple(thumb_arch.
GetTriple().getTriple());
1601 }
else if (arch.
IsMIPS()) {
1603 uint32_t arch_flags = arch.
GetFlags();
1605 features_str +=
"+mips16,";
1607 features_str +=
"+micromips,";
1610 triple_str, cpu, features_str.c_str(), flavor, *
this);
1619 const char *flavor) {
1620 if (arch.
GetTriple().getArch() != llvm::Triple::UnknownArch) {
1621 auto disasm_sp = std::make_shared<DisassemblerLLVMC>(arch, flavor);
1622 if (disasm_sp && disasm_sp->IsValid())
1631 size_t num_instructions,
1632 bool append,
bool data_from_file) {
1640 uint32_t data_cursor = data_offset;
1642 uint32_t instructions_parsed = 0;
1645 while (data_cursor < data_byte_size &&
1646 instructions_parsed < num_instructions) {
1659 uint32_t inst_size = inst_sp->Decode(*
this, data, data_cursor);
1665 data_cursor += inst_size;
1666 inst_addr.
Slide(inst_size);
1667 instructions_parsed++;
1670 return data_cursor - data_offset;
1675 "Disassembler that uses LLVM MC to disassemble "
1676 "i386, x86_64, ARM, and ARM64.",
1679 llvm::InitializeAllTargetInfos();
1680 llvm::InitializeAllTargetMCs();
1681 llvm::InitializeAllAsmParsers();
1682 llvm::InitializeAllDisassemblers();
1690 uint64_t offset, uint64_t size,
1691 int tag_type,
void *tag_bug) {
1693 ->
OpInfo(
pc, offset, size, tag_type, tag_bug);
1698 uint64_t *type, uint64_t
pc,
1699 const char **name) {
1707 if (flavor ==
nullptr || strcmp(flavor,
"default") == 0)
1710 if (triple.getArch() == llvm::Triple::x86 ||
1711 triple.getArch() == llvm::Triple::x86_64) {
1712 return strcmp(flavor,
"intel") == 0 || strcmp(flavor,
"att") == 0;
1720 int tag_type,
void *tag_bug) {
1725 memset(tag_bug, 0,
sizeof(::LLVMOpInfo1));
1732 uint64_t
pc,
const char **name) {
1742 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
1746 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
1753 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
1756 uint32_t addxri_inst;
1757 uint64_t adrp_imm, addxri_imm;
1763 if (adrp_imm & (1ULL << 20))
1764 adrp_imm |= ~((1ULL << 21) - 1);
1766 addxri_inst = value;
1767 addxri_imm = (addxri_inst >> 10) & 0xfff;
1770 if ((addxri_inst >> (12 + 5 + 5)) & 1)
1772 value = (
m_adrp_address & 0xfffffffffffff000LL) + (adrp_imm << 12) +
1782 module_sp->ResolveFileAddress(value, value_so_addr);
1783 module_sp->ResolveFileAddress(
pc, pc_so_addr);
1791 const SymbolContextItem resolve_scope =
1792 eSymbolContextFunction | eSymbolContextSymbol;
1794 pc_so_addr.
GetModule()->ResolveSymbolContextForAddress(
1795 pc_so_addr, resolve_scope, sym_ctx);
1801 bool format_omitting_current_func_name =
false;
1807 format_omitting_current_func_name =
true;
1815 if (format_omitting_current_func_name) {
1829 std::string str = std::string(ss.
GetString());
1830 size_t first_eol_char = str.find_first_of(
"\r\n");
1831 if (first_eol_char != std::string::npos) {
1832 str.erase(first_eol_char);
1846 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
#define LLDB_PLUGIN_DEFINE(PluginName)
bool HasDelaySlot(llvm::MCInst &mc_inst) const
~MCDisasmInstance()=default
bool IsAuthenticated(llvm::MCInst &mc_inst) const
std::unique_ptr< llvm::MCInstrInfo > m_instr_info_up
std::unique_ptr< llvm::MCRegisterInfo > m_reg_info_up
bool CanBranch(llvm::MCInst &mc_inst) const
std::unique_ptr< llvm::MCContext > m_context_up
std::unique_ptr< llvm::MCAsmInfo > m_asm_info_up
void PrintMCInst(llvm::MCInst &mc_inst, lldb::addr_t pc, std::string &inst_string, std::string &comments_string)
uint64_t GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, llvm::MCInst &mc_inst) const
void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style)
static std::unique_ptr< MCDisasmInstance > Create(const char *triple, const char *cpu, const char *features_str, unsigned flavor, DisassemblerLLVMC &owner)
bool IsLoad(llvm::MCInst &mc_inst) const
bool IsCall(llvm::MCInst &mc_inst) const
std::unique_ptr< llvm::MCSubtargetInfo > m_subtarget_info_up
std::unique_ptr< llvm::MCInstrAnalysis > m_instr_analysis_up
std::unique_ptr< llvm::MCDisassembler > m_disasm_up
std::unique_ptr< llvm::MCInstPrinter > m_instr_printer_up
MCDisasmInstance(std::unique_ptr< llvm::MCInstrInfo > &&instr_info_up, std::unique_ptr< llvm::MCRegisterInfo > &®_info_up, std::unique_ptr< llvm::MCSubtargetInfo > &&subtarget_info_up, std::unique_ptr< llvm::MCAsmInfo > &&asm_info_up, std::unique_ptr< llvm::MCContext > &&context_up, std::unique_ptr< llvm::MCDisassembler > &&disasm_up, std::unique_ptr< llvm::MCInstPrinter > &&instr_printer_up, std::unique_ptr< llvm::MCInstrAnalysis > &&instr_analysis_up)
void SetUseColor(bool use_color)
std::optional< uint32_t > m_adrp_insn
static const char * SymbolLookupCallback(void *DisInfo, uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
int OpInfo(uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBug)
DisassemblerLLVMC(const lldb_private::ArchSpec &arch, const char *flavor)
const lldb_private::ExecutionContext * m_exe_ctx
const char * SymbolLookup(uint64_t ReferenceValue, uint64_t *ReferenceType, uint64_t ReferencePC, const char **ReferenceName)
std::unique_ptr< MCDisasmInstance > m_disasm_up
static llvm::StringRef GetPluginNameStatic()
friend class InstructionLLVMC
static int OpInfoCallback(void *DisInfo, uint64_t PC, uint64_t Offset, uint64_t Size, int TagType, void *TagBug)
static lldb::DisassemblerSP CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor)
lldb::addr_t m_adrp_address
bool FlavorValidForArchSpec(const lldb_private::ArchSpec &arch, const char *flavor) override
~DisassemblerLLVMC() override
std::unique_ptr< MCDisasmInstance > m_alternate_disasm_up
InstructionLLVMC * m_inst
size_t DecodeInstructions(const lldb_private::Address &base_addr, const lldb_private::DataExtractor &data, lldb::offset_t data_offset, size_t num_instructions, bool append, bool data_from_file) override
Grants exclusive access to the disassembler and initializes it with the given InstructionLLVMC and an...
std::shared_ptr< DisassemblerLLVMC > m_disasm
DisassemblerScope(InstructionLLVMC &i, const lldb_private::ExecutionContext *exe_ctx=nullptr)
std::shared_ptr< DisassemblerLLVMC > operator->()
static std::pair< Operand, llvm::StringRef::const_iterator > ParseIntelIndexedAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
bool DoesBranch() override
DisassemblerLLVMC::MCDisasmInstance * GetDisasmToUse(bool &is_alternate_isa)
static void DumpOperand(const Operand &op, Stream &s)
size_t GetByteSize() const
static llvm::StringRef::const_iterator ConsumeWhitespace(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
std::weak_ptr< DisassemblerLLVMC > m_disasm_wp
void CalculateMnemonicOperandsAndComment(const lldb_private::ExecutionContext *exe_ctx) override
bool m_has_visited_instruction
static std::pair< Operand, llvm::StringRef::const_iterator > ParseARMOffsetAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
DisassemblerLLVMC::MCDisasmInstance * GetDisasmToUse(bool &is_alternate_isa, DisassemblerScope &disasm)
void AppendComment(std::string &description)
bool UsingFileAddress() const
bool IsAuthenticated() override
static std::pair< Operand, llvm::StringRef::const_iterator > ParseIntelDerefAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
static std::pair< bool, llvm::StringRef::const_iterator > ConsumeChar(llvm::StringRef::const_iterator osi, const char c, llvm::StringRef::const_iterator ose)
size_t Decode(const lldb_private::Disassembler &disassembler, const lldb_private::DataExtractor &data, lldb::offset_t data_offset) override
bool ParseOperands(llvm::SmallVectorImpl< Instruction::Operand > &operands) override
bool HasDelaySlot() override
static std::pair< Operand, llvm::StringRef::const_iterator > ParseARMDerefAccess(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
static std::pair< Operand, llvm::StringRef::const_iterator > ParseRegisterName(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
~InstructionLLVMC() override=default
lldb::InstructionControlFlowKind GetControlFlowKind(const lldb_private::ExecutionContext *exe_ctx) override
InstructionLLVMC(DisassemblerLLVMC &disasm, const lldb_private::Address &address, AddressClass addr_class)
static std::pair< Operand, llvm::StringRef::const_iterator > ParseImmediate(llvm::StringRef::const_iterator osi, llvm::StringRef::const_iterator ose)
A section + offset based address range class.
Address & GetBaseAddress()
Get accessor for the base address of the range.
bool ContainsLoadAddress(const Address &so_addr, Target *target) const
Check if a section offset so_addr when represented as a load address is contained within this object'...
A section + offset based address class.
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
lldb::SectionSP GetSection() const
Get const accessor for the section.
@ DumpStyleSectionNameOffset
Display as the section name + offset.
@ DumpStyleNoFunctionName
Elide the function name; display an offset into the current function.
@ DumpStyleResolvedDescriptionNoFunctionArguments
bool Slide(int64_t offset)
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX, bool all_ranges=false, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Dump a description of this object to a Stream.
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
lldb::addr_t GetFileAddress() const
Get the file address.
bool IsValid() const
Check if the object state is valid.
AddressClass GetAddressClass() const
An architecture specification class.
llvm::Triple & GetTriple()
Architecture triple accessor.
bool IsAlwaysThumbInstructions() const
Detect whether this architecture uses thumb code exclusively.
bool IsMIPS() const
if MIPS architecture return true.
uint32_t GetMinimumOpcodeByteSize() const
uint32_t GetFlags() const
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
@ eRISCV_float_abi_double
single precision floating point, +f
@ eRISCV_float_abi_quad
double precision floating point, +d
@ eRISCV_float_abi_single
soft float
uint32_t GetMaximumOpcodeByteSize() const
A uniqued constant string class.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
InstructionList m_instruction_list
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
void Append(lldb::InstructionSP &inst_sp)
std::string m_markup_mnemonics
const Address & GetAddress() const
const char * GetOperands(const ExecutionContext *exe_ctx, bool markup=false)
std::string m_opcode_name
std::string m_markup_opcode_name
AddressClass GetAddressClass()
void SetOpcode16(uint16_t inst, lldb::ByteOrder order)
void SetOpcodeBytes(const void *bytes, size_t length)
void SetOpcode16_2(uint32_t inst, lldb::ByteOrder order)
uint32_t GetByteSize() const
void SetOpcode64(uint64_t inst, lldb::ByteOrder order)
const void * GetOpcodeBytes() const
void SetOpcode8(uint8_t inst, lldb::ByteOrder order)
uint32_t GetData(DataExtractor &data) const
void SetOpcode32(uint32_t inst, lldb::ByteOrder order)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, bool allow_section_end=false) const
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Symbol * symbol
The Symbol for a given query.
bool GetUseHexImmediates() const
Disassembler::HexImmediateStyle GetHexImmediateStyle() const
SectionLoadList & GetSectionLoadList()
const ArchSpec & GetArchitecture() const
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Instruction > InstructionSP
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
ByteOrder
Byte ordering definitions.
InstructionControlFlowKind
Architecture-agnostic categorization of instructions for traversing the control flow of a trace.
@ eInstructionControlFlowKindReturn
The instruction is a near (function) return.
@ eInstructionControlFlowKindFarJump
The instruction is a jump-like far transfer.
@ eInstructionControlFlowKindOther
The instruction is something not listed below, i.e.
@ eInstructionControlFlowKindFarCall
The instruction is a call-like far transfer.
@ eInstructionControlFlowKindFarReturn
The instruction is a return-like far transfer.
@ eInstructionControlFlowKindUnknown
The instruction could not be classified.
@ eInstructionControlFlowKindJump
The instruction is a near unconditional jump.
@ eInstructionControlFlowKindCall
The instruction is a near (function) call.
@ eInstructionControlFlowKindCondJump
The instruction is a near conditional jump.
std::shared_ptr< lldb_private::Module > ModuleSP
std::optional< InstructionOpcodeAndModrm > InstructionLengthDecode(const uint8_t *inst_bytes, int bytes_len, bool is_exec_mode_64b)
Decode an instruction into opcode, modrm and opcode_len.
lldb::InstructionControlFlowKind GetControlFlowKind(bool is_exec_mode_64b, Opcode m_opcode)
lldb::InstructionControlFlowKind MapOpcodeIntoControlFlowKind(InstructionOpcodeAndModrm opcode_and_modrm)
Determine the InstructionControlFlowKind based on opcode and modrm bytes.
enum lldb_private::Instruction::Operand::Type m_type
std::vector< Operand > m_children
These are the three values deciding instruction control flow kind.