[llvm] r322741 - [WebAssembly] Remove debug names from symbol table

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 17 11:28:43 PST 2018


Author: sbc
Date: Wed Jan 17 11:28:43 2018
New Revision: 322741

URL: http://llvm.org/viewvc/llvm-project?rev=322741&view=rev
Log:
[WebAssembly] Remove debug names from symbol table

Get rid of DEBUG_FUNCTION_NAME symbols. When we actually debug
data, maybe we'll want somewhere to put it... but having a symbol
that just stores the name of another symbol seems odd.
It means you have multiple Symbols with the same name, one
containing the actual function and another containing the name!

Store the names in a vector on the WasmObjectFile when reading
them in. Also stash them on the WasmFunctions themselves.
The names are //not// "symbol names" or aliases or anything,
they're just the name that a debugger should show against the
function body itself. NB. The WasmObjectFile stores them so that
they can be exported in the YAML losslessly, and hence the tests
can be precise.

Enforce that the CODE section has been read in before reading
the "names" section. Requires minor adjustment to some tests.

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42075

Modified:
    llvm/trunk/include/llvm/BinaryFormat/Wasm.h
    llvm/trunk/include/llvm/Object/Wasm.h
    llvm/trunk/lib/MC/WasmObjectWriter.cpp
    llvm/trunk/lib/Object/WasmObjectFile.cpp
    llvm/trunk/test/MC/WebAssembly/weak-alias.ll
    llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml
    llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml
    llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml
    llvm/trunk/test/tools/llvm-objdump/WebAssembly/symbol-table.test
    llvm/trunk/test/tools/llvm-readobj/symbols.test
    llvm/trunk/tools/llvm-readobj/WasmDumper.cpp
    llvm/trunk/tools/obj2yaml/wasm2yaml.cpp

