How to get the original function name in C++?

Hi, everyone!
I’m new here trapped by a simple problem for days.
When LLVM translates C++ source code to IR, it will add a prefix to the function name. For example:
source code:

int foo(){
return 1;
}

IR form:

define i32 @_Z3foov() #0 {
entry:
ret i32 1, !dbg !20
}

The getName() method returns _Z3foov, then how can I get foo? I know the debugging information is contained in metadata, but I’ve no idea on using it.
Thanks a lot for any help.

This is called name mangling, and what you want is a demangler. c++filt is a program that does this.

If you're looking for a library function you can call that will do this for you, there's __cxa_demangle https://mentorembedded.github.io/cxx-abi/abi.html#demangler which should be provided by your c++ abi library. Clang might have a more convenient wrapper around that or it's own separate implementation, but I'm not sure where that would be.

Cheers,

Jon

In C++ names are "mangled" when translated to machine instructions. This allows for function overloading and such.

Are you looking for something you can call inside of llvm to get the demangled name? At the command line you can use C++flit to get the demangled name. For example:

~$ c++filt _Z3foov
foo()

When a C++ compiler translates source code it will perform name mangling to avoid name collisions due to type overloading. You can use a tool like c++filt to de-mangle the generated names

Cheers,
  Roel

If you want to get the original name by a library function, as Jonathan mentioned, you can call __cxa_demangle in cxxabi.h. However, this API is only available in gcc. If you want something more portable, try glog or libibert, notice libibert is GPL licensed.

zy jj wrote:

Hi, everyone!
     I'm new here trapped by a simple problem for days.
     When LLVM translates C++ source code to IR, it will add a prefix to
the function name. For example:
source code:
int foo(){
return 1;
}
IR form:
define i32 @_Z3foov() #0 {
entry:
   ret i32 1, !dbg !20
}
     The getName() method returns _Z3foov, then how can I get foo? I
know the debugging information is contained in metadata, but I've no
idea on using it.
     Thanks a lot for any help.

You got a few answers about name mangling, but you also asked about debug info. If you want to find the original name through debug info instead of demangling the mangled name, the relevant subset of debug info works like this.

!llvm.dbg.cu = !{!0}

"CU" is short for "compilation unit", the DWARF[1] term for what C and C++ call a translation unit, a single .c/.cc file with all its includes. The only CU in this .ll file is !0.

!0 = metadata !{metadata !"0x11\004\00clang version 3.6.0 (trunk 223918)\000\00\000\00\001", metadata !1, metadata !2, metadata !2, metadata !3, metadata !2, metadata !2} ; [ DW_TAG_compile_unit ] [/home/nicholas/zyjj.cc] [DW_LANG_C_plus_plus]

On the one hand our debug info format is free to change, but on the other hand it closely follows the DWARF spec. You can construct an llvm::DICompileUnit and pass the the MDNode* for !0, then query its getSubprograms() method to find what DWARF calls "subprograms" or what C/C++ call "functions". It returns a DIArray. For reference, the subprograms field is field 3 (zero-indexed) of the compile_unit, so it's "metadata !3".

!3 = metadata !{metadata !4}

Our array of subprograms, with one subprogram. Note that this need not include all subprograms; methods may only be listed in the DW_TAG_class_type for example.

!4 = metadata !{metadata !"0x2e\00foo\00foo\00_Z3foov\001\000\001\000\000\00256\000\001", metadata !1, metadata !5, metadata !6, null, i32 ()* @_Z3foov, null, null, metadata !2} ; [ DW_TAG_subprogram ] [line 1] [def] [foo]

You can construct a DISubprogram with this, and a DISubprogram has a few kinds of names. It has a "linkage name" which is the name that you need to know in order to link against it, aka. the mangled name. The other two names are called "name" and "display name". DWARF does define what a name should be, "a string representing the name as it appears in the source program" and goes on to clarify that it should be the unmangled form. Get it by calling DISubprogram::getName().

However, nowhere does DWARF define what a "display name" is. Taking a quick look through clang, I can't see where we ever set it differently from "name". If anyone else knows this, please let me know.

Nick

[1] - http://dwarfstd.org/Dwarf4Std.php

Welson Sun wrote:

If you want to get the original name by a library function, as Jonathan
mentioned, you can call __cxa_demangle in cxxabi.h. However, this API is
only available in gcc.

Huh? It's defined in the C++ ABI standard: http://mentorembedded.github.io/cxx-abi/abi.html#demangler

There are implementations in libstdc++, libcxxabi and libcxxrt.

  If you want something more portable, try glog or

Nick is right. Sorry for the confusion. I am actually trying to find one demangle library (for Itanium C++ ABI) that is not GPL, and can be compiled with Visual C++. I am currently using the one in glog, but just bumped into a bug which failed on demangling a function name.

libcxxabi says only support OSX, I compiled it fine with xcode 6.1, but Linux gcc-4.8.0 emits a lot of errors, haven’t tried Visual C yet.

Anybody tried libstdc++, libcxxrt with Visual C++ yet?

-Welson