From 7db449a6e7d7372041de8c4dc39f46194852ae19 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 17 Mar 2015 23:54:51 +0000 Subject: [PATCH] COFF: Let globals with private linkage reside in their own section COFF COMDATs (for selection kinds other than 'select any') require at least one non-section symbol in the symbol table. Satisfy this by morally enhancing the linkage from private to internal. Differential Revision: http://reviews.llvm.org/D8394 llvm-svn: 232570 --- .../CodeGen/TargetLoweringObjectFileImpl.h | 4 ++++ llvm/include/llvm/IR/DataLayout.h | 19 ++++++++++++++----- .../llvm/Target/TargetLoweringObjectFile.h | 5 +++++ .../CodeGen/TargetLoweringObjectFileImpl.cpp | 16 ++++++++++++++++ llvm/lib/IR/DataLayout.cpp | 9 ++++++--- llvm/lib/Target/TargetLoweringObjectFile.cpp | 6 ++++++ llvm/lib/Target/TargetMachine.cpp | 2 +- llvm/test/CodeGen/ARM/Windows/long-calls.ll | 2 +- .../CodeGen/X86/fastcall-correct-mangling.ll | 2 +- llvm/test/CodeGen/X86/global-sections.ll | 4 ++-- 10 files changed, 56 insertions(+), 13 deletions(-) diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h index 0d7c26cff8aa..75920a3fea4f 100644 --- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -147,6 +147,10 @@ public: 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; diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h index deda87795690..3e1f9744f9e8 100644 --- a/llvm/include/llvm/IR/DataLayout.h +++ b/llvm/include/llvm/IR/DataLayout.h @@ -108,7 +108,14 @@ private: unsigned StackNaturalAlign; - enum ManglingModeT { MM_None, MM_ELF, MM_MachO, MM_WINCOFF, MM_Mips }; + enum ManglingModeT { + MM_None, + MM_ELF, + MM_MachO, + MM_WinCOFF, + MM_WinCOFFX86, + MM_Mips + }; ManglingModeT ManglingMode; SmallVector LegalIntWidths; @@ -244,7 +251,7 @@ public: unsigned getStackAlignment() const { return StackNaturalAlign; } bool hasMicrosoftFastStdCallMangling() const { - return ManglingMode == MM_WINCOFF; + return ManglingMode == MM_WinCOFFX86; } bool hasLinkerPrivateGlobalPrefix() const { return ManglingMode == MM_MachO; } @@ -252,7 +259,7 @@ public: const char *getLinkerPrivateGlobalPrefix() const { if (ManglingMode == MM_MachO) return "l"; - return getPrivateGlobalPrefix(); + return ""; } char getGlobalPrefix() const { @@ -260,9 +267,10 @@ public: case MM_None: case MM_ELF: case MM_Mips: + case MM_WinCOFF: return '\0'; case MM_MachO: - case MM_WINCOFF: + case MM_WinCOFFX86: return '_'; } llvm_unreachable("invalid mangling mode"); @@ -277,7 +285,8 @@ public: case MM_Mips: return "$"; case MM_MachO: - case MM_WINCOFF: + case MM_WinCOFF: + case MM_WinCOFFX86: return "L"; } llvm_unreachable("invalid mangling mode"); diff --git a/llvm/include/llvm/Target/TargetLoweringObjectFile.h b/llvm/include/llvm/Target/TargetLoweringObjectFile.h index 8093d99882c5..62ae2372f59e 100644 --- a/llvm/include/llvm/Target/TargetLoweringObjectFile.h +++ b/llvm/include/llvm/Target/TargetLoweringObjectFile.h @@ -101,6 +101,11 @@ public: 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; diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp index 725b8246a35d..bcf2aa72c4bb 100644 --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -927,6 +927,11 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, StringRef COMDATSymName = Sym->getName(); return getContext().getCOFFSection(Name, Characteristics, Kind, COMDATSymName, Selection); + } else { + SmallString<256> TmpData; + getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true, Mang, TM); + return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData, + Selection); } } @@ -948,6 +953,17 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind, 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()))) + CannotUsePrivateLabel = true; + + 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 diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp index 5dcb5fbf490e..4d867efe1b3d 100644 --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -150,8 +150,8 @@ DataLayout::InvalidPointerElem = { 0U, 0U, 0U, ~0U }; const char *DataLayout::getManglingComponent(const Triple &T) { if (T.isOSBinFormatMachO()) return "-m:o"; - if (T.isOSWindows() && T.getArch() == Triple::x86 && T.isOSBinFormatCOFF()) - return "-m:w"; + if (T.isOSWindows() && T.isOSBinFormatCOFF()) + return T.getArch() == Triple::x86 ? "-m:x" : "-m:w"; return "-m:e"; } @@ -359,7 +359,10 @@ void DataLayout::parseSpecifier(StringRef Desc) { ManglingMode = MM_Mips; break; case 'w': - ManglingMode = MM_WINCOFF; + ManglingMode = MM_WinCOFF; + break; + case 'x': + ManglingMode = MM_WinCOFFX86; break; } break; diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp index faa6fbe6075d..75100fb99230 100644 --- a/llvm/lib/Target/TargetLoweringObjectFile.cpp +++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp @@ -343,3 +343,9 @@ const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol // 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); +} diff --git a/llvm/lib/Target/TargetMachine.cpp b/llvm/lib/Target/TargetMachine.cpp index bc2af5e9ad62..56e7e8b24bdb 100644 --- a/llvm/lib/Target/TargetMachine.cpp +++ b/llvm/lib/Target/TargetMachine.cpp @@ -175,7 +175,7 @@ void TargetMachine::getNameWithPrefix(SmallVectorImpl &Name, 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 { diff --git a/llvm/test/CodeGen/ARM/Windows/long-calls.ll b/llvm/test/CodeGen/ARM/Windows/long-calls.ll index e35f414579af..21c95fac91c5 100644 --- a/llvm/test/CodeGen/ARM/Windows/long-calls.ll +++ b/llvm/test/CodeGen/ARM/Windows/long-calls.ll @@ -10,7 +10,7 @@ entry: } ; CHECK-LABEL: caller -; CHECK: ldr [[REG:r[0-9]+]], [[CPI:.LCPI[_0-9]+]] +; CHECK: ldr [[REG:r[0-9]+]], [[CPI:LCPI[_0-9]+]] ; CHECK: bx [[REG]] ; CHECK: .align 2 ; CHECK: [[CPI]]: diff --git a/llvm/test/CodeGen/X86/fastcall-correct-mangling.ll b/llvm/test/CodeGen/X86/fastcall-correct-mangling.ll index 00dc44e75e8f..bc53fe8aa4ec 100644 --- a/llvm/test/CodeGen/X86/fastcall-correct-mangling.ll +++ b/llvm/test/CodeGen/X86/fastcall-correct-mangling.ll @@ -28,6 +28,6 @@ entry: define private x86_fastcallcc void @dontCrash() { ; The name is fairly arbitrary since it is private. Just don't crash. ; CHECK32-LABEL: {{^}}L@dontCrash@0: -; CHECK64-LABEL: {{^}}.LdontCrash: +; CHECK64-LABEL: {{^}}LdontCrash: ret void } diff --git a/llvm/test/CodeGen/X86/global-sections.ll b/llvm/test/CodeGen/X86/global-sections.ll index dcdc71efdc4e..8c61411e53eb 100644 --- a/llvm/test/CodeGen/X86/global-sections.ll +++ b/llvm/test/CodeGen/X86/global-sections.ll @@ -275,8 +275,8 @@ bb7: ; 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.