[llvm] r267473 - ARM: put extern __thread stubs in a special section.

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 25 14:12:04 PDT 2016


Author: tnorthover
Date: Mon Apr 25 16:12:04 2016
New Revision: 267473

URL: http://llvm.org/viewvc/llvm-project?rev=267473&view=rev
Log:
ARM: put extern __thread stubs in a special section.

The linker needs to know that the symbols are thread-local to do its job
properly.

Added:
    llvm/trunk/test/MC/ARM/tls-directives.s
Modified:
    llvm/trunk/include/llvm/CodeGen/MachineModuleInfoImpls.h
    llvm/trunk/include/llvm/MC/MCObjectFileInfo.h
    llvm/trunk/lib/MC/MCAsmInfoDarwin.cpp
    llvm/trunk/lib/MC/MCObjectFileInfo.cpp
    llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp
    llvm/trunk/lib/MC/MachObjectWriter.cpp
    llvm/trunk/lib/Target/ARM/ARMAsmPrinter.cpp
    llvm/trunk/test/CodeGen/ARM/darwin-tls.ll

Modified: llvm/trunk/include/llvm/CodeGen/MachineModuleInfoImpls.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineModuleInfoImpls.h?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineModuleInfoImpls.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineModuleInfoImpls.h Mon Apr 25 16:12:04 2016
@@ -38,6 +38,11 @@ class MachineModuleInfoMachO : public Ma
   /// this GV is external.
   DenseMap<MCSymbol *, StubValueTy> HiddenGVStubs;
 
+  /// ThreadLocalGVStubs - Darwin '$non_lazy_ptr' stubs.  The key is something
+  /// like "Lfoo$non_lazy_ptr", the value is something like "_foo". The extra
+  /// bit is true if this GV is external.
+  DenseMap<MCSymbol *, StubValueTy> ThreadLocalGVStubs;
+
   virtual void anchor(); // Out of line virtual method.
 public:
   MachineModuleInfoMachO(const MachineModuleInfo &) {}
@@ -57,10 +62,18 @@ public:
     return HiddenGVStubs[Sym];
   }
 
+  StubValueTy &getThreadLocalGVStubEntry(MCSymbol *Sym) {
+    assert(Sym && "Key cannot be null");
+    return ThreadLocalGVStubs[Sym];
+  }
+
   /// Accessor methods to return the set of stubs in sorted order.
   SymbolListTy GetFnStubList() { return getSortedStubs(FnStubs); }
   SymbolListTy GetGVStubList() { return getSortedStubs(GVStubs); }
   SymbolListTy GetHiddenGVStubList() { return getSortedStubs(HiddenGVStubs); }
+  SymbolListTy GetThreadLocalGVStubList() {
+    return getSortedStubs(ThreadLocalGVStubs);
+  }
 };
 
 /// MachineModuleInfoELF - This is a MachineModuleInfoImpl implementation

Modified: llvm/trunk/include/llvm/MC/MCObjectFileInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCObjectFileInfo.h?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCObjectFileInfo.h (original)
+++ llvm/trunk/include/llvm/MC/MCObjectFileInfo.h Mon Apr 25 16:12:04 2016
@@ -185,6 +185,7 @@ protected:
   MCSection *SixteenByteConstantSection;
   MCSection *LazySymbolPointerSection;
   MCSection *NonLazySymbolPointerSection;
+  MCSection *ThreadLocalPointerSection;
 
   /// COFF specific sections.
   MCSection *DrectveSection;
@@ -333,6 +334,9 @@ public:
   MCSection *getNonLazySymbolPointerSection() const {
     return NonLazySymbolPointerSection;
   }
+  MCSection *getThreadLocalPointerSection() const {
+    return ThreadLocalPointerSection;
+  }
 
   // COFF specific sections.
   MCSection *getDrectveSection() const { return DrectveSection; }

Modified: llvm/trunk/lib/MC/MCAsmInfoDarwin.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmInfoDarwin.cpp?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCAsmInfoDarwin.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmInfoDarwin.cpp Mon Apr 25 16:12:04 2016
@@ -48,6 +48,7 @@ bool MCAsmInfoDarwin::isSectionAtomizabl
   case MachO::S_LITERAL_POINTERS:
   case MachO::S_NON_LAZY_SYMBOL_POINTERS:
   case MachO::S_LAZY_SYMBOL_POINTERS:
