[lld] r302414 - [ELF] - Set DF_STATIC_TLS flag for i386 target.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue May 9 12:46:32 PDT 2017


George Rimar via llvm-commits <llvm-commits at lists.llvm.org> writes:

> Author: grimar
> Date: Mon May  8 05:24:38 2017
> New Revision: 302414
>
> URL: http://llvm.org/viewvc/llvm-project?rev=302414&view=rev
> Log:
> [ELF] - Set DF_STATIC_TLS flag for i386 target.
>
> This is PR32437.
>
> DF_STATIC_TLS
> If set in a shared object or executable, this flag instructs the 
> dynamic linker to reject attempts to load this file dynamically. 
> It indicates that the shared object or executable contains code 
> using a static thread-local storage scheme. Implementations need 
> not support any form of thread-local storage.
>
> Patch checks if IE/LE relocations were used to check if code uses
> static model. If so it sets the DF_STATIC_TLS flag.
>
> Differential revision: https://reviews.llvm.org/D32354
>
> Added:
>     lld/trunk/test/ELF/Inputs/i386-static-tls-model1.s
>     lld/trunk/test/ELF/Inputs/i386-static-tls-model2.s
>     lld/trunk/test/ELF/Inputs/i386-static-tls-model3.s
>     lld/trunk/test/ELF/Inputs/i386-static-tls-model4.s
>     lld/trunk/test/ELF/i386-static-tls-model.s
> Modified:
>     lld/trunk/ELF/Config.h
>     lld/trunk/ELF/SyntheticSections.cpp
>     lld/trunk/ELF/Target.cpp
>     lld/trunk/test/ELF/i386-tls-ie-shared.s
>     lld/trunk/test/ELF/tls-dynamic-i686.s
>     lld/trunk/test/ELF/tls-opt-iele-i686-nopic.s
>
> Modified: lld/trunk/ELF/Config.h
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=302414&r1=302413&r2=302414&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Config.h (original)
> +++ lld/trunk/ELF/Config.h Mon May  8 05:24:38 2017
> @@ -73,6 +73,7 @@ struct VersionDefinition {
>  // Most fields are initialized by the driver.
>  struct Configuration {
>    InputFile *FirstElf = nullptr;
> +  bool HasStaticTlsModel = false;
>    uint8_t OSABI = 0;
>    llvm::CachePruningPolicy ThinLTOCachePolicy;
>    llvm::StringMap<uint64_t> SectionStartMap;
>
> Modified: lld/trunk/ELF/SyntheticSections.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=302414&r1=302413&r2=302414&view=diff
> ==============================================================================
> --- lld/trunk/ELF/SyntheticSections.cpp (original)
> +++ lld/trunk/ELF/SyntheticSections.cpp Mon May  8 05:24:38 2017
> @@ -1038,6 +1038,15 @@ template <class ELFT> void DynamicSectio
>    if (!Config->SoName.empty())
>      add({DT_SONAME, In<ELFT>::DynStrTab->addString(Config->SoName)});
>  
> +  if (!Config->Shared && !Config->Relocatable)
> +    add({DT_DEBUG, (uint64_t)0});
> +}
> +
> +// Add remaining entries to complete .dynamic contents.
> +template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
> +  if (this->Size)
> +    return; // Already finalized.
> +
>    // Set DT_FLAGS and DT_FLAGS_1.
>    uint32_t DtFlags = 0;
>    uint32_t DtFlags1 = 0;
> @@ -1055,21 +1064,14 @@ template <class ELFT> void DynamicSectio
>      DtFlags |= DF_ORIGIN;
>      DtFlags1 |= DF_1_ORIGIN;
>    }
> +  if (Config->HasStaticTlsModel)
> +    DtFlags |= DF_STATIC_TLS;
>  
>    if (DtFlags)
>      add({DT_FLAGS, DtFlags});
>    if (DtFlags1)
>      add({DT_FLAGS_1, DtFlags1});
>  
> -  if (!Config->Shared && !Config->Relocatable)
> -    add({DT_DEBUG, (uint64_t)0});
> -}
> -
> -// Add remaining entries to complete .dynamic contents.
> -template <class ELFT> void DynamicSection<ELFT>::finalizeContents() {
> -  if (this->Size)
> -    return; // Already finalized.
> -
>    this->Link = In<ELFT>::DynStrTab->OutSec->SectionIndex;
>    if (In<ELFT>::RelaDyn->OutSec->Size > 0) {
>      bool IsRela = Config->IsRela;
>
> Modified: lld/trunk/ELF/Target.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=302414&r1=302413&r2=302414&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Target.cpp (original)
> +++ lld/trunk/ELF/Target.cpp Mon May  8 05:24:38 2017
> @@ -351,6 +351,15 @@ X86TargetInfo::X86TargetInfo() {
>  
>  RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &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. They cannot be used for dlopen(). If they are in use,
> +  // we set DF_STATIC_TLS in the ELF header so that the runtime can reject such
> +  // DSOs.
> +  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;

Have also have to set this for every other architecture we support, no?

Could we do it by looking at RelExpr?

Cheers,
Rafael


More information about the llvm-commits mailing list