[LLVMdev] How to get the original function name in C++?

Nick Lewycky
2014-12-11T19:35:30Z

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