Skip to content

Commit 0ca28c3

Browse files
committed
Auto merge of rust-lang#97571 - ehuss:symbol-mangling, r=michaelwoerister
Add documentation on v0 symbol mangling. This adds official documentation for the v0 symbol mangling format, migrating the documentation from [RFC 2603](https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html). The format was originally stabilized as the `-C symbol-mangling-version` option, but the specifics were not stabilized (per rust-lang#90128 (comment)). Per the discussion at rust-lang#93661 (comment) this adds those specifics as an official description of the format. cc rust-lang#89917
2 parents 0699d99 + da4f62e commit 0ca28c3

File tree

5 files changed

+1283
-2
lines changed

5 files changed

+1283
-2
lines changed

src/doc/rustc/book.toml

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ title = "The rustc book"
66
[output.html]
77
git-repository-url = "https://github.com/rust-lang/rust/tree/master/src/doc/rustc"
88
edit-url-template = "https://github.com/rust-lang/rust/edit/master/src/doc/rustc/{path}"
9+
10+
[output.html.playground]
11+
runnable = false

src/doc/rustc/src/SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@
5959
- [Instrumentation-based Code Coverage](instrument-coverage.md)
6060
- [Linker-plugin-based LTO](linker-plugin-lto.md)
6161
- [Exploit Mitigations](exploit-mitigations.md)
62+
- [Symbol Mangling](symbol-mangling/index.md)
63+
- [v0 Symbol Format](symbol-mangling/v0.md)
6264
- [Contributing to `rustc`](contributing.md)

src/doc/rustc/src/codegen-options/index.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,15 @@ for the purpose of generating object code and linking.
569569

570570
Supported values for this option are:
571571

572-
* `v0` — The "v0" mangling scheme. The specific format is not specified at
573-
this time.
572+
* `v0` — The "v0" mangling scheme.
574573

575574
The default, if not specified, will use a compiler-chosen default which may
576575
change in the future.
577576

577+
See the [Symbol Mangling] chapter for details on symbol mangling and the mangling format.
578+
578579
[name mangling]: https://en.wikipedia.org/wiki/Name_mangling
580+
[Symbol Mangling]: ../symbol-mangling/index.md
579581

580582
## target-cpu
581583

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)