Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -304,6 +304,19 @@ } } +template static void fixStaticRelocs(InputSection *I) { + for (Relocation &R : I->Relocations) + R.Offset += I->OutSecOff; +} + +template +static void fixDynamicRelocs(OutputSectionBase *OutSec) { + for (DynamicReloc &D : Out::RelaDyn->Relocs) + if (D.getOutputSec() == OutSec) + D.setSectionOffset(D.getOffset() + + cast>(D.getInputSec())->OutSecOff); +} + template void assignOffsets(OutputSectionBase *Sec) { auto *OutSec = dyn_cast>(Sec); if (!OutSec) { @@ -330,12 +343,14 @@ Off = alignTo(Off, I->Alignment); I->OutSecOff = Off; Off += I->getSize(); + fixStaticRelocs(I); } // Update section size inside for-loop, so that SIZEOF // works correctly in the case below: // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } Sec->setSize(Off); } + fixDynamicRelocs(Sec); } template Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -237,9 +237,13 @@ UseSymVA(UseSymVA), Addend(Addend) {} uintX_t getOffset() const; + void setSectionOffset(uintX_t Offset) { OffsetInSec = Offset; } uintX_t getAddend() const; uint32_t getSymIndex() const; - const OutputSectionBase *getOutputSec() const { return OutputSec; } + const OutputSectionBase *getOutputSec() const { + return OutputSec ? OutputSec : InputSec->OutSec; + } + const InputSectionBase *getInputSec() const { return InputSec; } uint32_t Type; @@ -379,9 +383,10 @@ typename Base::Kind getKind() const override { return Base::Reloc; } static bool classof(const Base *B) { return B->getKind() == Base::Reloc; } + std::vector> Relocs; + private: bool Sort; - std::vector> Relocs; }; template