2016-06-17 31 views
3

最近,我使用llvm在LLVM-IR中插入調用指令。問題是,如果我有一個名爲add的函數,我無法使用getFuntion(string)來查找它,因爲IR中的add()可能爲_Z3addv_。我知道IR中的所有功能都有一個新名字,但我不知道這個新名字到底是什麼。LLVM-如何通過函數的真實/原始名稱獲取函數

Module *m = f->getParent(); 
    IRBuilder<> builder(m->getContext()); 
    Function *call = m->getFunction("add"); 
    // call is NULL. 
    std::vector<Value *> args; 
    ...... 



Module *m = f->getParent(); 
IRBuilder<> builder(m->getContext()); 
Function *call = m->getFunction("_Z3addv"); 
// call is not NULL. 
std::vector<Value *> args; 
...... 

如何使用原始名稱查找函數?

+2

C++的名字mangling是由clang完成的,因此請查看clang的API。 –

回答

2

您可以重複使用LLVMCore中的Mangler

下面是使用的例子:

std::string mangledName; 
raw_string_ostream mangledNameStream(mangledName); 
Mangler::getNameWithPrefix(mangledNameStream, "add", m->getDataLayout()); 
// now mangledName contains, well, mangled name :) 
+0

認爲。這就是我要的 !!!注意:應在3行之後添加'mangledNameStream.flush()'以刷新緩衝區,否則mangledname可能爲NULL。 – qc1iu

+2

我剛剛試過這個,但似乎沒有工作。 'mangledName'包含未加密的名稱。 – executor21

0

的libstdC++有一個很好的demangling庫,只是包括cxxabi.h 然後你就可以改變Function *call = m->getFunction("_Z3addv");

int status; Function *call = m->getFunction(abi::__cxa_demangle("_Z3addv"), nullptr, nullptr, &status);