Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -192,6 +192,34 @@ }; } +template +static bool handleTlsRelocation(unsigned Type, SymbolBody *Body, + InputSectionBase &C, RelT &RI) { + if (Target->isTlsLocalDynamicReloc(Type)) { + if (Target->isTlsOptimized(Type, nullptr)) + return true; + if (Out::Got->addCurrentModuleTlsIndex()) + Out::RelaDyn->addReloc({&C, &RI}); + return true; + } + + if (!Body || !Body->isTls()) + return false; + + if (Target->isTlsGlobalDynamicReloc(Type)) { + bool Opt = Target->isTlsOptimized(Type, Body); + if (!Opt && Out::Got->addDynTlsEntry(Body)) { + Out::RelaDyn->addReloc({&C, &RI}); + Out::RelaDyn->addReloc({nullptr, nullptr}); + Body->setUsedInDynamicReloc(); + return true; + } + if (!canBePreempted(Body, true)) + return true; + } + return !Target->isTlsDynReloc(Type, *Body); +} + // The reason we have to do this early scan is as follows // * To mmap the output file, we need to know the size // * For that, we need to know how many dynamic relocs we will have. @@ -219,14 +247,6 @@ if (Target->isGotRelative(Type)) HasGotOffRel = true; - if (Target->isTlsLocalDynamicReloc(Type)) { - if (Target->isTlsOptimized(Type, nullptr)) - continue; - if (Out::Got->addCurrentModuleTlsIndex()) - Out::RelaDyn->addReloc({&C, &RI}); - continue; - } - // Set "used" bit for --as-needed. if (Body && Body->isUndefined() && !Body->isWeak()) if (auto *S = dyn_cast>(Body->repl())) @@ -235,19 +255,7 @@ if (Body) Body = Body->repl(); - if (Body && Body->isTls() && Target->isTlsGlobalDynamicReloc(Type)) { - bool Opt = Target->isTlsOptimized(Type, Body); - if (!Opt && Out::Got->addDynTlsEntry(Body)) { - Out::RelaDyn->addReloc({&C, &RI}); - Out::RelaDyn->addReloc({nullptr, nullptr}); - Body->setUsedInDynamicReloc(); - continue; - } - if (!canBePreempted(Body, true)) - continue; - } - - if (Body && Body->isTls() && !Target->isTlsDynReloc(Type, *Body)) + if (handleTlsRelocation(Type, Body, C, RI)) continue; if (Target->relocNeedsDynRelative(Type)) {