diff options
author | Chris Lattner <sabre@nondot.org> | 2009-03-31 22:37:52 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-03-31 22:37:52 +0000 |
commit | f41e87f4cdf8ab94a3eff198b6d76f685f7c23cc (patch) | |
tree | 13fa8f8a5bab6ea6890bee64c4cd2a24276c81b3 /clang/lib/CodeGen/CodeGenModule.cpp | |
parent | 2739d2bbe68efae3416b80294d8477c732105872 (diff) | |
download | bcm5719-llvm-f41e87f4cdf8ab94a3eff198b6d76f685f7c23cc.tar.gz bcm5719-llvm-f41e87f4cdf8ab94a3eff198b6d76f685f7c23cc.zip |
Change UsedArray to be a vector of WeakVH to fix a dangling pointer problem that occurs when
attribute(used) and asm renaming are used together.
llvm-svn: 68155
Diffstat (limited to 'clang/lib/CodeGen/CodeGenModule.cpp')
-rw-r--r-- | clang/lib/CodeGen/CodeGenModule.cpp | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 3de8156fe51..6c0cc7ef2bb 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -320,8 +320,7 @@ void CodeGenModule::SetFunctionAttributes(const FunctionDecl *FD, void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) { assert(!GV->isDeclaration() && "Only globals with definition can force usage."); - llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); - LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy)); + LLVMUsed.push_back(GV); } void CodeGenModule::EmitLLVMUsed() { @@ -329,12 +328,21 @@ void CodeGenModule::EmitLLVMUsed() { if (LLVMUsed.empty()) return; - llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(), - LLVMUsed.size()); + llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); + llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, LLVMUsed.size()); + + // Convert LLVMUsed to what ConstantArray needs. + std::vector<llvm::Constant*> UsedArray; + UsedArray.resize(LLVMUsed.size()); + for (unsigned i = 0, e = LLVMUsed.size(); i != e; ++i) { + UsedArray[i] = + llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(&*LLVMUsed[i]), i8PTy); + } + llvm::GlobalVariable *GV = new llvm::GlobalVariable(ATy, false, llvm::GlobalValue::AppendingLinkage, - llvm::ConstantArray::get(ATy, LLVMUsed), + llvm::ConstantArray::get(ATy, UsedArray), "llvm.used", &getModule()); GV->setSection("llvm.metadata"); @@ -725,7 +733,6 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D) { Entry->replaceAllUsesWith(NewPtrForOldDecl); // Erase the old global, since it is no longer used. - // FIXME: What if it was attribute used? Dangling pointer from LLVMUsed. cast<llvm::GlobalValue>(Entry)->eraseFromParent(); } @@ -839,8 +846,6 @@ void CodeGenModule::EmitGlobalFunctionDefinition(const FunctionDecl *D) { Entry->replaceAllUsesWith(NewPtrForOldDecl); // Ok, delete the old function now, which is dead. - // FIXME: If it was attribute(used) the pointer will dangle from the - // LLVMUsed array! cast<llvm::GlobalValue>(Entry)->eraseFromParent(); Entry = NewFn; @@ -905,7 +910,6 @@ void CodeGenModule::EmitAliasDefinition(const ValueDecl *D) { Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, Entry->getType())); - // FIXME: What if it was attribute used? Dangling pointer from LLVMUsed. Entry->eraseFromParent(); } |