abi::__cxa_demangle provides invalid result on non-mangled symbols

Ryan Stone rysto32 at gmail.com
Tue Jun 10 01:48:09 UTC 2014


abi::__cxa_demangle is giving me an invalid result if I pass it a
symbol that is not mangled.  This is causing me problems as in my
application, I'm getting symbol names from libelf and have no way of
know a priori whether a symbol is mangled or not.  The sample program
below demonstrates the problem.

I note that __cxa_demangle seems to work correctly on symbols that
start with an underscore, so I guess that I can use that as a
workaround, but it would be nice if this worked without hackery (plus
I'm not at all confident that all mangled symbols must start with an
underscore).


#include <cxxabi.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
        const char *symbol;
        char *demangled;
        size_t len;
        int status;

        if (argc != 2) {
                fprintf(stderr, "Usage: %s <symbol>\n", argv[0]);
                return (1);
        }

        symbol = argv[1];
        len = 0;
        status = 0;
        demangled = abi::__cxa_demangle(symbol, NULL, &len, &status);

        printf("__cxa_demangle(\"%s\") = \"%s\", status=%d\n", symbol,
             demangled, status);
        return (0);
}

[rstone at rstone-server demangle]uname -a
FreeBSD rstone-server 11.0-CURRENT FreeBSD 11.0-CURRENT #0
r254461+992b524(server_config): Thu May 22 22:19:47 EDT 2014
root at rstone-server:/usr/obj/usr/src/sys/STOCK  amd64
[rstone at rstone-server demangle]./demangle memcmp
__cxa_demangle("memcmp") = "unsigned long", status=0
[rstone at rstone-server demangle]./demangle _start
__cxa_demangle("_start") = "(null)", status=-2


[rstone at rstone-desktop shm]uname -a
Linux rstone-desktop 3.13.0-29-generic #53-Ubuntu SMP Wed Jun 4
21:00:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[rstone at rstone-desktop shm]./demangle
__cxa_demangle("memcmp") = "(null)", status=-2


More information about the freebsd-toolchain mailing list