| 1 | # `cpp_demangle`: a C++ linker symbol demangler |
| 2 | |
| 3 | [](https://docs.rs/cpp_demangle/) [ ](https://crates.io/crates/cpp_demangle) [](https://github.com/gimli-rs/cpp_demangle/actions) |
| 4 | |
| 5 | This crate can parse a C++ “mangled” linker symbol name into a Rust value |
| 6 | describing what the name refers to: a variable, a function, a virtual table, |
| 7 | etc. The description type implements functions such as `demangle()`, |
| 8 | producing human-readable text describing the mangled name. Debuggers and |
| 9 | profilers can use this crate to provide more meaningful output. |
| 10 | |
| 11 | C++ requires the compiler to choose names for linker symbols consistently across |
| 12 | compilation units, so that two compilation units that have seen the same |
| 13 | declarations can pair up definitions in one unit with references in another. |
| 14 | Almost all platforms other than Microsoft Windows follow the |
| 15 | [Itanium C++ ABI][itanium]'s rules for this. |
| 16 | |
| 17 | [itanium]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling |
| 18 | |
| 19 | For example, suppose a C++ compilation unit has the definition: |
| 20 | |
| 21 | namespace space { |
| 22 | int foo(int x, int y) { return x+y; } |
| 23 | } |
| 24 | |
| 25 | The Itanium C++ ABI specifies that the linker symbol for that function must be |
| 26 | named `_ZN5space3fooEii`. This crate can parse that name into a Rust value |
| 27 | representing its structure. That Rust value can be `demangle()`d to the |
| 28 | string `space::foo(int, int)`, which is more meaningful to the C++ |
| 29 | developer. |
| 30 | |
| 31 | ## Usage |
| 32 | |
| 33 | Add `cpp_demangle` to your crate's `Cargo.toml`: |
| 34 | |
| 35 | ```toml |
| 36 | [dependencies] |
| 37 | cpp_demangle = "0.5.1" |
| 38 | ``` |
| 39 | |
| 40 | And then demangle some C++ symbols! |
| 41 | |
| 42 | ```rust |
| 43 | extern crate cpp_demangle; |
| 44 | use cpp_demangle::Symbol; |
| 45 | use std::string::ToString; |
| 46 | |
| 47 | let mangled = b"_ZN5space3fooEibc"; |
| 48 | |
| 49 | let sym = Symbol::new(&mangled[..]) |
| 50 | .expect("Could not parse mangled symbol!"); |
| 51 | |
| 52 | let demangled = sym.demangle().unwrap(); |
| 53 | assert_eq!(demangled, "space::foo(int, bool, char)"); |
| 54 | ``` |
| 55 | |
| 56 | ### `no_std` Support |
| 57 | |
| 58 | `cpp_demangle` may be configured for working in `no_std` environments that still |
| 59 | have allocation support via the `alloc` crate. This is nightly rust only, at the |
| 60 | moment, since the `alloc` crate's collections aren't stabilized. |
| 61 | |
| 62 | Disable the "std" feature, and enable the "alloc" feature: |
| 63 | |
| 64 | ```toml |
| 65 | [dependencies] |
| 66 | cpp_demangle = { |
| 67 | version = "0.5.1", |
| 68 | default-features = false, |
| 69 | features = ["alloc"] |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | ## Documentation |
| 74 | |
| 75 | [Documentation on docs.rs](https://docs.rs/cpp_demangle) |
| 76 | |
| 77 | Example programs: |
| 78 | |
| 79 | * [A `c++filt` clone.](./examples/cppfilt.rs) |
| 80 | |
| 81 | Install it locally with this command: |
| 82 | |
| 83 | ``` |
| 84 | cargo install cpp_demangle --example cppfilt |
| 85 | ``` |
| 86 | |
| 87 | ## Implementation Status |
| 88 | |
| 89 | Work is ongoing. While `cpp_demangle` can parse every mangled symbol in |
| 90 | `libiberty`'s demangler's test suite (the canonical Itanium C++ symbol demangler |
| 91 | used by GNU tools such as `c++filt`), it does not format all of them |
| 92 | character-for-character identically. I'm working on fixing that ;) |
| 93 | |
| 94 | Despite that, I believe `cpp_demangle` is fairly robust. I've been |
| 95 | running [AFL][] on `cpp_demangle` overnight and it hasn't found any panics for a |
| 96 | long time now (and never found any crashes -- thanks Rust!). |
| 97 | |
| 98 | [AFL]: https://github.com/rust-fuzz/afl.rs |
| 99 | |
| 100 | ## License |
| 101 | |
| 102 | Licensed under either of |
| 103 | |
| 104 | * Apache License, Version 2.0 ([`LICENSE-APACHE`](./LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0) |
| 105 | * MIT license ([`LICENSE-MIT`](./LICENSE-MIT) or https://opensource.org/licenses/MIT) |
| 106 | |
| 107 | at your option. |
| 108 | |
| 109 | ## Contribution |
| 110 | |
| 111 | See [CONTRIBUTING.md](./CONTRIBUTING.md) for hacking. |
| 112 | |
| 113 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 114 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 115 | dual licensed as above, without any additional terms or conditions. |