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;