From 1bf8c89f2828449e20eebba99ee1950f57a85975 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Thu, 21 Feb 2019 18:41:41 +0000 Subject: [PATCH] Pull in r353299 from upstream lld trunk (by George Rimar): Recommit r353293 "[LLD][ELF] - Set DF_STATIC_TLS flag for i386 target." With the following changes: 1) Compilation fix: std::atomic HasStaticTlsModel = false; -> std::atomic HasStaticTlsModel{false}; 2) Adjusted the comment in code. Initial commit message: DF_STATIC_TLS flag indicates that the shared object or executable contains code using a static thread-local storage scheme. Patch checks if IE/LE relocations were used to check if the code uses a static model. If so it sets the DF_STATIC_TLS flag. Differential revision: https://reviews.llvm.org/D57749 Pull in r353378 from upstream lld trunk (by George Rimar): [LLD][ELF] - Set DF_STATIC_TLS flag for X64 target This is the same as D57749, but for x64 target. "ELF Handling For Thread-Local Storage" p41 says (https://www.akkadia.org/drepper/tls.pdf): R_X86_64_GOTTPOFF relocation is used for IE TLS models. Hence if linker sees this relocation we should add DF_STATIC_TLS flag. Differential revision: https://reviews.llvm.org/D57821 This adds support to lld for the DF_STATIC_TLS flag in shared objects, which signals to the dynamic linker that the shared object requires static thread local storage. See also: https://reviews.freebsd.org/D19072 MFC after: 1 week --- contrib/llvm/tools/lld/ELF/Arch/X86.cpp | 8 ++++++++ contrib/llvm/tools/lld/ELF/Arch/X86_64.cpp | 3 +++ contrib/llvm/tools/lld/ELF/Config.h | 2 ++ contrib/llvm/tools/lld/ELF/SyntheticSections.cpp | 2 ++ 4 files changed, 15 insertions(+) diff --git a/contrib/llvm/tools/lld/ELF/Arch/X86.cpp b/contrib/llvm/tools/lld/ELF/Arch/X86.cpp index 0624fe78750c..8513b9ad2389 100644 --- a/contrib/llvm/tools/lld/ELF/Arch/X86.cpp +++ b/contrib/llvm/tools/lld/ELF/Arch/X86.cpp @@ -70,6 +70,14 @@ static bool hasBaseReg(uint8_t ModRM) { return (ModRM & 0xc7) != 0x5; } RelExpr X86::getRelExpr(RelType Type, const Symbol &S, const uint8_t *Loc) const { + // There are 4 different TLS variable models with varying degrees of + // flexibility and performance. LocalExec and InitialExec models are fast but + // less-flexible models. If they are in use, we set DF_STATIC_TLS flag in the + // dynamic section to let runtime know about that. + if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 || Type == R_386_TLS_IE || + Type == R_386_TLS_GOTIE) + Config->HasStaticTlsModel = true; + switch (Type) { case R_386_8: case R_386_16: diff --git a/contrib/llvm/tools/lld/ELF/Arch/X86_64.cpp b/contrib/llvm/tools/lld/ELF/Arch/X86_64.cpp index d4bdb3730c58..24b4be9f95ec 100644 --- a/contrib/llvm/tools/lld/ELF/Arch/X86_64.cpp +++ b/contrib/llvm/tools/lld/ELF/Arch/X86_64.cpp @@ -76,6 +76,9 @@ template X86_64::X86_64() { template RelExpr X86_64::getRelExpr(RelType Type, const Symbol &S, const uint8_t *Loc) const { + if (Type == R_X86_64_GOTTPOFF) + Config->HasStaticTlsModel = true; + switch (Type) { case R_X86_64_8: case R_X86_64_16: diff --git a/contrib/llvm/tools/lld/ELF/Config.h b/contrib/llvm/tools/lld/ELF/Config.h index 2a488c3a1189..1425b66d1d41 100644 --- a/contrib/llvm/tools/lld/ELF/Config.h +++ b/contrib/llvm/tools/lld/ELF/Config.h @@ -18,6 +18,7 @@ #include "llvm/Support/CachePruning.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/Endian.h" +#include #include namespace lld { @@ -81,6 +82,7 @@ struct VersionDefinition { // and such fields have the same name as the corresponding options. // Most fields are initialized by the driver. struct Configuration { + std::atomic HasStaticTlsModel{false}; uint8_t OSABI = 0; llvm::CachePruningPolicy ThinLTOCachePolicy; llvm::StringMap SectionStartMap; diff --git a/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp b/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp index 8e2ad243c1c3..6ec6eeb46063 100644 --- a/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp +++ b/contrib/llvm/tools/lld/ELF/SyntheticSections.cpp @@ -1282,6 +1282,8 @@ template void DynamicSection::finalizeContents() { } if (!Config->ZText) DtFlags |= DF_TEXTREL; + if (Config->HasStaticTlsModel) + DtFlags |= DF_STATIC_TLS; if (DtFlags) addInt(DT_FLAGS, DtFlags);