From 96a77693be7cfc87208840b2a367362104c43a6b Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Mon, 29 Apr 2013 22:27:16 +0000 Subject: 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().' llvm-svn: 180739 --- clang/lib/CodeGen/CodeGenModule.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index bf67bd1..6247b15 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/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 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 Inits; + for (ArrayRef::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); -- cgit v1.1