Index: include/llvm/Object/Wasm.h =================================================================== --- include/llvm/Object/Wasm.h +++ include/llvm/Object/Wasm.h @@ -212,9 +212,6 @@ const WasmSection &getWasmSection(DataRefImpl Ref) const; const wasm::WasmRelocation &getWasmRelocation(DataRefImpl Ref) const; - WasmSection* findCustomSectionByName(StringRef Name); - WasmSection* findSectionByType(uint32_t Type); - const uint8_t *getPtr(size_t Offset) const; Error parseSection(WasmSection &Sec); Error parseCustomSection(WasmSection &Sec, const uint8_t *Ptr, Index: lib/MC/WasmObjectWriter.cpp =================================================================== --- lib/MC/WasmObjectWriter.cpp +++ lib/MC/WasmObjectWriter.cpp @@ -50,6 +50,7 @@ uint64_t SizeOffset; // Where the contents of the section starts (after the header). uint64_t ContentsOffset; + uint32_t Index; }; // The signature of a wasm function, in a struct capable of being used as a @@ -167,6 +168,18 @@ #endif }; +#if !defined(NDEBUG) +raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) { + Rel.print(OS); + return OS; +} +#endif + +struct RelocationSection { + uint32_t TargetSection; + std::vector Relocations; +}; + struct WasmCustomSection { StringRef Name; const SmallVectorImpl &Contents; @@ -175,22 +188,17 @@ : Name(Name), Contents(Contents) {} }; -#if !defined(NDEBUG) -raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) { - Rel.print(OS); - return OS; -} -#endif - class WasmObjectWriter : public MCObjectWriter { /// The target specific Wasm writer instance. std::unique_ptr TargetObjectWriter; // Relocations for fixing up references in the code section. std::vector CodeRelocations; + uint32_t CodeSectionIndex; // Relocations for fixing up references in the data section. std::vector DataRelocations; + uint32_t DataSectionIndex; // Index values to use for fixing up call_indirect type indices. // Maps function symbols to the index of the type of the function @@ -213,6 +221,7 @@ std::vector CustomSections; unsigned NumFunctionImports = 0; unsigned NumGlobalImports = 0; + uint32_t SectionCount; // TargetObjectWriter wrappers. bool is64Bit() const { return TargetObjectWriter->is64Bit(); } @@ -280,8 +289,8 @@ void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, ArrayRef Functions); void writeDataSection(); - void writeCodeRelocSection(); - void writeDataRelocSection(); + void writeRelocSection(uint32_t SectionIndex, StringRef Name, + ArrayRef Relocations); void writeLinkingMetaDataSection( ArrayRef SymbolInfos, ArrayRef> InitFuncs, @@ -292,10 +301,9 @@ void applyRelocations(ArrayRef Relocations, uint64_t ContentsOffset); - void writeRelocations(ArrayRef Relocations); uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry); - uint32_t getFunctionType(const MCSymbolWasm& Symbol); - uint32_t registerFunctionType(const MCSymbolWasm& Symbol); + uint32_t getFunctionType(const MCSymbolWasm &Symbol); + uint32_t registerFunctionType(const MCSymbolWasm &Symbol); }; } // end anonymous namespace @@ -316,6 +324,7 @@ // The position where the section starts, for measuring its size. Section.ContentsOffset = getStream().tell(); + Section.Index = SectionCount++; } void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section, @@ -622,25 +631,6 @@ } } -// Write out the portions of the relocation records that the linker will -// need to handle. -void WasmObjectWriter::writeRelocations( - ArrayRef Relocations) { - raw_pwrite_stream &Stream = getStream(); - for (const WasmRelocationEntry& RelEntry : Relocations) { - - uint64_t Offset = RelEntry.Offset + - RelEntry.FixupSection->getSectionOffset(); - uint32_t Index = getRelocationIndexValue(RelEntry); - - write8(RelEntry.Type); - encodeULEB128(Offset, Stream); - encodeULEB128(Index, Stream); - if (RelEntry.hasAddend()) - encodeSLEB128(RelEntry.Addend, Stream); - } -} - void WasmObjectWriter::writeTypeSection( ArrayRef FunctionTypes) { if (FunctionTypes.empty()) @@ -787,6 +777,7 @@ SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_CODE); + CodeSectionIndex = Section.Index; encodeULEB128(Functions.size(), getStream()); @@ -814,6 +805,7 @@ SectionBookkeeping Section; startSection(Section, wasm::WASM_SEC_DATA); + DataSectionIndex = Section.Index; encodeULEB128(DataSegments.size(), getStream()); // count @@ -833,38 +825,33 @@ endSection(Section); } -void WasmObjectWriter::writeCodeRelocSection() { +void WasmObjectWriter::writeRelocSection( + uint32_t SectionIndex, StringRef Name, + ArrayRef Relocations) { // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md // for descriptions of the reloc sections. - if (CodeRelocations.empty()) + if (Relocations.empty()) return; SectionBookkeeping Section; - startCustomSection(Section, "reloc.CODE"); - - encodeULEB128(wasm::WASM_SEC_CODE, getStream()); - encodeULEB128(CodeRelocations.size(), getStream()); - - writeRelocations(CodeRelocations); - - endSection(Section); -} - -void WasmObjectWriter::writeDataRelocSection() { - // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md - // for descriptions of the reloc sections. + startCustomSection(Section, std::string("reloc.") + Name.str()); - if (DataRelocations.empty()) - return; - - SectionBookkeeping Section; - startCustomSection(Section, "reloc.DATA"); + raw_pwrite_stream &Stream = getStream(); - encodeULEB128(wasm::WASM_SEC_DATA, getStream()); - encodeULEB128(DataRelocations.size(), getStream()); + encodeULEB128(SectionIndex, Stream); + encodeULEB128(Relocations.size(), Stream); + for (const WasmRelocationEntry& RelEntry : Relocations) { + uint64_t Offset = RelEntry.Offset + + RelEntry.FixupSection->getSectionOffset(); + uint32_t Index = getRelocationIndexValue(RelEntry); - writeRelocations(DataRelocations); + write8(RelEntry.Type); + encodeULEB128(Offset, Stream); + encodeULEB128(Index, Stream); + if (RelEntry.hasAddend()) + encodeSLEB128(RelEntry.Addend, Stream); + } endSection(Section); } @@ -1350,8 +1337,8 @@ writeDataSection(); writeUserCustomSections(CustomSections); writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); - writeCodeRelocSection(); - writeDataRelocSection(); + writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations); + writeRelocSection(DataSectionIndex, "DATA", DataRelocations); // TODO: Translate the .comment section to the output. // TODO: Translate debug sections to the output. Index: lib/Object/WasmObjectFile.cpp =================================================================== --- lib/Object/WasmObjectFile.cpp +++ lib/Object/WasmObjectFile.cpp @@ -524,38 +524,15 @@ return Error::success(); } -WasmSection* WasmObjectFile::findCustomSectionByName(StringRef Name) { - for (WasmSection& Section : Sections) { - if (Section.Type == wasm::WASM_SEC_CUSTOM && Section.Name == Name) - return &Section; - } - return nullptr; -} - -WasmSection* WasmObjectFile::findSectionByType(uint32_t Type) { - assert(Type != wasm::WASM_SEC_CUSTOM); - for (WasmSection& Section : Sections) { - if (Section.Type == Type) - return &Section; - } - return nullptr; -} - Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr, const uint8_t *End) { - uint8_t SectionCode = readUint8(Ptr); - WasmSection* Section = nullptr; - if (SectionCode == wasm::WASM_SEC_CUSTOM) { - StringRef Name = readString(Ptr); - Section = findCustomSectionByName(Name); - } else { - Section = findSectionByType(SectionCode); - } - if (!Section) - return make_error("Invalid section code", + uint8_t SectionIndex = readUint8(Ptr); + if (SectionIndex >= Sections.size()) + return make_error("Invalid section index", object_error::parse_failed); + WasmSection& Section = Sections[SectionIndex]; uint32_t RelocCount = readVaruint32(Ptr); - uint32_t EndOffset = Section->Content.size(); + uint32_t EndOffset = Section.Content.size(); while (RelocCount--) { wasm::WasmRelocation Reloc = {}; Reloc.Type = readVaruint32(Ptr); @@ -604,7 +581,7 @@ return make_error("Bad relocation offset", object_error::parse_failed); - Section->Relocations.push_back(Reloc); + Section.Relocations.push_back(Reloc); } if (Ptr != End) return make_error("Reloc section ended prematurely",