Skip to content

Latest commit

 

History

History
62 lines (39 loc) · 2.54 KB

File metadata and controls

62 lines (39 loc) · 2.54 KB

Mangling function names

We conducted a study to clearly understand the structure of mangled function names and how to demangle those names.

Study results

In Rust, two versions are available for the option for mangling symbols: legacy and v0. Currently, the default mangling version is legacy. With this version, a mangled symbol name contains a hash value generated based on the module path on which the function was defined and the type information.

You can use rustfilt, a demangling tool in Rust, to demangle symbols of the legacy and v0 mangling versions.

Details

Mangling versions

Two mangling versions are available in Rust: legacy and v0. The default version is legacy. The v0 version is proposed in RFC2603 and can be specified with the symbol-mangling-version option.

Structure of symbol mangled with legacy version

The structure of a symbol mangled with the legacy version is shown below.

legacy-mangling

Structure of symbol mangled with the v0 version

The structure of a symbol mangled with the v0 version is shown below: The difference from the legacy version is that information on generic parameters is not lost. v0 Symbol Format details the v0 version.

v0-mangling

How to demangle symbols

The symbol of a function name is kept mangled. The symbol shown below is obtained by monomorphizing the function fn test_add<T>(a: T, b: T) -> T where T: std::ops::Add<Output = T> with i32.

[legacy]
_ZN3no88test_add17he991e478e44b9c5fE

[v0]
_RINvCsg9W1Qrgvbiz_3no88test_addlEB2_

You can use rustfilt provided with Rust Toolchain to demangle symbols mangled as shown above, regardless of their mangling version. An example is shown below that uses rustfilt to demangle the function names shown above.

PS> rustfilt '_ZN3no88test_add17he991e478e44b9c5fE'
no8::test_add

PS> rustfilt '_RINvCsg9W1Qrgvbiz_3no88test_addlEB2_'
no8::test_add::<i32>

Differences for 32-bit and size-minimized binaries

Even for 32-bit binaries, the structure of mangled symbols is the same as 64-bit binaries. For the same function, however, we found differences in the hashed value between 32-bit and 64-bit binaries. We can presume that the type information of the function may be concerned with creation of the hash value. For size-minimized binaries, mangled symbols are not written into a pdb file.