Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/LLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ include("debuginfo.jl")
include("utils.jl")
include("orc.jl")
include("newpm.jl")
include("disasm.jl")

# high-level functionality
include("state.jl")
Expand Down
40 changes: 40 additions & 0 deletions src/disasm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@checked struct DisasmContext
ref::API.LLVMDisasmContextRef
end
Base.unsafe_convert(::Type{API.LLVMDisasmContextRef}, dc::DisasmContext) = dc.ref

function create_disasm(triple)
ctx = DisasmContext(API.LLVMCreateDisasm(triple, C_NULL, 0, C_NULL, C_NULL))
return ctx
end
Comment on lines +6 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is my OOP kicking in, but why not tie this to a DisasmContext constructor like we do for other objects-like things (Context, IRBuilder, etc)?


function dispose(ctx::DisasmContext)
API.LLVMDisasmDispose(ctx.ref)
return nothing
end

function disassemble_code(io, ctx::DisasmContext, native_code::Vector{UInt8}, code_addr::Csize_t = 0; bufsize=32)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about simply disassemble? A do-block version could be provided that does the context construction for you.

pos = 1
buf = Vector{UInt8}(undef, bufsize)

while pos < length(native_code)
GC.@preserve native_code buf begin
bytes = pointer(native_code, pos)
pc = code_addr + pos - 1
nbytes = length(native_code) - pos + 1
# Need to use pointer(buf) here since the API is annotated as Cstring
nb = API.LLVMDisasmInstruction(ctx, bytes, nbytes, pc, pointer(buf), bufsize)
end
if nb == 0
# Disassembly failed
break
end
pos += nb
for byte in buf
byte == 0x00 && break
write(io, byte)
end
write(io, '\n')
end
return nothing
end
13 changes: 13 additions & 0 deletions test/disasm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import LLVM: create_disasm, disassemble_code

@testset "X86 Disassembly" begin
# Example from jitdump
native_code = UInt8[0x55, 0x48, 0x89, 0xe5, 0x49, 0x8b, 0x45, 0x10, 0x48, 0x8b, 0x40, 0x10, 0x48, 0x8b, 0x00, 0x48, 0x8b, 0x16, 0x48, 0x83, 0xc6, 0x08, 0x48, 0xb8, 0xe0, 0x7c, 0xf5, 0x81, 0xe4, 0x7f, 0x00, 0x00, 0xff, 0xd0, 0x5d, 0xc3]
code_addr = 0x00007fe48befcde0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a comment explaining the PC


ctx = create_disasm("x86_64-pc-linux-gnu")
disassembled = sprint(disassemble_code, ctx, native_code, code_addr)
LLVM.dispose(ctx)

@test disassembled == "\tpushq\t%rbp\n\tmovq\t%rsp, %rbp\n\tmovq\t16(%r13), %rax\n\tmovq\t16(%rax), %rax\n\tmovq\t(%rax), %rax\n\tmovq\t(%rsi), %rdx\n\taddq\t\$8, %rsi\n\tmovabsq\t\$140619409620192, %rax\n\tcallq\t*%rax\n\tpopq\t%rbp\n"
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ include("debuginfo.jl")
include("util.jl")
include("interop.jl")
include("orc.jl")
include("disasm.jl")
if !Sys.iswindows()
# XXX: hangs on Windows
include("jljit.jl")
Expand Down
Loading