diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp --- a/lld/wasm/OutputSections.cpp +++ b/lld/wasm/OutputSections.cpp @@ -138,7 +138,7 @@ #ifndef NDEBUG unsigned activeCount = std::count_if( segments.begin(), segments.end(), [](OutputSegment *segment) { - return (segment->initFlags & WASM_SEGMENT_IS_PASSIVE) == 0; + return (segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0; }); #endif @@ -154,9 +154,9 @@ continue; raw_string_ostream os(segment->header); writeUleb128(os, segment->initFlags, "init flags"); - if (segment->initFlags & WASM_SEGMENT_HAS_MEMINDEX) + if (segment->initFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX) writeUleb128(os, 0, "memory index"); - if ((segment->initFlags & WASM_SEGMENT_IS_PASSIVE) == 0) { + if ((segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { WasmInitExpr initExpr; if (config->isPic) { initExpr.Opcode = WASM_OPCODE_GLOBAL_GET; diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -841,7 +841,7 @@ LLVM_DEBUG(dbgs() << "new segment: " << name << "\n"); s = make(name); if (config->sharedMemory) - s->initFlags = WASM_SEGMENT_IS_PASSIVE; + s->initFlags = WASM_DATA_SEGMENT_IS_PASSIVE; // Exported memories are guaranteed to be zero-initialized, so no need // to emit data segments for bss sections. // TODO: consider initializing bss sections with memory.fill @@ -886,7 +886,7 @@ } bool Writer::needsPassiveInitialization(const OutputSegment *segment) { - return segment->initFlags & WASM_SEGMENT_IS_PASSIVE && + return segment->initFlags & WASM_DATA_SEGMENT_IS_PASSIVE && segment->name != ".tdata" && !segment->isBss; } diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -146,8 +146,11 @@ struct WasmDataSegment { uint32_t InitFlags; - uint32_t MemoryIndex; // present if InitFlags & WASM_SEGMENT_HAS_MEMINDEX - WasmInitExpr Offset; // present if InitFlags & WASM_SEGMENT_IS_PASSIVE == 0 + // Present if InitFlags & WASM_DATA_SEGMENT_HAS_MEMINDEX. + uint32_t MemoryIndex; + // Present if InitFlags & WASM_DATA_SEGMENT_IS_PASSIVE == 0. + WasmInitExpr Offset; + ArrayRef Content; StringRef Name; // from the "segment info" section uint32_t Alignment; @@ -300,8 +303,8 @@ }; enum : unsigned { - WASM_SEGMENT_IS_PASSIVE = 0x01, - WASM_SEGMENT_HAS_MEMINDEX = 0x02, + WASM_DATA_SEGMENT_IS_PASSIVE = 0x01, + WASM_DATA_SEGMENT_HAS_MEMINDEX = 0x02, }; // Feature policy prefixes used in the custom "target_features" section diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -972,9 +972,9 @@ for (const WasmDataSegment &Segment : DataSegments) { encodeULEB128(Segment.InitFlags, W->OS); // flags - if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) + if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) encodeULEB128(0, W->OS); // memory index - if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { + if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { W->OS << char(Segment.Offset > INT32_MAX ? wasm::WASM_OPCODE_I64_CONST : wasm::WASM_OPCODE_I32_CONST); encodeSLEB128(Segment.Offset, W->OS); // offset @@ -1393,8 +1393,9 @@ DataSegments.emplace_back(); WasmDataSegment &Segment = DataSegments.back(); Segment.Name = SectionName; - Segment.InitFlags = - Section.getPassive() ? (uint32_t)wasm::WASM_SEGMENT_IS_PASSIVE : 0; + Segment.InitFlags = Section.getPassive() + ? (uint32_t)wasm::WASM_DATA_SEGMENT_IS_PASSIVE + : 0; Segment.Offset = DataSize; Segment.Section = &Section; addData(Segment.Data, Section); diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1366,9 +1366,11 @@ while (Count--) { WasmSegment Segment; Segment.Data.InitFlags = readVaruint32(Ctx); - Segment.Data.MemoryIndex = (Segment.Data.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) - ? readVaruint32(Ctx) : 0; - if ((Segment.Data.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { + Segment.Data.MemoryIndex = + (Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) + ? readVaruint32(Ctx) + : 0; + if ((Segment.Data.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { if (Error Err = readInitExpr(Segment.Data.Offset, Ctx)) return Err; } else { diff --git a/llvm/lib/ObjectYAML/WasmEmitter.cpp b/llvm/lib/ObjectYAML/WasmEmitter.cpp --- a/llvm/lib/ObjectYAML/WasmEmitter.cpp +++ b/llvm/lib/ObjectYAML/WasmEmitter.cpp @@ -526,9 +526,9 @@ encodeULEB128(Section.Segments.size(), OS); for (auto &Segment : Section.Segments) { encodeULEB128(Segment.InitFlags, OS); - if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) + if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) encodeULEB128(Segment.MemoryIndex, OS); - if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) + if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) writeInitExpr(OS, Segment.Offset); encodeULEB128(Segment.Content.binary_size(), OS); Segment.Content.writeAsBinary(OS); diff --git a/llvm/lib/ObjectYAML/WasmYAML.cpp b/llvm/lib/ObjectYAML/WasmYAML.cpp --- a/llvm/lib/ObjectYAML/WasmYAML.cpp +++ b/llvm/lib/ObjectYAML/WasmYAML.cpp @@ -448,12 +448,12 @@ IO &IO, WasmYAML::DataSegment &Segment) { IO.mapOptional("SectionOffset", Segment.SectionOffset); IO.mapRequired("InitFlags", Segment.InitFlags); - if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX) { + if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) { IO.mapRequired("MemoryIndex", Segment.MemoryIndex); } else { Segment.MemoryIndex = 0; } - if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) { + if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { IO.mapRequired("Offset", Segment.Offset); } else { Segment.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST;