emscripten-core / emscripten-core/emscripten

Removal of C++ name mangling is not correctly applied

Open
#14,205 12 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

In wasm backend, someone decided that the traditional C++ name mangling would not be applied. This has caused numerous issues into the toolchain and to the users of the toolchain in the past. After #13477 I was hopeful that would have been the last of it, but here we go again:

```c++
#include

class Foo
{
public:
Foo(int i, int j)
{
EM_ASM(console.log($0 + ' ' + $1), i, j);
}
};

int main()
{
Foo f(1,2);
}
```

Run with
```
$ em++ a.cpp -c -o a.o
$ em++ a.o -o a.html -g2
$ llvm-nm a.o
0000001b W _ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJiiEEE6bufferE
0000005f W _ZN3FooC2Eii
00000000 W _ZZN3FooC1EiiE1x
00000001 T __original_main
U __stack_pointer
U emscripten_asm_const_int
000000f9 T main

$ wasm-opt --nm a.wasm
__wasm_call_ctors : 1
__original_main : 40
Foo::Foo\28int\2c\20int\29 : 60
main : 5
stackSave : 1
stackRestore : 2
stackAlloc : 9
emscripten_stack_init : 9
emscripten_stack_get_free : 3
emscripten_stack_get_end : 1
__lockfile : 1
__unlockfile : 1
__lock : 1
__unlock : 1
__ofl_lock : 4
__ofl_unlock : 2
fflush : 82
__fflush_unlocked : 49
__errno_location : 1
warning: no output file specified, not emitting output
```

We see that when the code is still at the object file stage, the C++ name mangling is still applied, and e.g. the constructor `Foo::Foo` has a name `_ZN3FooC2Eii`. After the linking has occurred, the constructor instead has changed its name to `Foo::Foo\28int\2c\20int\29`.

This is preventing our tooling from analyzing which object files contribute which symbols (for size analysis) into the final output.

Is there a way that we could restore the proper C++ name mangling like the way it used to be? Maybe with a custom flag? This has been a major source of headache. Alternatively, is there a way that the symbols in .o files would have unmangled names also? (or maybe both solutions?)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.