Use simple section names for COMDAT sections on COFF.

With this patch we use simple names for COMDAT sections (like .text or .bss).
This matches the MSVC behavior.

When merging it is the COMDAT symbol that is used to decide if two sections
should be merged, so there is no point in building a fancy name.

This survived a bootstrap on mingw32.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@195798 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2013-11-27 01:18:37 +00:00
parent 72d1f9db27
commit 823c9c725d
4 changed files with 41 additions and 44 deletions

View File

@@ -723,33 +723,31 @@ getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Mangler *Mang, const TargetMachine &TM) const { Mangler *Mang, const TargetMachine &TM) const {
int Selection = 0; int Selection = 0;
unsigned Characteristics = getCOFFSectionFlags(Kind); unsigned Characteristics = getCOFFSectionFlags(Kind);
SmallString<128> Name(GV->getSection().c_str()); StringRef Name = GV->getSection();
StringRef COMDATSymName = "";
if (GV->isWeakForLinker()) { if (GV->isWeakForLinker()) {
Selection = COFF::IMAGE_COMDAT_SELECT_ANY; Selection = COFF::IMAGE_COMDAT_SELECT_ANY;
Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
Name.append("$"); MCSymbol *Sym = getSymbol(*Mang, GV);
Mang->getNameWithPrefix(Name, GV, false, false); COMDATSymName = Sym->getName();
} }
return getContext().getCOFFSection(Name, return getContext().getCOFFSection(Name,
Characteristics, Characteristics,
Kind, Kind,
"", COMDATSymName,
Selection); Selection);
} }
static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) { static const char *getCOFFSectionNameForUniqueGlobal(SectionKind Kind) {
if (Kind.isText()) if (Kind.isText())
return ".text$"; return ".text";
if (Kind.isBSS ()) if (Kind.isBSS ())
return ".bss$"; return ".bss";
if (Kind.isThreadLocal()) { if (Kind.isThreadLocal())
// 'LLVM' is just an arbitary string to ensure that the section name gets return ".tls";
// sorted in between '.tls$AAA' and '.tls$ZZZ' by the linker.
return ".tls$LLVM";
}
if (Kind.isWriteable()) if (Kind.isWriteable())
return ".data$"; return ".data";
return ".rdata$"; return ".rdata";
} }
@@ -760,16 +758,14 @@ SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
// If this global is linkonce/weak and the target handles this by emitting it // If this global is linkonce/weak and the target handles this by emitting it
// into a 'uniqued' section name, create and return the section now. // into a 'uniqued' section name, create and return the section now.
if (GV->isWeakForLinker()) { if (GV->isWeakForLinker()) {
const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind); const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Mang->getNameWithPrefix(Name, GV, false, false);
unsigned Characteristics = getCOFFSectionFlags(Kind); unsigned Characteristics = getCOFFSectionFlags(Kind);
Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT; Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
MCSymbol *Sym = getSymbol(*Mang, GV);
return getContext().getCOFFSection(Name.str(), Characteristics, return getContext().getCOFFSection(Name, Characteristics,
Kind, "", COFF::IMAGE_COMDAT_SELECT_ANY); Kind, Sym->getName(),
COFF::IMAGE_COMDAT_SELECT_ANY);
} }
if (Kind.isText()) if (Kind.isText())

View File

