From f05301ced090864a60ce65324dc1448638dfe1cb Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Tue, 26 Sep 2017 18:21:12 +0000 Subject: [PATCH] [WebAssembly] Use function/global index space in WasmSymbol It is useful for the symbol to contain the index of the function of global it represents in the function/global index space. For imports we also store the import index so that the linker can find, for example, the signature of the corresponding function, which is defined by the import In the long run we need to decide whether this API surface should be closer to binary (where imported functions are seperate) or the wasm spec (where the function index space is unified). Differential Revision: https://reviews.llvm.org/D38189 llvm-svn: 314230 --- include/llvm/Object/Wasm.h | 21 ++++++++++++++++----- lib/Object/WasmObjectFile.cpp | 20 +++++++++----------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/llvm/Object/Wasm.h b/include/llvm/Object/Wasm.h index 172b1502156..e138faeed34 100644 --- a/include/llvm/Object/Wasm.h +++ b/include/llvm/Object/Wasm.h @@ -43,18 +43,28 @@ public: }; WasmSymbol(StringRef Name, SymbolType Type, uint32_t Section, - uint32_t ElementIndex) - : Name(Name), Type(Type), Section(Section), ElementIndex(ElementIndex) {} + uint32_t ElementIndex, uint32_t ImportIndex = 0) + : Name(Name), Type(Type), Section(Section), ElementIndex(ElementIndex), + ImportIndex(ImportIndex) {} StringRef Name; SymbolType Type; uint32_t Section; uint32_t Flags = 0; - // Index into the imports, exports or functions array of the object depending - // on the type + // Index into either the function or global index space. uint32_t ElementIndex; + // For imports, the index into the import table + uint32_t ImportIndex; + + bool isFunction() const { + return Type == WasmSymbol::SymbolType::FUNCTION_IMPORT || + Type == WasmSymbol::SymbolType::FUNCTION_EXPORT || + Type == WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME; + } + + bool isWeak() const { return getBinding() == wasm::WASM_SYMBOL_BINDING_WEAK; } @@ -73,7 +83,8 @@ public: void print(raw_ostream &Out) const { Out << "Name=" << Name << ", Type=" << static_cast(Type) - << ", Flags=" << Flags << " ElemIndex=" << ElementIndex; + << ", Flags=" << Flags << " ElemIndex=" << ElementIndex + << ", ImportIndex=" << ImportIndex; } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) diff --git a/lib/Object/WasmObjectFile.cpp b/lib/Object/WasmObjectFile.cpp index 77fcac5578b..c320b915371 100644 --- a/lib/Object/WasmObjectFile.cpp +++ b/lib/Object/WasmObjectFile.cpp @@ -303,13 +303,15 @@ Error WasmObjectFile::parseNameSection(const uint8_t *Ptr, const uint8_t *End) { void WasmObjectFile::populateSymbolTable() { // Add imports to symbol table size_t ImportIndex = 0; + size_t GlobalIndex = 0; + size_t FunctionIndex = 0; for (const wasm::WasmImport& Import : Imports) { switch (Import.Kind) { case wasm::WASM_EXTERNAL_GLOBAL: assert(Import.Global.Type == wasm::WASM_TYPE_I32); SymbolMap.try_emplace(Import.Field, Symbols.size()); Symbols.emplace_back(Import.Field, WasmSymbol::SymbolType::GLOBAL_IMPORT, - ImportSection, ImportIndex); + ImportSection, GlobalIndex++, ImportIndex); DEBUG(dbgs() << "Adding import: " << Symbols.back() << " sym index:" << Symbols.size() << "\n"); break; @@ -317,7 +319,7 @@ void WasmObjectFile::populateSymbolTable() { SymbolMap.try_emplace(Import.Field, Symbols.size()); Symbols.emplace_back(Import.Field, WasmSymbol::SymbolType::FUNCTION_IMPORT, - ImportSection, ImportIndex); + ImportSection, FunctionIndex++, ImportIndex); DEBUG(dbgs() << "Adding import: " << Symbols.back() << " sym index:" << Symbols.size() << "\n"); break; @@ -328,7 +330,6 @@ void WasmObjectFile::populateSymbolTable() { } // Add exports to symbol table - size_t ExportIndex = 0; for (const wasm::WasmExport& Export : Exports) { if (Export.Kind == wasm::WASM_EXTERNAL_FUNCTION || Export.Kind == wasm::WASM_EXTERNAL_GLOBAL) { @@ -339,18 +340,17 @@ void WasmObjectFile::populateSymbolTable() { auto Pair = SymbolMap.try_emplace(Export.Name, Symbols.size()); if (Pair.second) { Symbols.emplace_back(Export.Name, ExportType, - ExportSection, ExportIndex); + ExportSection, Export.Index); DEBUG(dbgs() << "Adding export: " << Symbols.back() << " sym index:" << Symbols.size() << "\n"); } else { uint32_t SymIndex = Pair.first->second; Symbols[SymIndex] = - WasmSymbol(Export.Name, ExportType, ExportSection, ExportIndex); + WasmSymbol(Export.Name, ExportType, ExportSection, Export.Index); DEBUG(dbgs() << "Replacing existing symbol: " << Symbols[SymIndex] << " sym index:" << SymIndex << "\n"); } } - ExportIndex++; } } @@ -825,19 +825,17 @@ uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol& Sym) const { switch (Sym.Type) { case WasmSymbol::SymbolType::FUNCTION_IMPORT: case WasmSymbol::SymbolType::GLOBAL_IMPORT: - return 0; case WasmSymbol::SymbolType::FUNCTION_EXPORT: - return Exports[Sym.ElementIndex].Index; + case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME: + return Sym.ElementIndex; case WasmSymbol::SymbolType::GLOBAL_EXPORT: { - uint32_t GlobalIndex = Exports[Sym.ElementIndex].Index - NumImportedGlobals; + uint32_t GlobalIndex = Sym.ElementIndex - NumImportedGlobals; assert(GlobalIndex < Globals.size()); const wasm::WasmGlobal& Global = Globals[GlobalIndex]; // WasmSymbols correspond only to I32_CONST globals assert(Global.InitExpr.Opcode == wasm::WASM_OPCODE_I32_CONST); return Global.InitExpr.Value.Int32; } - case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME: - return Sym.ElementIndex; } llvm_unreachable("invalid symbol type"); }