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
55
2
56
3
The v0 mangling format was introduced in [ RFC 2603] .
57
4
It has the following properties:
@@ -78,7 +25,7 @@ There is no standardized demangled form of the symbols,
78
25
though suggestions are provided for how to demangle a symbol.
79
26
Implementers may choose to demangle in different ways.
80
27
81
- ### Grammar notation
28
+ ## Grammar notation
82
29
83
30
The format of an encoded symbol is illustrated as a context free grammar in an extended BNF-like syntax.
84
31
A 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].
93
40
| Option | <sub >opt</sub > | <nobr >A → * B* <sub >opt</sub > * C* </nobr > | An optional element. |
94
41
| Literal | ` monospace ` | <nobr >A → ` G ` </nobr > | A terminal matching the exact characters case-sensitive. |
95
42
96
- ### Symbol name
43
+ ## Symbol name
97
44
[ symbol-name ] : #symbol-name
98
45
99
46
> 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]*.
128
75
>
129
76
> Recommended demangling : `<std :: path :: PathBuf >:: new `
130
77
131
- ### Symbol path
78
+ ## Symbol path
132
79
[path ]: #symbol - path
133
80
134
81
> path → \
@@ -156,7 +103,7 @@ The initial tag character can be used to determine which kind of path it represe
156
103
| `I ` | * [generic - args ]* | Generic arguments . |
157
104
| `B ` | * [backref ]* | A back reference . |
158
105
159
- #### Path : Crate root
106
+ ### Path : Crate root
160
107
[crate - root ]: #path - crate - root
161
108
162
109
> crate - root → `C ` * [identifier ]*
@@ -196,7 +143,7 @@ the *[disambiguator]* is used to make the name unique across the crate graph.
196
143
>
197
144
> Recommended demangling : `mycrate :: example `
198
145
199
- #### Path : Inherent impl
146
+ ### Path : Inherent impl
200
147
[inherent - impl ]: #path - inherent - impl
201
148
202
149
> 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
230
177
>
231
178
> Recommended demangling : `<mycrate :: Example >:: foo `
232
179
233
- #### Path : Trait impl
180
+ ### Path : Trait impl
234
181
[trait - impl ]: #path - trait - impl
235
182
236
183
> 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
268
215
>
269
216
> Recommended demangling : `<mycrate :: Example as mycrate :: Trait >:: foo `
270
217
271
- #### Path : Impl
218
+ ### Path : Impl
272
219
[impl - path ]: #path - impl
273
220
274
221
> impl - path → * [disambiguator ]* <sub >opt </ sub > * [path ]*
@@ -316,7 +263,7 @@ The *[disambiguator]* can be used to distinguish between multiple impls within t
316
263
> * `foo `: `<mycrate :: Example >:: foo `
317
264
> * `bar `: `<mycrate :: Example >:: bar `
318
265
319
- #### Path : Trait definition
266
+ ### Path : Trait definition
320
267
[trait - definition ]: #path - trait - definition
321
268
322
269
> trait - definition → `Y ` * [type ]* * [path ]*
@@ -350,7 +297,7 @@ It consists of the character `Y` followed by the *[type]* which is the `Self` ty
350
297
>
351
298
> Recommended demangling : `<mycrate :: Example as mycrate :: Trait >:: example `
352
299
353
- #### Path : Nested path
300
+ ### Path : Nested path
354
301
[nested - path ]: #path - nested - path
355
302
356
303
> nested - path → `N ` * [namespace ]* * [path ]* * [identifier ]*
@@ -415,7 +362,7 @@ For example, entities like closures, tuple-like struct constructors, and anonymo
415
362
> * `x `: `mycrate :: main :: {closure #0 }`
416
363
> * `y `: `mycrate :: main :: {closure #1 }`
417
364
418
- #### Path : Generic arguments
365
+ ### Path : Generic arguments
419
366
[generic - args ]: #path - generic - arguments
420
367
[generic - arg ]: #path - generic - arguments
421
368
@@ -462,7 +409,7 @@ Each *[generic-arg]* is either a *[lifetime]* (starting with the character `L`),
462
409
>
463
410
> Recommended demangling : `mycrate :: example :: <i32 , 1 >`
464
411
465
- #### Namespace
412
+ ### Namespace
466
413
[namespace ]: #namespace
467
414
468
415
> namespace → * [lower ]* | * [upper ]*
@@ -482,7 +429,7 @@ Uppercase namespaces are:
482
429
>
483
430
> See * [nested - path ]* for recommended demangling .
484
431
485
- ### Identifier
432
+ ## Identifier
486
433
[identifier ]: #identifier
487
434
[undisambiguated - identifier ]: #identifier
488
435
[bytes ]: #identifier
@@ -515,7 +462,7 @@ The `_` is mandatory if the *bytes* starts with a decimal digit or `_` in order
515
462
>
516
463
> The * [disambiguator ]* may or may not be displayed ; see recommendations for rules that use * identifier* .
517
464
518
- #### Punycode identifiers
465
+ ### Punycode identifiers
519
466
[Punycode identifiers]: #punycode- identifiers
520
467
521
468
Because some environments are restricted to ASCII alphanumerics and `_`,
@@ -565,7 +512,7 @@ Here are some examples:
565
512
566
513
[Punycode ]: https: // tools.ietf.org/html/rfc3492
567
514
568
- ### Disambiguator
515
+ ## Disambiguator
569
516
[disambiguator]: #disambiguator
570
517
571
518
> disambiguator → `s` * [base- 62- number]*
@@ -582,7 +529,7 @@ This allows disambiguators that are encoded sequentially to use minimal bytes.
582
529
>
583
530
> The * disambiguator* may or may not be displayed; see recommendations for rules that use * disambiguator* .
584
531
585
- ### Lifetime
532
+ ## Lifetime
586
533
[lifetime]: #lifetime
587
534
588
535
> lifetime → `L ` * [base- 62- number]*
@@ -632,7 +579,7 @@ Indices starting from 1 refer (as de Bruijn indices) to a higher-ranked lifetime
632
579
>
633
580
> Recommended demangling : `mycrate :: example :: <for <'a , 'b > fn (& 'a u8 , & 'b u16 )>`
634
581
635
- ### Const
582
+ ## Const
636
583
[const ]: #const
637
584
[const - data ]: #const
638
585
[hex - digit ]: #const
@@ -695,7 +642,7 @@ The encoding of the *const-data* depends on the type:
695
642
> Recommended demangling : `mycrate :: example :: <305419896 >`
696
643
697
644
698
- ### Type
645
+ ## Type
699
646
[type ]: #type
700
647
[basic - type ]: #basic - type
701
648
[array - type ]: #array - type
@@ -881,7 +828,7 @@ The type encodings based on the initial tag character are:
881
828
>
882
829
> Recommended demangling : `mycrate :: example :: <[u16 ; 8]>`
883
830
884
- ### Binder
831
+ ## Binder
885
832
[binder ]: #binder
886
833
887
834
> 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
903
850
> A * binder * may be printed using `for <…>` syntax listing the lifetimes as recommended in * [lifetime ]* .
904
851
> See * [lifetime ]* for an example .
905
852
906
- ### Backref
853
+ ## Backref
907
854
[backref ]: #backref
908
855
909
856
> backref → `B ` * [base - 62 - number ]*
@@ -954,7 +901,7 @@ This is ensured by not allowing optional or repeating elements at the end of sub
954
901
>
955
902
> Recommended demangling : `mycrate :: example :: <mycrate :: Example , mycrate :: Example >`
956
903
957
- ### Instantiating crate
904
+ ## Instantiating crate
958
905
[instantiating - crate ]: #instantiating - crate
959
906
960
907
> instantiating - crate → * [path ]*
@@ -988,7 +935,7 @@ so it is usually encoded as a *[backref]* to the *[crate-root]* encoded elsewher
988
935
>
989
936
> Recommended demangling : `<std :: path :: Path >:: new :: <str >`
990
937
991
- ### Vendor - specific suffix
938
+ ## Vendor - specific suffix
992
939
[vendor - specific - suffix ]: #vendor - specific - suffix
993
940
[suffix ]: #vendor - specific - suffix
994
941
@@ -1030,7 +977,7 @@ the suffixed name has the same semantics as the original.
1030
977
>
1031
978
> Recommended demangling : `mycrate :: EXAMPLE :: __getit :: __KEY`
1032
979
1033
- ### Common rules
980
+ ## Common rules
1034
981
[decimal - number ]: #common - rules
1035
982
[digit ]: #common - rules
1036
983
[lower ]: #common - rules
@@ -1054,7 +1001,7 @@ A *digit* is an ASCII number.
1054
1001
1055
1002
A * lower * and * upper * is an ASCII lower and uppercase letter respectively .
1056
1003
1057
- ### base - 62 - number
1004
+ ## base - 62 - number
1058
1005
[base - 62 - number ]: #base - 62 - number
1059
1006
1060
1007
> [base - 62 - number ] → { * [digit ]* | * [lower ]* | * [upper ]* } `_ `
@@ -1088,7 +1035,7 @@ Examples:
1088
1035
| 63 | `10_ ` |
1089
1036
| 1000 | `g7_ ` |
1090
1037
1091
- ### Symbol grammar summary
1038
+ ## Symbol grammar summary
1092
1039
[summary ]: #symbol - grammar - summary
1093
1040
1094
1041
The 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.
1189
1136
> [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 ` \
1190
1137
> [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 `
1191
1138
1192
- ### Encoding of Rust entities
1139
+ ## Encoding of Rust entities
1193
1140
1194
1141
The following are guidelines for how Rust entities are encoded in a symbol .
1195
1142
The compiler has some latitude in how an entity is encoded as long as the symbol is unambiguous .
0 commit comments