Index: lld/wasm/OutputSections.h =================================================================== --- lld/wasm/OutputSections.h +++ lld/wasm/OutputSections.h @@ -85,26 +85,6 @@ raw_string_ostream BodyOutputStream; }; -// Some synthetic sections (e.g. "name" and "linking") have subsections. -// Just like the synthetic sections themselves these need to be created before -// they can be written out (since they are preceded by their length). This -// class is used to create subsections and then write them into the stream -// of the parent section. -class SubSection : public SyntheticSection { -public: - explicit SubSection(uint32_t Type) : SyntheticSection(Type) {} - - std::string getSectionName() const; - - void writeStr(StringRef Str, StringRef Msg); - void writeUleb128(uint32_t Number, StringRef Msg); - - void writeToStream(raw_ostream &OS) { - OS.write(Header.data(), Header.size()); - OS.write(Body.data(), Body.size()); - } -}; - class CodeSection : public OutputSection { public: explicit CodeSection(ArrayRef Functions); Index: lld/wasm/OutputSections.cpp =================================================================== --- lld/wasm/OutputSections.cpp +++ lld/wasm/OutputSections.cpp @@ -66,18 +66,6 @@ return sectionTypeToString(Type); } -std::string SubSection::getSectionName() const { - return std::string("subsection "; -} - -void SubSection::writeStr(StringRef Str, StringRef Msg) { - lld::wasm::writeStr(BodyOutputStream, Str, Msg); -} - -void SubSection::writeUleb128(uint32_t Number, StringRef Msg) { - lld::wasm::writeUleb128(BodyOutputStream, Number, Msg); -} - void OutputSection::createHeader(size_t BodySize) { raw_string_ostream OS(Header); debugWrite(OS.tell(), "section type [" + Twine(getSectionName()) + "]"); Index: lld/wasm/Writer.cpp =================================================================== --- lld/wasm/Writer.cpp +++ lld/wasm/Writer.cpp @@ -385,6 +385,36 @@ return Flags; } +// Some synthetic sections (e.g. "name" and "linking") have subsections. +// Just like the synthetic sections themselves these need to be created before +// they can be written out (since they are preceded by their length). This +// class is used to create subsections and then write them into the stream +// of the parent section. +class SubSection { +public: + explicit SubSection(uint32_t Type) : Type(Type) {} + + void writeStr(StringRef Str, StringRef Msg) { + lld::wasm::writeStr(OS, Str, Msg); + } + + void writeUleb128(uint32_t Num, StringRef Msg) { + lld::wasm::writeUleb128(OS, Num, Msg); + } + + void writeTo(raw_ostream &To) { + OS.flush(); + lld::wasm::writeUleb128(To, Type, "subsection type"); + lld::wasm::writeUleb128(To, Body.size(), "subsection size"); + To.write(Body.data(), Body.size()); + } + +private: + uint32_t Type; + std::string Body; + raw_string_ostream OS{Body}; +}; + // Create the custom "linking" section containing linker metadata. // This is only created when relocatable output is requested. void Writer::createLinkingSection() { @@ -394,8 +424,7 @@ SubSection DataSizeSubSection(WASM_DATA_SIZE); DataSizeSubSection.writeUleb128(DataSize, "data size"); - DataSizeSubSection.finalizeContents(); - DataSizeSubSection.writeToStream(OS); + DataSizeSubSection.writeTo(OS); if (!Config->Relocatable) return; @@ -430,8 +459,7 @@ } } - Sub.finalizeContents(); - Sub.writeToStream(OS); + Sub.writeTo(OS); } if (Segments.size()) { @@ -442,8 +470,7 @@ Sub.writeUleb128(S->Alignment, "alignment"); Sub.writeUleb128(0, "flags"); } - Sub.finalizeContents(); - Sub.writeToStream(OS); + Sub.writeTo(OS); } if (!InitFunctions.empty()) { @@ -453,8 +480,7 @@ Sub.writeUleb128(F.Priority, "priority"); Sub.writeUleb128(F.Sym->getOutputSymbolIndex(), "function index"); } - Sub.finalizeContents(); - Sub.writeToStream(OS); + Sub.writeTo(OS); } struct ComdatEntry { unsigned Kind; uint32_t Index; }; @@ -491,8 +517,7 @@ Sub.writeUleb128(Entry.Index, "entry index"); } } - Sub.finalizeContents(); - Sub.writeToStream(OS); + Sub.writeTo(OS); } } @@ -527,8 +552,7 @@ } } - Sub.finalizeContents(); - Sub.writeToStream(Section->getStream()); + Sub.writeTo(Section->getStream()); } void Writer::writeHeader() {