demangle¶
Read mangled symbol names in pure Python. Itanium C++ (GCC, Clang), MSVC, Rust, Swift, Objective-C, Go, D, Nim, Free Pascal, Delphi and C++Builder, Ada/GNAT, JNI, and the pre-Itanium C++ families (g++ 2.x, cfront/ARM, Lucid, HP aCC, CodeWarrior), with no dependencies.
>>> import demangle
>>> demangle.demangle("_ZNSt6vectorIiSaIiEE9push_backERKi")
'std::vector<int, std::allocator<int>>::push_back(int const&)'
>>> demangle.demangle("?f@@YAXH@Z")
'void __cdecl f(int)'
>>> demangle.demangle("_ZN4core3fmt9Formatter3pad17h9b2b3a0e5b4d1b31E")
'core::fmt::Formatter::pad'
>>> demangle.demangle("$s10Foundation4DataV5countSivg")
'Foundation.Data.count.getter : Swift.Int'
>>> demangle.demangle("@Unit@Class@Method$qqrv")
'__fastcall Unit::Class::Method()'
>>> demangle.demangle("CreateFileW") # not mangled, so returned as it is
'CreateFileW'
- Pure Python, everywhere. CPython 3.11+, PyPy, and the browser through Pyodide.
On CPython for Linux, macOS and Windows, pip picks a wheel whose Itanium parser is
compiled by mypyc from the same source, about twice as fast; everything else gets the
pure wheel, as does
pip install --no-binary demangle demangle. Nothing to compile at install time. - A tree, not just a string.
parse()returns nodes to walk andsignature()the parts of a name, so nothing has to take a C++ declaration apart with a regular expression. - Measured against the references. Every scheme is scored against its reference
demangler (
llvm-cxxfilt, GNUc++filt,llvm-undname,rustc-demangle,swift-demangleand others) or, where there is none, against what the compiler recorded, over checked-in corpora and about 2,500,000 real symbols. - Safe on hostile input. Recursion depth, output size, substitution count and input
length are bounded while a name is read, and
demangle()never raises over a name.
Install¶
pip install demangle
Quick start¶
demangle() returns the spelling, or the name unchanged when nothing reads it, so it
is safe to run over every symbol in a binary. When you need more than the string:
>>> demangle.parse("_ZNK3Foo3barIiEEvPKc").spell() # a tree; raises if unreadable
'void Foo::bar<int>(char const*) const'
>>> demangle.signature("_ZNSt6vectorIiSaIiEE9push_backERKi").base_name
'push_back'
>>> demangle.demangle_text("0000000000001139 T _ZN3foo3barEv") # every symbol in a line
'0000000000001139 T foo::bar()'
>>> demangle.demangle("_ZNSt6vectorIiSaIiEE9push_backERKi", style="gnu")
'std::vector<int, std::allocator<int> >::push_back(int const&)'
The command is a filter like c++filt, over any mix of schemes:
$ nm -D libfoo.so | demangle
$ demangle '?f@@YAXH@Z' _RNvC6_123foo3bar
$ nm -D libfoo.so | demangle --json-lines # one JSON object per symbol
Scripts for IDA Pro, Binary Ninja and Ghidra relabel a database with it, and the playground runs it in the browser.
Where next¶
-
Paste a name and read it in the browser. Nothing leaves the page.
-
Every entry point, the styles and options, and the command line.
-
A real question answered with
parse(), next to the regex that gets it wrong. -
Scripts that relabel an IDA Pro, Binary Ninja or Ghidra database.
-
Signatures and docstrings.
-
What each scheme is checked against, and every number.