Index: include/llvm/BinaryFormat/Wasm.h =================================================================== --- include/llvm/BinaryFormat/Wasm.h +++ include/llvm/BinaryFormat/Wasm.h @@ -25,7 +25,7 @@ // Wasm binary format version const uint32_t WasmVersion = 0x1; // Wasm linking metadata version -const uint32_t WasmMetadataVersion = 0x2; +const uint32_t WasmMetadataVersion = 0x3; // Wasm uses a 64k page size const uint32_t WasmPageSize = 65536; @@ -170,7 +170,8 @@ StringRef Name; uint8_t Kind; uint32_t Flags; - StringRef Module; // For undefined symbols the module name of the import + StringRef ImportModule; // For undefined symbols the module of the import + StringRef ImportName; // For undefined symbols the name of the import union { // For function or global symbols, the index in function or global index // space. Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -985,6 +985,9 @@ encodeULEB128(Sym.Flags, W.OS); switch (Sym.Kind) { case wasm::WASM_SYMBOL_TYPE_FUNCTION: + encodeULEB128(Sym.ElementIndex, W.OS); + writeString(Sym.Name); + break; case wasm::WASM_SYMBOL_TYPE_GLOBAL: case wasm::WASM_SYMBOL_TYPE_EVENT: encodeULEB128(Sym.ElementIndex, W.OS); Index: lib/Object/WasmObjectFile.cpp =================================================================== --- lib/Object/WasmObjectFile.cpp +++ lib/Object/WasmObjectFile.cpp @@ -496,8 +496,8 @@ IsDefined != isDefinedFunctionIndex(Info.ElementIndex)) return make_error("invalid function symbol index", object_error::parse_failed); + Info.Name = readString(Ctx); if (IsDefined) { - Info.Name = readString(Ctx); unsigned FuncIndex = Info.ElementIndex - NumImportedFunctions; Signature = &Signatures[FunctionTypes[FuncIndex]]; wasm::WasmFunction &Function = Functions[FuncIndex]; @@ -506,8 +506,8 @@ } else { wasm::WasmImport &Import = *ImportedFunctions[Info.ElementIndex]; Signature = &Signatures[Import.SigIndex]; - Info.Name = Import.Field; - Info.Module = Import.Module; + Info.ImportName = Import.Field; + Info.ImportModule = Import.Module; } break; Index: test/MC/WebAssembly/assembler-binary.ll =================================================================== --- test/MC/WebAssembly/assembler-binary.ll +++ test/MC/WebAssembly/assembler-binary.ll @@ -77,7 +77,7 @@ ; CHECK-NEXT: Body: 1080808080000B ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: test/MC/WebAssembly/comdat.ll =================================================================== --- test/MC/WebAssembly/comdat.ll +++ test/MC/WebAssembly/comdat.ll @@ -75,7 +75,7 @@ ; CHECK-NEXT: Content: '616263' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: test/MC/WebAssembly/event-section.ll =================================================================== --- test/MC/WebAssembly/event-section.ll +++ test/MC/WebAssembly/event-section.ll @@ -44,7 +44,7 @@ ; CHECK: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK: - Index: 1 Index: test/MC/WebAssembly/function-sections.ll =================================================================== --- test/MC/WebAssembly/function-sections.ll +++ test/MC/WebAssembly/function-sections.ll @@ -14,7 +14,7 @@ ; CHECK: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: test/MC/WebAssembly/global-ctor-dtor.ll =================================================================== --- test/MC/WebAssembly/global-ctor-dtor.ll +++ test/MC/WebAssembly/global-ctor-dtor.ll @@ -110,7 +110,7 @@ ; CHECK-NEXT: Content: '01040000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: test/MC/WebAssembly/unnamed-data.ll =================================================================== --- test/MC/WebAssembly/unnamed-data.ll +++ test/MC/WebAssembly/unnamed-data.ll @@ -44,7 +44,7 @@ ; CHECK-NEXT: Content: '06000000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: DATA Index: test/MC/WebAssembly/weak-alias.ll =================================================================== --- test/MC/WebAssembly/weak-alias.ll +++ test/MC/WebAssembly/weak-alias.ll @@ -138,7 +138,7 @@ ; CHECK-NEXT: Content: '01000000' ; CHECK-NEXT: - Type: CUSTOM ; CHECK-NEXT: Name: linking -; CHECK-NEXT: Version: 2 +; CHECK-NEXT: Version: 3 ; CHECK-NEXT: SymbolTable: ; CHECK-NEXT: - Index: 0 ; CHECK-NEXT: Kind: FUNCTION Index: test/Object/wasm-bad-metadata-version.yaml =================================================================== --- test/Object/wasm-bad-metadata-version.yaml +++ test/Object/wasm-bad-metadata-version.yaml @@ -8,4 +8,4 @@ Name: linking Version: 0 -# CHECK: {{.*}}: Unexpected metadata version: 0 (Expected: 2) +# CHECK: {{.*}}: Unexpected metadata version: 0 (Expected: 3) Index: test/ObjectYAML/wasm/code_section.yaml =================================================================== --- test/ObjectYAML/wasm/code_section.yaml +++ test/ObjectYAML/wasm/code_section.yaml @@ -39,7 +39,7 @@ Body: 108180808000210020000F0B - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: FUNCTION Index: test/ObjectYAML/wasm/data_section.yaml =================================================================== --- test/ObjectYAML/wasm/data_section.yaml +++ test/ObjectYAML/wasm/data_section.yaml @@ -24,7 +24,7 @@ Addend: -6 - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: DATA Index: test/ObjectYAML/wasm/event_section.yaml =================================================================== --- test/ObjectYAML/wasm/event_section.yaml +++ test/ObjectYAML/wasm/event_section.yaml @@ -32,7 +32,7 @@ Body: 200008808080800041000B - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: FUNCTION @@ -78,7 +78,7 @@ # CHECK-NEXT: Body: 200008808080800041000B # CHECK-NEXT: - Type: CUSTOM # CHECK-NEXT: Name: linking -# CHECK-NEXT: Version: 2 +# CHECK-NEXT: Version: 3 # CHECK-NEXT: SymbolTable: # CHECK-NEXT: - Index: 0 # CHECK-NEXT: Kind: FUNCTION Index: test/ObjectYAML/wasm/invalid_global_weak.yaml =================================================================== --- test/ObjectYAML/wasm/invalid_global_weak.yaml +++ test/ObjectYAML/wasm/invalid_global_weak.yaml @@ -13,7 +13,7 @@ GlobalMutable: false - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: GLOBAL Index: test/ObjectYAML/wasm/linking_section.yaml =================================================================== --- test/ObjectYAML/wasm/linking_section.yaml +++ test/ObjectYAML/wasm/linking_section.yaml @@ -29,7 +29,7 @@ Content: '11110000' - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: FUNCTION Index: test/ObjectYAML/wasm/weak_symbols.yaml =================================================================== --- test/ObjectYAML/wasm/weak_symbols.yaml +++ test/ObjectYAML/wasm/weak_symbols.yaml @@ -36,7 +36,7 @@ Body: 00 - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: FUNCTION Index: test/tools/llvm-nm/wasm/exports.yaml =================================================================== --- test/tools/llvm-nm/wasm/exports.yaml +++ test/tools/llvm-nm/wasm/exports.yaml @@ -37,7 +37,7 @@ Content: '616263' - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: FUNCTION Index: test/tools/llvm-nm/wasm/imports.yaml =================================================================== --- test/tools/llvm-nm/wasm/imports.yaml +++ test/tools/llvm-nm/wasm/imports.yaml @@ -25,7 +25,7 @@ GlobalMutable: false - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: FUNCTION Index: test/tools/llvm-nm/wasm/weak-symbols.yaml =================================================================== --- test/tools/llvm-nm/wasm/weak-symbols.yaml +++ test/tools/llvm-nm/wasm/weak-symbols.yaml @@ -43,7 +43,7 @@ Content: '616263' - Type: CUSTOM Name: linking - Version: 2 + Version: 3 SymbolTable: - Index: 0 Kind: DATA Index: tools/lld/wasm/InputFiles.cpp =================================================================== --- tools/lld/wasm/InputFiles.cpp +++ tools/lld/wasm/InputFiles.cpp @@ -378,7 +378,8 @@ switch (Sym.Info.Kind) { case WASM_SYMBOL_TYPE_FUNCTION: - return Symtab->addUndefinedFunction(Name, Sym.Info.Module, Flags, this, + return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName, + Sym.Info.ImportModule, Flags, this, Sym.Signature); case WASM_SYMBOL_TYPE_DATA: return Symtab->addUndefinedData(Name, Flags, this); @@ -447,7 +448,8 @@ if (ObjSym.isUndefined()) { if (ObjSym.isExecutable()) - return Symtab->addUndefinedFunction(Name, kDefaultModule, Flags, &F, nullptr); + return Symtab->addUndefinedFunction(Name, Name, kDefaultModule, Flags, &F, + nullptr); return Symtab->addUndefinedData(Name, Flags, &F); } Index: tools/lld/wasm/LTO.cpp =================================================================== --- tools/lld/wasm/LTO.cpp +++ tools/lld/wasm/LTO.cpp @@ -80,7 +80,8 @@ static void undefine(Symbol *S) { if (auto F = dyn_cast(S)) - replaceSymbol(F, F->getName(), kDefaultModule, 0, + replaceSymbol(F, F->getName(), F->getName(), + kDefaultModule, 0, F->getFile(), F->Signature); else if (isa(S)) replaceSymbol(S, S->getName(), 0, S->getFile()); Index: tools/lld/wasm/SymbolTable.h =================================================================== --- tools/lld/wasm/SymbolTable.h +++ tools/lld/wasm/SymbolTable.h @@ -58,7 +58,8 @@ Symbol *addDefinedEvent(StringRef Name, uint32_t Flags, InputFile *File, InputEvent *E); - Symbol *addUndefinedFunction(StringRef Name, StringRef Module, uint32_t Flags, + Symbol *addUndefinedFunction(StringRef Name, StringRef ImportName, + StringRef ImportModule, uint32_t Flags, InputFile *File, const WasmSignature *Signature); Symbol *addUndefinedData(StringRef Name, uint32_t Flags, InputFile *File); Symbol *addUndefinedGlobal(StringRef Name, uint32_t Flags, InputFile *File, Index: tools/lld/wasm/SymbolTable.cpp =================================================================== --- tools/lld/wasm/SymbolTable.cpp +++ tools/lld/wasm/SymbolTable.cpp @@ -315,7 +315,8 @@ return S; } -Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef Module, +Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName, + StringRef ImportModule, uint32_t Flags, InputFile *File, const WasmSignature *Sig) { LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name << @@ -326,7 +327,8 @@ std::tie(S, WasInserted) = insert(Name, File); if (WasInserted) - replaceSymbol(S, Name, Module, Flags, File, Sig); + replaceSymbol(S, Name, ImportName, ImportModule, Flags, + File, Sig); else if (auto *Lazy = dyn_cast(S)) Lazy->fetch(); else Index: tools/lld/wasm/Symbols.h =================================================================== --- tools/lld/wasm/Symbols.h +++ tools/lld/wasm/Symbols.h @@ -155,17 +155,19 @@ class UndefinedFunction : public FunctionSymbol { public: - UndefinedFunction(StringRef Name, StringRef Module, uint32_t Flags, + UndefinedFunction(StringRef Name, StringRef ImportName, + StringRef ImportModule, uint32_t Flags, InputFile *File = nullptr, const WasmSignature *Type = nullptr) : FunctionSymbol(Name, UndefinedFunctionKind, Flags, File, Type), - Module(Module) {} + ImportName(ImportName), ImportModule(ImportModule) {} static bool classof(const Symbol *S) { return S->kind() == UndefinedFunctionKind; } - StringRef Module; + StringRef ImportName; + StringRef ImportModule; }; class SectionSymbol : public Symbol { Index: tools/lld/wasm/Writer.cpp =================================================================== --- tools/lld/wasm/Writer.cpp +++ tools/lld/wasm/Writer.cpp @@ -184,12 +184,14 @@ for (const Symbol *Sym : ImportedSymbols) { WasmImport Import; - if (auto *F = dyn_cast(Sym)) - Import.Module = F->Module; - else + if (auto *F = dyn_cast(Sym)) { + Import.Field = F->ImportName; + Import.Module = F->ImportModule; + } else { + Import.Field = Sym->getName(); Import.Module = kDefaultModule; + } - Import.Field = Sym->getName(); if (auto *FunctionSym = dyn_cast(Sym)) { Import.Kind = WASM_EXTERNAL_FUNCTION; Import.SigIndex = lookupType(*FunctionSym->Signature); @@ -513,8 +515,7 @@ if (auto *F = dyn_cast(Sym)) { writeUleb128(Sub.OS, F->getFunctionIndex(), "index"); - if (Sym->isDefined()) - writeStr(Sub.OS, Sym->getName(), "sym name"); + writeStr(Sub.OS, Sym->getName(), "sym name"); } else if (auto *G = dyn_cast(Sym)) { writeUleb128(Sub.OS, G->getGlobalIndex(), "index"); if (Sym->isDefined()) Index: tools/yaml2obj/yaml2wasm.cpp =================================================================== --- tools/yaml2obj/yaml2wasm.cpp +++ tools/yaml2obj/yaml2wasm.cpp @@ -169,6 +169,9 @@ encodeULEB128(Info.Flags, SubSection.GetStream()); switch (Info.Kind) { case wasm::WASM_SYMBOL_TYPE_FUNCTION: + encodeULEB128(Info.ElementIndex, SubSection.GetStream()); + writeStringRef(Info.Name, SubSection.GetStream()); + break; case wasm::WASM_SYMBOL_TYPE_GLOBAL: case wasm::WASM_SYMBOL_TYPE_EVENT: encodeULEB128(Info.ElementIndex, SubSection.GetStream());