pre-RFC: a new symbol mangling scheme


This is a proposal for a new name mangling scheme for the Rust compiler. It should solve the known problems of the existing scheme (see the “Motivation” part). The pre-RFC version does not yet include the strict technical description but the “Guide Level Explanation” should give a good idea what it looks like. It would be great to hear what people think; whether it makes sense to fully flesh out the remaining details or if we should take a different approach altogether.

  • Feature Name: symbol_name_mangling_v2
  • Start Date: 2018-10-01
  • RFC PR: (leave this empty)
  • Rust Issue: (leave this empty)


This RFC proposes a new mangling scheme that describes what the symbol names for everything generated by the Rust compiler look like. This new scheme has a number of advantages over the existing one which has grown over time without a clear direction. The new scheme is consistent, does not depend on compiler internals, and the information it stores in symbol names can be decoded again which provides an improved experience for users of external tools that work with Rust symbol names. The new scheme is based on the name mangling scheme from the Itanium C++ ABI.


Due to its ad-hoc nature, the compiler’s current name mangling scheme has a number of drawbacks:

  • It depends on compiler internals and its results cannot be replicated by another compiler implementation or external tool.
  • Information about generic parameters and other things is lost in the mangling process. One cannot extract the type arguments of a monomorphized function from its symbol name.
  • The current scheme is inconsistent: most paths use Itanium style encoding, but some of them don’t.
  • The symbol names it generates can contain . characters which is not generally supported on all platforms. [1][2][3]

The proposed scheme solves these problems:

  • It is defined in terms of the language, not in terms of compiler data-structures that can change at any given point in time.
  • It encodes information about generic parameters in a reversible way.
  • It has a consistent definition that does not rely on pretty-printing certain language constructs.
  • It generates symbols that only consist of the characters A-Z, a-z, 0-9, _, and $.

This should make it easier for third party tools to work with Rust binaries.

Guide-level explanation

The following section will lay out the requirements for a name mangling scheme and then introduce the actual scheme through a series of ever more complex examples.

Requirements for a Symbol Mangling Scheme

A symbol mangling scheme has a few goals, one of them essential, the rest of them desirable. The essential one is:

  • The scheme must provide an unambiguous string encoding for everything that can end up in a binary’s symbol table.

“Unambiguous” means that no two distinct compiler-generated entities (that is, mostly object code for functions) must be mapped to the same symbol name. This disambiguation is the main purpose of the hash-suffix in the current, legacy mangling scheme. The scheme proposed here, on the other hand, achieves it in a way that allows to also satisfy a number of additional desirable properties of a mangling scheme:

  • A mangled symbol should be decodable to some degree. That is, it is desirable to be able to tell which exact concrete instance of e.g. a polymorphic function a given symbol identifies. This is true for external tools, backtraces, or just people only having the binary representation of some piece of code available to them. With the current scheme, this kind of information gets lost in the magical hash-suffix.

  • It should be possible to predict the symbol name for a given source-level construct. For example, given the definition fn foo<T>() { ... }, the scheme should allow to construct, by hand, the symbol names for e.g. foo<u32> or foo<extern fn(i32, &mut SomeStruct<(char, &str)>, ...) -> !>(). Since the current scheme generates its hash from the values of various compiler internal data structures, not even an alternative compiler implementation could predicate the symbol name even for simple cases.

  • A mangling should be platform-independent. This is mainly achieved by restricting the character set to A-Z, a-z, 0-9, _, and $. All other characters might have special meaning in some context (e.g. . for MSVC DEF files) or are simply not supported (e.g. Unicode).

  • The scheme should be efficient, meaning that the symbols it produces are not unnecessarily long (because that takes up space in object files and means more work for compiler and linker) and that generating a symbol should not be too computationally expensive.

