Index: include/llvm/Object/Wasm.h =================================================================== --- include/llvm/Object/Wasm.h +++ include/llvm/Object/Wasm.h @@ -183,7 +183,6 @@ bool isSectionData(DataRefImpl Sec) const override; bool isSectionBSS(DataRefImpl Sec) const override; bool isSectionVirtual(DataRefImpl Sec) const override; - bool isSectionBitcode(DataRefImpl Sec) const override; relocation_iterator section_rel_begin(DataRefImpl Sec) const override; relocation_iterator section_rel_end(DataRefImpl Sec) const override; @@ -208,6 +207,10 @@ const WasmSection &getWasmSection(DataRefImpl Ref) const; const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const; + const wasm::WasmDataSegment &getWasmDataSegment(DataRefImpl Ref) const; + + const ArrayRef &getDataRefContent(DataRefImpl Ref) const; + WasmSection* findCustomSectionByName(StringRef Name); WasmSection* findSectionByType(uint32_t Type); @@ -261,7 +264,7 @@ uint32_t ImportSection = 0; uint32_t ExportSection = 0; - StringMap SymbolMap; + StringMap SymbolMap; }; } // end namespace object Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -842,7 +842,13 @@ write8(wasm::WASM_OPCODE_I32_CONST); encodeSLEB128(Segment.Offset, getStream()); // offset write8(wasm::WASM_OPCODE_END); - encodeULEB128(Segment.Data.size(), getStream()); // size + + uint64_t Padding = OffsetToAlignment(getStream().tell() + 5, Segment.Alignment); + unsigned SizeLen = encodeULEB128(Segment.Data.size() + Padding, getStream(), 5); // size + assert(SizeLen == 5); + + WriteZeros(Padding); + assert(OffsetToAlignment(getStream().tell(), Segment.Alignment) == 0 && "DataSegments is not aligned"); Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset); writeBytes(Segment.Data); // data } Index: lib/Object/WasmObjectFile.cpp =================================================================== --- lib/Object/WasmObjectFile.cpp +++ lib/Object/WasmObjectFile.cpp @@ -24,6 +24,7 @@ #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/LEB128.h" +#include "llvm/Support/MathExtras.h" #include #include #include @@ -982,7 +983,14 @@ return section_iterator(SectionRef(Ref, this)); } -void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { Sec.d.a++; } +void WasmObjectFile::moveSectionNext(DataRefImpl &Sec) const { + if (isSectionData(Sec) && Sec.d.b < DataSegments.size()) { + Sec.d.b++; + } else { + Sec.d.a++; + Sec.d.b = 0; + } +} std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec, StringRef &Res) const { @@ -1002,7 +1010,11 @@ ECase(START); ECase(ELEM); ECase(CODE); - ECase(DATA); + case wasm::WASM_SEC_DATA: { + const WasmSegment &Seg = DataSegments[Sec.d.b]; + Res = Seg.Data.Name; + break; + } case wasm::WASM_SEC_CUSTOM: Res = S.Name; break; @@ -1020,22 +1032,45 @@ } uint64_t WasmObjectFile::getSectionSize(DataRefImpl Sec) const { - const WasmSection &S = Sections[Sec.d.a]; - return S.Content.size(); + return getDataRefContent(Sec).size(); } std::error_code WasmObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const { - const WasmSection &S = Sections[Sec.d.a]; - // This will never fail since wasm sections can never be empty (user-sections - // must have a name and non-user sections each have a defined structure). - Res = StringRef(reinterpret_cast(S.Content.data()), - S.Content.size()); + + const ArrayRef &Content = getDataRefContent(Sec); + + Res = StringRef(reinterpret_cast(Content.data()), + Content.size()); return std::error_code(); } uint64_t WasmObjectFile::getSectionAlignment(DataRefImpl Sec) const { - return 1; + if (isSectionData(Sec)) { + const wasm::WasmDataSegment &Data = getWasmDataSegment(Sec); + return Data.Alignment; + } + + return 1; +} + +const wasm::WasmDataSegment &WasmObjectFile::getWasmDataSegment(DataRefImpl Sec) const { + assert(isSectionData(Sec) && "Section should be Data"); + assert(Sec.d.b < DataSegments.size()); + return DataSegments[Sec.d.b].Data; +} + +const ArrayRef &WasmObjectFile::getDataRefContent(DataRefImpl Sec) const { + if (isSectionData(Sec)) { + const wasm::WasmDataSegment &Data = getWasmDataSegment(Sec); + size_t Padding = alignmentAdjustment(Data.Content.data(), Data.Alignment); + + ArrayRef AdjustedContent(Data.Content.data() + Padding, Data.Content.size() - Padding); + return std::move(AdjustedContent); + } + // This will never fail since wasm sections can never be empty (user-sections + // must have a name and non-user sections each have a defined structure). + return getWasmSection(Sec).Content; } bool WasmObjectFile::isSectionCompressed(DataRefImpl Sec) const { @@ -1054,8 +1089,6 @@ bool WasmObjectFile::isSectionVirtual(DataRefImpl Sec) const { return false; } -bool WasmObjectFile::isSectionBitcode(DataRefImpl Sec) const { return false; } - relocation_iterator WasmObjectFile::section_rel_begin(DataRefImpl Ref) const { DataRefImpl RelocRef; RelocRef.d.a = Ref.d.a; @@ -1113,6 +1146,7 @@ section_iterator WasmObjectFile::section_begin() const { DataRefImpl Ref; Ref.d.a = 0; + Ref.d.b = 0; return section_iterator(SectionRef(Ref, this)); }