diff --git a/lld/lib/ReaderWriter/ELF/DefaultLayout.h b/lld/lib/ReaderWriter/ELF/DefaultLayout.h index 14519962c7e5..0bfa51ceb0f6 100644 --- a/lld/lib/ReaderWriter/ELF/DefaultLayout.h +++ b/lld/lib/ReaderWriter/ELF/DefaultLayout.h @@ -247,6 +247,13 @@ public: return _relocationTable; } + uint64_t getTLSSize() const { + for (const auto &phdr : *_programHeader) + if (phdr->p_type == llvm::ELF::PT_TLS) + return phdr->p_memsz; + return 0; + } + private: SectionMapT _sectionMap; MergedSectionMapT _mergedSectionMap; @@ -316,6 +323,10 @@ StringRef DefaultLayout::getSectionName( return ".text"; if (name.startswith(".rodata")) return ".rodata"; + if (name.startswith(".tdata")) + return ".tdata"; + if (name.startswith(".tbss")) + return ".tbss"; return name; } diff --git a/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.cpp b/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.cpp index ec7ba4c7f2bc..db0fc94f2b50 100644 --- a/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.cpp +++ b/lld/lib/ReaderWriter/ELF/X86_64/X86_64TargetHandler.cpp @@ -78,19 +78,8 @@ ErrorOr X86_64TargetRelocationHandler::applyRelocation( case R_X86_64_TPOFF64: case R_X86_64_DTPOFF32: case R_X86_64_TPOFF32: { - // Get the start and end of the TLS segment. - if (_tlsSize == 0) { - auto tdata = _targetInfo.getTargetHandler().targetLayout() - .findOutputSection(".tdata"); - auto tbss = _targetInfo.getTargetHandler().targetLayout() - .findOutputSection(".tbss"); - // HACK: The tdata and tbss sections end up together to from the TLS - // segment. This should actually use the TLS program header entry. - if (tdata) - _tlsSize = tdata->memSize(); - if (tbss) - _tlsSize += tbss->memSize(); - } + _tlsSize = _targetInfo.getTargetHandler().targetLayout() + .getTLSSize(); if (ref.kind() == R_X86_64_TPOFF32 || ref.kind() == R_X86_64_DTPOFF32) { int32_t result = (int32_t)(targetVAddress - _tlsSize); *reinterpret_cast(location) = result;