最近,我使用llvm在LLVM-IR中插入调用指令。问题是,如果我有一个名为add
的函数,我无法使用getFuntion(string)找到它,因为IR中的add()可能_Z3addv_
。我知道IR中的所有功能都有一个新名称,但我不知道新名称到底是什么。
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;
......
如何使用原始名称找到该功能?
答案 0 :(得分:3)
您可以重复使用Mangler
中的LLVMCore
。
以下是一个使用示例:
std::string mangledName;
raw_string_ostream mangledNameStream(mangledName);
Mangler::getNameWithPrefix(mangledNameStream, "add", m->getDataLayout());
// now mangledName contains, well, mangled name :)
答案 1 :(得分:0)
libstdc ++有一个很好的demangling库,只包括cxxabi.h
然后,您可以更改Function *call = m->getFunction("_Z3addv");
到
int status;
Function *call = m->getFunction(abi::__cxa_demangle("_Z3addv"), nullptr, nullptr, &status);