Modified: llvm/trunk/include/llvm/BinaryFormat/Wasm.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/BinaryFormat/Wasm.h?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/include/llvm/BinaryFormat/Wasm.h (original)
+++ llvm/trunk/include/llvm/BinaryFormat/Wasm.h Wed Jan 17 11:28:43 2018
@@ -95,7 +95,8 @@ struct WasmFunction {
   ArrayRef<uint8_t> Body;
   uint32_t CodeSectionOffset;
   uint32_t Size;
-  StringRef Comdat;
+  StringRef Name; // from the "names" section
+  StringRef Comdat; // from the "comdat info" section
 };
 
 struct WasmDataSegment {
@@ -105,7 +106,7 @@ struct WasmDataSegment {
   StringRef Name;
   uint32_t Alignment;
   uint32_t Flags;
-  StringRef Comdat;
+  StringRef Comdat; // from the "comdat info" section
 };
 
 struct WasmElemSegment {
@@ -116,7 +117,7 @@ struct WasmElemSegment {
 
 struct WasmRelocation {
   uint32_t Type;   // The type of the relocation.
-  uint32_t Index;  // Index into function to global index space.
+  uint32_t Index;  // Index into function or global index space.
   uint64_t Offset; // Offset from the start of the section.
   int64_t Addend;  // A value to add to the symbol.
 };
@@ -126,6 +127,11 @@ struct WasmInitFunc {
   uint32_t FunctionIndex;
 };
 
+struct WasmFunctionName {
+  uint32_t Index;
+  StringRef Name;
+};
+
 struct WasmLinkingData {
   uint32_t DataSize;
   std::vector<WasmInitFunc> InitFunctions;

Modified: llvm/trunk/include/llvm/Object/Wasm.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Wasm.h?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Wasm.h (original)
+++ llvm/trunk/include/llvm/Object/Wasm.h Wed Jan 17 11:28:43 2018
@@ -39,7 +39,6 @@ public:
     FUNCTION_EXPORT,
     GLOBAL_IMPORT,
     GLOBAL_EXPORT,
-    DEBUG_FUNCTION_NAME,
   };
 
   WasmSymbol(StringRef Name, SymbolType Type, uint32_t Section,
@@ -70,8 +69,7 @@ public:
 
   bool isFunction() const {
     return Type == WasmSymbol::SymbolType::FUNCTION_IMPORT ||
-           Type == WasmSymbol::SymbolType::FUNCTION_EXPORT ||
-           Type == WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME;
+           Type == WasmSymbol::SymbolType::FUNCTION_EXPORT;
   }
 
 
@@ -150,6 +148,7 @@ public:
   ArrayRef<WasmSegment> dataSegments() const { return DataSegments; }
   ArrayRef<wasm::WasmFunction> functions() const { return Functions; }
   ArrayRef<StringRef> comdats() const { return Comdats; }
+  ArrayRef<wasm::WasmFunctionName> debugNames() const { return DebugNames; }
   uint32_t startFunction() const { return StartFunction; }
 
   void moveSymbolNext(DataRefImpl &Symb) const override;
@@ -253,6 +252,7 @@ private:
   std::vector<wasm::WasmFunction> Functions;
   std::vector<WasmSymbol> Symbols;
   std::vector<StringRef> Comdats;
+  std::vector<wasm::WasmFunctionName> DebugNames;
   uint32_t StartFunction = -1;
   bool HasLinkingSection = false;
   wasm::WasmLinkingData LinkingData;

Modified: llvm/trunk/lib/MC/WasmObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/WasmObjectWriter.cpp?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/lib/MC/WasmObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/WasmObjectWriter.cpp Wed Jan 17 11:28:43 2018
@@ -221,6 +221,7 @@ class WasmObjectWriter : public MCObject
       FunctionTypeIndices;
   SmallVector<WasmFunctionType, 4> FunctionTypes;
   SmallVector<WasmGlobal, 4> Globals;
+  unsigned NumFunctionImports = 0;
   unsigned NumGlobalImports = 0;
 
   // TargetObjectWriter wrappers.
@@ -252,6 +253,7 @@ private:
     FunctionTypes.clear();
     Globals.clear();
     MCObjectWriter::reset();
+    NumFunctionImports = 0;
     NumGlobalImports = 0;
   }
 
@@ -286,8 +288,7 @@ private:
                         ArrayRef<WasmFunction> Functions);
   void writeDataSection(ArrayRef<WasmDataSegment> Segments);
   void writeNameSection(ArrayRef<WasmFunction> Functions,
-                        ArrayRef<WasmImport> Imports,
-                        uint32_t NumFuncImports);
+                        ArrayRef<WasmImport> Imports);
   void writeCodeRelocSection();
   void writeDataRelocSection();
   void writeLinkingMetaDataSection(
@@ -852,11 +853,9 @@ void WasmObjectWriter::writeDataSection(
   endSection(Section);
 }
 
-void WasmObjectWriter::writeNameSection(
-    ArrayRef<WasmFunction> Functions,
-    ArrayRef<WasmImport> Imports,
-    unsigned NumFuncImports) {
-  uint32_t TotalFunctions = NumFuncImports + Functions.size();
+void WasmObjectWriter::writeNameSection(ArrayRef<WasmFunction> Functions,
+                                        ArrayRef<WasmImport> Imports) {
+  uint32_t TotalFunctions = NumFunctionImports + Functions.size();
   if (TotalFunctions == 0)
     return;
 
@@ -1023,7 +1022,6 @@ void WasmObjectWriter::writeObject(MCAss
   SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags;
   SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
   std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
-  unsigned NumFuncImports = 0;
   SmallVector<WasmDataSegment, 4> DataSegments;
   uint32_t DataSize = 0;
 
@@ -1113,8 +1111,7 @@ void WasmObjectWriter::writeObject(MCAss
     const auto &WS = static_cast<const MCSymbolWasm &>(S);
 
     // Register types for all functions, including those with private linkage
-    // (making them
-    // because wasm always needs a type signature.
+    // (because wasm always needs a type signature).
     if (WS.isFunction())
       registerFunctionType(WS);
 
@@ -1131,8 +1128,8 @@ void WasmObjectWriter::writeObject(MCAss
       if (WS.isFunction()) {
         Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
         Import.Type = getFunctionType(WS);
-        SymbolIndices[&WS] = NumFuncImports;
-        ++NumFuncImports;
+        SymbolIndices[&WS] = NumFunctionImports;
+        ++NumFunctionImports;
       } else {
         Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
         Import.Type = int32_t(PtrType);
@@ -1216,7 +1213,7 @@ void WasmObjectWriter::writeObject(MCAss
               "function symbols must have a size set with .size");
 
         // A definition. Take the next available index.
-        Index = NumFuncImports + Functions.size();
+        Index = NumFunctionImports + Functions.size();
 
         // Prepare the function.
         WasmFunction Func;
@@ -1410,7 +1407,7 @@ void WasmObjectWriter::writeObject(MCAss
   writeElemSection(TableElems);
   writeCodeSection(Asm, Layout, Functions);
   writeDataSection(DataSegments);
-  writeNameSection(Functions, Imports, NumFuncImports);
+  writeNameSection(Functions, Imports);
   writeCodeRelocSection();
   writeDataRelocSection();
   writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,

Modified: llvm/trunk/lib/Object/WasmObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/WasmObjectFile.cpp?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/lib/Object/WasmObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/WasmObjectFile.cpp Wed Jan 17 11:28:43 2018
@@ -270,6 +270,10 @@ Error WasmObjectFile::parseSection(WasmS
 
 Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) {
   llvm::DenseSet<uint64_t> Seen;
+  if (Functions.size() != FunctionTypes.size()) {
+    return make_error<GenericBinaryError>("Names must come after code section",
+                                          object_error::parse_failed);
+  }
 
   while (Ptr < End) {
     uint8_t Type = readVarint7(Ptr);
@@ -284,10 +288,15 @@ Error WasmObjectFile::parseNameSection(c
           return make_error<GenericBinaryError>("Function named more than once",
                                                 object_error::parse_failed);
         StringRef Name = readString(Ptr);
-        if (!Name.empty())
-          Symbols.emplace_back(Name,
-                               WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME,
-                               Sections.size(), Index);
+        if (!isValidFunctionIndex(Index) || Name.empty())
+          return make_error<GenericBinaryError>("Invalid name entry",
+                                                object_error::parse_failed);
+        DebugNames.push_back(wasm::WasmFunctionName{Index, Name});
+        if (Index >= NumImportedFunctions) {
+          // Override any existing name; the name specified by the "names"
+          // section is the Function's canonical name.
+          Functions[Index - NumImportedFunctions].Name = Name;
+        }
       }
       break;
     }
@@ -360,12 +369,24 @@ void WasmObjectFile::populateSymbolTable
                      << " sym index:" << SymIndex << "\n");
       }
     }
+    if (Export.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
+      auto &Function = Functions[Export.Index - NumImportedFunctions];
+      if (Function.Name.empty()) {
+        // Use the export's name to set a name for the Function, but only if one
+        // hasn't already been set.
+        Function.Name = Export.Name;
+      }
+    }
   }
 }
 
 Error WasmObjectFile::parseLinkingSection(const uint8_t *Ptr,
                                           const uint8_t *End) {
   HasLinkingSection = true;
+  if (Functions.size() != FunctionTypes.size()) {
+    return make_error<GenericBinaryError>(
+        "Linking data must come after code section", object_error::parse_failed);
+  }
 
   // Only populate the symbol table with imports and exports if the object
   // has a linking section (i.e. its a relocatable object file). Otherwise
@@ -867,10 +888,6 @@ uint32_t WasmObjectFile::getSymbolFlags(
   case WasmSymbol::SymbolType::FUNCTION_EXPORT:
     Result |= SymbolRef::SF_Executable;
     break;
-  case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
-    Result |= SymbolRef::SF_Executable;
-    Result |= SymbolRef::SF_FormatSpecific;
-    break;
   case WasmSymbol::SymbolType::GLOBAL_IMPORT:
     Result |= SymbolRef::SF_Undefined;
     break;
@@ -914,7 +931,6 @@ uint64_t WasmObjectFile::getWasmSymbolVa
   case WasmSymbol::SymbolType::FUNCTION_IMPORT:
   case WasmSymbol::SymbolType::GLOBAL_IMPORT:
   case WasmSymbol::SymbolType::FUNCTION_EXPORT:
-  case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
     return Sym.ElementIndex;
   case WasmSymbol::SymbolType::GLOBAL_EXPORT: {
     uint32_t GlobalIndex = Sym.ElementIndex - NumImportedGlobals;
@@ -949,7 +965,6 @@ WasmObjectFile::getSymbolType(DataRefImp
   switch (Sym.Type) {
   case WasmSymbol::SymbolType::FUNCTION_IMPORT:
   case WasmSymbol::SymbolType::FUNCTION_EXPORT:
-  case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
     return SymbolRef::ST_Function;
   case WasmSymbol::SymbolType::GLOBAL_IMPORT:
   case WasmSymbol::SymbolType::GLOBAL_EXPORT:

Modified: llvm/trunk/test/MC/WebAssembly/weak-alias.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/WebAssembly/weak-alias.ll?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/test/MC/WebAssembly/weak-alias.ll (original)
+++ llvm/trunk/test/MC/WebAssembly/weak-alias.ll Wed Jan 17 11:28:43 2018
@@ -239,12 +239,6 @@ entry:
 ; CHECK-NEXT: ...
 
 ; CHECK-SYMS: SYMBOL TABLE:
-; CHECK-SYMS-NEXT: 00000000 g     F name	foo_alias
-; CHECK-SYMS-NEXT: 00000001 g     F name	foo
-; CHECK-SYMS-NEXT: 00000002 g     F name	call_direct
-; CHECK-SYMS-NEXT: 00000003 g     F name	call_alias
-; CHECK-SYMS-NEXT: 00000004 g     F name	call_direct_ptr
-; CHECK-SYMS-NEXT: 00000005 g     F name	call_alias_ptr
 ; CHECK-SYMS-NEXT: 00000001 gw    F EXPORT	.hidden foo_alias
 ; CHECK-SYMS-NEXT: 00000000 gw      EXPORT	.hidden bar_alias
 ; CHECK-SYMS-NEXT: 00000001 g     F EXPORT	.hidden foo

Modified: llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml (original)
+++ llvm/trunk/test/ObjectYAML/wasm/weak_symbols.yaml Wed Jan 17 11:28:43 2018
@@ -26,6 +26,14 @@ Sections:
       - Name:            global_export
         Kind:            GLOBAL
         Index:           0
+  - Type:            CODE
+    Functions:
+      - Index:           0
+        Locals:
+        Body:            00
+      - Index:           1
+        Locals:
+        Body:            00
   - Type:            CUSTOM
     Name:            linking
     DataSize:        10

Modified: llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml (original)
+++ llvm/trunk/test/tools/llvm-nm/wasm/exports.yaml Wed Jan 17 11:28:43 2018
@@ -54,6 +54,23 @@ Sections:
       - Name:            bar
         Kind:            GLOBAL
         Index:           0x00000003
+  - Type:            CODE
+    Functions:
+      - Index:           1
+        Locals:
+        Body:            00
+      - Index:           2
+        Locals:
+        Body:            00
+      - Index:           3
+        Locals:
+        Body:            00
+      - Index:           4
+        Locals:
+        Body:            00
+      - Index:           5
+        Locals:
+        Body:            00
   - Type:            CUSTOM
     Name:            "linking"
     DataSize:        0

Modified: llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml (original)
+++ llvm/trunk/test/tools/llvm-nm/wasm/weak-symbols.yaml Wed Jan 17 11:28:43 2018
@@ -54,6 +54,20 @@ Sections:
       - Name:            weak_global_data
         Kind:            GLOBAL
         Index:           0x00000003
+  - Type:            CODE
+    Functions:
+      - Index:           1
+        Locals:
+        Body:            00
+      - Index:           2
+        Locals:
+        Body:            00
+      - Index:           3
+        Locals:
+        Body:            00
+      - Index:           4
+        Locals:
+        Body:            00
   - Type:            CUSTOM
     Name:            linking
     DataSize:        0

Modified: llvm/trunk/test/tools/llvm-objdump/WebAssembly/symbol-table.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objdump/WebAssembly/symbol-table.test?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-objdump/WebAssembly/symbol-table.test (original)
+++ llvm/trunk/test/tools/llvm-objdump/WebAssembly/symbol-table.test Wed Jan 17 11:28:43 2018
@@ -1,9 +1,6 @@
 RUN: llvm-objdump -t %p/../Inputs/trivial.obj.wasm | FileCheck %s
 
 CHECK:      SYMBOL TABLE:
-CHECK-NEXT: 00000000 g     F name	puts
-CHECK-NEXT: 00000001 g     F name	SomeOtherFunction
-CHECK-NEXT: 00000002 g     F name	main
 CHECK-NEXT: 00000000 g     F IMPORT	puts
 CHECK-NEXT: 00000000 g     F IMPORT	SomeOtherFunction
 CHECK-NEXT: 00000002 g     F EXPORT	main

Modified: llvm/trunk/test/tools/llvm-readobj/symbols.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/symbols.test?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/symbols.test (original)
+++ llvm/trunk/test/tools/llvm-readobj/symbols.test Wed Jan 17 11:28:43 2018
@@ -74,21 +74,6 @@ ELF-NEXT:   }
 WASM:      Symbols [
 WASM-NEXT:   Symbol {
 WASM-NEXT:     Name: puts
-WASM-NEXT:     Type: DEBUG_FUNCTION_NAME (0x4)
-WASM-NEXT:     Flags: 0x0
-WASM-NEXT:   }
-WASM-NEXT:   Symbol {
-WASM-NEXT:     Name: SomeOtherFunction
-WASM-NEXT:     Type: DEBUG_FUNCTION_NAME (0x4)
-WASM-NEXT:     Flags: 0x0
-WASM-NEXT:   }
-WASM-NEXT:   Symbol {
-WASM-NEXT:     Name: main
-WASM-NEXT:     Type: DEBUG_FUNCTION_NAME (0x4)
-WASM-NEXT:     Flags: 0x0
-WASM-NEXT:   }
-WASM-NEXT:   Symbol {
-WASM-NEXT:     Name: puts
 WASM-NEXT:     Type: FUNCTION_IMPORT (0x0)
 WASM-NEXT:     Flags: 0x0
 WASM-NEXT:   }

Modified: llvm/trunk/tools/llvm-readobj/WasmDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/WasmDumper.cpp?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/WasmDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/WasmDumper.cpp Wed Jan 17 11:28:43 2018
@@ -28,7 +28,6 @@ static const EnumEntry<unsigned> WasmSym
   ENUM_ENTRY(FUNCTION_EXPORT),
   ENUM_ENTRY(GLOBAL_IMPORT),
   ENUM_ENTRY(GLOBAL_EXPORT),
-  ENUM_ENTRY(DEBUG_FUNCTION_NAME),
 #undef ENUM_ENTRY
 };
 

Modified: llvm/trunk/tools/obj2yaml/wasm2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/wasm2yaml.cpp?rev=322741&r1=322740&r2=322741&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/wasm2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/wasm2yaml.cpp Wed Jan 17 11:28:43 2018
@@ -53,13 +53,10 @@ std::unique_ptr<WasmYAML::CustomSection>
   std::unique_ptr<WasmYAML::CustomSection> CustomSec;
   if (WasmSec.Name == "name") {
     std::unique_ptr<WasmYAML::NameSection> NameSec = make_unique<WasmYAML::NameSection>();
-    for (const object::SymbolRef& Sym: Obj.symbols()) {
-      const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym);
-      if (Symbol.Type != object::WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME)
-        continue;
+    for (const llvm::wasm::WasmFunctionName &Func: Obj.debugNames()) {
       WasmYAML::NameEntry NameEntry;
-      NameEntry.Name = Symbol.Name;
-      NameEntry.Index = Sym.getValue();
+      NameEntry.Name = Func.Name;
+      NameEntry.Index = Func.Index;
       NameSec->FunctionNames.push_back(NameEntry);
     }
     CustomSec = std::move(NameSec);




More information about the llvm-commits mailing list