+  case MachO::S_THREAD_LOCAL_VARIABLE_POINTERS:
   case MachO::S_MOD_INIT_FUNC_POINTERS:
   case MachO::S_MOD_TERM_FUNC_POINTERS:
   case MachO::S_INTERPOSING:

Modified: llvm/trunk/lib/MC/MCObjectFileInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCObjectFileInfo.cpp?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCObjectFileInfo.cpp (original)
+++ llvm/trunk/lib/MC/MCObjectFileInfo.cpp Mon Apr 25 16:12:04 2016
@@ -172,6 +172,11 @@ void MCObjectFileInfo::initMachOMCObject
                            MachO::S_NON_LAZY_SYMBOL_POINTERS,
                            SectionKind::getMetadata());
 
+  ThreadLocalPointerSection
+    = Ctx->getMachOSection("__DATA", "__thread_ptr",
+                           MachO::S_THREAD_LOCAL_VARIABLE_POINTERS,
+                           SectionKind::getMetadata());
+
   if (RelocM == Reloc::Static) {
     StaticCtorSection = Ctx->getMachOSection("__TEXT", "__constructor", 0,
                                              SectionKind::getData());

Modified: llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/DarwinAsmParser.cpp Mon Apr 25 16:12:04 2016
@@ -112,6 +112,9 @@ public:
     addDirectiveHandler<
       &DarwinAsmParser::parseSectionDirectiveNonLazySymbolPointers>(
         ".non_lazy_symbol_pointer");
+    addDirectiveHandler<
+      &DarwinAsmParser::parseSectionDirectiveThreadLocalVariablePointers>(
+        ".thread_local_variable_pointer");
     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatClsMeth>(
       ".objc_cat_cls_meth");
     addDirectiveHandler<&DarwinAsmParser::parseSectionDirectiveObjCCatInstMeth>(
@@ -263,6 +266,10 @@ public:
     return parseSectionSwitch("__DATA", "__la_symbol_ptr",
                               MachO::S_LAZY_SYMBOL_POINTERS, 4);
   }
+  bool parseSectionDirectiveThreadLocalVariablePointers(StringRef, SMLoc) {
+    return parseSectionSwitch("__DATA", "__thread_ptr",
+                              MachO::S_THREAD_LOCAL_VARIABLE_POINTERS, 4);
+  }
   bool parseSectionDirectiveDyld(StringRef, SMLoc) {
     return parseSectionSwitch("__DATA", "__dyld");
   }
@@ -467,6 +474,7 @@ bool DarwinAsmParser::parseDirectiveIndi
   MachO::SectionType SectionType = Current->getType();
   if (SectionType != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
       SectionType != MachO::S_LAZY_SYMBOL_POINTERS &&
+      SectionType != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
       SectionType != MachO::S_SYMBOL_STUBS)
     return Error(Loc, "indirect symbol not in a symbol pointer or stub "
                       "section");

Modified: llvm/trunk/lib/MC/MachObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MachObjectWriter.cpp?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MachObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/MachObjectWriter.cpp Mon Apr 25 16:12:04 2016
@@ -457,6 +457,7 @@ void MachObjectWriter::bindIndirectSymbo
 
     if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
         Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
+        Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
         Section.getType() != MachO::S_SYMBOL_STUBS) {
       MCSymbol &Symbol = *it->Symbol;
       report_fatal_error("indirect symbol '" + Symbol.getName() +

Modified: llvm/trunk/lib/Target/ARM/ARMAsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMAsmPrinter.cpp?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/ARMAsmPrinter.cpp (original)
+++ llvm/trunk/lib/Target/ARM/ARMAsmPrinter.cpp Mon Apr 25 16:12:04 2016
@@ -527,6 +527,19 @@ void ARMAsmPrinter::EmitEndOfAsmFile(Mod
       OutStreamer->AddBlankLine();
     }
 
+    Stubs = MMIMacho.GetThreadLocalGVStubList();
+    if (!Stubs.empty()) {
+      // Switch with ".non_lazy_symbol_pointer" directive.
+      OutStreamer->SwitchSection(TLOFMacho.getThreadLocalPointerSection());
+      EmitAlignment(2);
+
+      for (auto &Stub : Stubs)
+        emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
+
+      Stubs.clear();
+      OutStreamer->AddBlankLine();
+    }
+
     // Funny Darwin hack: This flag tells the linker that no global symbols
     // contain code that falls through to other global symbols (e.g. the obvious
     // implementation of multiple entry points).  If this doesn't occur, the
@@ -891,8 +904,11 @@ MCSymbol *ARMAsmPrinter::GetARMGVSymbol(
     MachineModuleInfoMachO &MMIMachO =
       MMI->getObjFileInfo<MachineModuleInfoMachO>();
     MachineModuleInfoImpl::StubValueTy &StubSym =
-      GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym)
-                                : MMIMachO.getGVStubEntry(MCSym);
+        GV->isThreadLocal()
+            ? MMIMachO.getThreadLocalGVStubEntry(MCSym)
+            : (GV->hasHiddenVisibility() ? MMIMachO.getHiddenGVStubEntry(MCSym)
+                                         : MMIMachO.getGVStubEntry(MCSym));
+
     if (!StubSym.getPointer())
       StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
                                                    !GV->hasInternalLinkage());

Modified: llvm/trunk/test/CodeGen/ARM/darwin-tls.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/ARM/darwin-tls.ll?rev=267473&r1=267472&r2=267473&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/ARM/darwin-tls.ll (original)
+++ llvm/trunk/test/CodeGen/ARM/darwin-tls.ll Mon Apr 25 16:12:04 2016
@@ -10,6 +10,8 @@
 
 @local_tls_var = thread_local global i32 0
 @external_tls_var = external thread_local global i32
+ at hidden_external_tls_var = external hidden thread_local global i32
+
 
 define i32 @test_local_tls() {
 ; T2-MOVT-PIC-LABEL: test_local_tls:
@@ -163,3 +165,18 @@ define i32 @test_external_tls() {
   %val = load i32, i32* @external_tls_var, align 4
   ret i32 %val
 }
+
+; Just need something to trigger an indirect reference to the var.
+define i32 @use_hidden_external_tls() {
+  %val = load i32, i32* @hidden_external_tls_var, align 4
+  ret i32 %val
+}
+
+; T2-MOVT-PIC: .section __DATA,__thread_ptr,thread_local_variable_pointers
+; T2-MOVT-PIC: .p2align 2
+; T2-MOVT-PIC: L_external_tls_var$non_lazy_ptr:
+; T2-MOVT-PIC:     .indirect_symbol _external_tls_var
+; T2-MOVT-PIC:     .long 0
+; T2-MOVT-PIC: L_hidden_external_tls_var$non_lazy_ptr:
+; T2-MOVT-PIC:     .indirect_symbol _hidden_external_tls_var
+; T2-MOVT-PIC:     .long 0

Added: llvm/trunk/test/MC/ARM/tls-directives.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ARM/tls-directives.s?rev=267473&view=auto
==============================================================================
--- llvm/trunk/test/MC/ARM/tls-directives.s (added)
+++ llvm/trunk/test/MC/ARM/tls-directives.s Mon Apr 25 16:12:04 2016
@@ -0,0 +1,46 @@
+@ RUN: llvm-mc -triple thumbv7-apple-ios -filetype=obj -o %t %s
+@ RUN: llvm-objdump -p %t | FileCheck %s
+
+@ CHECK: sectname __thread_data
+@ CHECK: segname __DATA
+@ CHECK: type S_THREAD_LOCAL_REGULAR
+
+@ CHECK: sectname __thread_vars
+@ CHECK: segname __DATA
+@ CHECK: type S_THREAD_LOCAL_VARIABLES
+
+@ CHECK: sectname __thread_bss
+@ CHECK: segname __DATA
+@ CHECK: type S_THREAD_LOCAL_ZEROFILL
+
+@ CHECK: sectname __thread_ptr
+@ CHECK: segname __DATA
+@ CHECK: type S_THREAD_LOCAL_VARIABLE_POINTERS
+
+
+        .section        __DATA,__thread_data,thread_local_regular
+        .p2align        2
+_b$tlv$init:
+        .long 42
+
+        .section        __DATA,__thread_vars,thread_local_variables
+        .globl        _b
+_b:
+        .long        __tlv_bootstrap
+        .long        0
+        .long        _b$tlv$init
+
+.tbss _c$tlv$init, 4, 2                 @ @c
+
+        .globl        _c
+_c:
+        .long        __tlv_bootstrap
+        .long        0
+        .long        _c$tlv$init
+
+
+        .section        __DATA,__thread_ptr,thread_local_variable_pointers
+        .p2align        2
+L_a$non_lazy_ptr:
+        .indirect_symbol        _a
+        .long        0




More information about the llvm-commits mailing list