Index: include/llvm/CodeGen/TargetLoweringObjectFileImpl.h =================================================================== --- include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -147,6 +147,10 @@ SectionKind Kind, Mangler &Mang, const TargetMachine &TM) const override; + void getNameWithPrefix(SmallVectorImpl &OutName, const GlobalValue *GV, + bool CannotUsePrivateLabel, Mangler &Mang, + const TargetMachine &TM) const override; + const MCSection * getSectionForJumpTable(const Function &F, Mangler &Mang, const TargetMachine &TM) const override; Index: include/llvm/IR/Mangler.h =================================================================== --- include/llvm/IR/Mangler.h +++ include/llvm/IR/Mangler.h @@ -52,9 +52,11 @@ /// If the global variable doesn't have a name, this fills in a unique name /// for the global. void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, - bool CannotUsePrivateLabel) const; + bool CannotUsePrivateLabel, + bool ForceNonPrivate = false) const; void getNameWithPrefix(SmallVectorImpl &OutName, const GlobalValue *GV, - bool CannotUsePrivateLabel) const; + bool CannotUsePrivateLabel, + bool ForceNonPrivate = false) const; /// Print the appropriate prefix and the specified name as the global variable /// name. GVName must not be empty. Index: include/llvm/Target/TargetLoweringObjectFile.h =================================================================== --- include/llvm/Target/TargetLoweringObjectFile.h +++ include/llvm/Target/TargetLoweringObjectFile.h @@ -101,6 +101,11 @@ return SectionForGlobal(GV, getKindForGlobal(GV, TM), Mang, TM); } + virtual void getNameWithPrefix(SmallVectorImpl &OutName, + const GlobalValue *GV, + bool CannotUsePrivateLabel, Mangler &Mang, + const TargetMachine &TM) const; + virtual const MCSection * getSectionForJumpTable(const Function &F, Mangler &Mang, const TargetMachine &TM) const; Index: lib/CodeGen/TargetLoweringObjectFileImpl.cpp =================================================================== --- lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -927,6 +927,11 @@ StringRef COMDATSymName = Sym->getName(); return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, Selection); + } else { + SmallString<256> TmpData; + getNameWithPrefix(TmpData, GV, true, Mang, TM); + return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, + Selection); } } @@ -948,6 +953,20 @@ return DataSection; } +void TargetLoweringObjectFileCOFF::getNameWithPrefix( + SmallVectorImpl &OutName, const GlobalValue *GV, + bool CannotUsePrivateLabel, Mangler &Mang, const TargetMachine &TM) const { + if (GV->hasPrivateLinkage() && + ((isa(GV) && TM.getFunctionSections()) || + (isa(GV) && TM.getDataSections()))) { + SmallString<256> Tmp; + Mang.getNameWithPrefix(Tmp, GV, CannotUsePrivateLabel, /*ForceNonPrivate=*/true); + OutName.append(Tmp.begin(), Tmp.end()); + return; + } + Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); +} + const MCSection *TargetLoweringObjectFileCOFF::getSectionForJumpTable( const Function &F, Mangler &Mang, const TargetMachine &TM) const { // If the function can be removed, produce a unique section so that Index: lib/IR/Mangler.cpp =================================================================== --- lib/IR/Mangler.cpp +++ lib/IR/Mangler.cpp @@ -91,9 +91,10 @@ } void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, - bool CannotUsePrivateLabel) const { + bool CannotUsePrivateLabel, + bool ForceNonPrivate) const { ManglerPrefixTy PrefixTy = Mangler::Default; - if (GV->hasPrivateLinkage()) { + if (GV->hasPrivateLinkage() && !ForceNonPrivate) { if (CannotUsePrivateLabel) PrefixTy = Mangler::LinkerPrivate; else @@ -152,7 +153,8 @@ void Mangler::getNameWithPrefix(SmallVectorImpl &OutName, const GlobalValue *GV, - bool CannotUsePrivateLabel) const { + bool CannotUsePrivateLabel, + bool ForceNonPrivate) const { raw_svector_ostream OS(OutName); - getNameWithPrefix(OS, GV, CannotUsePrivateLabel); + getNameWithPrefix(OS, GV, CannotUsePrivateLabel, ForceNonPrivate); } Index: lib/Target/TargetLoweringObjectFile.cpp =================================================================== --- lib/Target/TargetLoweringObjectFile.cpp +++ lib/Target/TargetLoweringObjectFile.cpp @@ -343,3 +343,9 @@ // null return could mean 'no location' & we should just do that here. return MCSymbolRefExpr::Create(Sym, *Ctx); } + +void TargetLoweringObjectFile::getNameWithPrefix( + SmallVectorImpl &OutName, const GlobalValue *GV, + bool CannotUsePrivateLabel, Mangler &Mang, const TargetMachine &TM) const { + Mang.getNameWithPrefix(OutName, GV, CannotUsePrivateLabel); +} Index: lib/Target/TargetMachine.cpp =================================================================== --- lib/Target/TargetMachine.cpp +++ lib/Target/TargetMachine.cpp @@ -175,7 +175,7 @@ const TargetLoweringObjectFile *TLOF = getObjFileLowering(); const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this); bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection); - Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel); + TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this); } MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { Index: test/CodeGen/X86/global-sections.ll =================================================================== --- test/CodeGen/X86/global-sections.ll +++ test/CodeGen/X86/global-sections.ll @@ -275,8 +275,8 @@ ; LINUX-SECTIONS: .asciz "foo" ; LINUX-SECTIONS: .size .LG14, 4 -; WIN32-SECTIONS: .section .rdata,"dr" -; WIN32-SECTIONS: L_G14: +; WIN32-SECTIONS: .section .rdata,"dr",one_only,_G14 +; WIN32-SECTIONS: _G14: ; WIN32-SECTIONS: .asciz "foo" ; cannot be merged on MachO, but can on other formats.