I want to add support for Rust symbol demangling to Bloaty McBloatface, my generic ELF/Mach-O size profiling tool.
I was really happy to see Bloaty providing some useful results on a Rust binary yesterday. Torste Aikio wrote a proof-of-concept patch to Bloaty to add native Rust symbol demangling, and he opened an issue on Bloaty’s GitHub to discuss.
I was hoping that I could add Rust demangling to Bloaty without requiring my users to put any Rust-specific switch on their command-line. I wanted to do something like:
if (RustDemangle(sym, &rust_demangled)) {
demangled = rust_demangled;
} else if (CppDemangle(sym, &cpp_demangled)) {
demangled = cpp_demangled;
} else {
demangled = sym;
}
My theory was that a symbol wouldn’t successfully demangle as both Rust and C++ – that the set of demanglable symbols would be disjoint. Unfortunately it appears this is not the case. It appears that Rust intentionally uses a scheme similar to C++, to get reasonable output with tools that don’t support Rust.
I can see why that would provide a smoother experience for tools that support C++ but not Rust. Unfortunately it makes it harder to provide dedicated/first-class Rust support in a tool like Bloaty, especially for mixed C++/Rust binaries. If some symbols are Rust and others are C++, ideally both could be demangled properly on a symbol-by-symbol basis.
I haven’t looked in-depth at Rust’s mangling scheme. Is there anything that could give me a hint about whether a given symbol is actually Rust or C++?