Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -154,17 +154,18 @@ SymbolBody &Body, InputSectionBase &C, uint64_t Offset, int64_t Addend, RelExpr Expr) { - auto addModuleReloc = [&](uint64_t Off, bool LD) { - // The Dynamic TLS Module Index Relocation can be statically resolved to 1 - // if we know that the TLS Symbol is in an executable. - if (!Body.isPreemptible() && !Config->Pic) - Got->Relocations.push_back( - {R_ABS, Target->TlsModuleIndexRel, Off, 0, &Body}); - else { - SymbolBody *Dest = LD ? nullptr : &Body; - In::RelaDyn->addReloc( - {Target->TlsModuleIndexRel, Got, Off, false, Dest, 0}); - } + // The Dynamic TLS Module Index Relocation for a symbol defined in an + // executable is always 1. If the target Symbol is not preemtible then + // we know the offset into the TLS block at static link time. + bool NeedDynId = Body.isPreemptible() || Config->Pic; + bool NeedDynOff = Body.isPreemptible(); + + auto AddTlsReloc = [&](uint64_t Off, uint32_t Type, SymbolBody *Dest, + bool Dyn) { + if (Dyn) + In::RelaDyn->addReloc({Type, Got, Off, false, Dest, 0}); + else + Got->Relocations.push_back({R_ABS, Type, Off, 0, Dest}); }; // Local Dynamic is for access to module local TLS variables, while still @@ -172,7 +173,8 @@ // GOT[e0] is the module index, with a special value of 0 for the current // module. GOT[e1] is unused. There only needs to be one module index entry. if (Expr == R_TLSLD_PC && Got->addTlsIndex()) { - addModuleReloc(Got->getTlsIndexOff(), true); + AddTlsReloc(Got->getTlsIndexOff(), Target->TlsModuleIndexRel, + NeedDynId ? nullptr : &Body, NeedDynId); C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); return 1; } @@ -183,13 +185,9 @@ if (Expr == R_TLSGD_PC) { if (Got->addDynTlsEntry(Body)) { uint64_t Off = Got->getGlobalDynOffset(Body); - addModuleReloc(Off, false); - if (Body.isPreemptible()) - In::RelaDyn->addReloc({Target->TlsOffsetRel, Got, - Off + Config->Wordsize, false, &Body, 0}); - else - Got->Relocations.push_back( - {R_ABS, Target->TlsOffsetRel, Off + Config->Wordsize, 0, &Body}); + AddTlsReloc(Off, Target->TlsModuleIndexRel, &Body, NeedDynId); + AddTlsReloc(Off + Config->Wordsize, Target->TlsOffsetRel, &Body, + NeedDynOff); } C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); return 1;