Tiger
Tiger

Reputation: 165

LLVM- How to get function by function's real/original name

Recently, I use llvm to insert call Instruction in LLVM-IR. The problem is that , if I have a funtion named add, I can not find it using getFuntion(string) since the add() in IR may _Z3addv_. I know that all fucntion in IR has a new name, but I didn't know what the new name exactly is.

  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;
......

How can I find the function using its original name?

Upvotes: 6

Views: 3592

Answers (2)

hmatar
hmatar

Reputation: 2419

libstdc++ has a nice demangling library, just include cxxabi.h Then you can change Function *call = m->getFunction("_Z3addv");

to

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

Upvotes: 1

AlexDenisov
AlexDenisov

Reputation: 4117

You may reuse Mangler from LLVMCore.

Here is an example of usage:

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

Upvotes: 6

Related Questions