Index: test/wasm/cxx-mangling.ll =================================================================== --- test/wasm/cxx-mangling.ll +++ test/wasm/cxx-mangling.ll @@ -1,8 +1,8 @@ ; RUN: llc -filetype=obj %s -o %t.o ; RUN: wasm-ld --export=_Z3fooi --demangle -o %t_demangle.wasm %t.o -; RUN: obj2yaml %t_demangle.wasm | FileCheck %s +; RUN: obj2yaml %t_demangle.wasm | FileCheck --check-prefix=DEMANGLE %s ; RUN: wasm-ld --export=_Z3fooi --no-demangle -o %t_nodemangle.wasm %t.o -; RUN: obj2yaml %t_nodemangle.wasm | FileCheck %s +; RUN: obj2yaml %t_nodemangle.wasm | FileCheck --check-prefix=MANGLE %s target triple = "wasm32-unknown-unknown" @@ -21,46 +21,90 @@ ret void } -; CHECK: - Type: EXPORT -; CHECK-NEXT: Exports: -; CHECK-NEXT: - Name: memory -; CHECK-NEXT: Kind: MEMORY -; CHECK-NEXT: Index: 0 -; CHECK-NEXT: - Name: __heap_base -; CHECK-NEXT: Kind: GLOBAL -; CHECK-NEXT: Index: 1 -; CHECK-NEXT: - Name: __data_end -; CHECK-NEXT: Kind: GLOBAL -; CHECK-NEXT: Index: 2 -; CHECK-NEXT: - Name: _Z3fooi -; CHECK-NEXT: Kind: FUNCTION -; CHECK-NEXT: Index: 2 -; CHECK-NEXT: - Name: _start -; CHECK-NEXT: Kind: FUNCTION -; CHECK-NEXT: Index: 3 -; CHECK-NEXT: - Type: CODE -; CHECK-NEXT: Functions: -; CHECK-NEXT: - Index: 0 -; CHECK-NEXT: Locals: -; CHECK-NEXT: Body: 0B -; CHECK-NEXT: - Index: 1 -; CHECK-NEXT: Locals: -; CHECK-NEXT: Body: 000B -; CHECK-NEXT: - Index: 2 -; CHECK-NEXT: Locals: -; CHECK-NEXT: Body: 0B -; CHECK-NEXT: - Index: 3 -; CHECK-NEXT: Locals: -; CHECK-NEXT: Body: 410110828080800041011081808080000B -; CHECK-NEXT: - Type: CUSTOM -; CHECK-NEXT: Name: name -; CHECK-NEXT: FunctionNames: -; CHECK-NEXT: - Index: 0 -; CHECK-NEXT: Name: __wasm_call_ctors -; CHECK-NEXT: - Index: 1 -; CHECK-NEXT: Name: 'undefined function bar(int)' -; CHECK-NEXT: - Index: 2 -; CHECK-NEXT: Name: 'foo(int)' -; CHECK-NEXT: - Index: 3 -; CHECK-NEXT: Name: _start -; CHECK-NEXT: ... +; DEMANGLE: - Type: EXPORT +; DEMANGLE-NEXT: Exports: +; DEMANGLE-NEXT: - Name: memory +; DEMANGLE-NEXT: Kind: MEMORY +; DEMANGLE-NEXT: Index: 0 +; DEMANGLE-NEXT: - Name: __heap_base +; DEMANGLE-NEXT: Kind: GLOBAL +; DEMANGLE-NEXT: Index: 1 +; DEMANGLE-NEXT: - Name: __data_end +; DEMANGLE-NEXT: Kind: GLOBAL +; DEMANGLE-NEXT: Index: 2 +; DEMANGLE-NEXT: - Name: _Z3fooi +; DEMANGLE-NEXT: Kind: FUNCTION +; DEMANGLE-NEXT: Index: 2 +; DEMANGLE-NEXT: - Name: _start +; DEMANGLE-NEXT: Kind: FUNCTION +; DEMANGLE-NEXT: Index: 3 +; DEMANGLE-NEXT: - Type: CODE +; DEMANGLE-NEXT: Functions: +; DEMANGLE-NEXT: - Index: 0 +; DEMANGLE-NEXT: Locals: +; DEMANGLE-NEXT: Body: 0B +; DEMANGLE-NEXT: - Index: 1 +; DEMANGLE-NEXT: Locals: +; DEMANGLE-NEXT: Body: 000B +; DEMANGLE-NEXT: - Index: 2 +; DEMANGLE-NEXT: Locals: +; DEMANGLE-NEXT: Body: 0B +; DEMANGLE-NEXT: - Index: 3 +; DEMANGLE-NEXT: Locals: +; DEMANGLE-NEXT: Body: 410110828080800041011081808080000B +; DEMANGLE-NEXT: - Type: CUSTOM +; DEMANGLE-NEXT: Name: name +; DEMANGLE-NEXT: FunctionNames: +; DEMANGLE-NEXT: - Index: 0 +; DEMANGLE-NEXT: Name: __wasm_call_ctors +; DEMANGLE-NEXT: - Index: 1 +; DEMANGLE-NEXT: Name: 'undefined function bar(int)' +; DEMANGLE-NEXT: - Index: 2 +; DEMANGLE-NEXT: Name: 'foo(int)' +; DEMANGLE-NEXT: - Index: 3 +; DEMANGLE-NEXT: Name: _start +; DEMANGLE-NEXT: ... + +; MANGLE: - Type: EXPORT +; MANGLE-NEXT: Exports: +; MANGLE-NEXT: - Name: memory +; MANGLE-NEXT: Kind: MEMORY +; MANGLE-NEXT: Index: 0 +; MANGLE-NEXT: - Name: __heap_base +; MANGLE-NEXT: Kind: GLOBAL +; MANGLE-NEXT: Index: 1 +; MANGLE-NEXT: - Name: __data_end +; MANGLE-NEXT: Kind: GLOBAL +; MANGLE-NEXT: Index: 2 +; MANGLE-NEXT: - Name: _Z3fooi +; MANGLE-NEXT: Kind: FUNCTION +; MANGLE-NEXT: Index: 2 +; MANGLE-NEXT: - Name: _start +; MANGLE-NEXT: Kind: FUNCTION +; MANGLE-NEXT: Index: 3 +; MANGLE-NEXT: - Type: CODE +; MANGLE-NEXT: Functions: +; MANGLE-NEXT: - Index: 0 +; MANGLE-NEXT: Locals: +; MANGLE-NEXT: Body: 0B +; MANGLE-NEXT: - Index: 1 +; MANGLE-NEXT: Locals: +; MANGLE-NEXT: Body: 000B +; MANGLE-NEXT: - Index: 2 +; MANGLE-NEXT: Locals: +; MANGLE-NEXT: Body: 0B +; MANGLE-NEXT: - Index: 3 +; MANGLE-NEXT: Locals: +; MANGLE-NEXT: Body: 410110828080800041011081808080000B +; MANGLE-NEXT: - Type: CUSTOM +; MANGLE-NEXT: Name: name +; MANGLE-NEXT: FunctionNames: +; MANGLE-NEXT: - Index: 0 +; MANGLE-NEXT: Name: __wasm_call_ctors +; MANGLE-NEXT: - Index: 1 +; MANGLE-NEXT: Name: undefined function _Z3bari +; MANGLE-NEXT: - Index: 2 +; MANGLE-NEXT: Name: _Z3fooi +; MANGLE-NEXT: - Index: 3 +; MANGLE-NEXT: Name: _start +; MANGLE-NEXT: ... Index: wasm/Driver.cpp =================================================================== --- wasm/Driver.cpp +++ wasm/Driver.cpp @@ -310,10 +310,8 @@ // Add a synthetic dummy for weak undefined functions. These dummies will // be GC'd if not used as the target of any "call" instructions. - Optional SymName = demangleItanium(Sym->getName()); - StringRef DebugName = - Saver.save("undefined function " + - (SymName ? StringRef(*SymName) : Sym->getName())); + std::string SymName = toString(*Sym); + StringRef DebugName = Saver.save("undefined function " + SymName); SyntheticFunction *Func = make(Sig, Sym->getName(), DebugName); Func->setBody(UnreachableFn); Index: wasm/Symbols.h =================================================================== --- wasm/Symbols.h +++ wasm/Symbols.h @@ -349,6 +349,7 @@ // Returns a symbol name for an error message. std::string toString(const wasm::Symbol &Sym); std::string toString(wasm::Symbol::Kind Kind); +std::string maybeDemangleSymbol(StringRef Name); } // namespace lld Index: wasm/Symbols.cpp =================================================================== --- wasm/Symbols.cpp +++ wasm/Symbols.cpp @@ -226,10 +226,14 @@ void LazySymbol::fetch() { cast(File)->addMember(&ArchiveSymbol); } std::string lld::toString(const wasm::Symbol &Sym) { + return lld::maybeDemangleSymbol(Sym.getName()); +} + +std::string lld::maybeDemangleSymbol(StringRef Name) { if (Config->Demangle) - if (Optional S = demangleItanium(Sym.getName())) + if (Optional S = demangleItanium(Name)) return *S; - return Sym.getName(); + return Name; } std::string lld::toString(wasm::Symbol::Kind Kind) { Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -554,8 +554,7 @@ for (const Symbol *S : ImportedSymbols) { if (auto *F = dyn_cast(S)) { writeUleb128(Sub.OS, F->getFunctionIndex(), "func index"); - Optional Name = demangleItanium(F->getName()); - writeStr(Sub.OS, Name ? StringRef(*Name) : F->getName(), "symbol name"); + writeStr(Sub.OS, toString(*S), "symbol name"); } } for (const InputFunction *F : InputFunctions) { @@ -564,8 +563,7 @@ if (!F->getDebugName().empty()) { writeStr(Sub.OS, F->getDebugName(), "symbol name"); } else { - Optional Name = demangleItanium(F->getName()); - writeStr(Sub.OS, Name ? StringRef(*Name) : F->getName(), "symbol name"); + writeStr(Sub.OS, maybeDemangleSymbol(F->getName()), "symbol name"); } } }