Ste*_*ham 5 llvm disassembly mcjit
我有一个我写的程序,它使用LLVM 3.5作为JIT编译器,我正在尝试更新以在LLVM 3.7中使用MCJIT.我主要使用它,但我很难重现我用LLVM 3.5实现的一个仅调试功能.
我希望能够看到JIT过程生成的主机代码(例如x86,x64或ARM,而不是LLVM IR); 在调试版本中,我在程序运行时将其记录下来.使用LLVM 3.5,我可以通过调用ExecutionEngine :: runJITOnFunction()来填充llvm :: MachineCodeInfo对象,它给出了生成代码的起始地址和大小.然后我可以反汇编代码.
我似乎无法在MCJIT中找到任何等效物.我可以得到函数的起始地址(例如通过getPointerToFunction())但不是大小.
我见过Disassemble Memory但除了在答案中没有那么多细节之外,它似乎更多的是关于如何反汇编一系列字节.我知道怎么做,我的问题是:我怎样才能掌握字节序列?
如果它有助于使其更具体,请将此问题重新解释为:"如何扩展示例Kaleidoscope JIT以显示它生成的机器代码(x86,ARM等),而不仅仅是LLVM IR?"
谢谢.
您在这里至少有两个选择。
提供您自己的内存管理器。这必须有完整的文档记录,并且在许多项目中使用 MCJIT 完成。但为了完整起见,这里是代码:
class MCJITMemoryManager : public llvm::RTDyldMemoryManager {
public:
static std::unique_ptr<MCJITMemoryManager> Create();
MCJITMemoryManager();
virtual ~MCJITMemoryManager();
// Allocate a memory block of (at least) the given size suitable for
// executable code. The section_id is a unique identifier assigned by the
// MCJIT engine, and optionally recorded by the memory manager to access a
// loaded section.
byte* allocateCodeSection(uintptr_t size, unsigned alignment,
unsigned section_id,
llvm::StringRef section_name) override;
// Allocate a memory block of (at least) the given size suitable for data.
// The SectionID is a unique identifier assigned by the JIT engine, and
// optionally recorded by the memory manager to access a loaded section.
byte* allocateDataSection(uintptr_t size, unsigned alignment,
unsigned section_id, llvm::StringRef section_name,
bool is_readonly) override;
...
}
Run Code Online (Sandbox Code Playgroud)
将内存管理器实例传递给 EngineBuilder:
std::unique_ptr<MCJITMemoryManager> manager = MCJITMemoryManager::Create();
llvm::ExecutionEngine* raw = lvm::EngineBuilder(std::move(module))
.setMCJITMemoryManager(std::move(manager))
...
.create();
Run Code Online (Sandbox Code Playgroud)
现在,通过这些回调,您可以控制发出代码的内存。(并且大小直接传递给您的方法)。只需记住为代码段分配的缓冲区地址,然后在 gdb 中停止程序并反汇编内存(或将其转储到某处,甚至使用 LLVM 的反汇编器)。
llc
在 LLVM IR 上使用适当的选项(优化级别等)即可。在我看来,MCJIT 被如此称呼是有原因的,这个原因是它重用了现有的代码生成模块(与 llc 相同)。