[PATCH] D44316: [WebAssembly] Demangle symbol names for use by the browser debugger

Nicholas Wilson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 9 10:20:01 PST 2018


ncw created this revision.
ncw added reviewers: sbc100, sunfish.
Herald added subscribers: llvm-commits, JDevlieghere, aheejin, jgravelle-google, aprantl, dschuff, jfb.

I hope this isn't too controversial?

The "name" section is what the browser debugger will show, and it of course knows nothing about the Itanium mangling conventions - it doesn't even know that the Wasm file came from C++. So the demangled names should go in there?

I seem to remember a GitHub comment, perhaps from @sunfish or someone else saying that this had been the intention - but maybe I'm misremembering.

Alternatively, DWARF-like debugging data attached to the Wasm file could play a similar role, but then, what's the point of the "name" section if the demangled name for the user is actually going to be coming from somewhere else?


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D44316

Files:
  test/wasm/cxx-symbols.ll
  wasm/Writer.cpp


Index: wasm/Writer.cpp
===================================================================
--- wasm/Writer.cpp
+++ wasm/Writer.cpp
@@ -527,19 +527,26 @@
   SubSection Sub(WASM_NAMES_FUNCTION);
   writeUleb128(Sub.OS, NumNames, "name count");
 
+  auto Demangle = [](StringRef Name) {
+    if (Config->Demangle)
+      if (Optional<std::string> S = demangleItanium(Name))
+        return *S;
+    return Name.str();
+  };
+
   // Names must appear in function index order.  As it happens ImportedSymbols
   // and InputFunctions are numbered in order with imported functions coming
   // first.
   for (const Symbol *S : ImportedSymbols) {
     if (!isa<FunctionSymbol>(S))
       continue;
     writeUleb128(Sub.OS, S->getOutputIndex(), "import index");
-    writeStr(Sub.OS, S->getName(), "symbol name");
+    writeStr(Sub.OS, Demangle(S->getName()), "symbol name");
   }
   for (const InputFunction *F : InputFunctions) {
     if (!F->getName().empty()) {
       writeUleb128(Sub.OS, F->getOutputIndex(), "func index");
-      writeStr(Sub.OS, F->getName(), "symbol name");
+      writeStr(Sub.OS, Demangle(F->getName()), "symbol name");
     }
   }
 
Index: test/wasm/cxx-symbols.ll
===================================================================
--- /dev/null
+++ test/wasm/cxx-symbols.ll
@@ -0,0 +1,50 @@
+; RUN: llc -filetype=obj %s -o %t.o
+; RUN: wasm-ld --check-signatures -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+target triple = "wasm32-unknown-unknown-wasm"
+
+define hidden void @_Z3fooi(i32 %arg) {
+  ret void
+}
+
+define hidden void @_start() {
+  call void @_Z3fooi(i32 1)
+  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:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           1
+; 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:            41011080808080000B
+; CHECK-NEXT:       - Index:           2
+; CHECK-NEXT:         Locals:          
+; CHECK-NEXT:         Body:            0B
+; CHECK-NEXT:   - Type:            CUSTOM
+; CHECK-NEXT:     Name:            name
+; CHECK-NEXT:     FunctionNames:   
+; CHECK-NEXT:       - Index:           0
+; CHECK-NEXT:         Name:            'foo(int)'
+; CHECK-NEXT:       - Index:           1
+; CHECK-NEXT:         Name:            _start
+; CHECK-NEXT:       - Index:           2
+; CHECK-NEXT:         Name:            __wasm_call_ctors
+; CHECK-NEXT: ...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44316.137792.patch
Type: text/x-patch
Size: 3158 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180309/a26236f1/attachment-0001.bin>


More information about the llvm-commits mailing list