| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/HostDisassembler.h" |
| 5 | |
| 6 | #include <span> |
| 7 | |
| 8 | #include <fmt/format.h> |
| 9 | #include <fmt/ostream.h> |
| 10 | #include <fmt/ranges.h> |
| 11 | |
| 12 | #if defined(HAVE_LLVM) |
| 13 | #include <llvm-c/Disassembler.h> |
| 14 | #include <llvm-c/Target.h> |
| 15 | #elif defined(_M_X86_64) |
| 16 | #include <disasm.h> // Bochs |
| 17 | #endif |
| 18 | |
| 19 | #if defined(HAVE_LLVM) |
| 20 | class HostDisassemblerLLVM final : public HostDisassembler |
| 21 | { |
| 22 | public: |
| 23 | explicit HostDisassemblerLLVM(const char* host_disasm, const char* cpu = "", |
| 24 | std::size_t inst_size = 0); |
| 25 | ~HostDisassemblerLLVM(); |
| 26 | |
| 27 | private: |
| 28 | LLVMDisasmContextRef m_llvm_context; |
| 29 | std::size_t m_instruction_size; |
| 30 | |
| 31 | std::size_t Disassemble(const u8* begin, const u8* end, std::ostream& stream) override; |
| 32 | }; |
| 33 | |
| 34 | HostDisassemblerLLVM::HostDisassemblerLLVM(const char* host_disasm, const char* cpu, |
| 35 | std::size_t inst_size) |
| 36 | : m_instruction_size(inst_size) |
| 37 | { |
| 38 | LLVMInitializeAllTargetInfos(); |
| 39 | LLVMInitializeAllTargetMCs(); |
| 40 | LLVMInitializeAllDisassemblers(); |
| 41 | |
| 42 | m_llvm_context = LLVMCreateDisasmCPU(host_disasm, cpu, nullptr, 0, nullptr, nullptr); |
| 43 | |
| 44 | // Couldn't create llvm context |
| 45 | if (!m_llvm_context) |
| 46 | return; |
| 47 | |
| 48 | LLVMSetDisasmOptions(m_llvm_context, LLVMDisassembler_Option_AsmPrinterVariant | |
| 49 | LLVMDisassembler_Option_PrintLatency); |
| 50 | } |
| 51 | |
| 52 | HostDisassemblerLLVM::~HostDisassemblerLLVM() |
| 53 | { |
| 54 | if (m_llvm_context) |
| 55 | LLVMDisasmDispose(m_llvm_context); |
| 56 | } |
| 57 | |
| 58 | std::size_t HostDisassemblerLLVM::Disassemble(const u8* begin, const u8* end, std::ostream& stream) |
| 59 | { |
| 60 | std::size_t instruction_count = 0; |
| 61 | if (!m_llvm_context) |
| 62 | return instruction_count; |
| 63 | |
| 64 | while (begin < end) |
| 65 | { |
| 66 | char inst_disasm[256]; |
| 67 | |
| 68 | const auto inst_size = |
| 69 | LLVMDisasmInstruction(m_llvm_context, const_cast<u8*>(begin), static_cast<u64>(end - begin), |
| 70 | reinterpret_cast<u64>(begin), inst_disasm, sizeof(inst_disasm)); |
| 71 | if (inst_size == 0) |
| 72 | { |
| 73 | if (m_instruction_size != 0) |
| 74 | { |
| 75 | // If we are on an architecture that has a fixed instruction |
| 76 | // size, we can continue onward past this bad instruction. |
| 77 | fmt::println(stream, "{}\tInvalid inst: {:02x}", fmt::ptr(begin), |
| 78 | fmt::join(std::span{begin, m_instruction_size}, "")); |
| 79 | begin += m_instruction_size; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | // We can't continue if we are on an architecture that has flexible |
| 84 | // instruction sizes. Dump the rest of the block instead. |
| 85 | fmt::println(stream, "{}\tInvalid inst: {:02x}", fmt::ptr(begin), |
| 86 | fmt::join(std::span{begin, end}, "")); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | fmt::println(stream, "{}{}", fmt::ptr(begin), inst_disasm); |
| 93 | begin += inst_size; |
| 94 | } |
| 95 | ++instruction_count; |
| 96 | } |
| 97 | return instruction_count; |
| 98 | } |
| 99 | #elif defined(_M_X86_64) |
| 100 | class HostDisassemblerBochs final : public HostDisassembler |
| 101 | { |
| 102 | public: |
| 103 | explicit HostDisassemblerBochs(); |
| 104 | ~HostDisassemblerBochs() override = default; |
| 105 | |
| 106 | private: |
| 107 | disassembler m_disasm; |
| 108 | |
| 109 | std::size_t Disassemble(const u8* begin, const u8* end, std::ostream& stream) override; |
| 110 | }; |
| 111 | |
| 112 | HostDisassemblerBochs::HostDisassemblerBochs() |
| 113 | { |
| 114 | m_disasm.set_syntax_intel(); |
| 115 | } |
| 116 | |
| 117 | std::size_t HostDisassemblerBochs::Disassemble(const u8* begin, const u8* end, std::ostream& stream) |
| 118 | { |
| 119 | std::size_t instruction_count = 0; |
| 120 | while (begin < end) |
| 121 | { |
| 122 | char inst_disasm[256]; |
| 123 | |
| 124 | const auto inst_size = |
| 125 | m_disasm.disasm64(0, reinterpret_cast<bx_address>(begin), begin, inst_disasm); |
| 126 | fmt::println(stream, "{}\t{}", fmt::ptr(begin), inst_disasm); |
| 127 | begin += inst_size; |
| 128 | ++instruction_count; |
| 129 | } |
| 130 | return instruction_count; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | std::unique_ptr<HostDisassembler> HostDisassembler::Factory(Platform arch) |
| 135 | { |
| 136 | switch (arch) |
| 137 | { |
| 138 | #if defined(HAVE_LLVM) |
| 139 | case Platform::x86_64: |
| 140 | return std::make_unique<HostDisassemblerLLVM>("x86_64-none-unknown"); |
| 141 | case Platform::aarch64: |
| 142 | return std::make_unique<HostDisassemblerLLVM>("aarch64-none-unknown", "cortex-a57", 4); |
| 143 | #elif defined(_M_X86_64) |
| 144 | case Platform::x86_64: |
| 145 | return std::make_unique<HostDisassemblerBochs>(); |
| 146 | #else |
| 147 | case Platform{}: // warning C4065: "switch statement contains 'default' but no 'case' labels" |
| 148 | #endif |
| 149 | default: |
| 150 | return std::make_unique<HostDisassembler>(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | std::size_t HostDisassembler::Disassemble(const u8* begin, const u8* end, std::ostream& stream) |
| 155 | { |
| 156 | fmt::println(stream, "{}\t{:02x}", fmt::ptr(begin), fmt::join(std::span{begin, end}, "")); |
| 157 | return 0; |
| 158 | } |