Index: wasm/OutputSections.h =================================================================== --- wasm/OutputSections.h +++ wasm/OutputSections.h @@ -126,11 +126,10 @@ explicit DataSection(ArrayRef Segments); size_t getSize() const override { return Header.size() + BodySize; } void writeTo(uint8_t *Buf) override; - uint32_t numRelocations() const override { return Relocations.size(); } + uint32_t numRelocations() const override; void writeRelocations(raw_ostream &OS) const override; protected: - std::vector Relocations; ArrayRef Segments; std::string DataSectionHeader; size_t BodySize = 0; Index: wasm/OutputSections.cpp =================================================================== --- wasm/OutputSections.cpp +++ wasm/OutputSections.cpp @@ -109,10 +109,11 @@ } static void applyRelocations(uint8_t *Buf, ArrayRef Relocs) { + if (!Relocs.size()) + return; log("applyRelocations: count=" + Twine(Relocs.size())); - for (const OutputRelocation &Reloc : Relocs) { + for (const OutputRelocation &Reloc : Relocs) applyRelocation(Buf, Reloc); - } } // Relocations contain an index into the function, global or table index @@ -251,8 +252,7 @@ PayloadSize); log("applying relocations for: " + File->getName()); - if (File->CodeRelocations.size()) - applyRelocations(ContentsStart, File->CodeRelocations); + applyRelocations(ContentsStart, File->CodeRelocations); }); } @@ -293,8 +293,8 @@ uint32_t OutputOffset = Segment->getSectionOffset() + Segment->Header.size() + InputSeg->getOutputSegmentOffset(); - calcRelocations(*InputSeg->File, InputSeg->Relocations, Relocations, - OutputOffset - InputOffset); + calcRelocations(*InputSeg->File, InputSeg->Relocations, + Segment->OutRelocations, OutputOffset - InputOffset); } BodySize += Segment->Size; } @@ -328,12 +328,20 @@ Input->getOutputSegmentOffset(), Content.data(), Content.size()); } + applyRelocations(ContentsStart, Segment->OutRelocations); }); - applyRelocations(ContentsStart, Relocations); +} + +uint32_t DataSection::numRelocations() const { + uint32_t Count = 0; + for (const OutputSegment *Seg: Segments) + Count += Seg->OutRelocations.size(); + return Count; } void DataSection::writeRelocations(raw_ostream &OS) const { - for (const OutputRelocation &Reloc : Relocations) - writeReloc(OS, Reloc); + for (const OutputSegment *Seg: Segments) + for (const OutputRelocation &Reloc : Seg->OutRelocations) + writeReloc(OS, Reloc); } Index: wasm/OutputSegment.h =================================================================== --- wasm/OutputSegment.h +++ wasm/OutputSegment.h @@ -11,6 +11,7 @@ #define LLD_WASM_OUTPUT_SEGMENT_H #include "InputSegment.h" +#include "WriterUtils.h" #include "lld/Common/ErrorHandler.h" #include "llvm/Object/Wasm.h" @@ -39,6 +40,7 @@ uint32_t Alignment = 0; uint32_t StartVA = 0; std::vector InputSegments; + std::vector OutRelocations; // Sum of the size of the all the input segments uint32_t Size = 0; Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -174,7 +174,7 @@ Import.Field = Sym->getName(); Import.Kind = WASM_EXTERNAL_GLOBAL; Import.Global.Mutable = false; - Import.Global.Type = WASM_TYPE_I32; // Sym->getGlobalType(); + Import.Global.Type = WASM_TYPE_I32; writeImport(OS, Import); } } @@ -196,11 +196,9 @@ raw_ostream &OS = Section->getStream(); writeUleb128(OS, NumFunctions, "function count"); - for (ObjFile *File : Symtab->ObjectFiles) { - for (uint32_t Sig : File->getWasmObj()->functionTypes()) { + for (ObjFile *File : Symtab->ObjectFiles) + for (uint32_t Sig : File->getWasmObj()->functionTypes()) writeUleb128(OS, File->relocateTypeIndex(Sig), "sig index"); - } - } } void Writer::createMemorySection() { @@ -358,7 +356,7 @@ OutputSections.push_back(Section); } -// Create reloctions sections in the final output. +// Create relocations sections in the final output. // These are only created when relocatable output is requested. void Writer::createRelocSections() { log("createRelocSections"); @@ -376,7 +374,7 @@ else if (S->Type == WASM_SEC_CODE) name = "reloc.CODE"; else - llvm_unreachable("relocations only support for code and data"); + llvm_unreachable("relocations only supported for code and data"); SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name); raw_ostream &OS = Section->getStream(); @@ -417,10 +415,9 @@ for (ObjFile *File : Symtab->ObjectFiles) { const WasmLinkingData &L = File->getWasmObj()->linkingData(); InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size()); - for (const WasmInitFunc &F : L.InitFunctions) { + for (const WasmInitFunc &F : L.InitFunctions) InitFunctions.emplace_back(WasmInitFunc{ F.Priority, File->relocateFunctionIndex(F.FunctionIndex)}); - } } if (!InitFunctions.empty()) {