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 bool use_hex_immediates =
true;
607 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
610 mc_disasm_ptr->
SetStyle(use_hex_immediates, hex_style);
612 const bool saved_use_color = mc_disasm_ptr->
GetUseColor();
614 mc_disasm_ptr->
PrintMCInst(inst,
pc, out_string, comment_string);
617 markup_comment_string);
620 if (!comment_string.empty()) {
625 if (inst_size == 0) {
633 const uint8_t uval8 = data.
GetU8(&offset);
636 mnemonic_strm.
Printf(
"0x%2.2x", uval8);
639 const uint16_t uval16 = data.
GetU16(&offset);
642 mnemonic_strm.
Printf(
"0x%4.4x", uval16);
645 const uint32_t uval32 = data.
GetU32(&offset);
648 mnemonic_strm.
Printf(
"0x%8.8x", uval32);
651 const uint64_t uval64 = data.
GetU64(&offset);
654 mnemonic_strm.
Printf(
"0x%16.16" PRIx64, uval64);
660 const uint8_t *bytes = data.
PeekData(offset, inst_size);
661 if (bytes ==
nullptr)
665 mnemonic_strm.
Printf(
"0x%2.2x", bytes[0]);
666 for (uint32_t i = 1; i < inst_size; ++i)
667 mnemonic_strm.
Printf(
" 0x%2.2x", bytes[i]);
676 llvm::StringRef(
"[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?"));
678 llvm::SmallVector<llvm::StringRef, 4> matches;
679 if (s_regex.
Execute(out_string, &matches)) {
684 if (s_regex.
Execute(markup_out_string, &matches)) {
714 operator bool()
const {
return static_cast<bool>(
m_disasm); }
719 static llvm::StringRef::const_iterator
721 llvm::StringRef::const_iterator ose) {
736 static std::pair<bool, llvm::StringRef::const_iterator>
738 llvm::StringRef::const_iterator ose) {
742 if (osi != ose && *osi == c) {
747 return std::make_pair(found, osi);
750 static std::pair<Operand, llvm::StringRef::const_iterator>
752 llvm::StringRef::const_iterator ose) {
754 ret.
m_type = Operand::Type::Register;
760 if (*osi >=
'0' && *osi <=
'9') {
762 return std::make_pair(
Operand(), osi);
766 }
else if (*osi >=
'a' && *osi <=
'z') {
772 return std::make_pair(
Operand(), osi);
775 return std::make_pair(ret, osi);
779 return std::make_pair(
Operand(), osi);
788 return std::make_pair(ret, osi);
791 static std::pair<Operand, llvm::StringRef::const_iterator>
793 llvm::StringRef::const_iterator ose) {
795 ret.
m_type = Operand::Type::Immediate;
802 if (*osi >=
'0' && *osi <=
'9') {
804 }
else if (*osi >=
'a' && *osi <=
'f') {
808 return std::make_pair(
Operand(), osi);
814 return std::make_pair(
Operand(), osi);
816 ret.
m_immediate = strtoull(str.c_str(),
nullptr, 0);
817 return std::make_pair(ret, osi);
820 if (!str.compare(
"0")) {
824 return std::make_pair(
Operand(), osi);
830 return std::make_pair(
Operand(), osi);
837 return std::make_pair(
Operand(), osi);
844 ret.
m_immediate = strtoull(str.c_str(),
nullptr, 0);
845 return std::make_pair(ret, osi);
849 static std::pair<Operand, llvm::StringRef::const_iterator>
851 llvm::StringRef::const_iterator ose) {
852 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
854 if (offset_and_iterator.first.IsValid()) {
855 osi = offset_and_iterator.second;
861 return std::make_pair(
Operand(), osi);
864 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
866 if (base_and_iterator.first.IsValid()) {
867 osi = base_and_iterator.second;
869 return std::make_pair(
Operand(), osi);
874 return std::make_pair(
Operand(), osi);
877 std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator =
879 if (index_and_iterator.first.IsValid()) {
880 osi = index_and_iterator.second;
882 return std::make_pair(
Operand(), osi);
887 return std::make_pair(
Operand(), osi);
890 std::pair<Operand, llvm::StringRef::const_iterator>
892 if (index_and_iterator.first.IsValid()) {
893 osi = index_and_iterator.second;
895 return std::make_pair(
Operand(), osi);
900 return std::make_pair(
Operand(), osi);
904 product.
m_type = Operand::Type::Product;
905 product.
m_children.push_back(index_and_iterator.first);
906 product.
m_children.push_back(multiplier_and_iterator.first);
909 index.
m_type = Operand::Type::Sum;
910 index.
m_children.push_back(base_and_iterator.first);
913 if (offset_and_iterator.first.IsValid()) {
915 offset.
m_type = Operand::Type::Sum;
916 offset.
m_children.push_back(offset_and_iterator.first);
920 deref.
m_type = Operand::Type::Dereference;
922 return std::make_pair(deref, osi);
925 deref.
m_type = Operand::Type::Dereference;
927 return std::make_pair(deref, osi);
932 static std::pair<Operand, llvm::StringRef::const_iterator>
934 llvm::StringRef::const_iterator ose) {
935 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
937 if (offset_and_iterator.first.IsValid()) {
938 osi = offset_and_iterator.second;
944 return std::make_pair(
Operand(), osi);
947 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
949 if (base_and_iterator.first.IsValid()) {
950 osi = base_and_iterator.second;
952 return std::make_pair(
Operand(), osi);
957 return std::make_pair(
Operand(), osi);
960 if (offset_and_iterator.first.IsValid()) {
962 offset.
m_type = Operand::Type::Sum;
963 offset.
m_children.push_back(offset_and_iterator.first);
964 offset.
m_children.push_back(base_and_iterator.first);
967 deref.
m_type = Operand::Type::Dereference;
969 return std::make_pair(deref, osi);
972 deref.
m_type = Operand::Type::Dereference;
973 deref.
m_children.push_back(base_and_iterator.first);
974 return std::make_pair(deref, osi);
979 static std::pair<Operand, llvm::StringRef::const_iterator>
981 llvm::StringRef::const_iterator ose) {
985 return std::make_pair(
Operand(), osi);
988 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
990 if (base_and_iterator.first.IsValid()) {
991 osi = base_and_iterator.second;
993 return std::make_pair(
Operand(), osi);
998 return std::make_pair(
Operand(), osi);
1001 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
1003 if (offset_and_iterator.first.IsValid()) {
1004 osi = offset_and_iterator.second;
1007 std::tie(found, osi) =
ConsumeChar(osi,
']', ose);
1009 return std::make_pair(
Operand(), osi);
1013 offset.
m_type = Operand::Type::Sum;
1014 offset.
m_children.push_back(offset_and_iterator.first);
1015 offset.
m_children.push_back(base_and_iterator.first);
1018 deref.
m_type = Operand::Type::Dereference;
1020 return std::make_pair(deref, osi);
1024 static std::pair<Operand, llvm::StringRef::const_iterator>
1026 llvm::StringRef::const_iterator ose) {
1028 std::tie(found, osi) =
ConsumeChar(osi,
'[', ose);
1030 return std::make_pair(
Operand(), osi);
1033 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
1035 if (base_and_iterator.first.IsValid()) {
1036 osi = base_and_iterator.second;
1038 return std::make_pair(
Operand(), osi);
1041 std::tie(found, osi) =
ConsumeChar(osi,
']', ose);
1043 return std::make_pair(
Operand(), osi);
1047 deref.
m_type = Operand::Type::Dereference;
1048 deref.
m_children.push_back(base_and_iterator.first);
1049 return std::make_pair(deref, osi);
1054 case Operand::Type::Dereference:
1058 case Operand::Type::Immediate:
1064 case Operand::Type::Invalid:
1067 case Operand::Type::Product:
1074 case Operand::Type::Register:
1077 case Operand::Type::Sum:
1089 const char *operands_string =
GetOperands(
nullptr);
1091 if (!operands_string) {
1095 llvm::StringRef operands_ref(operands_string);
1097 llvm::StringRef::const_iterator osi = operands_ref.begin();
1098 llvm::StringRef::const_iterator ose = operands_ref.end();
1100 while (osi != ose) {
1102 llvm::StringRef::const_iterator iter;
1117 operands.push_back(operand);
1122 std::pair<bool, llvm::StringRef::const_iterator> found_and_iter =
1124 if (found_and_iter.first) {
1125 osi = found_and_iter.second;
1133 if (disasm_sp && operands.size() > 1) {
1135 switch (disasm_sp->GetArchitecture().GetMachine()) {
1138 case llvm::Triple::x86:
1139 case llvm::Triple::x86_64:
1140 operands[operands.size() - 1].m_clobbered =
true;
1142 case llvm::Triple::arm:
1143 operands[0].m_clobbered =
true;
1148 if (
Log *log =
GetLog(LLDBLog::Process)) {
1151 ss.
Printf(
"[%s] expands to %zu operands:\n", operands_string,
1153 for (
const Operand &operand : operands) {
1201 bool is_alternate_isa;
1206 const size_t opcode_data_len = data.
GetByteSize();
1208 const size_t inst_size =
1209 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
1224 is_alternate_isa =
false;
1226 if (disasm->m_alternate_disasm_up) {
1229 if (address_class == AddressClass::eCodeAlternateISA) {
1230 is_alternate_isa =
true;
1231 return disasm->m_alternate_disasm_up.get();
1234 return disasm->m_disasm_up.get();
1240std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>
1242 const char *features_str,
1245 using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>;
1248 const llvm::Target *curr_target =
1249 llvm::TargetRegistry::lookupTarget(triple,
Status);
1253 std::unique_ptr<llvm::MCInstrInfo> instr_info_up(
1254 curr_target->createMCInstrInfo());
1258 std::unique_ptr<llvm::MCRegisterInfo> reg_info_up(
1259 curr_target->createMCRegInfo(triple));
1263 std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info_up(
1264 curr_target->createMCSubtargetInfo(triple, cpu, features_str));
1265 if (!subtarget_info_up)
1268 llvm::MCTargetOptions MCOptions;
1269 std::unique_ptr<llvm::MCAsmInfo> asm_info_up(
1270 curr_target->createMCAsmInfo(*reg_info_up, triple, MCOptions));
1274 std::unique_ptr<llvm::MCContext> context_up(
1275 new llvm::MCContext(llvm::Triple(triple), asm_info_up.get(),
1276 reg_info_up.get(), subtarget_info_up.get()));
1280 std::unique_ptr<llvm::MCDisassembler> disasm_up(
1281 curr_target->createMCDisassembler(*subtarget_info_up, *context_up));
1285 std::unique_ptr<llvm::MCRelocationInfo> rel_info_up(
1286 curr_target->createMCRelocationInfo(triple, *context_up));
1290 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(
1291 curr_target->createMCSymbolizer(
1293 context_up.get(), std::move(rel_info_up)));
1294 disasm_up->setSymbolizer(std::move(symbolizer_up));
1296 unsigned asm_printer_variant =
1297 flavor == ~0U ? asm_info_up->getAssemblerDialect() : flavor;
1299 std::unique_ptr<llvm::MCInstPrinter> instr_printer_up(
1300 curr_target->createMCInstPrinter(llvm::Triple{triple},
1301 asm_printer_variant, *asm_info_up,
1302 *instr_info_up, *reg_info_up));
1303 if (!instr_printer_up)
1306 instr_printer_up->setPrintBranchImmAsAddress(
true);
1309 std::unique_ptr<llvm::MCInstrAnalysis> instr_analysis_up(
1310 curr_target->createMCInstrAnalysis(instr_info_up.get()));
1313 std::move(instr_info_up), std::move(reg_info_up),
1314 std::move(subtarget_info_up), std::move(asm_info_up),
1315 std::move(context_up), std::move(disasm_up), std::move(instr_printer_up),
1316 std::move(instr_analysis_up)));
1320 std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
1321 std::unique_ptr<llvm::MCRegisterInfo> &®_info_up,
1322 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
1323 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
1324 std::unique_ptr<llvm::MCContext> &&context_up,
1325 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
1326 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up,
1327 std::unique_ptr<llvm::MCInstrAnalysis> &&instr_analysis_up)
1328 : m_instr_info_up(std::move(instr_info_up)),
1329 m_reg_info_up(std::move(reg_info_up)),
1330 m_subtarget_info_up(std::move(subtarget_info_up)),
1331 m_asm_info_up(std::move(asm_info_up)),
1332 m_context_up(std::move(context_up)),
m_disasm_up(std::move(disasm_up)),
1333 m_instr_printer_up(std::move(instr_printer_up)),
1334 m_instr_analysis_up(std::move(instr_analysis_up)) {
1340 const uint8_t *opcode_data,
size_t opcode_data_len,
lldb::addr_t pc,
1341 llvm::MCInst &mc_inst)
const {
1342 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
1343 llvm::MCDisassembler::DecodeStatus status;
1345 uint64_t new_inst_size;
1346 status =
m_disasm_up->getInstruction(mc_inst, new_inst_size, data,
pc,
1348 if (status == llvm::MCDisassembler::Success)
1349 return new_inst_size;
1355 llvm::MCInst &mc_inst,
lldb::addr_t pc, std::string &inst_string,
1356 std::string &comments_string) {
1357 llvm::raw_string_ostream inst_stream(inst_string);
1358 llvm::raw_string_ostream comments_stream(comments_string);
1360 inst_stream.enable_colors(m_instr_printer_up->getUseColor());
1361 m_instr_printer_up->setCommentStream(comments_stream);
1362 m_instr_printer_up->printInst(&mc_inst,
pc, llvm::StringRef(),
1363 *m_subtarget_info_up, inst_stream);
1364 m_instr_printer_up->setCommentStream(llvm::nulls());
1366 static std::string g_newlines(
"\r\n");
1368 for (
size_t newline_pos = 0;
1369 (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) !=
1370 comments_string.npos;
1372 comments_string.replace(comments_string.begin() + newline_pos,
1373 comments_string.begin() + newline_pos + 1, 1,
' ');
1379 m_instr_printer_up->setPrintImmHex(use_hex_immed);
1380 switch (hex_style) {
1382 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::C);
1385 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::Asm);
1391 m_instr_printer_up->setUseColor(use_color);
1395 return m_instr_printer_up->getUseColor();
1399 llvm::MCInst &mc_inst)
const {
1400 if (m_instr_analysis_up)
1401 return m_instr_analysis_up->mayAffectControlFlow(mc_inst, *m_reg_info_up);
1402 return m_instr_info_up->get(mc_inst.getOpcode())
1403 .mayAffectControlFlow(mc_inst, *m_reg_info_up);
1407 llvm::MCInst &mc_inst)
const {
1408 return m_instr_info_up->get(mc_inst.getOpcode()).hasDelaySlot();
1412 if (m_instr_analysis_up)
1413 return m_instr_analysis_up->isCall(mc_inst);
1414 return m_instr_info_up->get(mc_inst.getOpcode()).isCall();
1418 return m_instr_info_up->get(mc_inst.getOpcode()).mayLoad();
1422 llvm::MCInst &mc_inst)
const {
1423 const auto &InstrDesc = m_instr_info_up->get(mc_inst.getOpcode());
1428 bool IsBrkC47x =
false;
1429 if (InstrDesc.isTrap() && mc_inst.getNumOperands() == 1) {
1430 const llvm::MCOperand &Op0 = mc_inst.getOperand(0);
1431 if (Op0.isImm() && Op0.getImm() >= 0xc470 && Op0.getImm() <= 0xc474)
1435 return InstrDesc.isAuthenticated() || IsBrkC47x;
1439 const char *flavor_string,
1440 const char *cpu_string,
1441 const char *features_string)
1449 const bool cpu_or_features_overriden = cpu_string || features_string;
1450 unsigned flavor = ~0U;
1455 if (triple.getArch() == llvm::Triple::x86 ||
1456 triple.getArch() == llvm::Triple::x86_64) {
1465 if (triple.getArch() == llvm::Triple::arm) {
1466 std::string thumb_arch_name(thumb_arch.
GetTriple().getArchName().str());
1468 if (thumb_arch_name.size() > 3) {
1469 thumb_arch_name.erase(0, 3);
1470 thumb_arch_name.insert(0,
"thumb");
1472 thumb_arch_name =
"thumbv9.3a";
1474 thumb_arch.
GetTriple().setArchName(llvm::StringRef(thumb_arch_name));
1482 if (triple.getArch() == llvm::Triple::arm &&
1483 triple.getSubArch() == llvm::Triple::NoSubArch)
1484 triple.setArchName(
"armv9.3a");
1486 std::string features_str =
1487 features_string ? std::string(features_string) :
"";
1488 const char *triple_str = triple.getTriple().c_str();
1492 triple_str = thumb_arch.
GetTriple().getTriple().c_str();
1493 if (!features_string)
1494 features_str +=
"+fp-armv8,";
1497 const char *cpu = cpu_string;
1499 if (!cpu_or_features_overriden) {
1547 if (arch.
IsMIPS() && !cpu_or_features_overriden) {
1548 uint32_t arch_flags = arch.
GetFlags();
1550 features_str +=
"+msa,";
1552 features_str +=
"+dsp,";
1554 features_str +=
"+dspr2,";
1559 if (triple.isAArch64() && !cpu_or_features_overriden) {
1560 features_str +=
"+all,";
1561 if (triple.getVendor() == llvm::Triple::Apple)
1562 cpu =
"apple-latest";
1565 if (triple.isRISCV() && !cpu_or_features_overriden) {
1566 uint32_t arch_flags = arch.
GetFlags();
1568 features_str +=
"+c,";
1570 features_str +=
"+e,";
1573 features_str +=
"+f,";
1576 features_str +=
"+f,+d,";
1579 features_str +=
"+f,+d,+q,";
1582 features_str +=
"+a,+m,";
1591 llvm::Triple::ArchType llvm_arch = triple.getArch();
1595 if (llvm_arch == llvm::Triple::arm) {
1596 std::string thumb_triple(thumb_arch.
GetTriple().getTriple());
1603 }
else if (arch.
IsMIPS()) {
1605 uint32_t arch_flags = arch.
GetFlags();
1607 features_str +=
"+mips16,";
1609 features_str +=
"+micromips,";
1612 triple_str, cpu, features_str.c_str(), flavor, *
this);
1623 const char *features) {
1624 if (arch.
GetTriple().getArch() != llvm::Triple::UnknownArch) {
1626 std::make_shared<DisassemblerLLVMC>(arch, flavor, cpu, features);
1627 if (disasm_sp && disasm_sp->IsValid())
1636 size_t num_instructions,
1637 bool append,
bool data_from_file) {
1645 uint32_t data_cursor = data_offset;
1647 uint32_t instructions_parsed = 0;
1650 while (data_cursor < data_byte_size &&
1651 instructions_parsed < num_instructions) {
1664 uint32_t inst_size = inst_sp->Decode(*
this, data, data_cursor);
1670 data_cursor += inst_size;
1671 inst_addr.
Slide(inst_size);
1672 instructions_parsed++;
1675 return data_cursor - data_offset;
1680 "Disassembler that uses LLVM MC to disassemble "
1681 "i386, x86_64, ARM, and ARM64.",
1684 llvm::InitializeAllTargetInfos();
1685 llvm::InitializeAllTargetMCs();
1686 llvm::InitializeAllAsmParsers();
1687 llvm::InitializeAllDisassemblers();
1695 uint64_t offset, uint64_t size,
1696 int tag_type,
void *tag_bug) {
1698 ->
OpInfo(
pc, offset, size, tag_type, tag_bug);
1703 uint64_t *type, uint64_t
pc,
1704 const char **name) {
1712 if (flavor ==
nullptr || strcmp(flavor,
"default") == 0)
1715 if (triple.getArch() == llvm::Triple::x86 ||
1716 triple.getArch() == llvm::Triple::x86_64) {
1717 return strcmp(flavor,
"intel") == 0 || strcmp(flavor,
"att") == 0;
1725 int tag_type,
void *tag_bug) {
1730 memset(tag_bug, 0,
sizeof(::LLVMOpInfo1));
1737 uint64_t
pc,
const char **name) {
1747 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
1751 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
1758 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
1761 uint32_t addxri_inst;
1762 uint64_t adrp_imm, addxri_imm;
1768 if (adrp_imm & (1ULL << 20))
1769 adrp_imm |= ~((1ULL << 21) - 1);
1771 addxri_inst = value;
1772 addxri_imm = (addxri_inst >> 10) & 0xfff;
1775 if ((addxri_inst >> (12 + 5 + 5)) & 1)
1777 value = (
m_adrp_address & 0xfffffffffffff000LL) + (adrp_imm << 12) +
1787 module_sp->ResolveFileAddress(value, value_so_addr);
1788 module_sp->ResolveFileAddress(
pc, pc_so_addr);
1796 const SymbolContextItem resolve_scope =
1797 eSymbolContextFunction | eSymbolContextSymbol;
1799 pc_so_addr.
GetModule()->ResolveSymbolContextForAddress(
1800 pc_so_addr, resolve_scope, sym_ctx);
1806 bool format_omitting_current_func_name =
false;
1812 format_omitting_current_func_name =
true;
1820 if (format_omitting_current_func_name) {
1834 std::string str = std::string(ss.
GetString());
1835 size_t first_eol_char = str.find_first_of(
"\r\n");
1836 if (first_eol_char != std::string::npos) {
1837 str.erase(first_eol_char);
1851 *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
DisassemblerLLVMC(const lldb_private::ArchSpec &arch, const char *flavor, const char *cpu, const char *features)
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)
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, const char *cpu, const char *features)
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.