From 6fdea1bda839653c05a523e20760997d59dfb030 Mon Sep 17 00:00:00 2001 From: Eric Christopher Date: Sat, 22 May 2010 00:10:22 +0000 Subject: [PATCH] Add full bss data support for darwin tls variables. llvm-svn: 104414 --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 27 ++++++++++++++++++- .../CodeGen/TargetLoweringObjectFileImpl.cpp | 10 +++++-- llvm/test/CodeGen/X86/tls-1.ll | 19 +++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 llvm/test/CodeGen/X86/tls-1.ll diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 50a50750e8bf..d0b48b0db8d2 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -314,7 +314,32 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) { // Handle the tbss directive on darwin which is a thread local bss directive // like zerofill. if (GVKind.isThreadBSS() && MAI->hasMachoTBSSDirective()) { - OutStreamer.EmitTBSSSymbol(TheSection, GVSym, Size, 1 << AlignLog); + // Emit the .tbss symbol + MCSymbol *MangSym = + OutContext.GetOrCreateSymbol(GVSym->getName() + Twine("$tlv$init")); + OutStreamer.EmitTBSSSymbol(TheSection, MangSym, Size, 1 << AlignLog); + OutStreamer.AddBlankLine(); + + // Emit the variable struct for the runtime. + const MCSection *TLVSect + = getObjFileLowering().getTLSExtraDataSection(); + + OutStreamer.SwitchSection(TLVSect); + // Emit the linkage here. + EmitLinkage(GV->getLinkage(), GVSym); + OutStreamer.EmitLabel(GVSym); + + // Three pointers in size: + // - __tlv_bootstrap - used to make sure support exists + // - spare pointer, used when mapped by the runtime + // - pointer to mangled symbol above with initializer + unsigned PtrSize = TD->getPointerSizeInBits()/8; + OutStreamer.EmitSymbolValue(GetExternalSymbolSymbol("__tlv_bootstrap"), + PtrSize, 0); + OutStreamer.EmitIntValue(0, PtrSize, 0); + OutStreamer.EmitSymbolValue(MangSym, PtrSize, 0); + + OutStreamer.AddBlankLine(); return; } diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 837681985859..605e2a817b1f 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -627,6 +627,8 @@ void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx, getContext().getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG, SectionKind::getMetadata()); + + TLSExtraDataSection = TLSTLVSection; } const MCSection *TargetLoweringObjectFileMachO:: @@ -666,9 +668,13 @@ getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind, const MCSection *TargetLoweringObjectFileMachO:: SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, - Mangler *Mang, const TargetMachine &TM) const { - assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); + Mangler *Mang, const TargetMachine &TM) const { + + // Handle one kind of thread local... + if (Kind.isThreadBSS()) return TLSBSSSection; + assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS"); + if (Kind.isText()) return GV->isWeakForLinker() ? TextCoalSection : TextSection; diff --git a/llvm/test/CodeGen/X86/tls-1.ll b/llvm/test/CodeGen/X86/tls-1.ll new file mode 100644 index 000000000000..408e5bb0c60c --- /dev/null +++ b/llvm/test/CodeGen/X86/tls-1.ll @@ -0,0 +1,19 @@ +; RUN: llc < %s -march=x86-64 | FileCheck %s + +@a = thread_local global i32 0 ; [#uses=0] +@b = thread_local global i32 0 ; [#uses=0] + +; CHECK: .tbss _a$tlv$init, 4, 2 +; CHECK: .section __DATA,__thread_vars,thread_local_variables +; CHECK: .globl _a +; CHECK: _a: +; CHECK: .quad ___tlv_bootstrap +; CHECK: .quad 0 +; CHECK: .quad _a$tlv$init + +; CHECK: .tbss _b$tlv$init, 4, 2 +; CHECK: .globl _b +; CHECK: _b: +; CHECK: .quad ___tlv_bootstrap +; CHECK: .quad 0 +; CHECK: .quad _b$tlv$init