Index: wasm/InputChunks.h =================================================================== --- wasm/InputChunks.h +++ wasm/InputChunks.h @@ -194,7 +194,7 @@ } virtual const WasmInitExpr &initExpr() const { return Global->InitExpr; } - StringRef getComdat() const override { return StringRef(); } + StringRef getComdat() const override { return Global->Comdat; } uint32_t getOutputIndex() const override { return OutputIndex.getValue(); } bool hasOutputIndex() const override { return OutputIndex.hasValue(); } void setOutputIndex(uint32_t Index); Index: wasm/InputFiles.cpp =================================================================== --- wasm/InputFiles.cpp +++ wasm/InputFiles.cpp @@ -296,8 +296,13 @@ case WasmSymbol::SymbolType::GLOBAL: if (WasmSym.WasmIndex >= NumGlobalImports) { InputGlobal *Global = getGlobal(WasmSym); - S = createDefined(WasmSym, Symbol::Kind::DefinedGlobalKind, Global); - break; + if (!isExcludedByComdat(Global)) { + S = createDefined(WasmSym, Symbol::Kind::DefinedGlobalKind, Global); + break; + } else { + Global->Discarded = true; + // Exclude global, and add the symbol as undefined + } } S = createUndefined(WasmSym, Symbol::Kind::UndefinedGlobalKind); break; Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -462,6 +462,12 @@ Comdats[Comdat].emplace_back( ComdatEntry{WASM_COMDAT_FUNCTION, F->getOutputIndex()}); } + for (const InputGlobal *G : DefinedGlobals) { + StringRef Comdat = G->getComdat(); + if (!Comdat.empty()) + Comdats[Comdat].emplace_back( + ComdatEntry{WASM_COMDAT_GLOBAL, G->getOutputIndex()}); + } for (uint32_t I = 0; I < Segments.size(); ++I) { const auto &InputSegments = Segments[I]->InputSegments; if (InputSegments.empty())