From 24dd10c6483d0246464277ecc0e89aaeb561ec88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 9 Oct 2023 13:08:39 +0300 Subject: [PATCH] [CodeGen] Sort .ctors in reverse on MinGW just like on other platforms On MinGW targets, the .ctors section is always used (as opposed to on ELF platforms, where .init_section is the default but one can select using .ctors, with the -use-ctors option, or the UseInitArray field in TargetOptions). Apply the reverse ordering regardless of whether the caller has set the UseInitArray flag for this target, as this target unconditionally uses the .ctors section anyway. For the CodeGen/X86/constructor.ll testcase, note how this now produces the same output ordering as for ELF targets with -use-ctors. This fixes https://github.com/llvm/llvm-project/issues/55938. --- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 6 +++++- llvm/test/CodeGen/X86/constructor.ll | 12 ++++++------ llvm/test/MC/COFF/global_ctors_dtors.ll | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 97d2fe3426406f..5f4141e17541bf 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -2807,7 +2807,11 @@ void AsmPrinter::emitXXStructorList(const DataLayout &DL, const Constant *List, // Emit the structors in reverse order if we are using the .ctor/.dtor // initialization scheme. - if (!TM.Options.UseInitArray) + bool UseCtorSection = !TM.Options.UseInitArray; + // MinGW targets always use the .ctors section. + if (TM.getTargetTriple().isWindowsGNUEnvironment()) + UseCtorSection = true; + if (UseCtorSection) std::reverse(Structors.begin(), Structors.end()); const Align Align = DL.getPointerPrefAlignment(); diff --git a/llvm/test/CodeGen/X86/constructor.ll b/llvm/test/CodeGen/X86/constructor.ll index 0fea69b5a7bcb5..4bc4e72b6472a6 100644 --- a/llvm/test/CodeGen/X86/constructor.ll +++ b/llvm/test/CodeGen/X86/constructor.ll @@ -77,14 +77,14 @@ entry: ; MCU-CTORS: .section .ctors,"aw",@progbits ; MCU-INIT-ARRAY: .section .init_array,"aw",@init_array -; COFF-CTOR: .section .ctors.65520,"dw",associative,v +; COFF-CTOR: .section .ctors,"dw" ; COFF-CTOR-NEXT: .p2align 3 -; COFF-CTOR-NEXT: .quad g +; COFF-CTOR-NEXT: .quad j +; COFF-CTOR-NEXT: .quad i +; COFF-CTOR-NEXT: .quad f ; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v ; COFF-CTOR-NEXT: .p2align 3 ; COFF-CTOR-NEXT: .quad h -; COFF-CTOR-NEXT: .section .ctors,"dw" +; COFF-CTOR-NEXT: .section .ctors.65520,"dw",associative,v ; COFF-CTOR-NEXT: .p2align 3 -; COFF-CTOR-NEXT: .quad f -; COFF-CTOR-NEXT: .quad i -; COFF-CTOR-NEXT: .quad j +; COFF-CTOR-NEXT: .quad g diff --git a/llvm/test/MC/COFF/global_ctors_dtors.ll b/llvm/test/MC/COFF/global_ctors_dtors.ll index 7df4d3e500b54c..9b321a082a32e5 100644 --- a/llvm/test/MC/COFF/global_ctors_dtors.ll +++ b/llvm/test/MC/COFF/global_ctors_dtors.ll @@ -56,10 +56,10 @@ define i32 @main() nounwind { ; WIN32-NOT: c_global_ctor ; WIN32: .section .CRT$XTX,"dr" ; WIN32: a_global_dtor -; MINGW32: .section .ctors,"dw" -; MINGW32: a_global_ctor ; MINGW32: .section .ctors,"dw",associative,{{_?}}b ; MINGW32: b_global_ctor ; MINGW32-NOT: c_global_ctor +; MINGW32: .section .ctors,"dw" +; MINGW32: a_global_ctor ; MINGW32: .section .dtors,"dw" ; MINGW32: a_global_dtor