Crate rustc_symbol_mangling

source ·
Expand description

§The Rust Linkage Model and Symbol Names

The semantic model of Rust linkage is, broadly, that “there’s no global namespace” between crates. Our aim is to preserve the illusion of this model despite the fact that it’s not quite possible to implement on modern linkers. We initially didn’t use system linkers at all, but have been convinced of their utility.

There are a few issues to handle:

  • Linkers operate on a flat namespace, so we have to flatten names. We do this using the C++ namespace-mangling technique. Foo::bar symbols and such.

  • Symbols for distinct items with the same name need to get different linkage-names. Examples of this are monomorphizations of functions or items within anonymous scopes that end up having the same path.

  • Symbols in different crates but with same names “within” the crate need to get different linkage-names.

  • Symbol names should be deterministic: Two consecutive runs of the compiler over the same code base should produce the same symbol names for the same items.

  • Symbol names should not depend on any global properties of the code base, so that small modifications to the code base do not result in all symbols changing. In previous versions of the compiler, symbol names incorporated the SVH (Stable Version Hash) of the crate. This scheme turned out to be infeasible when used in conjunction with incremental compilation because small code changes would invalidate all symbols generated previously.

  • Even symbols from different versions of the same crate should be able to live next to each other without conflict.

In order to fulfill the above requirements the following scheme is used by the compiler:

The main tool for avoiding naming conflicts is the incorporation of a 64-bit hash value into every exported symbol name. Anything that makes a difference to the symbol being named, but does not show up in the regular path needs to be fed into this hash:

  • Different monomorphizations of the same item have the same path but differ in their concrete type parameters, so these parameters are part of the data being digested for the symbol hash.

  • Rust allows items to be defined in anonymous scopes, such as in fn foo() { { fn bar() {} } { fn bar() {} } }. Both bar functions have the path foo::bar, since the anonymous scopes do not contribute to the path of an item. The compiler already handles this case via so-called disambiguating DefPaths which use indices to distinguish items with the same name. The DefPaths of the functions above are thus foo[0]::bar[0] and foo[0]::bar[1]. In order to incorporate this disambiguation information into the symbol name too, these indices are fed into the symbol hash, so that the above two symbols would end up with different hash values.

The two measures described above suffice to avoid intra-crate conflicts. In order to also avoid inter-crate conflicts two more measures are taken:

  • The name of the crate containing the symbol is prepended to the symbol name, i.e., symbols are “crate qualified”. For example, a function foo in module bar in crate baz would get a symbol name like baz::bar::foo::{hash} instead of just bar::foo::{hash}. This avoids simple conflicts between functions from different crates.

  • In order to be able to also use symbols from two versions of the same crate (which naturally also have the same name), a stronger measure is required: The compiler accepts an arbitrary “disambiguator” value via the -C metadata command-line argument. This disambiguator is then fed into the symbol hash of every exported item. Consequently, the symbols in two identical crates but with different disambiguators are not in conflict with each other. This facility is mainly intended to be used by build tools like Cargo.

§A note on symbol name stability

Previous versions of the compiler resorted to feeding NodeIds into the symbol hash in order to disambiguate between items with the same path. The current version of the name generation algorithm takes great care not to do that, since NodeIds are notoriously unstable: A small change to the code base will offset all NodeIds after the change and thus, much as using the SVH in the hash, invalidate an unbounded number of symbol names. This makes re-using previously compiled code for incremental compilation virtually impossible. Thus, symbol hash generation exclusively relies on DefPaths which are much more robust in the face of changes to the code base.


  • Errors emitted by symbol_mangling.
  • hashed 🔒
  • legacy 🔒
  • Walks the crate looking for items/impl-items/trait-items that have either a rustc_symbol_name or rustc_def_path attribute and generates an error giving, respectively, the symbol name or def-path. This is used for unit testing the code that generates paths etc in all kinds of annoying scenarios.
  • v0 🔒