From 060062a433d1c2c5f1f89f6c42858a5d48bef9e1 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 6 Mar 2014 22:15:10 +0000 Subject: Use llvm.compiler.used instead of llvm.used for objc symbols. LLVM currently has a hack (shouldEmitUsedDirectiveFor) that causes it to not print no_dead_strip for symbols starting with 'l' or 'L'. These are exactly the ones that the clang's objc codegen is producing. The net result, is that it is equivalent to llvm.compiler.used. The need for putting the private symbol in llvm.compiler.used should be clear (the objc runtime uses them). The reason for also putting the weak symbols in it is for LTO: ld64 will not ask us to preserve the it. llvm-svn: 203172 --- clang/lib/CodeGen/CodeGenModule.cpp | 40 ++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index f0265b33..9b3e329 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -258,7 +258,7 @@ void CodeGenModule::Release() { EmitCtorList(GlobalDtors, "llvm.global_dtors"); EmitGlobalAnnotations(); EmitStaticExternCAliases(); - EmitLLVMUsed(); + emitLLVMUsed(); if (CodeGenOpts.Autolink && (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { @@ -699,7 +699,7 @@ void CodeGenModule::SetCommonAttributes(const Decl *D, GV->setVisibility(llvm::GlobalValue::DefaultVisibility); if (D->hasAttr()) - AddUsedGlobal(GV); + addUsedGlobal(GV); if (const SectionAttr *SA = D->getAttr()) GV->setSection(SA->getName()); @@ -776,39 +776,51 @@ void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Attribute::NoBuiltin); } -void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { +void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { assert(!GV->isDeclaration() && "Only globals with definition can force usage."); LLVMUsed.push_back(GV); } -void CodeGenModule::EmitLLVMUsed() { +void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { + assert(!GV->isDeclaration() && + "Only globals with definition can force usage."); + LLVMCompilerUsed.push_back(GV); +} + +static void emitUsed(CodeGenModule &CGM, StringRef Name, + std::vector &List) { // Don't create llvm.used if there is no need. - if (LLVMUsed.empty()) + if (List.empty()) return; - // Convert LLVMUsed to what ConstantArray needs. + // Convert List to what ConstantArray needs. SmallVector UsedArray; - UsedArray.resize(LLVMUsed.size()); - for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { + UsedArray.resize(List.size()); + for (unsigned i = 0, e = List.size(); i != e; ++i) { UsedArray[i] = - llvm::ConstantExpr::getBitCast(cast(&*LLVMUsed[i]), - Int8PtrTy); + llvm::ConstantExpr::getBitCast(cast(&*List[i]), + CGM.Int8PtrTy); } if (UsedArray.empty()) return; - llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size()); + llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); llvm::GlobalVariable *GV = - new llvm::GlobalVariable(getModule(), ATy, false, + new llvm::GlobalVariable(CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, llvm::ConstantArray::get(ATy, UsedArray), - "llvm.used"); + Name); GV->setSection("llvm.metadata"); } +void CodeGenModule::emitLLVMUsed() { + emitUsed(*this, "llvm.used", LLVMUsed); + emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); +} + void CodeGenModule::AppendLinkerOptions(StringRef Opts) { llvm::Value *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); @@ -3054,7 +3066,7 @@ void CodeGenModule::EmitStaticExternCAliases() { IdentifierInfo *Name = I->first; llvm::GlobalValue *Val = I->second; if (Val && !getModule().getNamedValue(Name->getName())) - AddUsedGlobal(new llvm::GlobalAlias(Val->getType(), Val->getLinkage(), + addUsedGlobal(new llvm::GlobalAlias(Val->getType(), Val->getLinkage(), Name->getName(), Val, &getModule())); } } -- cgit v1.1