@@ -20,6 +20,8 @@ MCSectionCOFF::~MCSectionCOFF() {} // anchor.
// should be printed before the section name // should be printed before the section name
bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name, bool MCSectionCOFF::ShouldOmitSectionDirective(StringRef Name,
const MCAsmInfo &MAI) const { const MCAsmInfo &MAI) const {
if (COMDATSymbol)
return false;
// FIXME: Does .section .bss/.data/.text work everywhere?? // FIXME: Does .section .bss/.data/.text work everywhere??
if (Name == ".text" || Name == ".data" || Name == ".bss") if (Name == ".text" || Name == ".data" || Name == ".bss")
@@ -58,36 +60,41 @@ void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI,
OS << 'r'; OS << 'r';
if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE) if (getCharacteristics() & COFF::IMAGE_SCN_MEM_DISCARDABLE)
OS << 'n'; OS << 'n';
OS << "\"\n";
OS << '"';
if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
OS << ",";
switch (Selection) { switch (Selection) {
case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES: case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES:
OS << "\t.linkonce one_only\n"; OS << "one_only,";
break; break;
case COFF::IMAGE_COMDAT_SELECT_ANY: case COFF::IMAGE_COMDAT_SELECT_ANY:
OS << "\t.linkonce discard\n"; OS << "discard,";
break; break;
case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE: case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE:
OS << "\t.linkonce same_size\n"; OS << "same_size,";
break; break;
case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH: case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH:
OS << "\t.linkonce same_contents\n"; OS << "same_contents,";
break; break;
case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE: case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE:
OS << "\t.linkonce associative " << Assoc->getSectionName() << "\n"; OS << "associative " << Assoc->getSectionName() << ",";
break; break;
case COFF::IMAGE_COMDAT_SELECT_LARGEST: case COFF::IMAGE_COMDAT_SELECT_LARGEST:
OS << "\t.linkonce largest\n"; OS << "largest,";
break; break;
case COFF::IMAGE_COMDAT_SELECT_NEWEST: case COFF::IMAGE_COMDAT_SELECT_NEWEST:
OS << "\t.linkonce newest\n"; OS << "newest,";
break; break;
default: default:
assert (0 && "unsupported COFF selection type"); assert (0 && "unsupported COFF selection type");
break; break;
} }
assert(COMDATSymbol);
OS << *COMDATSymbol;
} }
OS << '\n';
} }
bool MCSectionCOFF::UseCodeAlign() const { bool MCSectionCOFF::UseCodeAlign() const {

View File

@@ -25,11 +25,11 @@ define weak i32 @"\01??_B?$num_put@_WV?$back_insert_iterator@V?$basic_string@_WU
; ASM-NOT: .globl "@foo.bar" ; ASM-NOT: .globl "@foo.bar"
; READOBJ: Symbol ; READOBJ: Symbol
; READOBJ: Name: .text$??_B?$num_put@_WV?$back_insert_iterator@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@std@@@std@@51 ; READOBJ: Name: .text
; READOBJ: Section: .text$??_B?$num_put@_WV?$back_insert_iterator@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@std@@@std@@51 ; READOBJ: Section: .text
; READOBJ: Symbol ; READOBJ: Symbol
; READOBJ: Name: ??_B?$num_put@_WV?$back_insert_iterator@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@std@@@std@@51 ; READOBJ: Name: ??_B?$num_put@_WV?$back_insert_iterator@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@std@@@std@@51
; READOBJ: Section: .text$??_B?$num_put@_WV?$back_insert_iterator@V?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@std@@@std@@51 ; READOBJ: Section: .text
; READOBJ: Symbol ; READOBJ: Symbol
; READOBJ: Name: ??__E_Generic_object@?$_Error_objects@H@std@@YAXXZ ; READOBJ: Name: ??__E_Generic_object@?$_Error_objects@H@std@@YAXXZ
; READOBJ: Symbol ; READOBJ: Symbol

View File

@@ -8,37 +8,31 @@
; RUN: llc -mtriple=x86_64-pc-mingw32 %s -o - | FileCheck %s --check-prefix=X64 ; RUN: llc -mtriple=x86_64-pc-mingw32 %s -o - | FileCheck %s --check-prefix=X64
; Mangled function ; Mangled function
; X86: .section .text$_Z3foo ; X86: .section .text,"xr",discard,__Z3foo
; X86: .linkonce discard
; X86: .globl __Z3foo ; X86: .globl __Z3foo
; ;
; X64: .section .text$_Z3foo ; X64: .section .text,"xr",discard,_Z3foo
; X64: .linkonce discard
; X64: .globl _Z3foo ; X64: .globl _Z3foo
define weak void @_Z3foo() { define weak void @_Z3foo() {
ret void ret void
} }
; Unmangled function ; Unmangled function
; X86: .section .sect$f ; X86: .section .sect,"xr",discard,_f
; X86: .linkonce discard
; X86: .globl _f ; X86: .globl _f
; ;
; X64: .section .sect$f ; X64: .section .sect,"xr",discard,f
; X64: .linkonce discard
; X64: .globl f ; X64: .globl f
define weak void @f() section ".sect" { define weak void @f() section ".sect" {
ret void ret void
} }
; Weak global ; Weak global
; X86: .section .data$a ; X86: .section .data,"r",discard,_a
; X86: .linkonce discard
; X86: .globl _a ; X86: .globl _a
; X86: .zero 12 ; X86: .zero 12
; ;
; X64: .section .data$a ; X64: .section .data,"r",discard,a
; X64: .linkonce discard
; X64: .globl a ; X64: .globl a
; X64: .zero 12 ; X64: .zero 12
@a = weak unnamed_addr constant { i32, i32, i32 } { i32 0, i32 0, i32 0}, section ".data" @a = weak unnamed_addr constant { i32, i32, i32 } { i32 0, i32 0, i32 0}, section ".data"