Skip to content

Commit

Permalink
[clang] [MinGW] Explicitly always pass the -fno-use-init-array (#68571)
Browse files Browse the repository at this point in the history
On MinGW targets, the .ctors section is always used for constructors.

When using the .ctors section, the constructors need to be emitted in
reverse order to get them execute in the right order. (Constructors with
a specific priority are sorted separately by the linker later.) In LLVM,
in CodeGen/AsmPrinter/AsmPrinter.cpp, there's code that reverses them
before writing them out, executed when using the .ctors section. This
logic is done whenever TM.Options.UseInitArray is set to false. Thus,
make sure to set UseInitArray to false for this target.

This fixes llvm/llvm-project#55938.

(cherry picked from commit a2b8c49c1839076b540c542c024fcfe2361a3e47)
  • Loading branch information
mstorsjo authored and tru committed Oct 16, 2023
1 parent d10b731 commit f50c638
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/MinGW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,8 @@ void toolchains::MinGW::addClangTargetOptions(
}
}

CC1Args.push_back("-fno-use-init-array");

for (auto Opt : {options::OPT_mthreads, options::OPT_mwindows,
options::OPT_mconsole, options::OPT_mdll}) {
if (Arg *A = DriverArgs.getLastArgNoClaim(Opt))
Expand Down
3 changes: 3 additions & 0 deletions clang/test/Driver/mingw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@
// CHECK_NO_SUBSYS-NOT: "--subsystem"
// CHECK_SUBSYS_CONSOLE: "--subsystem" "console"
// CHECK_SUBSYS_WINDOWS: "--subsystem" "windows"

// RUN: %clang -target i686-windows-gnu -### %s 2>&1 | FileCheck -check-prefix=CHECK_NO_INIT_ARRAY %s
// CHECK_NO_INIT_ARRAY: "-fno-use-init-array"

0 comments on commit f50c638

Please sign in to comment.