gimli-rs/cpp_demangle

Name: cpp_demangle

Owner: gimli-rs

Description: A crate for demangling C++ symbols

Created: 2016-12-07 19:07:37.0

Updated: 2018-05-21 09:21:59.0

Pushed: 2018-05-14 22:19:08.0

Homepage: null

Size: 3322

Language: Rust

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

cpp_demangle: a C++ linker symbol demangler

Build Status

This crate can parse a C++ ?mangled? linker symbol name into a Rust value describing what the name refers to: a variable, a function, a virtual table, etc. The description type implements Display, producing human-readable text describing the mangled name. Debuggers and profilers can use this crate to provide more meaningful output.

C++ requires the compiler to choose names for linker symbols consistently across compilation units, so that two compilation units that have seen the same declarations can pair up definitions in one unit with references in another. Almost all platforms other than Microsoft Windows follow the Itanium C++ ABI's rules for this.

For example, suppose a C++ compilation unit has the definition:

namespace space {
  int foo(int x, int y) { return x+y; }
}

The Itanium C++ ABI specifies that the linker symbol for that function must be named _ZN5space3fooEii. This crate can parse that name into a Rust value representing its structure. Formatting the value with the format! macro or the std::string::ToString::to_string trait method yields the string space::foo(int, int), which is more meaningful to the C++ developer.

Usage

Add cpp_demangle to your crate's Cargo.toml:

endencies]
demangle = "0.2.9"

And then demangle some C++ symbols!

rn crate cpp_demangle;
cpp_demangle::Symbol;
std::string::ToString;

mangled = b"_ZN5space3fooEibc";

sym = Symbol::new(&mangled[..])
.expect("Could not parse mangled symbol!");

demangled = sym.to_string();
rt_eq!(demangled, "space::foo(int, bool, char)");
Documentation

Documentation on docs.rs

Example programs:

Implementation Status

Work is ongoing. While cpp_demangle can parse every mangled symbol in libiberty's demangler's test suite (the canonical Itanium C++ symbol demangler used by GNU tools such as c++filt), it does not format all of them character-for-character identically. I'm working on fixing that ;)

Despite that, I believe cpp_demangle is fairly robust. I've been running AFL on cpp_demangle overnight and it hasn't found any panics for a long time now (and never found any crashes – thanks Rust!).

License

Licensed under either of

at your option.

Contribution

See CONTRIBUTING.md for hacking.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.