Index: lld/trunk/wasm/InputSegment.h =================================================================== --- lld/trunk/wasm/InputSegment.h +++ lld/trunk/wasm/InputSegment.h @@ -21,6 +21,7 @@ #ifndef LLD_WASM_INPUT_SEGMENT_H #define LLD_WASM_INPUT_SEGMENT_H +#include "WriterUtils.h" #include "lld/Common/ErrorHandler.h" #include "llvm/Object/Wasm.h" @@ -62,6 +63,7 @@ const WasmSegment *Segment; const ObjFile *File; std::vector Relocations; + std::vector OutRelocations; protected: const OutputSegment *OutputSeg = nullptr; Index: lld/trunk/wasm/OutputSections.h =================================================================== --- lld/trunk/wasm/OutputSections.h +++ lld/trunk/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: lld/trunk/wasm/OutputSections.cpp =================================================================== --- lld/trunk/wasm/OutputSections.cpp +++ lld/trunk/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); }); } @@ -288,13 +288,13 @@ Segment->setSectionOffset(BodySize); BodySize += Segment->Header.size(); log("Data segment: size=" + Twine(Segment->Size)); - for (const InputSegment *InputSeg : Segment->InputSegments) { + for (InputSegment *InputSeg : Segment->InputSegments) { uint32_t InputOffset = InputSeg->getInputSectionOffset(); uint32_t OutputOffset = Segment->getSectionOffset() + Segment->Header.size() + InputSeg->getOutputSegmentOffset(); - calcRelocations(*InputSeg->File, InputSeg->Relocations, Relocations, - OutputOffset - InputOffset); + calcRelocations(*InputSeg->File, InputSeg->Relocations, + InputSeg->OutRelocations, OutputOffset - InputOffset); } BodySize += Segment->Size; } @@ -327,13 +327,22 @@ memcpy(SegStart + Segment->Header.size() + Input->getOutputSegmentOffset(), Content.data(), Content.size()); + applyRelocations(ContentsStart, Input->OutRelocations); } }); +} - applyRelocations(ContentsStart, Relocations); +uint32_t DataSection::numRelocations() const { + uint32_t Count = 0; + for (const OutputSegment *Seg : Segments) + for (const InputSegment *InputSeg : Seg->InputSegments) + Count += InputSeg->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 InputSegment *InputSeg : Seg->InputSegments) + for (const OutputRelocation &Reloc : InputSeg->OutRelocations) + writeReloc(OS, Reloc); } Index: lld/trunk/wasm/OutputSegment.h =================================================================== --- lld/trunk/wasm/OutputSegment.h +++ lld/trunk/wasm/OutputSegment.h @@ -38,7 +38,7 @@ StringRef Name; uint32_t Alignment = 0; uint32_t StartVA = 0; - std::vector InputSegments; + std::vector InputSegments; // Sum of the size of the all the input segments uint32_t Size = 0;