LLVM-如何通过函数的 real/original 名称获取函数
LLVM- How to get function by function's real/original name
最近使用llvm在LLVM-IR中插入call指令。问题是,如果我有一个名为 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;
......
如何使用原始名称找到函数?
您可以重复使用 LLVMCore
中的 Mangler
。
这是一个用法示例:
std::string mangledName;
raw_string_ostream mangledNameStream(mangledName);
Mangler::getNameWithPrefix(mangledNameStream, "add", m->getDataLayout());
// now mangledName contains, well, mangled name :)
libstdc++ 有一个很好的 demangling 库,只需包含 cxxabi.h
然后你可以改变Function *call = m->getFunction("_Z3addv");
至
int status;
Function *call = m->getFunction(abi::__cxa_demangle("_Z3addv"), nullptr, nullptr, &status);
最近使用llvm在LLVM-IR中插入call指令。问题是,如果我有一个名为 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;
......
如何使用原始名称找到函数?
您可以重复使用 LLVMCore
中的 Mangler
。
这是一个用法示例:
std::string mangledName;
raw_string_ostream mangledNameStream(mangledName);
Mangler::getNameWithPrefix(mangledNameStream, "add", m->getDataLayout());
// now mangledName contains, well, mangled name :)
libstdc++ 有一个很好的 demangling 库,只需包含 cxxabi.h
然后你可以改变Function *call = m->getFunction("_Z3addv");
至
int status;
Function *call = m->getFunction(abi::__cxa_demangle("_Z3addv"), nullptr, nullptr, &status);