Index: lib/MC/MCStreamer.cpp =================================================================== --- lib/MC/MCStreamer.cpp +++ lib/MC/MCStreamer.cpp @@ -682,9 +682,11 @@ if (TextSecCOFF->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { KeySym = TextSecCOFF->getCOMDATSymbol(); - // In a GNU environment, we can't use associative comdats. Instead, do what - // GCC does, which is to make plain comdat selectany section named like - // ".[px]data$_Z3foov". + // In a GNU environment, we can't rely on associative comdats. Instead, do + // almost what GCC does, which is to make plain comdat selectany section + // named like ".[px]data$_Z3foov". We make them associative though, to help + // LLD keep them if the function itself is kept, while the symbol suffixed + // section name is enough for make things work for ld.bfd. if (!Context.getAsmInfo()->hasCOFFAssociativeComdats()) { std::string SectionName = (MainCFISecCOFF->getSectionName() + "$" + @@ -693,7 +695,8 @@ return Context.getCOFFSection( SectionName, MainCFISecCOFF->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT, - MainCFISecCOFF->getKind(), "", COFF::IMAGE_COMDAT_SELECT_ANY); + MainCFISecCOFF->getKind(), KeySym->getName(), + COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE); } } Index: test/CodeGen/X86/mingw-comdats-xdata.ll =================================================================== --- test/CodeGen/X86/mingw-comdats-xdata.ll +++ test/CodeGen/X86/mingw-comdats-xdata.ll @@ -54,8 +54,7 @@ ; GNU: .section .text$_Z3fooi,"xr",discard,_Z3fooi ; GNU: _Z3fooi: -; GNU: .section .xdata$_Z3fooi,"dr" -; GNU: .linkonce discard +; GNU: .section .xdata$_Z3fooi,"dr",associative,_Z3fooi ; GNU: GCC_except_table1: ; GNU: .section .data$gv,"dw",discard,gv ; GNU: gv: @@ -64,8 +63,8 @@ ; Make sure the assembler puts the .xdata and .pdata in sections with the right ; names. ; GNUOBJ: .text$_Z3fooi -; GNUOBJ: .xdata$_Z3fooi ; GNUOBJ: .data$gv +; GNUOBJ: .xdata$_Z3fooi ; GNUOBJ: .pdata$_Z3fooi declare dso_local i32 @_Z3bari(i32) Index: test/CodeGen/X86/mingw-comdats.ll =================================================================== --- test/CodeGen/X86/mingw-comdats.ll +++ test/CodeGen/X86/mingw-comdats.ll @@ -60,8 +60,8 @@ ; Make sure the assembler puts the .xdata and .pdata in sections with the right ; names. ; GNUOBJ: .text$_Z3fooi -; GNUOBJ: .xdata$_Z3fooi ; GNUOBJ: .data$gv +; GNUOBJ: .xdata$_Z3fooi ; GNUOBJ: .pdata$_Z3fooi declare dso_local i32 @_Z3bari(i32)