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

1,710 views
Skip to first unread message

zy jj

unread,
Dec 11, 2014, 10:22:08 AM12/11/14
to llv...@cs.uiuc.edu
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.


Jonathan Roelofs

unread,
Dec 11, 2014, 10:34:22 AM12/11/14
to zy jj, llv...@cs.uiuc.edu
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
> _______________________________________________
> LLVM Developers mailing list
> LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

--
Jon Roelofs
jona...@codesourcery.com
CodeSourcery / Mentor Embedded
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Bill Seurer

unread,
Dec 11, 2014, 10:39:04 AM12/11/14
to llv...@cs.uiuc.edu
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()

--

-Bill Seurer

Roel Jordans

unread,
Dec 11, 2014, 11:01:05 AM12/11/14
to llv...@cs.uiuc.edu
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

Welson Sun

unread,
Dec 11, 2014, 1:57:24 PM12/11/14
to Roel Jordans, LLVM Developers Mailing List
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.
--
- Welson

Nick Lewycky

unread,
Dec 11, 2014, 2:40:03 PM12/11/14
to zy jj, llv...@cs.uiuc.edu
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

Nick Lewycky

unread,
Dec 11, 2014, 3:29:21 PM12/11/14
to Welson Sun, LLVM Developers Mailing List
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.
> LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu>
> http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/ mailman/listinfo/llvmdev
> <http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>
>
> ______________________________ _________________
> LLVM Developers mailing list
> LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu> http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/ mailman/listinfo/llvmdev

Welson Sun

unread,
Dec 11, 2014, 4:29:17 PM12/11/14
to Nick Lewycky, LLVM Developers Mailing List
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
--
- Welson

Reply all
Reply to author
Forward
0 new messages