Index: llvm/include/llvm/MC/MCSymbolWasm.h =================================================================== --- llvm/include/llvm/MC/MCSymbolWasm.h +++ llvm/include/llvm/MC/MCSymbolWasm.h @@ -14,7 +14,7 @@ namespace llvm { class MCSymbolWasm : public MCSymbol { - wasm::WasmSymbolType Type = wasm::WASM_SYMBOL_TYPE_DATA; + Optional Type; bool IsWeak = false; bool IsHidden = false; bool IsComdat = false; @@ -40,13 +40,22 @@ const MCExpr *getSize() const { return SymbolSize; } void setSize(const MCExpr *SS) { SymbolSize = SS; } - bool isFunction() const { return Type == wasm::WASM_SYMBOL_TYPE_FUNCTION; } - bool isData() const { return Type == wasm::WASM_SYMBOL_TYPE_DATA; } - bool isGlobal() const { return Type == wasm::WASM_SYMBOL_TYPE_GLOBAL; } - bool isTable() const { return Type == wasm::WASM_SYMBOL_TYPE_TABLE; } - bool isSection() const { return Type == wasm::WASM_SYMBOL_TYPE_SECTION; } - bool isEvent() const { return Type == wasm::WASM_SYMBOL_TYPE_EVENT; } - wasm::WasmSymbolType getType() const { return Type; } + bool isFunction() const { return getType() == wasm::WASM_SYMBOL_TYPE_FUNCTION; } + bool isData() const { return getType() == wasm::WASM_SYMBOL_TYPE_DATA; } + bool isGlobal() const { return getType() == wasm::WASM_SYMBOL_TYPE_GLOBAL; } + bool isTable() const { return getType() == wasm::WASM_SYMBOL_TYPE_TABLE; } + bool isSection() const { return getType() == wasm::WASM_SYMBOL_TYPE_SECTION; } + bool isEvent() const { return getType() == wasm::WASM_SYMBOL_TYPE_EVENT; } + + // Deprecated: For backwards compatibility, WASM_SYMBOL_TYPE_DATA used to be + // the default. If possible, call getDefinedType instead, and/or ensure + // symbols always have the correct type set when constructed. + wasm::WasmSymbolType getType() const { + return Type.getValueOr(wasm::WASM_SYMBOL_TYPE_DATA); + } + + Optional getDefinedType() const { return Type; } + void setType(wasm::WasmSymbolType type) { Type = type; } bool isExported() const { Index: llvm/lib/MC/WasmObjectWriter.cpp =================================================================== --- llvm/lib/MC/WasmObjectWriter.cpp +++ llvm/lib/MC/WasmObjectWriter.cpp @@ -488,10 +488,14 @@ const MCSymbol *SectionSymbol = nullptr; const MCSection &SecA = SymA->getSection(); - if (SecA.getKind().isText()) - SectionSymbol = SectionFunctions.find(&SecA)->second; - else + if (SecA.getKind().isText()) { + auto SecSymIt = SectionFunctions.find(&SecA); + if (SecSymIt == SectionFunctions.end()) + report_fatal_error("section doesn\'t have defining symbol"); + SectionSymbol = SecSymIt->second; + } else { SectionSymbol = SecA.getBeginSymbol(); + } if (!SectionSymbol) report_fatal_error("section symbol is required for relocation"); Index: llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp =================================================================== --- llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp +++ llvm/lib/Target/WebAssembly/AsmParser/WebAssemblyAsmParser.cpp @@ -990,6 +990,20 @@ } void doBeforeLabelEmit(MCSymbol *Symbol) override { + // Code below only applies to labels in text sections. + auto CWS = cast(getStreamer().getCurrentSection().first); + if (!CWS || !CWS->getKind().isText()) + return; + + auto WasmSym = cast(Symbol); + // Unlike other targets, we don't allow data in text sections (labels + // declared with .type @object). + if (WasmSym->getDefinedType() == wasm::WASM_SYMBOL_TYPE_DATA) { + Parser.Error(Parser.getTok().getLoc(), + "WASM doesn\'t support data symbols in text sections"); + return; + } + // Start a new section for the next function automatically, since our // object writer expects each function to have its own section. This way // The user can't forget this "convention". @@ -997,14 +1011,10 @@ if (SymName.startswith(".L")) return; // Local Symbol. - // Only create a new text section if we're already in one. // TODO: If the user explicitly creates a new function section, we ignore // its name when we create this one. It would be nice to honor their // choice, while still ensuring that we create one if they forget. // (that requires coordination with WasmAsmParser::parseSectionDirective) - auto CWS = cast(getStreamer().getCurrentSection().first); - if (!CWS || !CWS->getKind().isText()) - return; auto SecName = ".text." + SymName; auto *Group = CWS->getGroup(); @@ -1013,7 +1023,7 @@ // for importing comdat functions. But there's no way to specify that in // assembly currently. if (Group) - cast(Symbol)->setComdat(true); + WasmSym->setComdat(true); auto *WS = getContext().getWasmSection(SecName, SectionKind::getText(), Group, MCContext::GenericSectionID, nullptr); Index: llvm/test/MC/WebAssembly/basic-assembly-errors.s =================================================================== --- llvm/test/MC/WebAssembly/basic-assembly-errors.s +++ llvm/test/MC/WebAssembly/basic-assembly-errors.s @@ -4,6 +4,10 @@ # (must be 0.0 or similar) f32.const 0 +# CHECK: WASM doesn't support data symbols in text sections + .type objerr,@object +objerr: + # CHECK: End of block construct with no start: end_try end_try test0: