diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 5cabae30f8c0..eae8bd2fadb7 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -113,8 +113,7 @@ void InputSectionBase::relocate( if (Target->isTlsLocalDynamicReloc(Type) && !Target->isTlsOptimized(Type, nullptr)) { Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, - Out::Got->getVA() + - Out::LocalModuleTlsIndexOffset + + Out::Got->getLocalTlsIndexVA() + getAddend(RI)); continue; } diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index e3c910f52309..57d1e76b6178 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -87,10 +87,13 @@ template void GotSection::addDynTlsEntry(SymbolBody *Sym) { Entries.push_back(nullptr); } -template uint32_t GotSection::addLocalModuleTlsIndex() { +template bool GotSection::addLocalModelTlsIndex() { + if (LocalTlsIndexOff != uint32_t(-1)) + return false; Entries.push_back(nullptr); Entries.push_back(nullptr); - return (Entries.size() - 2) * sizeof(uintX_t); + LocalTlsIndexOff = (Entries.size() - 2) * sizeof(uintX_t); + return true; } template @@ -201,8 +204,7 @@ bool RelocationSection::applyTlsDynamicReloc(SymbolBody *Body, Elf_Rel *N) { if (Target->isTlsLocalDynamicReloc(Type)) { P->setSymbolAndType(0, Target->getTlsModuleIndexReloc(), Config->Mips64EL); - P->r_offset = - Out::Got->getVA() + Out::LocalModuleTlsIndexOffset; + P->r_offset = Out::Got->getLocalTlsIndexVA(); return true; } diff --git a/lld/ELF/OutputSections.h b/lld/ELF/OutputSections.h index dea9bb3c120e..a676ce31f37a 100644 --- a/lld/ELF/OutputSections.h +++ b/lld/ELF/OutputSections.h @@ -119,7 +119,7 @@ public: void writeTo(uint8_t *Buf) override; void addEntry(SymbolBody *Sym); void addDynTlsEntry(SymbolBody *Sym); - uint32_t addLocalModuleTlsIndex(); + bool addLocalModelTlsIndex(); bool empty() const { return Entries.empty(); } uintX_t getEntryAddr(const SymbolBody &B) const; @@ -133,8 +133,11 @@ public: // the number of reserved entries. This method is MIPS-specific. unsigned getMipsLocalEntriesNum() const; + uint32_t getLocalTlsIndexVA() { return getVA() + LocalTlsIndexOff; } + private: std::vector Entries; + uint32_t LocalTlsIndexOff = -1; }; template @@ -430,7 +433,6 @@ template struct Out { static SymbolTableSection *DynSymTab; static SymbolTableSection *SymTab; static Elf_Phdr *TlsPhdr; - static uint32_t LocalModuleTlsIndexOffset; }; template DynamicSection *Out::Dynamic; @@ -452,7 +454,6 @@ template StringTableSection *Out::StrTab; template SymbolTableSection *Out::DynSymTab; template SymbolTableSection *Out::SymTab; template typename Out::Elf_Phdr *Out::TlsPhdr; -template uint32_t Out::LocalModuleTlsIndexOffset = -1; } // namespace elf2 } // namespace lld diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 11fb9b020d46..16dedc53a4ed 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -205,11 +205,8 @@ void Writer::scanRelocs( if (Target->isTlsLocalDynamicReloc(Type)) { if (Target->isTlsOptimized(Type, nullptr)) continue; - if (Out::LocalModuleTlsIndexOffset == uint32_t(-1)) { - Out::LocalModuleTlsIndexOffset = - Out::Got->addLocalModuleTlsIndex(); + if (Out::Got->addLocalModelTlsIndex()) Out::RelaDyn->addReloc({&C, &RI}); - } continue; }