Skip to content

Commit 24dd10c

Browse files
committed
[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 llvm#55938.
1 parent 29b2082 commit 24dd10c

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2807,7 +2807,11 @@ void AsmPrinter::emitXXStructorList(const DataLayout &DL, const Constant *List,
28072807

28082808
// Emit the structors in reverse order if we are using the .ctor/.dtor
28092809
// initialization scheme.
2810-
if (!TM.Options.UseInitArray)
2810+
bool UseCtorSection = !TM.Options.UseInitArray;
2811+
// MinGW targets always use the .ctors section.
2812+
if (TM.getTargetTriple().isWindowsGNUEnvironment())
2813+
UseCtorSection = true;
2814+
if (UseCtorSection)
28112815
std::reverse(Structors.begin(), Structors.end());
28122816

28132817
const Align Align = DL.getPointerPrefAlignment();

llvm/test/CodeGen/X86/constructor.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ entry:
7777
; MCU-CTORS: .section .ctors,"aw",@progbits
7878
; MCU-INIT-ARRAY: .section .init_array,"aw",@init_array
7979

80-
; COFF-CTOR: .section .ctors.65520,"dw",associative,v
80+
; COFF-CTOR: .section .ctors,"dw"
8181
; COFF-CTOR-NEXT: .p2align 3
82-
; COFF-CTOR-NEXT: .quad g
82+
; COFF-CTOR-NEXT: .quad j
83+
; COFF-CTOR-NEXT: .quad i
84+
; COFF-CTOR-NEXT: .quad f
8385
; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v
8486
; COFF-CTOR-NEXT: .p2align 3
8587
; COFF-CTOR-NEXT: .quad h
86-
; COFF-CTOR-NEXT: .section .ctors,"dw"
88+
; COFF-CTOR-NEXT: .section .ctors.65520,"dw",associative,v
8789
; COFF-CTOR-NEXT: .p2align 3
88-
; COFF-CTOR-NEXT: .quad f
89-
; COFF-CTOR-NEXT: .quad i
90-
; COFF-CTOR-NEXT: .quad j
90+
; COFF-CTOR-NEXT: .quad g

llvm/test/MC/COFF/global_ctors_dtors.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ define i32 @main() nounwind {
5656
; WIN32-NOT: c_global_ctor
5757
; WIN32: .section .CRT$XTX,"dr"
5858
; WIN32: a_global_dtor
59-
; MINGW32: .section .ctors,"dw"
60-
; MINGW32: a_global_ctor
6159
; MINGW32: .section .ctors,"dw",associative,{{_?}}b
6260
; MINGW32: b_global_ctor
6361
; MINGW32-NOT: c_global_ctor
62+
; MINGW32: .section .ctors,"dw"
63+
; MINGW32: a_global_ctor
6464
; MINGW32: .section .dtors,"dw"
6565
; MINGW32: a_global_dtor

0 commit comments

Comments
 (0)