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/MCInstrInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCTargetOptions.h"
25#include "llvm/MC/TargetRegistry.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/ScopedPrinter.h"
28#include "llvm/Support/TargetSelect.h"
29#include "llvm/TargetParser/AArch64TargetParser.h"
54 static std::unique_ptr<MCDisasmInstance>
55 Create(
const char *triple,
const char *cpu,
const char *features_str,
60 uint64_t GetMCInst(
const uint8_t *opcode_data,
size_t opcode_data_len,
62 void PrintMCInst(llvm::MCInst &mc_inst, std::string &inst_string,
63 std::string &comments_string);
65 bool CanBranch(llvm::MCInst &mc_inst)
const;
66 bool HasDelaySlot(llvm::MCInst &mc_inst)
const;
67 bool IsCall(llvm::MCInst &mc_inst)
const;
68 bool IsLoad(llvm::MCInst &mc_inst)
const;
69 bool IsAuthenticated(llvm::MCInst &mc_inst)
const;
73 std::unique_ptr<llvm::MCRegisterInfo> &®_info_up,
74 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
75 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
76 std::unique_ptr<llvm::MCContext> &&context_up,
77 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
78 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up);
127 uint8_t opcode_len = opcode_and_modrm.
opcode_len;
128 uint8_t modrm = opcode_and_modrm.
modrm;
133 if (opcode >= 0x70 && opcode <= 0x7F) {
140 if (opcode >= 0x80 && opcode <= 0x8F) {
153 if (opcode_len == 1) {
154 uint8_t modrm_reg = (modrm >> 3) & 7;
157 else if (modrm_reg == 3)
159 else if (modrm_reg == 4)
161 else if (modrm_reg == 5)
217 if (opcode_len == 2) {
257std::optional<InstructionOpcodeAndModrm>
259 bool is_exec_mode_64b) {
261 bool prefix_done =
false;
268 while (!prefix_done) {
269 if (op_idx >= bytes_len)
308 if (is_exec_mode_64b)
316 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
323 ret.
modrm = inst_bytes[op_idx + 3];
327 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
331 ret.
opcode_len = inst_bytes[op_idx + 1] & 0x1f;
333 ret.
modrm = inst_bytes[op_idx + 4];
338 if (!is_exec_mode_64b && (inst_bytes[op_idx + 1] & 0xc0) != 0xc0) {
342 ret.
opcode_len = inst_bytes[op_idx + 1] & 0x03;
344 ret.
modrm = inst_bytes[op_idx + 5];
354 ret.
modrm = inst_bytes[op_idx + 1];
364 ret.
modrm = inst_bytes[op_idx + 1];
368 ret.
modrm = inst_bytes[op_idx + 1];
372 ret.
modrm = inst_bytes[op_idx + 1];
376 ret.
modrm = inst_bytes[op_idx + 1];
379 ret.
modrm = inst_bytes[op_idx + 1];
388 std::optional<InstructionOpcodeAndModrm> ret;
414 disasm.shared_from_this())) {}
451 const ArchSpec &arch = disasm->GetArchitecture();
456 if (min_op_byte_size == max_op_byte_size) {
461 switch (min_op_byte_size) {
490 bool is_alternate_isa =
false;
494 const llvm::Triple::ArchType machine = arch.
GetMachine();
495 if (machine == llvm::Triple::arm || machine == llvm::Triple::thumb) {
496 if (machine == llvm::Triple::thumb || is_alternate_isa) {
498 if ((thumb_opcode & 0xe000) != 0xe000 ||
499 ((thumb_opcode & 0x1800u) == 0)) {
504 thumb_opcode |= data.
GetU16(&data_offset);
515 uint8_t *opcode_data =
516 const_cast<uint8_t *
>(data.
PeekData(data_offset, 1));
517 const size_t opcode_data_len = data.
BytesLeft(data_offset);
521 const size_t inst_size =
522 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
549 if (disasm->GetArchitecture().GetMachine() == llvm::Triple::x86)
551 else if (disasm->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
564 std::string out_string;
565 std::string comment_string;
571 if (address_class == AddressClass::eCodeAlternateISA)
572 mc_disasm_ptr = disasm->m_alternate_disasm_up.get();
574 mc_disasm_ptr = disasm->m_disasm_up.get();
579 const bool data_from_file = disasm->m_data_from_file;
580 bool use_hex_immediates =
true;
589 if (!data_from_file) {
603 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
606 mc_disasm_ptr->
SetStyle(use_hex_immediates, hex_style);
607 mc_disasm_ptr->
PrintMCInst(inst, out_string, comment_string);
609 if (!comment_string.empty()) {
614 if (inst_size == 0) {
622 const uint8_t uval8 = data.
GetU8(&offset);
625 mnemonic_strm.
Printf(
"0x%2.2x", uval8);
631 mnemonic_strm.
Printf(
"0x%4.4x", uval16);
637 mnemonic_strm.
Printf(
"0x%8.8x", uval32);
640 const uint64_t uval64 = data.
GetU64(&offset);
643 mnemonic_strm.
Printf(
"0x%16.16" PRIx64, uval64);
649 const uint8_t *bytes = data.
PeekData(offset, inst_size);
650 if (bytes ==
nullptr)
654 mnemonic_strm.
Printf(
"0x%2.2x", bytes[0]);
655 for (
uint32_t i = 1; i < inst_size; ++i)
656 mnemonic_strm.
Printf(
" 0x%2.2x", bytes[i]);
665 llvm::StringRef(
"[ \t]*([^ ^\t]+)[ \t]*([^ ^\t].*)?"));
667 llvm::SmallVector<llvm::StringRef, 4> matches;
668 if (s_regex.
Execute(out_string, &matches)) {
698 operator bool()
const {
return static_cast<bool>(
m_disasm); }
703 static llvm::StringRef::const_iterator
705 llvm::StringRef::const_iterator ose) {
720 static std::pair<bool, llvm::StringRef::const_iterator>
722 llvm::StringRef::const_iterator ose) {
726 if (osi != ose && *osi == c) {
731 return std::make_pair(found, osi);
734 static std::pair<Operand, llvm::StringRef::const_iterator>
736 llvm::StringRef::const_iterator ose) {
738 ret.
m_type = Operand::Type::Register;
744 if (*osi >=
'0' && *osi <=
'9') {
746 return std::make_pair(
Operand(), osi);
750 }
else if (*osi >=
'a' && *osi <=
'z') {
756 return std::make_pair(
Operand(), osi);
759 return std::make_pair(ret, osi);
763 return std::make_pair(
Operand(), osi);
772 return std::make_pair(ret, osi);
775 static std::pair<Operand, llvm::StringRef::const_iterator>
777 llvm::StringRef::const_iterator ose) {
779 ret.
m_type = Operand::Type::Immediate;
786 if (*osi >=
'0' && *osi <=
'9') {
788 }
else if (*osi >=
'a' && *osi <=
'f') {
792 return std::make_pair(
Operand(), osi);
798 return std::make_pair(
Operand(), osi);
800 ret.
m_immediate = strtoull(str.c_str(),
nullptr, 0);
801 return std::make_pair(ret, osi);
804 if (!str.compare(
"0")) {
808 return std::make_pair(
Operand(), osi);
814 return std::make_pair(
Operand(), osi);
821 return std::make_pair(
Operand(), osi);
828 ret.
m_immediate = strtoull(str.c_str(),
nullptr, 0);
829 return std::make_pair(ret, osi);
833 static std::pair<Operand, llvm::StringRef::const_iterator>
835 llvm::StringRef::const_iterator ose) {
836 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
838 if (offset_and_iterator.first.IsValid()) {
839 osi = offset_and_iterator.second;
845 return std::make_pair(
Operand(), osi);
848 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
850 if (base_and_iterator.first.IsValid()) {
851 osi = base_and_iterator.second;
853 return std::make_pair(
Operand(), osi);
858 return std::make_pair(
Operand(), osi);
861 std::pair<Operand, llvm::StringRef::const_iterator> index_and_iterator =
863 if (index_and_iterator.first.IsValid()) {
864 osi = index_and_iterator.second;
866 return std::make_pair(
Operand(), osi);
871 return std::make_pair(
Operand(), osi);
874 std::pair<Operand, llvm::StringRef::const_iterator>
876 if (index_and_iterator.first.IsValid()) {
877 osi = index_and_iterator.second;
879 return std::make_pair(
Operand(), osi);
884 return std::make_pair(
Operand(), osi);
888 product.
m_type = Operand::Type::Product;
889 product.
m_children.push_back(index_and_iterator.first);
890 product.
m_children.push_back(multiplier_and_iterator.first);
893 index.
m_type = Operand::Type::Sum;
894 index.
m_children.push_back(base_and_iterator.first);
897 if (offset_and_iterator.first.IsValid()) {
899 offset.
m_type = Operand::Type::Sum;
900 offset.
m_children.push_back(offset_and_iterator.first);
904 deref.
m_type = Operand::Type::Dereference;
906 return std::make_pair(deref, osi);
909 deref.
m_type = Operand::Type::Dereference;
911 return std::make_pair(deref, osi);
916 static std::pair<Operand, llvm::StringRef::const_iterator>
918 llvm::StringRef::const_iterator ose) {
919 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
921 if (offset_and_iterator.first.IsValid()) {
922 osi = offset_and_iterator.second;
928 return std::make_pair(
Operand(), osi);
931 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
933 if (base_and_iterator.first.IsValid()) {
934 osi = base_and_iterator.second;
936 return std::make_pair(
Operand(), osi);
941 return std::make_pair(
Operand(), osi);
944 if (offset_and_iterator.first.IsValid()) {
946 offset.
m_type = Operand::Type::Sum;
947 offset.
m_children.push_back(offset_and_iterator.first);
948 offset.
m_children.push_back(base_and_iterator.first);
951 deref.
m_type = Operand::Type::Dereference;
953 return std::make_pair(deref, osi);
956 deref.
m_type = Operand::Type::Dereference;
957 deref.
m_children.push_back(base_and_iterator.first);
958 return std::make_pair(deref, osi);
963 static std::pair<Operand, llvm::StringRef::const_iterator>
965 llvm::StringRef::const_iterator ose) {
969 return std::make_pair(
Operand(), osi);
972 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
974 if (base_and_iterator.first.IsValid()) {
975 osi = base_and_iterator.second;
977 return std::make_pair(
Operand(), osi);
982 return std::make_pair(
Operand(), osi);
985 std::pair<Operand, llvm::StringRef::const_iterator> offset_and_iterator =
987 if (offset_and_iterator.first.IsValid()) {
988 osi = offset_and_iterator.second;
993 return std::make_pair(
Operand(), osi);
997 offset.
m_type = Operand::Type::Sum;
998 offset.
m_children.push_back(offset_and_iterator.first);
999 offset.
m_children.push_back(base_and_iterator.first);
1002 deref.
m_type = Operand::Type::Dereference;
1004 return std::make_pair(deref, osi);
1008 static std::pair<Operand, llvm::StringRef::const_iterator>
1010 llvm::StringRef::const_iterator ose) {
1012 std::tie(found, osi) =
ConsumeChar(osi,
'[', ose);
1014 return std::make_pair(
Operand(), osi);
1017 std::pair<Operand, llvm::StringRef::const_iterator> base_and_iterator =
1019 if (base_and_iterator.first.IsValid()) {
1020 osi = base_and_iterator.second;
1022 return std::make_pair(
Operand(), osi);
1025 std::tie(found, osi) =
ConsumeChar(osi,
']', ose);
1027 return std::make_pair(
Operand(), osi);
1031 deref.
m_type = Operand::Type::Dereference;
1032 deref.
m_children.push_back(base_and_iterator.first);
1033 return std::make_pair(deref, osi);
1038 case Operand::Type::Dereference:
1042 case Operand::Type::Immediate:
1048 case Operand::Type::Invalid:
1051 case Operand::Type::Product:
1058 case Operand::Type::Register:
1061 case Operand::Type::Sum:
1073 const char *operands_string =
GetOperands(
nullptr);
1075 if (!operands_string) {
1079 llvm::StringRef operands_ref(operands_string);
1081 llvm::StringRef::const_iterator osi = operands_ref.begin();
1082 llvm::StringRef::const_iterator ose = operands_ref.end();
1084 while (osi != ose) {
1086 llvm::StringRef::const_iterator iter;
1101 operands.push_back(operand);
1106 std::pair<bool, llvm::StringRef::const_iterator> found_and_iter =
1108 if (found_and_iter.first) {
1109 osi = found_and_iter.second;
1117 if (disasm_sp && operands.size() > 1) {
1119 switch (disasm_sp->GetArchitecture().GetMachine()) {
1122 case llvm::Triple::x86:
1123 case llvm::Triple::x86_64:
1124 operands[operands.size() - 1].m_clobbered =
true;
1126 case llvm::Triple::arm:
1127 operands[0].m_clobbered =
true;
1132 if (
Log *log =
GetLog(LLDBLog::Process)) {
1135 ss.
Printf(
"[%s] expands to %zu operands:\n", operands_string,
1137 for (
const Operand &operand : operands) {
1185 bool is_alternate_isa;
1190 const size_t opcode_data_len = data.
GetByteSize();
1192 const size_t inst_size =
1193 mc_disasm_ptr->
GetMCInst(opcode_data, opcode_data_len,
pc, inst);
1208 is_alternate_isa =
false;
1210 if (disasm->m_alternate_disasm_up) {
1213 if (address_class == AddressClass::eCodeAlternateISA) {
1214 is_alternate_isa =
true;
1215 return disasm->m_alternate_disasm_up.get();
1218 return disasm->m_disasm_up.get();
1224std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>
1226 const char *features_str,
1229 using Instance = std::unique_ptr<DisassemblerLLVMC::MCDisasmInstance>;
1232 const llvm::Target *curr_target =
1233 llvm::TargetRegistry::lookupTarget(triple,
Status);
1237 std::unique_ptr<llvm::MCInstrInfo> instr_info_up(
1238 curr_target->createMCInstrInfo());
1242 std::unique_ptr<llvm::MCRegisterInfo> reg_info_up(
1243 curr_target->createMCRegInfo(triple));
1247 std::unique_ptr<llvm::MCSubtargetInfo> subtarget_info_up(
1248 curr_target->createMCSubtargetInfo(triple, cpu, features_str));
1249 if (!subtarget_info_up)
1252 llvm::MCTargetOptions MCOptions;
1253 std::unique_ptr<llvm::MCAsmInfo> asm_info_up(
1254 curr_target->createMCAsmInfo(*reg_info_up, triple, MCOptions));
1258 std::unique_ptr<llvm::MCContext> context_up(
1259 new llvm::MCContext(llvm::Triple(triple), asm_info_up.get(),
1260 reg_info_up.get(), subtarget_info_up.get()));
1264 std::unique_ptr<llvm::MCDisassembler> disasm_up(
1265 curr_target->createMCDisassembler(*subtarget_info_up, *context_up));
1269 std::unique_ptr<llvm::MCRelocationInfo> rel_info_up(
1270 curr_target->createMCRelocationInfo(triple, *context_up));
1274 std::unique_ptr<llvm::MCSymbolizer> symbolizer_up(
1275 curr_target->createMCSymbolizer(
1277 context_up.get(), std::move(rel_info_up)));
1278 disasm_up->setSymbolizer(std::move(symbolizer_up));
1280 unsigned asm_printer_variant =
1281 flavor == ~0U ? asm_info_up->getAssemblerDialect() : flavor;
1283 std::unique_ptr<llvm::MCInstPrinter> instr_printer_up(
1284 curr_target->createMCInstPrinter(llvm::Triple{triple},
1285 asm_printer_variant, *asm_info_up,
1286 *instr_info_up, *reg_info_up));
1287 if (!instr_printer_up)
1292 std::move(subtarget_info_up), std::move(asm_info_up),
1293 std::move(context_up), std::move(disasm_up),
1294 std::move(instr_printer_up)));
1298 std::unique_ptr<llvm::MCInstrInfo> &&instr_info_up,
1299 std::unique_ptr<llvm::MCRegisterInfo> &®_info_up,
1300 std::unique_ptr<llvm::MCSubtargetInfo> &&subtarget_info_up,
1301 std::unique_ptr<llvm::MCAsmInfo> &&asm_info_up,
1302 std::unique_ptr<llvm::MCContext> &&context_up,
1303 std::unique_ptr<llvm::MCDisassembler> &&disasm_up,
1304 std::unique_ptr<llvm::MCInstPrinter> &&instr_printer_up)
1305 : m_instr_info_up(std::move(instr_info_up)),
1306 m_reg_info_up(std::move(reg_info_up)),
1307 m_subtarget_info_up(std::move(subtarget_info_up)),
1308 m_asm_info_up(std::move(asm_info_up)),
1309 m_context_up(std::move(context_up)),
m_disasm_up(std::move(disasm_up)),
1310 m_instr_printer_up(std::move(instr_printer_up)) {
1316 const uint8_t *opcode_data,
size_t opcode_data_len,
lldb::addr_t pc,
1317 llvm::MCInst &mc_inst)
const {
1318 llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len);
1319 llvm::MCDisassembler::DecodeStatus status;
1321 uint64_t new_inst_size;
1322 status =
m_disasm_up->getInstruction(mc_inst, new_inst_size, data,
pc,
1324 if (status == llvm::MCDisassembler::Success)
1325 return new_inst_size;
1331 llvm::MCInst &mc_inst, std::string &inst_string,
1332 std::string &comments_string) {
1333 llvm::raw_string_ostream inst_stream(inst_string);
1334 llvm::raw_string_ostream comments_stream(comments_string);
1336 m_instr_printer_up->setCommentStream(comments_stream);
1337 m_instr_printer_up->printInst(&mc_inst, 0, llvm::StringRef(),
1338 *m_subtarget_info_up, inst_stream);
1339 m_instr_printer_up->setCommentStream(llvm::nulls());
1340 comments_stream.flush();
1342 static std::string g_newlines(
"\r\n");
1344 for (
size_t newline_pos = 0;
1345 (newline_pos = comments_string.find_first_of(g_newlines, newline_pos)) !=
1346 comments_string.npos;
1348 comments_string.replace(comments_string.begin() + newline_pos,
1349 comments_string.begin() + newline_pos + 1, 1,
' ');
1355 m_instr_printer_up->setPrintImmHex(use_hex_immed);
1356 switch (hex_style) {
1358 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::C);
1361 m_instr_printer_up->setPrintHexStyle(llvm::HexStyle::Asm);
1367 llvm::MCInst &mc_inst)
const {
1368 return m_instr_info_up->get(mc_inst.getOpcode())
1369 .mayAffectControlFlow(mc_inst, *m_reg_info_up);
1373 llvm::MCInst &mc_inst)
const {
1374 return m_instr_info_up->get(mc_inst.getOpcode()).hasDelaySlot();
1378 return m_instr_info_up->get(mc_inst.getOpcode()).isCall();
1382 return m_instr_info_up->get(mc_inst.getOpcode()).mayLoad();
1386 llvm::MCInst &mc_inst)
const {
1387 const auto &InstrDesc = m_instr_info_up->get(mc_inst.getOpcode());
1392 bool IsBrkC47x =
false;
1393 if (InstrDesc.isTrap() && mc_inst.getNumOperands() == 1) {
1394 const llvm::MCOperand &Op0 = mc_inst.getOperand(0);
1395 if (Op0.isImm() && Op0.getImm() >= 0xc470 && Op0.getImm() <= 0xc474)
1399 return InstrDesc.isAuthenticated() || IsBrkC47x;
1403 const char *flavor_string)
1411 unsigned flavor = ~0U;
1416 if (triple.getArch() == llvm::Triple::x86 ||
1417 triple.getArch() == llvm::Triple::x86_64) {
1426 if (triple.getArch() == llvm::Triple::arm) {
1427 std::string thumb_arch_name(thumb_arch.
GetTriple().getArchName().str());
1429 if (thumb_arch_name.size() > 3) {
1430 thumb_arch_name.erase(0, 3);
1431 thumb_arch_name.insert(0,
"thumb");
1433 thumb_arch_name =
"thumbv9.3a";
1435 thumb_arch.
GetTriple().setArchName(llvm::StringRef(thumb_arch_name));
1443 if (triple.getArch() == llvm::Triple::arm &&
1444 triple.getSubArch() == llvm::Triple::NoSubArch)
1445 triple.setArchName(
"armv9.3a");
1447 std::string features_str;
1448 const char *triple_str = triple.getTriple().c_str();
1452 triple_str = thumb_arch.
GetTriple().getTriple().c_str();
1453 features_str +=
"+fp-armv8,";
1456 const char *cpu =
"";
1507 features_str +=
"+msa,";
1509 features_str +=
"+dsp,";
1511 features_str +=
"+dspr2,";
1515 if (triple.isAArch64()) {
1516 features_str +=
"+all,";
1518 if (triple.getVendor() == llvm::Triple::Apple)
1519 cpu =
"apple-latest";
1522 if (triple.isRISCV()) {
1525 features_str +=
"+c,";
1527 features_str +=
"+e,";
1530 features_str +=
"+f,";
1533 features_str +=
"+f,+d,";
1536 features_str +=
"+f,+d,+q,";
1546 llvm::Triple::ArchType llvm_arch = triple.getArch();
1550 if (llvm_arch == llvm::Triple::arm) {
1551 std::string thumb_triple(thumb_arch.
GetTriple().getTriple());
1558 }
else if (arch.
IsMIPS()) {
1562 features_str +=
"+mips16,";
1564 features_str +=
"+micromips,";
1567 triple_str, cpu, features_str.c_str(), flavor, *
this);
1576 const char *flavor) {
1577 if (arch.
GetTriple().getArch() != llvm::Triple::UnknownArch) {
1578 std::unique_ptr<DisassemblerLLVMC> disasm_up(
1581 if (disasm_up.get() && disasm_up->IsValid())
1582 return disasm_up.release();
1590 size_t num_instructions,
1591 bool append,
bool data_from_file) {
1599 uint32_t data_cursor = data_offset;
1604 while (data_cursor < data_byte_size &&
1605 instructions_parsed < num_instructions) {
1612 InstructionSP inst_sp(
1618 uint32_t inst_size = inst_sp->Decode(*
this, data, data_cursor);
1624 data_cursor += inst_size;
1625 inst_addr.
Slide(inst_size);
1626 instructions_parsed++;
1629 return data_cursor - data_offset;
1634 "Disassembler that uses LLVM MC to disassemble "
1635 "i386, x86_64, ARM, and ARM64.",
1638 llvm::InitializeAllTargetInfos();
1639 llvm::InitializeAllTargetMCs();
1640 llvm::InitializeAllAsmParsers();
1641 llvm::InitializeAllDisassemblers();
1649 uint64_t offset, uint64_t size,
1650 int tag_type,
void *tag_bug) {
1652 ->
OpInfo(
pc, offset, size, tag_type, tag_bug);
1657 uint64_t *type, uint64_t
pc,
1658 const char **name) {
1666 if (flavor ==
nullptr || strcmp(flavor,
"default") == 0)
1669 if (triple.getArch() == llvm::Triple::x86 ||
1670 triple.getArch() == llvm::Triple::x86_64) {
1671 return strcmp(flavor,
"intel") == 0 || strcmp(flavor,
"att") == 0;
1679 int tag_type,
void *tag_bug) {
1684 memset(tag_bug, 0,
sizeof(::LLVMOpInfo1));
1691 uint64_t
pc,
const char **name) {
1701 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADRP) {
1705 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
1712 if (*type_ptr == LLVMDisassembler_ReferenceType_In_ARM64_ADDXri &&
1716 uint64_t adrp_imm, addxri_imm;
1722 if (adrp_imm & (1ULL << 20))
1723 adrp_imm |= ~((1ULL << 21) - 1);
1725 addxri_inst = value;
1726 addxri_imm = (addxri_inst >> 10) & 0xfff;
1729 if ((addxri_inst >> (12 + 5 + 5)) & 1)
1731 value = (
m_adrp_address & 0xfffffffffffff000LL) + (adrp_imm << 12) +
1741 module_sp->ResolveFileAddress(value, value_so_addr);
1742 module_sp->ResolveFileAddress(
pc, pc_so_addr);
1750 const SymbolContextItem resolve_scope =
1751 eSymbolContextFunction | eSymbolContextSymbol;
1753 pc_so_addr.
GetModule()->ResolveSymbolContextForAddress(
1754 pc_so_addr, resolve_scope, sym_ctx);
1760 bool format_omitting_current_func_name =
false;
1766 format_omitting_current_func_name =
true;
1774 if (format_omitting_current_func_name) {
1788 std::string str = std::string(ss.
GetString());
1789 size_t first_eol_char = str.find_first_of(
"\r\n");
1790 if (first_eol_char != std::string::npos) {
1791 str.erase(first_eol_char);
1805 *type_ptr = LLVMDisassembler_ReferenceType_InOut_None;
#define LLDB_PLUGIN_DEFINE(PluginName)
bool HasDelaySlot(llvm::MCInst &mc_inst) const
void PrintMCInst(llvm::MCInst &mc_inst, std::string &inst_string, std::string &comments_string)
~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
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::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::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)
lldb::addr_t m_adrp_address
static lldb_private::Disassembler * CreateInstance(const lldb_private::ArchSpec &arch, const char *flavor)
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) 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)
const Address & GetAddress() const
const char * GetOperands(const ExecutionContext *exe_ctx)
std::string m_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.
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::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.