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` ] : 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 below] ( #v0-mangling-format ) .
53-
54- ## v0 mangling format
1+ # v0 Symbol Format
552
563The v0 mangling format was introduced in [ RFC 2603] .
574It has the following properties:
@@ -78,7 +25,7 @@ There is no standardized demangled form of the symbols,
7825though suggestions are provided for how to demangle a symbol.
7926Implementers may choose to demangle in different ways.
8027
81- ### Grammar notation
28+ ## Grammar notation
8229
8330The format of an encoded symbol is illustrated as a context free grammar in an extended BNF-like syntax.
8431A consolidated summary can be found in the [ Symbol grammar summary] [ summary ] .
@@ -93,7 +40,7 @@ A consolidated summary can be found in the [Symbol grammar summary][summary].
9340| Option | <sub >opt</sub > | <nobr >A → * B* <sub >opt</sub > * C* </nobr > | An optional element. |
9441| Literal | ` monospace ` | <nobr >A → ` G ` </nobr > | A terminal matching the exact characters case-sensitive. |
9542
96- ### Symbol name
43+ ## Symbol name
9744[ symbol-name ] : #symbol-name
9845
9946> symbol-name → ` _R ` * [ decimal-number] * <sub >opt</sub > * [ path] * * [ instantiating-crate] * <sub >opt</sub > * [ vendor-specific-suffix] * <sub >opt</sub >
@@ -128,7 +75,7 @@ The final part is an optional *[vendor-specific-suffix]*.
12875>
12976> Recommended demangling : `<std :: path :: PathBuf >:: new `
13077
131- ### Symbol path
78+ ## Symbol path
13279[path ]: #symbol - path
13380
13481> path → \
@@ -156,7 +103,7 @@ The initial tag character can be used to determine which kind of path it represe
156103| `I ` | * [generic - args ]* | Generic arguments . |
157104| `B ` | * [backref ]* | A back reference . |
158105
159- #### Path : Crate root
106+ ### Path : Crate root
160107[crate - root ]: #path - crate - root
161108
162109> crate - root → `C ` * [identifier ]*
@@ -196,7 +143,7 @@ the *[disambiguator]* is used to make the name unique across the crate graph.
196143>
197144> Recommended demangling : `mycrate :: example `
198145
199- #### Path : Inherent impl
146+ ### Path : Inherent impl
200147[inherent - impl ]: #path - inherent - impl
201148
202149> inherent - impl → `M ` * [impl - path ]* * [type ]*
@@ -230,7 +177,7 @@ It consists of the character `M` followed by an *[impl-path]* to the impl's pare
230177>
231178> Recommended demangling : `<mycrate :: Example >:: foo `
232179
233- #### Path : Trait impl
180+ ### Path : Trait impl
234181[trait - impl ]: #path - trait - impl
235182
236183> trait - impl → `X ` * [impl - path ]* * [type ]* * [path ]*
@@ -268,7 +215,7 @@ It consists of the character `X` followed by an *[impl-path]* to the impl's pare
268215>
269216> Recommended demangling : `<mycrate :: Example as mycrate :: Trait >:: foo `
270217
271- #### Path : Impl
218+ ### Path : Impl
272219[impl - path ]: #path - impl
273220
274221> impl - path → * [disambiguator ]* <sub >opt </ sub > * [path ]*
@@ -316,7 +263,7 @@ The *[disambiguator]* can be used to distinguish between multiple impls within t
316263> * `foo `: `<mycrate :: Example >:: foo `
317264> * `bar `: `<mycrate :: Example >:: bar `
318265
319- #### Path : Trait definition
266+ ### Path : Trait definition
320267[trait - definition ]: #path - trait - definition
321268
322269> trait - definition → `Y ` * [type ]* * [path ]*
@@ -350,7 +297,7 @@ It consists of the character `Y` followed by the *[type]* which is the `Self` ty
350297>
351298> Recommended demangling : `<mycrate :: Example as mycrate :: Trait >:: example `
352299
353- #### Path : Nested path
300+ ### Path : Nested path
354301[nested - path ]: #path - nested - path
355302
356303> nested - path → `N ` * [namespace ]* * [path ]* * [identifier ]*
@@ -415,7 +362,7 @@ For example, entities like closures, tuple-like struct constructors, and anonymo
415362> * `x `: `mycrate :: main :: {closure #0 }`
416363> * `y `: `mycrate :: main :: {closure #1 }`
417364
418- #### Path : Generic arguments
365+ ### Path : Generic arguments
419366[generic - args ]: #path - generic - arguments
420367[generic - arg ]: #path - generic - arguments
421368
@@ -462,7 +409,7 @@ Each *[generic-arg]* is either a *[lifetime]* (starting with the character `L`),
462409>
463410> Recommended demangling : `mycrate :: example :: <i32 , 1 >`
464411
465- #### Namespace
412+ ### Namespace
466413[namespace ]: #namespace
467414
468415> namespace → * [lower ]* | * [upper ]*
@@ -482,7 +429,7 @@ Uppercase namespaces are:
482429>
483430> See * [nested - path ]* for recommended demangling .
484431
485- ### Identifier
432+ ## Identifier
486433[identifier ]: #identifier
487434[undisambiguated - identifier ]: #identifier
488435[bytes ]: #identifier
@@ -515,7 +462,7 @@ The `_` is mandatory if the *bytes* starts with a decimal digit or `_` in order
515462>
516463> The * [disambiguator ]* may or may not be displayed ; see recommendations for rules that use * identifier* .
517464
518- #### Punycode identifiers
465+ ### Punycode identifiers
519466[Punycode identifiers]: #punycode- identifiers
520467
521468Because some environments are restricted to ASCII alphanumerics and `_`,
@@ -565,7 +512,7 @@ Here are some examples:
565512
566513[Punycode ]: https: // tools.ietf.org/html/rfc3492
567514
568- ### Disambiguator
515+ ## Disambiguator
569516[disambiguator]: #disambiguator
570517
571518> disambiguator → `s` * [base- 62- number]*
@@ -582,7 +529,7 @@ This allows disambiguators that are encoded sequentially to use minimal bytes.
582529>
583530> The * disambiguator* may or may not be displayed; see recommendations for rules that use * disambiguator* .
584531
585- ### Lifetime
532+ ## Lifetime
586533[lifetime]: #lifetime
587534
588535> lifetime → `L ` * [base- 62- number]*
@@ -632,7 +579,7 @@ Indices starting from 1 refer (as de Bruijn indices) to a higher-ranked lifetime
632579>
633580> Recommended demangling : `mycrate :: example :: <for <'a , 'b > fn (& 'a u8 , & 'b u16 )>`
634581
635- ### Const
582+ ## Const
636583[const ]: #const
637584[const - data ]: #const
638585[hex - digit ]: #const
@@ -695,7 +642,7 @@ The encoding of the *const-data* depends on the type:
695642> Recommended demangling : `mycrate :: example :: <305419896 >`
696643
697644
698- ### Type
645+ ## Type
699646[type ]: #type
700647[basic - type ]: #basic - type
701648[array - type ]: #array - type
@@ -881,7 +828,7 @@ The type encodings based on the initial tag character are:
881828>
882829> Recommended demangling : `mycrate :: example :: <[u16 ; 8]>`
883830
884- ### Binder
831+ ## Binder
885832[binder ]: #binder
886833
887834> binder → `G ` * [base - 62 - number ]*
@@ -903,7 +850,7 @@ For example, in `for<'a, 'b> fn(for<'c> fn (...))`, any <em>[lifetime]</em>s in
903850> A * binder * may be printed using `for <…>` syntax listing the lifetimes as recommended in * [lifetime ]* .
904851> See * [lifetime ]* for an example .
905852
906- ### Backref
853+ ## Backref
907854[backref ]: #backref
908855
909856> backref → `B ` * [base - 62 - number ]*
@@ -954,7 +901,7 @@ This is ensured by not allowing optional or repeating elements at the end of sub
954901>
955902> Recommended demangling : `mycrate :: example :: <mycrate :: Example , mycrate :: Example >`
956903
957- ### Instantiating crate
904+ ## Instantiating crate
958905[instantiating - crate ]: #instantiating - crate
959906
960907> instantiating - crate → * [path ]*
@@ -988,7 +935,7 @@ so it is usually encoded as a *[backref]* to the *[crate-root]* encoded elsewher
988935>
989936> Recommended demangling : `<std :: path :: Path >:: new :: <str >`
990937
991- ### Vendor - specific suffix
938+ ## Vendor - specific suffix
992939[vendor - specific - suffix ]: #vendor - specific - suffix
993940[suffix ]: #vendor - specific - suffix
994941
@@ -1030,7 +977,7 @@ the suffixed name has the same semantics as the original.
1030977>
1031978> Recommended demangling : `mycrate :: EXAMPLE :: __getit :: __KEY`
1032979
1033- ### Common rules
980+ ## Common rules
1034981[decimal - number ]: #common - rules
1035982[digit ]: #common - rules
1036983[lower ]: #common - rules
@@ -1054,7 +1001,7 @@ A *digit* is an ASCII number.
10541001
10551002A * lower * and * upper * is an ASCII lower and uppercase letter respectively .
10561003
1057- ### base - 62 - number
1004+ ## base - 62 - number
10581005[base - 62 - number ]: #base - 62 - number
10591006
10601007> [base - 62 - number ] → { * [digit ]* | * [lower ]* | * [upper ]* } `_ `
@@ -1088,7 +1035,7 @@ Examples:
10881035| 63 | `10_ ` |
10891036| 1000 | `g7_ ` |
10901037
1091- ### Symbol grammar summary
1038+ ## Symbol grammar summary
10921039[summary ]: #symbol - grammar - summary
10931040
10941041The following is a summary of all of the productions of the symbol grammar .
@@ -1189,7 +1136,7 @@ The following is a summary of all of the productions of the symbol grammar.
11891136> [lower ] → `a ` | `b ` | `c ` | `d ` | `e ` | `f ` | `g ` | `h ` | `i ` | `j ` | `k ` | `l ` | `m ` | `n ` | `o ` | `p ` | `q ` | `r ` | `s ` | `t ` | `u ` | `v ` | `w ` | `x ` | `y ` | `z ` \
11901137> [upper ] → `A ` | `B ` | `C ` | `D ` | `E ` | `F ` | `G ` | `H ` | `I ` | `J ` | `K ` | `L ` | `M ` | `N ` | `O ` | `P ` | `Q ` | `R ` | `S ` | `T ` | `U ` | `V ` | `W ` | `X ` | `Y ` | `Z `
11911138
1192- ### Encoding of Rust entities
1139+ ## Encoding of Rust entities
11931140
11941141The following are guidelines for how Rust entities are encoded in a symbol .
11951142The compiler has some latitude in how an entity is encoded as long as the symbol is unambiguous .
0 commit comments