如何在LLVM上提取完整的函数信息?

How to extract complete function information on LLVM?

我正在尝试

  1. 提取函数的 LLVM IR
  2. 将它们每个保存到不同的文件(或只是一个字符串对象)
  3. 从文件(或只是一个字符串对象)中读取保存的函数
  4. 在框架上重新使用它

但是,我目前的提取方法只复制了部分信息,之后我无法重新构建函数。我主要有 IR Reader 抱怨的问题:

error: use of undefined type named 'class.std::allocator'
...
error: use of undefined comdat '$_ZNSt10_Iter_baseIPiLb0EE7_S_baseES0_'

可以通过在提取的 IR 顶部添加适当的声明(comdat 和类型)来解决这个问题。

之前:

; Function Attrs: noinline nounwind uwtable
define linkonce_odr i64 @_ZNK9__gnu_cxx13new_allocatorIiE8max_sizeEv(%"class.__gnu_cxx::new_allocator"*) #4 comdat align 2 {
  ret i64 4611686018427387903
}

之后:

%"class.__gnu_cxx::new_allocator" = type { i8 }
$_ZNK9__gnu_cxx13new_allocatorIiE8max_sizeEv = comdat any

; Function Attrs: noinline nounwind uwtable
define linkonce_odr i64 @_ZNK9__gnu_cxx13new_allocatorIiE8max_sizeEv(%"class.__gnu_cxx::new_allocator"*) #4 comdat align 2 {
  ret i64 4611686018427387903
}

我目前正在使用以下方法实施步骤 1、2、3、4:

#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/SourceMgr.h" // SMDDiagnostic
#include "llvm/Support/MemoryBuffer.h"

using namespace llvm;

...

// A method receives a LLVM::Function& F as its argument
// Steps 1 and 2
std::string IRString;
raw_string_ostream os(IRString);
F.print(os, nullptr);
os.flush();

// IRString has the LLVM IR for the function (currently with the BEFORE version)
// Now it is necessary to read back this IR
// Steps 3 and 4
SMDiagnostic Err;
LLVMContext Context;
std::unique_ptr<llvm::MemoryBuffer> buff =
llvm::MemoryBuffer::getMemBuffer(SU.getIRInfo());

std::unique_ptr<Module> Mod(parseIR(buff->getMemBufferRef(), Err, Context));
if (!Mod) {
    Err.print("Reading Problems", errs());
    return 1;
}

...

我怎样才能让这个过程自动进行?

看看 llvm-extract 或 GVExtraction class(由 llvm-extract 使用)。