aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2013-04-29 22:27:16 +0000
committerBill Wendling <isanbard@gmail.com>2013-04-29 22:27:16 +0000
commit7c6b7ba5f5410d4b552ee53c60d0690522c99959 (patch)
treec45ba694fd5902138b6f7b76d9665327513b6373 /lib/CodeGen/CodeGenModule.cpp
parentd237d2e6b85a74f31c986dc585fa6c39733b74a2 (diff)
Emit the TLS intialization functions into a list.
Add the TLS initialization functions to a list of initialization functions. The back-end takes this list and places the function pointers into the correct section. This way they're called before `main().' <rdar://problem/13733006> git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@180739 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--lib/CodeGen/CodeGenModule.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index bf67bd1007..6247b15ce9 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -182,6 +182,7 @@ void CodeGenModule::Release() {
AddGlobalCtor(ObjCInitFunction);
EmitCtorList(GlobalCtors, "llvm.global_ctors");
EmitCtorList(GlobalDtors, "llvm.global_dtors");
+ EmitTLSList(TLSInitFuncs);
EmitGlobalAnnotations();
EmitStaticExternCAliases();
EmitLLVMUsed();
@@ -479,6 +480,12 @@ void CodeGenModule::AddGlobalDtor(llvm::Function * Dtor, int Priority) {
GlobalDtors.push_back(std::make_pair(Dtor, Priority));
}
+/// AddTLSInitFunc - Add a function to the list that will initialize TLS
+/// variables before main() runs.
+void CodeGenModule::AddTLSInitFunc(llvm::Function *Init) {
+ TLSInitFuncs.push_back(Init);
+}
+
void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
// Ctor function type is void()*.
llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
@@ -507,6 +514,25 @@ void CodeGenModule::EmitCtorList(const CtorList &Fns, const char *GlobalName) {
}
}
+void CodeGenModule::EmitTLSList(ArrayRef<llvm::Constant*> Fns) {
+ if (Fns.empty()) return;
+
+ // TLS init function types are void()*.
+ llvm::FunctionType* TLSFTy = llvm::FunctionType::get(VoidTy, false);
+ llvm::Type *TLSPFTy = llvm::PointerType::getUnqual(TLSFTy);
+
+ SmallVector<llvm::Constant*, 8> Inits;
+ for (ArrayRef<llvm::Constant*>::iterator I = Fns.begin(),
+ E = Fns.end(); I != E; ++I)
+ Inits.push_back(llvm::ConstantExpr::getBitCast(*I, TLSPFTy));
+
+ llvm::ArrayType *AT = llvm::ArrayType::get(TLSPFTy, Inits.size());
+ new llvm::GlobalVariable(TheModule, AT, false,
+ llvm::GlobalValue::AppendingLinkage,
+ llvm::ConstantArray::get(AT, Inits),
+ "llvm.tls_init_funcs");
+}
+
llvm::GlobalValue::LinkageTypes
CodeGenModule::getFunctionLinkage(const FunctionDecl *D) {
GVALinkage Linkage = getContext().GetGVALinkageForFunction(D);