diff --git a/include/llvm/MC/MCSectionCOFF.h b/include/llvm/MC/MCSectionCOFF.h index a428f9efb2b..33ba45c2783 100644 --- a/include/llvm/MC/MCSectionCOFF.h +++ b/include/llvm/MC/MCSectionCOFF.h @@ -76,6 +76,7 @@ class MCSymbol; return SectionName.str() + "_end"; } unsigned getCharacteristics() const { return Characteristics; } + const MCSymbol *getCOMDATSymbol() const { return COMDATSymbol; } int getSelection() const { return Selection; } const MCSectionCOFF *getAssocSection() const { return Assoc; } diff --git a/lib/MC/WinCOFFObjectWriter.cpp b/lib/MC/WinCOFFObjectWriter.cpp index 961cbc6a8f7..c3fc61b2904 100644 --- a/lib/MC/WinCOFFObjectWriter.cpp +++ b/lib/MC/WinCOFFObjectWriter.cpp @@ -347,6 +347,11 @@ void WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) { COFFSection *coff_section = createSection(Sec.getSectionName()); COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName()); + if (const MCSymbol *S = Sec.getCOMDATSymbol()) { + COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S); + assert(!COMDATSymbol->Section); + COMDATSymbol->Section = coff_section; + } coff_section->Symbol = coff_symbol; coff_symbol->Section = coff_section; @@ -458,9 +463,15 @@ void WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData, coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE; } else { const MCSymbolData &BaseData = Assembler.getSymbolData(*Base); - if (BaseData.Fragment) - coff_symbol->Section = + if (BaseData.Fragment) { + COFFSection *Sec = SectionMap[&BaseData.Fragment->getParent()->getSection()]; + + if (coff_symbol->Section && coff_symbol->Section != Sec) + report_fatal_error("conflicting sections for symbol"); + + coff_symbol->Section = Sec; + } } coff_symbol->MCData = &ResSymData; diff --git a/test/MC/COFF/section-comdat-conflict.s b/test/MC/COFF/section-comdat-conflict.s new file mode 100644 index 00000000000..7ed452a5cdc --- /dev/null +++ b/test/MC/COFF/section-comdat-conflict.s @@ -0,0 +1,13 @@ +// RUN: not llvm-mc -triple i386-pc-win32 -filetype=obj < %s 2>&1 | FileCheck %s + +// CHECK: conflicting sections for symbol + + .section .xyz + .global bar +bar: + .long 42 + + .section .abcd,"xr",discard,bar + .global foo +foo: + .long 42 diff --git a/test/MC/COFF/section-comdat.s b/test/MC/COFF/section-comdat.s index dd5be871b05..e166a508bc1 100644 --- a/test/MC/COFF/section-comdat.s +++ b/test/MC/COFF/section-comdat.s @@ -40,6 +40,11 @@ Symbol6: Symbol7: .long 1 +.section SecName, "dr", newest, "Symbol8" +.globl AnotherSymbol +AnotherSymbol: +.long 1 + // CHECK: Sections [ // CHECK: Section { // CHECK: Number: 1 @@ -114,6 +119,10 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { +// CHECK: Name: Symbol1 +// CHECK: Section: secName (2) +// CHECK: } +// CHECK: Symbol { // CHECK: Name: secName // CHECK: Section: secName (3) // CHECK: AuxSectionDef { @@ -121,6 +130,10 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { +// CHECK: Name: Symbol2 +// CHECK: Section: secName (3) +// CHECK: } +// CHECK: Symbol { // CHECK: Name: SecName // CHECK: Section: SecName (4) // CHECK: AuxSectionDef { @@ -128,6 +141,10 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { +// CHECK: Name: Symbol3 +// CHECK: Section: SecName (4) +// CHECK: } +// CHECK: Symbol { // CHECK: Name: SecName // CHECK: Section: SecName (5) // CHECK: AuxSymbolCount: 1 @@ -136,6 +153,10 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { +// CHECK: Name: Symbol4 +// CHECK: Section: SecName (5) +// CHECK: } +// CHECK: Symbol { // CHECK: Name: SecName // CHECK: Section: SecName (6) // CHECK: AuxSectionDef { @@ -144,6 +165,10 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { +// CHECK: Name: Symbol5 +// CHECK: Section: SecName (6) +// CHECK: } +// CHECK: Symbol { // CHECK: Name: SecName // CHECK: Section: SecName (7) // CHECK: AuxSectionDef { @@ -151,6 +176,10 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { +// CHECK: Name: Symbol6 +// CHECK: Section: SecName (7) +// CHECK: } +// CHECK: Symbol { // CHECK: Name: SecName // CHECK: Section: SecName (8) // CHECK: AuxSectionDef { @@ -158,31 +187,22 @@ Symbol7: // CHECK: } // CHECK: } // CHECK: Symbol { -// CHECK: Name: Symbol1 -// CHECK: Section: secName (2) -// CHECK: } -// CHECK: Symbol { -// CHECK: Name: Symbol2 -// CHECK: Section: secName (3) -// CHECK: } -// CHECK: Symbol { -// CHECK: Name: Symbol3 -// CHECK: Section: SecName (4) -// CHECK: } -// CHECK: Symbol { -// CHECK: Name: Symbol4 -// CHECK: Section: SecName (5) -// CHECK: } -// CHECK: Symbol { -// CHECK: Name: Symbol5 -// CHECK: Section: SecName (6) -// CHECK: } -// CHECK: Symbol { -// CHECK: Name: Symbol6 -// CHECK: Section: SecName (7) -// CHECK: } -// CHECK: Symbol { // CHECK: Name: Symbol7 // CHECK: Section: SecName (8) // CHECK: } +// CHECK: Symbol { +// CHECK: Name: SecName +// CHECK: Section: SecName (9) +// CHECK: AuxSectionDef { +// CHECK: Selection: Newest (0x7) +// CHECK: } +// CHECK: } +// CHECK: Symbol { +// CHECK: Name: Symbol8 +// CHECK: Section: SecName (9) +// CHECK: } +// CHECK: Symbol { +// CHECK: Name: AnotherSymbol +// CHECK: Section: SecName (9) +// CHECK: } // CHECK: ]