[lug] C++ name mangling

Tom Tromey tromey at cygnus.com
Wed May 24 23:23:02 MDT 2000


>>>>> ">" == D Stimits <stimits at idcomm.com> writes:

>> I'm in search of detailed information on the name mangling during
>> creation of C++ dynamic libraries. Although I could wrap portions
>> extern "C", this would cause problems in some areas.

Believe me, you almost certainly don't want to do this.  `extern "C"'
wrapper functions really are the way to go.  Or, you can do the moral
equivalent, like JNI does, and use vectors of function pointers.

One reason you don't want to know the mangling is (I believe) that the
current g++ name mangler is known to have some bugs.  This means you
will have to be bug compatible.  But if the bugs are fixed, you're
screwed.

Also, there is going to be a C++ ABI change "someday".  The new ABI is
being implemented in g++ right now.  You can see part of it, I think
(I'm not 100% sure of the status) if you use `-fsquangle'.  (I think
the new ABI is slated to come with gcc 3.0, but you'd have to check to
make sure.  I only really pay attention to the changes that affect
me.)

Finally, if you want to port to non-Linux platforms, you have to be
aware that different C++ compilers have different mangling schemes.

>> One of the original authors of glibc (Eric Youngdale) indicated
>> that the scheme should work, but did not know where to find
>> functions or information on dealing with name mangling in
>> g++. However, he did think that there are probably some
>> undocumented functions available which might aid me in either
>> predicting the mangled symbol name in the library, or else the
>> decoding of mangled symbols to get the original class/method
>> information. Does anyone here know of name mangling/demangling
>> functions, or information on where I might find what I need to
>> implement these myself?

Of course there is name mangling code in g++.  It is probably
intimately tied to the compiler.  Also, it is GPL, so it might not be
licensed suitably for you, depending on your applications.

There is demangling code in libiberty.  This code is a bit more
modular.  However, I wouldn't exactly say that you could read this
code and figure out how the mangling works.  The demangling code in
libiberty handles several different kinds of mangling.

You can find libiberty in several places: gcc, binutils, gdb.

The g++ mangling might be documented.  I don't recall.

Tom




More information about the LUG mailing list