-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Support disassembling RISC-V proprietary instructions #145793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
""" | ||
Defines a command, fdis, that does filtered disassembly. The command does the | ||
lldb disassemble command with -b and any other arguments passed in, and | ||
pipes that through a provided filter program. | ||
The intention is to support disassembly of RISC-V proprietary instructions. | ||
This is handled with llvm-objdump by piping the output of llvm-objdump through | ||
a filter program. This script is intended to mimic that workflow. | ||
""" | ||
|
||
import lldb | ||
import subprocess | ||
|
||
filter_program = "crustfilt" | ||
|
||
def __lldb_init_module(debugger, dict): | ||
debugger.HandleCommand( | ||
'command script add -f filter_disasm.fdis fdis') | ||
print("Disassembly filter command (fdis) loaded") | ||
print("Filter program set to %s" % filter_program) | ||
|
||
|
||
def fdis(debugger, args, result, dict): | ||
""" | ||
Call the built in disassembler, then pass its output to a filter program | ||
to add in disassembly for hidden opcodes. | ||
Except for get and set, use the fdis command like the disassemble command. | ||
By default, the filter program is crustfilt, from | ||
https://github.com/quic/crustfilt . This can be changed by changing | ||
the global variable filter_program. | ||
Usage: | ||
fdis [[get] [set <program>] [<disassembly options>]] | ||
Choose one of the following: | ||
get | ||
Gets the current filter program | ||
set <program> | ||
Sets the current filter program. This can be an executable, which | ||
will be found on PATH, or an absolute path. | ||
<disassembly options> | ||
If the first argument is not get or set, the args will be passed | ||
to the disassemble command as is. | ||
""" | ||
|
||
global filter_program | ||
args_list = args.split(' ') | ||
result.Clear() | ||
|
||
if len(args_list) == 1 and args_list[0] == 'get': | ||
result.PutCString(filter_program) | ||
result.SetStatus(lldb.eReturnStatusSuccessFinishResult) | ||
return | ||
|
||
if len(args_list) == 2 and args_list[0] == 'set': | ||
filter_program = args_list[1] | ||
result.PutCString("Filter program set to %s" % filter_program) | ||
result.SetStatus(lldb.eReturnStatusSuccessFinishResult) | ||
return | ||
|
||
res = lldb.SBCommandReturnObject() | ||
debugger.GetCommandInterpreter().HandleCommand('disassemble -b ' + args, res) | ||
if (len(res.GetError()) > 0): | ||
result.SetError(res.GetError()) | ||
result.SetStatus(lldb.eReturnStatusFailed) | ||
return | ||
output = res.GetOutput() | ||
|
||
try: | ||
proc = subprocess.run([filter_program], capture_output=True, text=True, input=output) | ||
except (subprocess.SubprocessError, OSError) as e: | ||
result.PutCString("Error occurred. Original disassembly:\n\n" + output) | ||
result.SetError(str(e)) | ||
result.SetStatus(lldb.eReturnStatusFailed) | ||
return | ||
|
||
print(proc.stderr) | ||
if proc.stderr: | ||
pass | ||
#result.SetError(proc.stderr) | ||
#result.SetStatus(lldb.eReturnStatusFailed) | ||
Comment on lines
+80
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume this needs to be swapped. Put stderr in the error so that the user will see it. |
||
else: | ||
result.PutCString(proc.stdout) | ||
result.SetStatus(lldb.eReturnStatusSuccessFinishResult) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,8 @@ class DisassemblerLLVMC::MCDisasmInstance { | |
|
||
uint64_t GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len, | ||
lldb::addr_t pc, llvm::MCInst &mc_inst) const; | ||
bool GetMCInst(const uint8_t *opcode_data, size_t opcode_data_len, | ||
lldb::addr_t pc, llvm::MCInst &mc_inst, size_t &size) const; | ||
void PrintMCInst(llvm::MCInst &mc_inst, lldb::addr_t pc, | ||
std::string &inst_string, std::string &comments_string); | ||
void SetStyle(bool use_hex_immed, HexImmediateStyle hex_style); | ||
|
@@ -524,11 +526,11 @@ class InstructionLLVMC : public lldb_private::Instruction { | |
const addr_t pc = m_address.GetFileAddress(); | ||
llvm::MCInst inst; | ||
|
||
const size_t inst_size = | ||
mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); | ||
if (inst_size == 0) | ||
m_opcode.Clear(); | ||
else { | ||
size_t inst_size = 0; | ||
m_is_valid = mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, | ||
pc, inst, inst_size); | ||
m_opcode.Clear(); | ||
if (inst_size != 0) { | ||
m_opcode.SetOpcodeBytes(opcode_data, inst_size); | ||
m_is_valid = true; | ||
} | ||
|
@@ -604,10 +606,11 @@ class InstructionLLVMC : public lldb_private::Instruction { | |
const uint8_t *opcode_data = data.GetDataStart(); | ||
const size_t opcode_data_len = data.GetByteSize(); | ||
llvm::MCInst inst; | ||
size_t inst_size = | ||
mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); | ||
|
||
if (inst_size > 0) { | ||
size_t inst_size = 0; | ||
bool valid = mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, | ||
inst, inst_size); | ||
|
||
if (valid && inst_size > 0) { | ||
mc_disasm_ptr->SetStyle(use_hex_immediates, hex_style); | ||
|
||
const bool saved_use_color = mc_disasm_ptr->GetUseColor(); | ||
|
@@ -1206,9 +1209,10 @@ class InstructionLLVMC : public lldb_private::Instruction { | |
const uint8_t *opcode_data = data.GetDataStart(); | ||
const size_t opcode_data_len = data.GetByteSize(); | ||
llvm::MCInst inst; | ||
const size_t inst_size = | ||
mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, pc, inst); | ||
if (inst_size == 0) | ||
size_t inst_size = 0; | ||
const bool valid = mc_disasm_ptr->GetMCInst(opcode_data, opcode_data_len, | ||
pc, inst, inst_size); | ||
if (!valid) | ||
return; | ||
|
||
m_has_visited_instruction = true; | ||
|
@@ -1337,19 +1341,18 @@ DisassemblerLLVMC::MCDisasmInstance::MCDisasmInstance( | |
m_asm_info_up && m_context_up && m_disasm_up && m_instr_printer_up); | ||
} | ||
|
||
uint64_t DisassemblerLLVMC::MCDisasmInstance::GetMCInst( | ||
bool DisassemblerLLVMC::MCDisasmInstance::GetMCInst( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to say this should be |
||
const uint8_t *opcode_data, size_t opcode_data_len, lldb::addr_t pc, | ||
llvm::MCInst &mc_inst) const { | ||
llvm::MCInst &mc_inst, size_t &size) const { | ||
llvm::ArrayRef<uint8_t> data(opcode_data, opcode_data_len); | ||
llvm::MCDisassembler::DecodeStatus status; | ||
|
||
uint64_t new_inst_size; | ||
status = m_disasm_up->getInstruction(mc_inst, new_inst_size, data, pc, | ||
status = m_disasm_up->getInstruction(mc_inst, size, data, pc, | ||
llvm::nulls()); | ||
if (status == llvm::MCDisassembler::Success) | ||
return new_inst_size; | ||
return true; | ||
else | ||
return 0; | ||
return false; | ||
} | ||
|
||
void DisassemblerLLVMC::MCDisasmInstance::PrintMCInst( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,9 +228,9 @@ static const CoreDefinition g_core_definitions[] = { | |
{eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon, | ||
ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5"}, | ||
|
||
{eByteOrderLittle, 4, 2, 4, llvm::Triple::riscv32, ArchSpec::eCore_riscv32, | ||
{eByteOrderLittle, 4, 2, 8, llvm::Triple::riscv32, ArchSpec::eCore_riscv32, | ||
"riscv32"}, | ||
{eByteOrderLittle, 8, 2, 4, llvm::Triple::riscv64, ArchSpec::eCore_riscv64, | ||
{eByteOrderLittle, 8, 2, 8, llvm::Triple::riscv64, ArchSpec::eCore_riscv64, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I read that RISC-V instructions are variable length in multiples of 16, though nothing standard uses greater than 32. So what's the logic of this change, that a really large number is very silly, but you do know of people using 64 bit custom instructions? |
||
"riscv64"}, | ||
|
||
{eByteOrderLittle, 4, 4, 4, llvm::Triple::loongarch32, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Istr there is a way to add options for custom commands that show up in
help
like built in ones do.