No_mangle crate-level attribute

Hello, Rust is an incredible language and I think it has a great potential in many software industries, especially in the embedded's one. But at the moment rustc is producing a really awful assembly, and I understand we have no choice. The biggest problem for me is the name mangling. I think Rust lacks a no_mangling crate-level attribute, which would disable any name mangling for all symbols that are not generated for tricky languages features. I guess it would be possible for example, a code like this:

struct Foo;

impl Foo {
    pub fn new() -> Self {
        Self {}
    }
}

to get something like:

Foo::new:
    ;instructions here
1 Like

What benefit would this provide over tagging the specific items you need?

1 Like

Tagging everything directly would: 1/ Improve code readability, no no_mangle attribute everywhere 2/ Way clearer assembly since the one generated by rustc is so chaotic

It would also come at a cost: by using a crate-level attribute, a reader might think that e.g. generic fns or methods will also not be mangled. But that is impossible, and thus the attribute would be misleading.

There is way clearer ways for a generic function/method to be named than something like _ZN4test4main17hd3e36f845d6357eeE

Does _ZN4test4mainEv work?

Joking asside, Foo::bar isn't a valid symbol name on all platforms. How would this work if the unmangled name could not possibly be used on the platform?

4 Likes

It was an example, there is many ways to format symbols clearer than name mangling

Symbols aren't supposed to be human readable by default. Mangled names are an efficient, backwards compatible, representation allowing symbols with the same stem names to differ. The #[no_mangle] attribute spceifically makes the symbol name equal to the stem or declaration name, you're propsing something that has different behaviour (it keeps name scoping intact, but in a more "readable form") If you have a better name mangling/name decoration scheme, that may be reasonable to pitch, but I'd argue that's a compiler-domain thing, not a language-domain thing (rust the language does not currently have any kind of rule imposing mangling).

If you want these names just for human use of generated assembly, this already can be done. I believe rustc has a demangle option, and many disassembles do.

6 Likes

Is your goal about linking or about readable assembly? If it's merely about readability, what about a tool similar to c++filt that demangles mangled symbol names in whatever file/stream you give it? (EDIT: rustfilt)