|
| 1 | +# Symbol Mangling |
| 2 | + |
| 3 | +[Symbol name mangling] is used by `rustc` to encode a unique name for symbols that are used during code generation. |
| 4 | +The encoded names are used by the linker to associate the name with the thing it refers to. |
| 5 | + |
| 6 | +The method for mangling the names can be controlled with the [`-C symbol-mangling-version`] option. |
| 7 | + |
| 8 | +[Symbol name mangling]: https://en.wikipedia.org/wiki/Name_mangling |
| 9 | +[`-C symbol-mangling-version`]: ../codegen-options/index.md#symbol-mangling-version |
| 10 | + |
| 11 | +## Per-item control |
| 12 | + |
| 13 | +The [`#[no_mangle]` attribute][reference-no_mangle] can be used on items to disable name mangling on that item. |
| 14 | + |
| 15 | +The [`#[export_name]`attribute][reference-export_name] can be used to specify the exact name that will be used for a function or static. |
| 16 | + |
| 17 | +Items listed in an [`extern` block][reference-extern-block] use the identifier of the item without mangling to refer to the item. |
| 18 | +The [`#[link_name]` attribute][reference-link_name] can be used to change that name. |
| 19 | + |
| 20 | +<!-- |
| 21 | +FIXME: This is incomplete for wasm, per https://github.com/rust-lang/rust/blob/d4c364347ce65cf083d4419195b8232440928d4d/compiler/rustc_symbol_mangling/src/lib.rs#L191-L210 |
| 22 | +--> |
| 23 | + |
| 24 | +[reference-no_mangle]: ../../reference/abi.html#the-no_mangle-attribute |
| 25 | +[reference-export_name]: ../../reference/abi.html#the-export_name-attribute |
| 26 | +[reference-link_name]: ../../reference/items/external-blocks.html#the-link_name-attribute |
| 27 | +[reference-extern-block]: ../../reference/items/external-blocks.html |
| 28 | + |
| 29 | +## Decoding |
| 30 | + |
| 31 | +The encoded names may need to be decoded in some situations. |
| 32 | +For example, debuggers and other tooling may need to demangle the name so that it is more readable to the user. |
| 33 | +Recent versions of `gdb` and `lldb` have built-in support for demangling Rust identifiers. |
| 34 | +In situations where you need to do your own demangling, the [`rustc-demangle`] crate can be used to programmatically demangle names. |
| 35 | +[`rustfilt`] is a CLI tool which can demangle names. |
| 36 | + |
| 37 | +An example of running rustfilt: |
| 38 | + |
| 39 | +```text |
| 40 | +$ rustfilt _RNvCskwGfYPst2Cb_3foo16example_function |
| 41 | +foo::example_function |
| 42 | +``` |
| 43 | + |
| 44 | +[`rustc-demangle`]: https://crates.io/crates/rustc-demangle |
| 45 | +[`rustfilt`]: https://crates.io/crates/rustfilt |
| 46 | + |
| 47 | +## Mangling versions |
| 48 | + |
| 49 | +`rustc` supports different mangling versions which encode the names in different ways. |
| 50 | +The legacy version (which is currently the default) is not described here. |
| 51 | +The "v0" mangling scheme addresses several limitations of the legacy format, |
| 52 | +and is described in the [v0 Symbol Format](v0.md) chapter. |
0 commit comments