From 100fc97051c19ef3675fc105dd428955b7f5f285 Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Mon, 23 Jul 2018 22:15:19 +0000 Subject: [PATCH] [COFF] Fix assembly output of comdat sections without an attached symbol Since SVN r335286, the .xdata sections are produced without an attached symbol, which requires using a different syntax when printing assembly output. Instead of the usual syntax of '.section ,"dr",discard,', use '.section ,"dr"' + '.linkonce discard' (which is what GCC uses for all assembly output). This fixes PR38254. Differential Revision: https://reviews.llvm.org/D49651 llvm-svn: 337756 --- llvm/lib/MC/MCSectionCOFF.cpp | 25 +++--- llvm/test/CodeGen/X86/mingw-comdats-xdata.ll | 81 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 llvm/test/CodeGen/X86/mingw-comdats-xdata.ll diff --git a/llvm/lib/MC/MCSectionCOFF.cpp b/llvm/lib/MC/MCSectionCOFF.cpp index 72a7fc36a460..c861963eec8a 100644 --- a/llvm/lib/MC/MCSectionCOFF.cpp +++ b/llvm/lib/MC/MCSectionCOFF.cpp @@ -69,35 +69,40 @@ void MCSectionCOFF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, OS << '"'; if (getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) { - OS << ","; + if (COMDATSymbol) + OS << ","; + else + OS << "\n\t.linkonce\t"; switch (Selection) { case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES: - OS << "one_only,"; + OS << "one_only"; break; case COFF::IMAGE_COMDAT_SELECT_ANY: - OS << "discard,"; + OS << "discard"; break; case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE: - OS << "same_size,"; + OS << "same_size"; break; case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH: - OS << "same_contents,"; + OS << "same_contents"; break; case COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE: - OS << "associative,"; + OS << "associative"; break; case COFF::IMAGE_COMDAT_SELECT_LARGEST: - OS << "largest,"; + OS << "largest"; break; case COFF::IMAGE_COMDAT_SELECT_NEWEST: - OS << "newest,"; + OS << "newest"; break; default: assert(false && "unsupported COFF selection type"); break; } - assert(COMDATSymbol); - COMDATSymbol->print(OS, &MAI); + if (COMDATSymbol) { + OS << ","; + COMDATSymbol->print(OS, &MAI); + } } OS << '\n'; } diff --git a/llvm/test/CodeGen/X86/mingw-comdats-xdata.ll b/llvm/test/CodeGen/X86/mingw-comdats-xdata.ll new file mode 100644 index 000000000000..b8a2167a8785 --- /dev/null +++ b/llvm/test/CodeGen/X86/mingw-comdats-xdata.ll @@ -0,0 +1,81 @@ +; RUN: llc -mtriple=x86_64-w64-windows-gnu < %s | FileCheck %s --check-prefix=GNU +; RUN: llc -mtriple=x86_64-w64-windows-gnu < %s -filetype=obj | llvm-objdump - -headers | FileCheck %s --check-prefix=GNUOBJ + +; When doing GCC style comdats for MinGW, the .xdata sections don't have a normal comdat +; symbol attached, which requires a bit of adjustments for the assembler output. + +; Generated with this C++ source: +; int bar(int); +; __declspec(selectany) int gv = 42; +; inline int foo(int x) { try { return bar(x) + gv; } catch (...) { return 0; } } +; int main() { return foo(1); } + +$_Z3fooi = comdat any + +$gv = comdat any + +@gv = weak_odr dso_local global i32 42, comdat, align 4 + +; Function Attrs: norecurse uwtable +define dso_local i32 @main() #0 { +entry: + %call = tail call i32 @_Z3fooi(i32 1) + ret i32 %call +} + +; GNU: main: + +; Function Attrs: inlinehint uwtable +define linkonce_odr dso_local i32 @_Z3fooi(i32 %x) #1 comdat personality i8* bitcast (i32 (...)* @__gxx_personality_seh0 to i8*) { +entry: + %call = invoke i32 @_Z3bari(i32 %x) + to label %invoke.cont unwind label %lpad + +invoke.cont: ; preds = %entry + %0 = load i32, i32* @gv, align 4 + %add = add nsw i32 %0, %call + br label %return + +lpad: ; preds = %entry + %1 = landingpad { i8*, i32 } + catch i8* null + %2 = extractvalue { i8*, i32 } %1, 0 + %3 = tail call i8* @__cxa_begin_catch(i8* %2) #3 + tail call void @__cxa_end_catch() + br label %return + +return: ; preds = %lpad, %invoke.cont + %retval.0 = phi i32 [ %add, %invoke.cont ], [ 0, %lpad ] + ret i32 %retval.0 +} + +; The .xdata section below doesn't have the usual comdat symbol attached, which requires +; a different syntax for the assembly output. + +; GNU: .section .text$_Z3fooi,"xr",discard,_Z3fooi +; GNU: _Z3fooi: +; GNU: .section .xdata$_Z3fooi,"dr" +; GNU: .linkonce discard +; GNU: GCC_except_table1: +; GNU: .section .data$gv,"dw",discard,gv +; GNU: gv: +; GNU: .long 42 + +; 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: .pdata$_Z3fooi + +declare dso_local i32 @_Z3bari(i32) + +declare dso_local i32 @__gxx_personality_seh0(...) + +declare dso_local i8* @__cxa_begin_catch(i8*) local_unnamed_addr + +declare dso_local void @__cxa_end_catch() local_unnamed_addr + +attributes #0 = { norecurse uwtable } +attributes #1 = { inlinehint uwtable } +attributes #3 = { nounwind }