- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Add documentation on v0 symbol mangling. #97571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      5d61dca
              
                Add documentation on v0 symbol mangling.
              
              
                ehuss d5d4619
              
                Rearrange symbol-mangling chapter out of codegen-options.
              
              
                ehuss ddd26b4
              
                Remove 64-bit limit for base-62-numbers.
              
              
                ehuss d782e87
              
                Update from review from michaelwoerister.
              
              
                ehuss 769f938
              
                Clarify missing tick.
              
              
                ehuss 9e0c19d
              
                Clarify grammar for decimal-number cannot have leading zeroes.
              
              
                ehuss 0e66b00
              
                Rewrite recommended demangling for lifetimes using "De Bruijn level".
              
              
                ehuss b2d401f
              
                Add an example of placeholders.
              
              
                ehuss ceda03d
              
                Add missing word "the".
              
              
                ehuss d376e63
              
                Add description of forwards-compatible behavior.
              
              
                ehuss da4f62e
              
                Fix span for punnycode
              
              
                ehuss File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Symbol Mangling | ||
|  | ||
| [Symbol name mangling] is used by `rustc` to encode a unique name for symbols that are used during code generation. | ||
| The encoded names are used by the linker to associate the name with the thing it refers to. | ||
|  | ||
| The method for mangling the names can be controlled with the [`-C symbol-mangling-version`] option. | ||
|  | ||
| [Symbol name mangling]: https://en.wikipedia.org/wiki/Name_mangling | ||
| [`-C symbol-mangling-version`]: ../codegen-options/index.md#symbol-mangling-version | ||
|  | ||
| ## Per-item control | ||
|  | ||
| The [`#[no_mangle]` attribute][reference-no_mangle] can be used on items to disable name mangling on that item. | ||
|  | ||
| The [`#[export_name]`attribute][reference-export_name] can be used to specify the exact name that will be used for a function or static. | ||
|  | ||
| Items listed in an [`extern` block][reference-extern-block] use the identifier of the item without mangling to refer to the item. | ||
| The [`#[link_name]` attribute][reference-link_name] can be used to change that name. | ||
|  | ||
| <!-- | ||
| FIXME: This is incomplete for wasm, per https://github.com/rust-lang/rust/blob/d4c364347ce65cf083d4419195b8232440928d4d/compiler/rustc_symbol_mangling/src/lib.rs#L191-L210 | ||
| --> | ||
|  | ||
| [reference-no_mangle]: ../../reference/abi.html#the-no_mangle-attribute | ||
| [reference-export_name]: ../../reference/abi.html#the-export_name-attribute | ||
| [reference-link_name]: ../../reference/items/external-blocks.html#the-link_name-attribute | ||
| [reference-extern-block]: ../../reference/items/external-blocks.html | ||
|  | ||
| ## Decoding | ||
|  | ||
| The encoded names may need to be decoded in some situations. | ||
| For example, debuggers and other tooling may need to demangle the name so that it is more readable to the user. | ||
| Recent versions of `gdb` and `lldb` have built-in support for demangling Rust identifiers. | ||
| In situations where you need to do your own demangling, the [`rustc-demangle`] crate can be used to programmatically demangle names. | ||
| [`rustfilt`] is a CLI tool which can demangle names. | ||
|  | ||
| An example of running rustfilt: | ||
|  | ||
| ```text | ||
| $ rustfilt _RNvCskwGfYPst2Cb_3foo16example_function | ||
| foo::example_function | ||
| ``` | ||
|  | ||
| [`rustc-demangle`]: https://crates.io/crates/rustc-demangle | ||
| [`rustfilt`]: https://crates.io/crates/rustfilt | ||
|  | ||
| ## Mangling versions | ||
|  | ||
| `rustc` supports different mangling versions which encode the names in different ways. | ||
| The legacy version (which is currently the default) is not described here. | ||
| The "v0" mangling scheme addresses several limitations of the legacy format, | ||
| and is described in the [v0 Symbol Format](v0.md) chapter. | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.