Getting Mangled Name from Demangled Name

Getting mangled name from demangled name

You can simply use g++ to compile an empty function with the signature you require and extract the name from that. For example:

echo "int f1(char *, int) {} " | g++ -x c++ -S - -o- | grep "^_.*:$" | sed -e 's/:$//'

gives output

_Z2f1Pci

which is I think what you require. Make sure that you include any relevant header files as they will affect the way the symbols are mangled.

How to get swift mangled object name

xcode ships with a command line tool to de-mangle names.

Type in:
swift demangle

Then find your mangled class name..
__TMaC17find_the_treasure15YD_Secret_Class

Then watch it spit out the name..
find_the_treasure.YD_Secret_Class

If you want to observe the mangled names, as you asked, you can put the produced Swift iOS or macOS binary into a dissassembler like Hopper, iDAPro or radare2.

The tools to automatically de-mangle names are already here (refer to Hopper v4.0 Dissassembler).

How to unmangle mangled names of C++ lambdas?

You can use GCC's special abi::__cxa_demangle function:

#include <memory>
#include <cstdlib>
#include <cxxabi.h>
#include <iostream>

// delete malloc'd memory
struct malloc_deleter
{
void operator()(void* p) const { std::free(p); }
};

// custom smart pointer for c-style strings allocated with std::malloc
using cstring_uptr = std::unique_ptr<char, malloc_deleter>;

int main()
{
// special function to de-mangle names
int error;
cstring_uptr name(abi::__cxa_demangle(typeid([]{}).name(), 0, 0, &error));

if(!error)
std::cout << name.get() << '\n';
else if(error == -1)
std::cerr << "memory allocation failed" << '\n';
else if(error == -2)
std::cerr << "not a valid mangled name" << '\n';
else if(error == -3)
std::cerr << "bad argument" << '\n';
}

Output:

main::{lambda()#1}

According to The Documentation this function returns a c-style zero-terminated string allocated using std::malloc which the caller needs to free using std::free. This example uses a smart pointer to free the returned string automatically at the end of the scope.

How to see mangled name of C++ function in Mac Terminal

If you don't have binutils installed, install that package.
This is probably a good place to start: Install binutils on Mac OSX

Then nm a.out should show you the mangled names and nm -C a.out should show you the demangled names.

How reliable is the format of demangled names?


Unreliable.

If you compile with the same compiler on the same OS then you should have some stability — but that is absolutely not guaranteed. ABI changes in name mangling can happen at any time in a compiler’s release cycle.

Individual compiler teams may have some information about this in their documentation. I am not going to look it up. Sorry.

All bets are off if you compile with either different compilers or different operating systems.

For example, LLVM/Clang on Windows comes with a version that uses MSVC as the backend. Consequently, name mangling on the native Windows Clang port is not compatible with the native Linux Clang.

Finally, just running a few tests with your (current) compiler is always a good way to shoot yourself in the foot. As the adage goes, “just because it works on your compiler, today...”

how to get method argument list from mangled name?

You can extract the types of the arguments and result from the method descriptor, but you can't extract the parameter names.

I don't know of a library that will do this extraction, though I'm sure this code has been implemented a few times. It is not difficult to implement yourself. The syntax of a method descriptor string is specified in the Java Virtual Machine Specification - JVMS 4.3.3.



Related Topics



Leave a reply



Submit