Note that a source-level definition can contain components that will not show up in symbol names, like lifetimes (as in fn foo<'a>()). It is an explicit non-goal of this RFC to define a mangling for cases like the above. One might want to cover them “for completeness” but they are not actually needed.

The Mangling Scheme by Example

This section will develop an overview of the mangling scheme by walking through a number of examples. We’ll start with the simplest case – and see how that already involves things that might be surprising.

Free-standing Functions and Statics

A free-standing function is fully identified via its absolute path. For example, the following function

mod foo {
  fn bar() {}

has the path foo::bar and N3foo3barE is a mangling of that path that complies to the character set we are restricted to. Why this format with numbers embedded in it? It is the encoding that the Itanium C++ ABI name mangling scheme uses for “nested names” (i.e. paths). The scheme proposed here will also use this format.

However, the symbol name above does not unambiguously identify the function in every context. It is perfectly valid for another crate to also define mod foo { fn bar() {} } somewhere. So in order to avoid conflicts in such cases, fully qualified names always include the crate name and disambiguator, as in N15mycrate_4a3b56d3foo3barE (the crate disambiguator is used to disambiguate different versions of the same crate. It is an existing concept and not introduced by this RFC).

There is one more possible ambiguity that we have to take care of: Rust has two distinct namespaces: the type and the value namespace. This leads to a path of the form crate_id::foo::bar not uniquely identifying the item bar because the following snippet is legal Rust code:

fn foo() {
    fn bar() {}

mod foo {
    fn bar() {}

The function foo lives in the value namespaces while the module foo lives in the type namespace. They don’t interfere. In order to make the symbol names for the two distinct bar functions unique, we thus add a suffix to name components in the value namespace, so case one would get the symbol name N15mycrate_4a3b56d3fooF3barFE and case two get the name N15mycrate_4a3b56d3foo3barFE (notice the difference: 3fooF vs 3foo).

As opposed to C++ and other languages that support function overloading, we don’t need to include the argument types in the symbol name. Rust does not allow two functions of the same name but different arguments.

The final symbol name for the function would also include the prefix _R that is common to all symbol names generated by this scheme:


   |             |
prefix   fully qualified name

Generic Functions

Each monomorphization of a generic function has its own symbol name. The monomorphizations are disambiguated by the list of concrete generic arguments. These arguments are listed as suffix, starting with I, after the name they belong to. So the instance


would be mangled to

     start of argument list ---+|+--- end of argument list

where I starts the list of arguments, d designates f64 and E ends the argument list. As we can see, we need to be able to represent all kinds of types that can be part of such an argument list. (In the future we might also need to represent values when const generics get added to the language.) These kinds of types are:

  • basic types (char, (), str, !, i8, i16, …)
  • reference and pointers types, shared and mut
  • tuples
  • arrays, with and without fixed size (e.g. [u8], [u8; 17], or as part of a slice type &[char])
  • structs, enums, closures, and other named types, possibly with their own set of type arguments
  • function types such as fn(&i32) -> u16

Basic types are all encoded via a single lower-case letter, like in the Itanium scheme. Named types are encoded as their fully qualified name (plus arguments) like is done for function symbols. Composites like references, tuples, and function types all follow simple grammar given in the reference-level explanation below. Here are some examples manglings to get a general feel of what they look like:

  • std::mem::align_of::<usize>: _RN12std_a1b2c3d43mem8align_ofFIjEE
  • std::mem::align_of::<&char>: _RN12std_a1b2c3d43mem8align_ofFIRcEE
  • std::mem::align_of::<std::mem::Discriminant>: _RN12std_a1b2c3d43mem8align_ofFIN12std_a1b2c3d43mem12DiscriminantEEE
  • std::mem::align_of::<&mut (&str,())>: _RN12std_a1b2c3d43mem8align_ofFIWTRrvEEE

There’s one more thing we have to take into account for generic functions: The compiler may produce “crate-local” copies of a monomorphization. That is, if there is a function foo<T> which gets used as foo<u32> in two different crates, the compiler (depending on the optimization level) might generate two distinct functions at the LLVM IR level, each with it’s own symbol. In order to support this without running into conflicts, symbol names for monomorphizations must include the id of the crate they are instantiated for. This scheme does this by appending an $<crate-id> suffix to the symbol. So for example the mangling for std::mem::align_of::<usize> would actually look like this:

_RN12std_a1b2c3d43mem8align_ofFIjEE$foo_a1b2c3d4 (for crate "foo/a1b2c3d4")
_RN12std_a1b2c3d43mem8align_ofFIjEE$bar_11223344 (for crate "bar/11223344")

Closures and Closure Environments

The scheme needs to be able to generate symbol names for the function containing the code of a closure and it needs to be able to refer to the type of a closure if it occurs as a type argument. As closures don’t have a name, we need to generate one. The scheme takes a simple approach here: Each closure gets assigned an index (unique within the item defining it) and from that we generate a name of the form c$<index>. The $ makes sure that the name cannot clash with user-defined names. The full name of a closure is then constructed like for any other named item:

mod foo {
  fn bar(x: u32) {
    let a = |x| { x + 1 }; // ~ c$0
    let b = |x| { x + 2 }; // ~ c$1


In the above example we have two closures, the one assigned to a and the one assigned to b. The first one would get the local name c$0 and the second one the name c$1. Their full names would then be N15mycrate_4a3b56d3foo3barF3c$0FE and N15mycrate_4a3b56d3foo3barF3c$1FE respectively. The type of their environment would be the same, except for not having the F suffix to their local name.

Inherent Methods

Inherent methods (that is, methods that are not part of a trait implementation) are represented by a symbol of the form:

_RM <self-type> <method-name> [<generic-arguments>] [<instantiating-crate>]

The M designates the symbol as an inherent method. The self-type is encoded like any other type argument and already contains the concrete type arguments of the impl defining the method. The method name is unique among all inherent methods for the given type, so we don’t need to further qualify it. The method can have type arguments of its own. These are encoded like other argument lists as I <type>+ E. If the method is generic in any way, it will also need the instantiating crate suffix, like any other generic item.

Here’s an example for a non-generic method:

mod foo {
  struct Bar;

  impl Bar {
    pub fn panic_please() { panic!() }

The resulting symbol name looks like:


            self-type          method name

A method with a generic self-type is a bit longer, since it also contains the instantiating-crate-suffix:

mod foo {
  struct Bar<T>;

  impl<T> Bar<T> {
    pub fn panic_please() { panic!() }

The symbol for foo::Bar<char>::panic_please would look like this:

             self-type            method name      instantiating crate

Trait Methods

Trait methods are similar to inherent methods, but in addition to the self-type the symbol name must also contain the trait being implemented:

_RX <trait-name-and-params> <self-type> <method-name> [<generic-arguments>] [<instantiating-crate>]

The X signifies that this is a trait method. The trait being implemented is encoded N <name-component>+ [I<generic-param>+ E] E, like a named type. Here is a complex example with generics in all the places:

mod foo {
  trait Foo<T> {
    fn id<T>(x: T) -> T;

mod bar {
  struct Bar<T>;

mod baz {
  impl<T1, T2> Foo<T1> for Bar<T2>  {
    fn id<T>(x: T) -> T { x }

The mangling for <Bar<char> as Foo<isize>>::id::<usize> would be:

               trait                        self-type          method    instantitating crate

One thing that’s interesting here is that baz, the module the impl is situated in, does not show up anywhere in the mangled name.

Items Within Specialized Trait Impls

In Rust one can define items within generic items, e.g. functions or impls, like in the following example:

fn foo<T>(a: T) -> (u32, T) {
  static mut X: u32 = 0;

  unsafe {
    X += 1;
    (X, a)

The X here (or any other such nested definition) does not inherit the generic context. X is non-generic, and a function defined in its place would be too. Consequently, when giving the path to something defined within a generic item, one does not specify the generic arguments because they add no information. The fully qualified name of X is thus my_crate/a1b2c3d4::foo::X and its symbol name:


However, there is at least one case where the type arguments do matter for a defintion like this, and that is when impl specialization is used. Consider the following piece of code:

trait Foo<T> {
  fn foo() -> T;

struct Bar<T>(T);

impl<T> Foo<T> for Bar<T> {
  default fn foo() -> T {
    static MSG: &str = "sry, no can do";
    panic!("{}", MSG)

impl<T: Default> Foo<T> for Bar<T> {
  fn foo() -> T {
    static MSG: &str = "it's a go!";
    println!("{}", MSG);

Notice that both MSG statics have the path <Bar as Foo>::foo::MSG if you just leave off the type arguments. However, we also don’t have any concrete types to substitute the arguments for. Therefore, we have to encode the type parameters and their bounds for cases like this so that the symbol name will be a mangling of something like <Bar<T> as Foo<T>>::foo::MSG where T: Clone:


   -------------------------------------- "where clause"
    -- T
      ---------------------------------- bounds to T
       -------------------------------- std::default::Default
                                         --------------------------- Foo<T>
                                                                    --------------------------- Bar<T>
                                                                                           foo ----
                                                                                               MSG ----

Compiler-generated Items (Drop-Glue, Shims, etc)

The compiler generates a number of things that can end up needing an entry in the symbol table:

  • Drop-glue is what recursively calls Drop::drop() for components of composite type. Generating symbol names for it is straightforward. They are of the form _RG<type> where <type> is the usual mangling as used for generic arguments.

  • Various “shims”, that is, compiler-generated implementations of built-in traits like Fn, FnMut, FnOnce, or Clone, or for dynamic dispatch via trait objects. These are similar in structure to drop glue. Their precise mangling is specified in the reference-level explanation below.

Unicode Identifiers

Rust allows unicode identifiers but our character set is restricted to ASCII alphanumerics, _, and $. In order to transcode the former to the latter, we use the same approach as Swift, which is: encode all identifiers via Punycode, a standardized and efficient encoding that keeps encoded strings in a rather human-readable format. So for example, the string

"Gödel, Escher, Bach"

is encoded as

"Gdel, Escher, Bach-d3b"

which, as opposed to something like Base64, still gives a pretty good idea of what the original string looked like.

Each component of a name, i.e. anything that starts with the number of bytes to read in the examples above, is encoded individually. Components encoded this way also start with the number of bytes to read, but that number is prefixed with a 0. As an example, the function:

mod gödel {
  mod escher {
    fn bach() {}

would be mangled as:

                unicode component


The length of symbol names has an influence on how much work compiler, linker, and loader have to perform. The shorter the names, the better. At the same time, Rust’s generics can lead to rather long names (which are often not visible in the code because of type inference and impl Trait). For example, the return type of the following function:

fn quux(s: Vec<u32>) -> impl Iterator<Item=(u32, usize)> {
     .map(|x| x+1)
     .filter(|&x| x > 10)
     .chain(iter::once((0, 0)))


        [closure@src/ 16:18]>,
      [closure@src/ 17:25]>,
  std::iter::Once<(u32, usize)>>

It would make for a symbol name if this types is used (maybe repeatedly) as a generic argument somewhere. C++ has the same problem with its templates; which is why the Itanium mangling introduces the concept of compression. If a component of a definition occurs more than once, it will not be repeated and instead be emitted as a substitution marker that allows to reconstruct which component it refers to. The scheme proposed here will use the same approach.

The exact scheme will be described in detail in the reference level explanation below but it roughly works as follows: As a mangled symbol name is being built or parsed, we build up a dictionary of “substitutions”, that is we keep track of things a subsequent occurrence of which could be replaced by a substitution marker. The substitution marker is then the lookup key into this dictionary. The things that are eligible for substitution are (1) all prefixes of qualified names (including the entire name itself) and (2) all types except for basic types. If a substitutable item is already present in the dictionary it does not generate a new key. Here’s an example in order to illustrate the concept:

    std::iter::Chain<std::iter::Zip<std::vec::IntoIter<u32>, std::vec::IntoIter<u32>>>
$0: ---
$1: ---------
$2: ----------------
$3:                  --------------
$4:                                 --------
$5:                                 ------------------
$6:                                 -----------------------
$7:                  ----------------------------------------------------------------
$8: ----------------------------------------------------------------------------------

The indices on the left are the dictionary keys. The prefixes std, std::iter, and std::iter::Chain all get added to the dictionary because we have not seen them before. After that we encounter std again. We’ve already seen it, so we don’t add anything to the dictionary. The same goes for when we encounter std::iter the second time. Next we encounter std::iter::Zip, which we have not seen before, so it’s added to the dictionary. Next we encounter std again (already seen, no insertion), then std::vec and std::vec::IntoIter which both generate a new entry. Next we see std::vec::IntoIter<u32>, the first full type. It generates an entry too. The second type parameter is the same as the first. No part of it introduces a new entry. After the next > we have completely processed std::iter::Zip<std::vec::IntoIter<u32>, std::vec::IntoIter<u32>>, which adds another type entry. Finally, the full std::iter::Chain<std::iter::Zip<std::vec::IntoIter<u32>, std::vec::IntoIter<u32>>> adds another entry.

Using the dictionary above, we can compress to:

std::iter::Chain<$1::Zip<$0::vec::IntoIter<u32>, $6>>

A couple of things to note:

  • The first occurrence of a dictionary entry is never substituted. We don’t store the dictionary anywhere and need to be able to reconstruct it from the compressed version.
  • Longer substitutions are preferred to shorter ones. std::iter::Chain<$1::Zip<$0::vec::IntoIter<u32>, $4::IntoIter<u32>>> would also decompress to the original version but the compiler is supposed to always pick the longest substitution available.

The mangled version of a substitution marker is S <key - 1> _ (and S_ for key 0) like it in Itanium mangling. So the above definition would be mangled to:


The uncompressed version would be:


Reference-level explanation

This is still to be done. Not part of the pre-RFC.


Why should we not do this?

  • The scheme is rather complex, especially due to compression (albeit not more complex than prior art)
  • The current/legacy scheme based on symbol-hashes is flexible in that hashes can be changed at will. That is, the unstable part of the current scheme mangling is nicely contained and does not keep breaking external tools. The danger of breakage is greater with the scheme proposed here because it exposes more information.

Rationale and alternatives

The alternatives considered are:

  • Keeping the current scheme. It does meet the minimum requirements after all. It also has pretty big downsides.
  • Keeping the current scheme but cleaning it up by making the non-hash part more consistent and more expressive. Keep the hash part as a safe guard against symbol conflicts and the rest as something just for demangling. The downside of this is that the hash would still not be predictable, and symbols would get rather long if they should contain more human-readable information about generic arguments.
  • Define a standardized pretty-printing format for things that end up as symbols, and then encode that via Punycode in order to meet the character set restrictions. This would be rather simple. Symbol names would remain somewhat human-readable (but not very, because all separators would be stripped out). But without some kind of additional compression, symbol names would become rather long.
  • Use the scheme from the previous bullet point but apply the compression scheme described above. We could do this but it wouldn’t really be less complex than the Itanium inspired scheme proposed above.

The Itanium mangling (and by extension the scheme proposed here) could be considered somewhat arcane. But it is well-known from C++ and provides a good trade-off between readability, complexity, and length of generated symbols.

Prior art

The mangling scheme described here is an adaptation of the Itanium C++ ABI scheme, which is the scheme used by the GCC toolchain (and clang when it’s not compiling for MSVC). In fact, the scheme proposed here tries to stay as close as possible to Itanium mangling and only deviates where something does not make sense for Rust.

One notable improvement the proposed scheme makes upon Itanium mangling is explicit handling of unicode identifiers. The idea of using Punycode for this is taken from the Swift programming language’s mangling scheme (which is also based on Itanium mangling).

Unresolved questions

  • Should we introduce a St substitution for the ::std:: to the compression scheme (like Itanium does). This would preclude mixing symbols from different versions of the standard library into a single binary (we’d not have a crate disambiguator for libstd). It’s unclear whether that can occur in practice.
  • Similar to the above, common items, like built-in bounds, could get predefined abbreviations.
  • Is the compression scheme unambiguous? That is, is it always clear which substitutions the compiler should choose? (a reference implementation of the algorithm will solve this)
  • Is the scheme for disambiguating specialized impls sound?
  • Should symbols include information that might help during debugging/analyzing a program but that is not strictly necessary for avoiding name conflicts? Examples of such information would be names and types of function parameters or the ABI of functions.
  • Should named items (everything of the form N...E) not start with N but instead with something that gives a hint of what it is? E.g. F for functions, S for statics, C for closures, etc? This is not needed for disambiguation but it would add more information to the symbol name without really increasing the complexity of the scheme or the length of names. (Although it makes compression a bit less straightforward to describe.)

Appendix - Interesting Examples

TODO (not part of the pre-RFC)

  • specializing impls
  • impl Trait
  • closure environment as a type parameter
  • various examples of compression


Thanks for doing this. +1 from me. In gdb it would be good if Rust manglings were immediately recognizable from the symbol name, so that Rust-specific demangling can be used even in the absence of debuginfo. This proposal would solve that problem.

According to the GCC sources, the AIX assembler does not support $ in symbol names. Not sure if this also a linker restriction. That platform does support ., which GCC uses as a fallback; I suppose rustc could do likewise if this is ever necessary. The other GCC ports not supporting $ seem very obscure, like MMIX. So on the whole I think your plan is fine.


According to the GCC sources, the AIX assembler does not support $ in symbol names.

It shouldn’t be hard to get rid of the $ symbol.


This is good, a huge part of having any kind of stable ABI!

Would it make sense to treat the self type just as the first trait parameter? This is probably just a bike shed color but it seems a little simpler and maybe more general.


Yes, that might sense. Then we could express most things as a kind of generalized path of the form:

 ("X"|"M"|"N") (<name-component> [<generic-params>])+ "E"


Why isn’t it the mangled name of core::ptr::drop_in_place::<type>? Likewise, the other shims could use the name of the function that they’re shimming, it’s not like there’s a “real” version that they can collide with.


This will be HUGE for tooling and profilers! (eg for Twiggy’s monos sub-command)


Yes, that’s one of the main motivations for changing the current scheme. I’m glad it’s appreciated :)


Maybe this is a non-goal, but I kind of expected that this would let tools like c++filt work on Rust names as-is. This doesn’t seem to work with any of your examples though, even changing the _R prefix to _Z.


Because ptr::drop_in_place wasn’t thing yet the last time I dealt with drop-glue :) You’re right of course.

I’ll look into it.


To what extent is the goal here to stabilize a symbol mangling scheme and thus make it a part of “the spec”? Is the goal here just to replace the current unstable scheme with another, but better, scheme?

I’m wary of giving any stable guarantees that all Rust compilers must adhere to and which tools can expect will always hold. If this is just improving the unstable scheme then I have no objections.


I think the main problem here is the two namespaces Rust has but C++ doesn’t (or at least not in this form). For manglings that only describe things in the type namespace (which won’t occur in practice), c++filt should work. E.g. _RN12std_a1b2c3d43foo3FooIN12std_a1b2c3d43bar3BarIiEEEE should demangle to std_a1b2c3d4::foo::Foo<std_a1b2c3d4::bar::Bar<int>> (modulo the _R).

Other problems would be the different set of basic types and things like the instantiating-crate suffix. The scheme described above could probably be brought closer to pure Itanium mangling in some places but actual compatibility is not a goal, I’d say.


I wouldn’t want to commit to this yet. Maybe after a year of testing, or so. But this RFC would not stabilize the scheme as something to be prescribed for all implementations of Rust.


Would it make sense to standardise a version number somewhere, such that the mangling scheme could evolve, but tools could get an accurate demangling from only the binary (assuming they are aware of all versions)?


So to clarify, a year of testing is unlikely to be enough here I think. My concern about committing to anything is that it makes it difficult to evolve the language with things such as const generics, higher kinded types, type-generic bounds (for<T: Debug>) and various unknown unknowns. That said, @nicoburns’s notion of versioning is interesting – what are your thoughts on that?

Great; so long as this is the case the language team need not be involved and T-compiler can deal with this alone (which improves the chances of adoption).


Out of curiosity, could you elaborate a little on why stabilizing an ABI is something that Rust is so reluctant to do? Could there not be ABI “Editions” kind of like Rust Language Editions where backwards incompatible changes to ABI are introduced over time?

EDIT: Never mind. I see that you already have a reply to someone else along these lines. Also, someone else mentioned “Versioning” as well, so, nothing new here. Please move along. :slight_smile:


Why? This is what inline linkage is for.

This is silly. We should just use utf-8 in our symbols, like the C++ compilers do ( (note that MSVC’s output is broken due to compiler explorer, not MSVC))


How about use Puny-Code on back-ends/platforms that don’t support utf-8 identifiers and use utf-8 on those that do as part of the spec?


If that’s necessary, but if LLVM is our backend shouldn’t they take care of it? On any system with an OS, it shouldn’t be a big deal, because C++ has already trod this road.


That shouldn’t be assumed by the spec should it?