From 554fd99dd569e8a2053040f16d71894ac97e0754 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Fri, 11 Nov 2016 17:50:09 +0000 Subject: Revert "Use private linkage for MergedGlobals variables" on Darwin. This is a partial revert of r244615 (http://reviews.llvm.org/D11942), which caused a major regression in debug info quality. Turning the artificial __MergedGlobal symbols into private symbols (l__MergedGlobal) means that the linker will not include them in the symbol table of the final executable. Without a symbol table entry dsymutil is not be able to process the debug info for any of the merged globals and thus drops the debug info for all of them. This patch is enabling the old behavior for all MachO targets while leaving all other targets unaffected. rdar://problem/29160481 https://reviews.llvm.org/D26531 llvm-svn: 286607 --- llvm/lib/CodeGen/GlobalMerge.cpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'llvm/lib/CodeGen/GlobalMerge.cpp') diff --git a/llvm/lib/CodeGen/GlobalMerge.cpp b/llvm/lib/CodeGen/GlobalMerge.cpp index 2d9e0938fea..e0ffd475e64 100644 --- a/llvm/lib/CodeGen/GlobalMerge.cpp +++ b/llvm/lib/CodeGen/GlobalMerge.cpp @@ -432,6 +432,8 @@ bool GlobalMerge::doMerge(const SmallVectorImpl &Globals, std::vector Tys; std::vector Inits; + bool HasExternal = false; + GlobalVariable *TheFirstExternal = nullptr; for (j = i; j != -1; j = GlobalSet.find_next(j)) { Type *Ty = Globals[j]->getValueType(); MergedSize += DL.getTypeAllocSize(Ty); @@ -440,14 +442,34 @@ bool GlobalMerge::doMerge(const SmallVectorImpl &Globals, } Tys.push_back(Ty); Inits.push_back(Globals[j]->getInitializer()); + + if (Globals[j]->hasExternalLinkage() && !HasExternal) { + HasExternal = true; + TheFirstExternal = Globals[j]; + } } + // If merged variables doesn't have external linkage, we needn't to expose + // the symbol after merging. + GlobalValue::LinkageTypes Linkage = HasExternal + ? GlobalValue::ExternalLinkage + : GlobalValue::InternalLinkage; StructType *MergedTy = StructType::get(M.getContext(), Tys); Constant *MergedInit = ConstantStruct::get(MergedTy, Inits); - GlobalVariable *MergedGV = new GlobalVariable( - M, MergedTy, isConst, GlobalValue::PrivateLinkage, MergedInit, - "_MergedGlobals", nullptr, GlobalVariable::NotThreadLocal, AddrSpace); + // On Darwin external linkage needs to be preserved, otherwise dsymutil + // cannot preserve the debug info for the merged variables. If they have + // external linkage, use the symbol name of the first variable merged as the + // suffix of global symbol name. This avoids a link-time naming conflict + // for the _MergedGlobals symbols. + Twine MergedName = + (IsMachO && HasExternal) + ? "_MergedGlobals_" + TheFirstExternal->getName() + : "_MergedGlobals"; + auto MergedLinkage = IsMachO ? Linkage : GlobalValue::PrivateLinkage; + auto *MergedGV = new GlobalVariable( + M, MergedTy, isConst, MergedLinkage, MergedInit, MergedName, nullptr, + GlobalVariable::NotThreadLocal, AddrSpace); const StructLayout *MergedLayout = DL.getStructLayout(MergedTy); -- cgit v1.2.3