From 12da8ce3d2b9c365b2e204fcf2b4002177fc6b8b Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Sat, 7 Mar 2009 15:45:40 +0000 Subject: Introduce new linkage types linkonce_odr, weak_odr, common_odr and extern_weak_odr. These are the same as the non-odr versions, except that they indicate that the global will only be overridden by an *equivalent* global. In C, a function with weak linkage can be overridden by a function which behaves completely differently. This means that IP passes have to skip weak functions, since any deductions made from the function definition might be wrong, since the definition could be replaced by something completely different at link time. This is not allowed in C++, thanks to the ODR (One-Definition-Rule): if a function is replaced by another at link-time, then the new function must be the same as the original function. If a language knows that a function or other global can only be overridden by an equivalent global, it can give it the weak_odr linkage type, and the optimizers will understand that it is alright to make deductions based on the function body. The code generators on the other hand map weak and weak_odr linkage to the same thing. llvm-svn: 66339 --- llvm/lib/CodeGen/AsmPrinter/DwarfWriter.cpp | 17 ++++++++++------- llvm/lib/CodeGen/ELFWriter.cpp | 6 ++++-- llvm/lib/CodeGen/IntrinsicLowering.cpp | 4 ++-- llvm/lib/CodeGen/MachOWriter.cpp | 9 ++++++--- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 2 +- llvm/lib/CodeGen/ShadowStackGC.cpp | 4 ++-- 6 files changed, 25 insertions(+), 17 deletions(-) (limited to 'llvm/lib/CodeGen') diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfWriter.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfWriter.cpp index 5c4957aba11..9181216b5ab 100644 --- a/llvm/lib/CodeGen/AsmPrinter/DwarfWriter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/DwarfWriter.cpp @@ -3135,9 +3135,8 @@ public: GlobalVariable *GV = getGlobalVariable(V); if (!GV) return false; - - if (GV->getLinkage() != GlobalValue::InternalLinkage - && GV->getLinkage() != GlobalValue::LinkOnceLinkage) + + if (!GV->hasInternalLinkage () && !GV->hasLinkOnceLinkage()) return false; DIDescriptor DI(GV); @@ -3449,8 +3448,10 @@ class DwarfException : public Dwarf { } // If corresponding function is weak definition, this should be too. - if ((linkage == Function::WeakLinkage || - linkage == Function::LinkOnceLinkage) && + if ((linkage == Function::WeakAnyLinkage || + linkage == Function::WeakODRLinkage || + linkage == Function::LinkOnceAnyLinkage || + linkage == Function::LinkOnceODRLinkage) && TAI->getWeakDefDirective()) O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n"; @@ -3461,8 +3462,10 @@ class DwarfException : public Dwarf { // unwind info is to be available for non-EH uses. if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory && - ((linkage != Function::WeakLinkage && - linkage != Function::LinkOnceLinkage) || + ((linkage != Function::WeakAnyLinkage && + linkage != Function::WeakODRLinkage && + linkage != Function::LinkOnceAnyLinkage && + linkage != Function::LinkOnceODRLinkage) || !TAI->getWeakDefDirective() || TAI->getSupportsWeakOmittedEHFrame())) { diff --git a/llvm/lib/CodeGen/ELFWriter.cpp b/llvm/lib/CodeGen/ELFWriter.cpp index 7cca74b1fb1..ddc4e3588b8 100644 --- a/llvm/lib/CodeGen/ELFWriter.cpp +++ b/llvm/lib/CodeGen/ELFWriter.cpp @@ -174,8 +174,10 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &F) { case GlobalValue::ExternalLinkage: FnSym.SetBind(ELFWriter::ELFSym::STB_GLOBAL); break; - case GlobalValue::LinkOnceLinkage: - case GlobalValue::WeakLinkage: + case GlobalValue::LinkOnceAnyLinkage: + case GlobalValue::LinkOnceODRLinkage: + case GlobalValue::WeakAnyLinkage: + case GlobalValue::WeakODRLinkage: FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK); break; case GlobalValue::PrivateLinkage: diff --git a/llvm/lib/CodeGen/IntrinsicLowering.cpp b/llvm/lib/CodeGen/IntrinsicLowering.cpp index 5b4de27b8a9..56b1736b415 100644 --- a/llvm/lib/CodeGen/IntrinsicLowering.cpp +++ b/llvm/lib/CodeGen/IntrinsicLowering.cpp @@ -316,7 +316,7 @@ static Instruction *LowerPartSelect(CallInst *CI) { Name[i] = '_'; Module* M = F->getParent(); F = cast(M->getOrInsertFunction(Name, FT)); - F->setLinkage(GlobalValue::WeakLinkage); + F->setLinkage(GlobalValue::WeakAnyLinkage); // If we haven't defined the impl function yet, do so now if (F->isDeclaration()) { @@ -490,7 +490,7 @@ static Instruction *LowerPartSet(CallInst *CI) { Name[i] = '_'; Module* M = F->getParent(); F = cast(M->getOrInsertFunction(Name, FT)); - F->setLinkage(GlobalValue::WeakLinkage); + F->setLinkage(GlobalValue::WeakAnyLinkage); // If we haven't defined the impl function yet, do so now if (F->isDeclaration()) { diff --git a/llvm/lib/CodeGen/MachOWriter.cpp b/llvm/lib/CodeGen/MachOWriter.cpp index 22c21578ec0..5fe05a4e8f0 100644 --- a/llvm/lib/CodeGen/MachOWriter.cpp +++ b/llvm/lib/CodeGen/MachOWriter.cpp @@ -956,9 +956,12 @@ MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect, default: assert(0 && "Unexpected linkage type!"); break; - case GlobalValue::WeakLinkage: - case GlobalValue::LinkOnceLinkage: - case GlobalValue::CommonLinkage: + case GlobalValue::WeakAnyLinkage: + case GlobalValue::WeakODRLinkage: + case GlobalValue::LinkOnceAnyLinkage: + case GlobalValue::LinkOnceODRLinkage: + case GlobalValue::CommonAnyLinkage: + case GlobalValue::CommonODRLinkage: assert(!isa(gv) && "Unexpected linkage type for Function!"); case GlobalValue::ExternalLinkage: GVName = TAI->getGlobalPrefix() + name; diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp index 7913c019631..a08dcb2e49d 100644 --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -728,7 +728,7 @@ TargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { if (getTargetMachine().getRelocationModel() == Reloc::DynamicNoPIC && GA && !GA->getGlobal()->isDeclaration() && - !GA->getGlobal()->mayBeOverridden()) + !GA->getGlobal()->isWeakForLinker()) return true; // Otherwise assume nothing is safe. diff --git a/llvm/lib/CodeGen/ShadowStackGC.cpp b/llvm/lib/CodeGen/ShadowStackGC.cpp index 5a55760c7c5..2402f81bb04 100644 --- a/llvm/lib/CodeGen/ShadowStackGC.cpp +++ b/llvm/lib/CodeGen/ShadowStackGC.cpp @@ -293,12 +293,12 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) { // If the root chain does not exist, insert a new one with linkonce // linkage! Head = new GlobalVariable(StackEntryPtrTy, false, - GlobalValue::LinkOnceLinkage, + GlobalValue::LinkOnceAnyLinkage, Constant::getNullValue(StackEntryPtrTy), "llvm_gc_root_chain", &M); } else if (Head->hasExternalLinkage() && Head->isDeclaration()) { Head->setInitializer(Constant::getNullValue(StackEntryPtrTy)); - Head->setLinkage(GlobalValue::LinkOnceLinkage); + Head->setLinkage(GlobalValue::LinkOnceAnyLinkage); } return true; -- cgit v1.2.3