[clang] [flang] [flang][Driver] Let the linker fail on multiple definitions of main() (PR #73124)
Michael Klemm via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 28 00:50:24 PST 2023
https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/73124
>From 2a2693364cb8e9b657b9ff54aa78df0466b55fe4 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Wed, 22 Nov 2023 14:22:20 +0100
Subject: [PATCH 01/13] Let the linker fail on multiple definitions of main()
---
clang/lib/Driver/ToolChains/CommonArgs.cpp | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 1f31c6395206ee8..740ae71177b2c3a 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -982,7 +982,20 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC,
// These are handled earlier on Windows by telling the frontend driver to add
// the correct libraries to link against as dependents in the object file.
if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
+ // --whole-archive needs to be part of the link line to make sure
+ // that the main() function from Fortran_main.a is pulled in by
+ // the linker.
+ //
+ // We are using this --whole-archive/--no-whole-archive bracket w/o
+ // any further checks, because -Wl,--whole-archive at the flang-new new
+ // line will not sucessfully complete, unless the user correctly specified
+ // -Wl,--no-whole-archive (e.g., -Wl,--whole-archive -ldummy
+ // -Wl,--no-whole-archive).
+ CmdArgs.push_back("--whole-archive");
CmdArgs.push_back("-lFortran_main");
+ CmdArgs.push_back("--no-whole-archive");
+
+ // Perform regular linkage of the remaining runtime libraries.
CmdArgs.push_back("-lFortranRuntime");
CmdArgs.push_back("-lFortranDecimal");
}
@@ -993,7 +1006,7 @@ void tools::addFortranRuntimeLibraryPath(const ToolChain &TC,
ArgStringList &CmdArgs) {
// Default to the <driver-path>/../lib directory. This works fine on the
// platforms that we have tested so far. We will probably have to re-fine
- // this in the future. In particular, on some platforms, we may need to use
+ // this in the future. In particular, on some platforms, we may need to useq
// lib64 instead of lib.
SmallString<256> DefaultLibPath =
llvm::sys::path::parent_path(TC.getDriver().Dir);
>From 0d652282f4dbed2dde11df53ead3e6c8b6856bed Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Wed, 22 Nov 2023 15:18:51 +0100
Subject: [PATCH 02/13] Improve comments and remove accidental typo
---
clang/lib/Driver/ToolChains/CommonArgs.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 740ae71177b2c3a..464a87737de062c 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -987,10 +987,10 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC,
// the linker.
//
// We are using this --whole-archive/--no-whole-archive bracket w/o
- // any further checks, because -Wl,--whole-archive at the flang-new new
- // line will not sucessfully complete, unless the user correctly specified
- // -Wl,--no-whole-archive (e.g., -Wl,--whole-archive -ldummy
- // -Wl,--no-whole-archive).
+ // any further checks, because -Wl,--whole-archive at the flang
+ // driver's link line will not sucessfully complete, unless the user
+ // correctly specified -Wl,--whole-archive/-Wl,--no-whole-archive
+ // (e.g., -Wl,--whole-archive -ldummy -Wl,--no-whole-archive).
CmdArgs.push_back("--whole-archive");
CmdArgs.push_back("-lFortran_main");
CmdArgs.push_back("--no-whole-archive");
@@ -1006,7 +1006,7 @@ void tools::addFortranRuntimeLibraryPath(const ToolChain &TC,
ArgStringList &CmdArgs) {
// Default to the <driver-path>/../lib directory. This works fine on the
// platforms that we have tested so far. We will probably have to re-fine
- // this in the future. In particular, on some platforms, we may need to useq
+ // this in the future. In particular, on some platforms, we may need to use
// lib64 instead of lib.
SmallString<256> DefaultLibPath =
llvm::sys::path::parent_path(TC.getDriver().Dir);
>From 39612e237cb815cf4ea0120027783d35304bcb6b Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Wed, 22 Nov 2023 20:26:02 +0100
Subject: [PATCH 03/13] Correct link line test for flang-new (for Linux)
---
flang/test/Driver/linker-flags.f90 | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 85c4d60b3f09862..ea91946316cfaa6 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -28,7 +28,7 @@
! executable and may find the GNU linker from MinGW or Cygwin.
! UNIX-LABEL: "{{.*}}ld{{(\.exe)?}}"
! UNIX-SAME: "[[object_file]]"
-! UNIX-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal" "-lm"
+! UNIX-SAME: "--whole-archive" "-lFortran_main" "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal" "-lm"
! DARWIN-LABEL: "{{.*}}ld{{(\.exe)?}}"
! DARWIN-SAME: "[[object_file]]"
@@ -38,7 +38,7 @@
! HAIKU-LABEL: "{{.*}}ld{{(\.exe)?}}"
! HAIKU-SAME: "[[object_file]]"
-! HAIKU-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal"
+! HAIKU-SAME: "--whole-archive" "-lFortran_main" "--no-whole-archive" "-lFortranRuntime" "-lFortranDecimal"
! MINGW-LABEL: "{{.*}}ld{{(\.exe)?}}"
! MINGW-SAME: "[[object_file]]"
>From f2efed8a45c06937a3f9bd72112c344582fd0b05 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 23 Nov 2023 14:26:18 +0100
Subject: [PATCH 04/13] Extend flang test system with %clang substitution
---
flang/test/lit.cfg.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index ac504dc5d1fbdfa..faddaee94b6b313 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -122,6 +122,7 @@
# the build directory holding that tool.
tools = [
ToolSubst("%flang", command=FindTool("flang-new"), unresolved="fatal"),
+ ToolSubst("%clang", command=FindTool("clang"), unresolved="fatal"),
ToolSubst(
"%flang_fc1",
command=FindTool("flang-new"),
>From f9a023bc40f1d290ead4793ae56f10aafe4c05ca Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 23 Nov 2023 12:25:13 +0100
Subject: [PATCH 05/13] Add tests for failing linker (albeit not very
sophisticated yet)
---
flang/test/Driver/main_dupes.f90 | 13 +++++++++++++
flang/test/Driver/main_dupes.f90.c-part | 8 ++++++++
2 files changed, 21 insertions(+)
create mode 100644 flang/test/Driver/main_dupes.f90
create mode 100644 flang/test/Driver/main_dupes.f90.c-part
diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90
new file mode 100644
index 000000000000000..7ed5128ca5dd32f
--- /dev/null
+++ b/flang/test/Driver/main_dupes.f90
@@ -0,0 +1,13 @@
+! RUN: %clang -x c -o %t.c-part -c %s.c-part
+! RUN: %flang -o %t -c %s
+! RUN: not %flang -o %t.exe %t %t.c-part 2>&1
+
+! TODO: potentially add further checks to ensure that proper
+! linker error messages are detected and checked via
+! FileCheck.
+
+program main_dupes
+ ! Irrelevant what to do in here.
+ ! Test is supposed to fail at link time.
+ print '(A)', 'Hello from Fortran'
+end program main_dupes
diff --git a/flang/test/Driver/main_dupes.f90.c-part b/flang/test/Driver/main_dupes.f90.c-part
new file mode 100644
index 000000000000000..baae10f94e26364
--- /dev/null
+++ b/flang/test/Driver/main_dupes.f90.c-part
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main(int argc, char * argv[]) {
+ // Irrelevant what to do in here.
+ // Test is supposed to fail at link time.
+ printf("Hello from C [%s]\n", __FUNCTION__);
+ return 0;
+}
>From 95556a1e421b5e523ee1f9f2739f5b2cc5cb98c5 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 23 Nov 2023 18:00:58 +0100
Subject: [PATCH 06/13] Move the C part of the main_dupes test to the Inputs
sub-folder
---
.../Driver/{main_dupes.f90.c-part => Inputs/main_dupes.c} | 0
flang/test/Driver/main_dupes.f90 | 6 ++++--
2 files changed, 4 insertions(+), 2 deletions(-)
rename flang/test/Driver/{main_dupes.f90.c-part => Inputs/main_dupes.c} (100%)
diff --git a/flang/test/Driver/main_dupes.f90.c-part b/flang/test/Driver/Inputs/main_dupes.c
similarity index 100%
rename from flang/test/Driver/main_dupes.f90.c-part
rename to flang/test/Driver/Inputs/main_dupes.c
diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90
index 7ed5128ca5dd32f..017a86b79f0d609 100644
--- a/flang/test/Driver/main_dupes.f90
+++ b/flang/test/Driver/main_dupes.f90
@@ -1,6 +1,8 @@
-! RUN: %clang -x c -o %t.c-part -c %s.c-part
+! UNSUPPORTED: system-windows
+
+! RUN: %clang -o %t.c-object -c %S/Inputs/main_dupes.c
! RUN: %flang -o %t -c %s
-! RUN: not %flang -o %t.exe %t %t.c-part 2>&1
+! RUN: not %flang -o %t.exe %t %t.c-object 2>&1
! TODO: potentially add further checks to ensure that proper
! linker error messages are detected and checked via
>From 1d78798a0d363b67c3cb17d0b7dfd21ece640248 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 23 Nov 2023 18:05:10 +0100
Subject: [PATCH 07/13] Make clang-format happy
---
flang/test/Driver/Inputs/main_dupes.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/flang/test/Driver/Inputs/main_dupes.c b/flang/test/Driver/Inputs/main_dupes.c
index baae10f94e26364..dd318c0816f2af9 100644
--- a/flang/test/Driver/Inputs/main_dupes.c
+++ b/flang/test/Driver/Inputs/main_dupes.c
@@ -1,8 +1,8 @@
#include <stdio.h>
-int main(int argc, char * argv[]) {
- // Irrelevant what to do in here.
- // Test is supposed to fail at link time.
- printf("Hello from C [%s]\n", __FUNCTION__);
- return 0;
+int main(int argc, char *argv[]) {
+ // Irrelevant what to do in here.
+ // Test is supposed to fail at link time.
+ printf("Hello from C [%s]\n", __FUNCTION__);
+ return 0;
}
>From cf3c4d7760e047c00ebe7ee65443c10da6cba813 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 24 Nov 2023 14:23:28 +0100
Subject: [PATCH 08/13] Revert "Extend flang test system with %clang
substitution"
This reverts commit 72f56d7f03343b9c183b929a4226de89f92472b4.
---
flang/test/lit.cfg.py | 1 -
1 file changed, 1 deletion(-)
diff --git a/flang/test/lit.cfg.py b/flang/test/lit.cfg.py
index faddaee94b6b313..ac504dc5d1fbdfa 100644
--- a/flang/test/lit.cfg.py
+++ b/flang/test/lit.cfg.py
@@ -122,7 +122,6 @@
# the build directory holding that tool.
tools = [
ToolSubst("%flang", command=FindTool("flang-new"), unresolved="fatal"),
- ToolSubst("%clang", command=FindTool("clang"), unresolved="fatal"),
ToolSubst(
"%flang_fc1",
command=FindTool("flang-new"),
>From 327566eb92087ee4871ac6902821d469767c1574 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 24 Nov 2023 14:23:21 +0100
Subject: [PATCH 09/13] Remove dependency to clang by using an IR file
---
flang/test/Driver/Inputs/main_dupes.c | 8 --------
flang/test/Driver/Inputs/main_dupes.ll | 7 +++++++
flang/test/Driver/main_dupes.f90 | 2 +-
3 files changed, 8 insertions(+), 9 deletions(-)
delete mode 100644 flang/test/Driver/Inputs/main_dupes.c
create mode 100644 flang/test/Driver/Inputs/main_dupes.ll
diff --git a/flang/test/Driver/Inputs/main_dupes.c b/flang/test/Driver/Inputs/main_dupes.c
deleted file mode 100644
index dd318c0816f2af9..000000000000000
--- a/flang/test/Driver/Inputs/main_dupes.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#include <stdio.h>
-
-int main(int argc, char *argv[]) {
- // Irrelevant what to do in here.
- // Test is supposed to fail at link time.
- printf("Hello from C [%s]\n", __FUNCTION__);
- return 0;
-}
diff --git a/flang/test/Driver/Inputs/main_dupes.ll b/flang/test/Driver/Inputs/main_dupes.ll
new file mode 100644
index 000000000000000..ff2b19dfe9e288b
--- /dev/null
+++ b/flang/test/Driver/Inputs/main_dupes.ll
@@ -0,0 +1,7 @@
+; Create the symbol 'main'; does not have to be the correct
+; signature for 'main', we just need the symbol for the linker
+; to fail during the test.
+
+define i32 @main() {
+ ret i32 0
+}
diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90
index 017a86b79f0d609..ce97e543ed8578b 100644
--- a/flang/test/Driver/main_dupes.f90
+++ b/flang/test/Driver/main_dupes.f90
@@ -1,6 +1,6 @@
! UNSUPPORTED: system-windows
-! RUN: %clang -o %t.c-object -c %S/Inputs/main_dupes.c
+! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/main_dupes.ll
! RUN: %flang -o %t -c %s
! RUN: not %flang -o %t.exe %t %t.c-object 2>&1
>From 11ab38d3c7ddd70e961768ad1df299af2cbedb55 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 24 Nov 2023 18:01:22 +0100
Subject: [PATCH 10/13] Simplify test
---
flang/test/Driver/main_dupes.f90 | 1 -
1 file changed, 1 deletion(-)
diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/main_dupes.f90
index ce97e543ed8578b..5abc56428f0c144 100644
--- a/flang/test/Driver/main_dupes.f90
+++ b/flang/test/Driver/main_dupes.f90
@@ -11,5 +11,4 @@
program main_dupes
! Irrelevant what to do in here.
! Test is supposed to fail at link time.
- print '(A)', 'Hello from Fortran'
end program main_dupes
>From 6805e38375375bc0e6af6c1f345e54596e3608e9 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Mon, 27 Nov 2023 09:23:17 +0100
Subject: [PATCH 11/13] Rename test, according to reviewer comments
---
.../test/Driver/Inputs/{main_dupes.ll => no_duplicate_main.ll} | 0
flang/test/Driver/{main_dupes.f90 => no_duplicate_main.f90} | 2 +-
2 files changed, 1 insertion(+), 1 deletion(-)
rename flang/test/Driver/Inputs/{main_dupes.ll => no_duplicate_main.ll} (100%)
rename flang/test/Driver/{main_dupes.f90 => no_duplicate_main.f90} (84%)
diff --git a/flang/test/Driver/Inputs/main_dupes.ll b/flang/test/Driver/Inputs/no_duplicate_main.ll
similarity index 100%
rename from flang/test/Driver/Inputs/main_dupes.ll
rename to flang/test/Driver/Inputs/no_duplicate_main.ll
diff --git a/flang/test/Driver/main_dupes.f90 b/flang/test/Driver/no_duplicate_main.f90
similarity index 84%
rename from flang/test/Driver/main_dupes.f90
rename to flang/test/Driver/no_duplicate_main.f90
index 5abc56428f0c144..4e33f4f2aeba3f7 100644
--- a/flang/test/Driver/main_dupes.f90
+++ b/flang/test/Driver/no_duplicate_main.f90
@@ -1,6 +1,6 @@
! UNSUPPORTED: system-windows
-! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/main_dupes.ll
+! RUN: %flang -x ir -o %t.c-object -c %S/Inputs/no_duplicate_main.ll
! RUN: %flang -o %t -c %s
! RUN: not %flang -o %t.exe %t %t.c-object 2>&1
>From ea429fef01cc3261082ffefb1159b24d0976de3f Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael at dontknow.de>
Date: Mon, 27 Nov 2023 19:15:36 +0100
Subject: [PATCH 12/13] Put enough of commit 0bc7cd4d5122 back, to make the
MSVC linker fail
---
clang/lib/Driver/ToolChains/CommonArgs.cpp | 26 +++++++++++++++++++++-
clang/lib/Driver/ToolChains/CommonArgs.h | 2 +-
clang/lib/Driver/ToolChains/Darwin.cpp | 2 +-
clang/lib/Driver/ToolChains/DragonFly.cpp | 2 +-
clang/lib/Driver/ToolChains/FreeBSD.cpp | 2 +-
clang/lib/Driver/ToolChains/Gnu.cpp | 2 +-
clang/lib/Driver/ToolChains/Haiku.cpp | 2 +-
clang/lib/Driver/ToolChains/MSVC.cpp | 2 +-
clang/lib/Driver/ToolChains/MinGW.cpp | 2 +-
clang/lib/Driver/ToolChains/NetBSD.cpp | 2 +-
clang/lib/Driver/ToolChains/OpenBSD.cpp | 2 +-
clang/lib/Driver/ToolChains/Solaris.cpp | 2 +-
12 files changed, 36 insertions(+), 12 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 464a87737de062c..4cac3210c6b0aea 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -977,7 +977,7 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
return true;
}
-void tools::addFortranRuntimeLibs(const ToolChain &TC,
+void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
llvm::opt::ArgStringList &CmdArgs) {
// These are handled earlier on Windows by telling the frontend driver to add
// the correct libraries to link against as dependents in the object file.
@@ -998,6 +998,30 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC,
// Perform regular linkage of the remaining runtime libraries.
CmdArgs.push_back("-lFortranRuntime");
CmdArgs.push_back("-lFortranDecimal");
+ } else {
+ unsigned RTOptionID = options::OPT__SLASH_MT;
+ if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
+ RTOptionID = llvm::StringSwitch<unsigned>(rtl->getValue())
+ .Case("static", options::OPT__SLASH_MT)
+ .Case("static_dbg", options::OPT__SLASH_MTd)
+ .Case("dll", options::OPT__SLASH_MD)
+ .Case("dll_dbg", options::OPT__SLASH_MDd)
+ .Default(options::OPT__SLASH_MT);
+ }
+ switch (RTOptionID) {
+ case options::OPT__SLASH_MT:
+ CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.static.lib");
+ break;
+ case options::OPT__SLASH_MTd:
+ CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.static_dbg.lib");
+ break;
+ case options::OPT__SLASH_MD:
+ CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.dynamic.lib");
+ break;
+ case options::OPT__SLASH_MDd:
+ CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.dynamic_dbg.lib");
+ break;
+ }
}
}
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index f364c9793c9be62..0a0951c5386e601 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -116,7 +116,7 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
bool IsOffloadingHost = false, bool GompNeedsRT = false);
/// Adds Fortran runtime libraries to \p CmdArgs.
-void addFortranRuntimeLibs(const ToolChain &TC,
+void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs);
/// Adds the path for the Fortran runtime libraries to \p CmdArgs.
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 1f61bb02c6ae226..7c3d4982d8f6a8c 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -678,7 +678,7 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// to generate executables.
if (getToolChain().getDriver().IsFlangMode()) {
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
- addFortranRuntimeLibs(getToolChain(), CmdArgs);
+ addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
}
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
diff --git a/clang/lib/Driver/ToolChains/DragonFly.cpp b/clang/lib/Driver/ToolChains/DragonFly.cpp
index 181d0ad0d834b2b..9942fc632e0a917 100644
--- a/clang/lib/Driver/ToolChains/DragonFly.cpp
+++ b/clang/lib/Driver/ToolChains/DragonFly.cpp
@@ -153,7 +153,7 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// AddRunTimeLibs).
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
- addFortranRuntimeLibs(ToolChain, CmdArgs);
+ addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
CmdArgs.push_back("-lm");
}
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index d08764aeef77539..d46feb3459a6344 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -310,7 +310,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// AddRunTimeLibs).
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
- addFortranRuntimeLibs(ToolChain, CmdArgs);
+ addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
if (Profiling)
CmdArgs.push_back("-lm_p");
else
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 16cf5707227264d..b875991844ffff5 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -564,7 +564,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// AddRunTimeLibs).
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
- addFortranRuntimeLibs(ToolChain, CmdArgs);
+ addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
CmdArgs.push_back("-lm");
}
diff --git a/clang/lib/Driver/ToolChains/Haiku.cpp b/clang/lib/Driver/ToolChains/Haiku.cpp
index 3fe92054ac53e15..e0d94035823fd37 100644
--- a/clang/lib/Driver/ToolChains/Haiku.cpp
+++ b/clang/lib/Driver/ToolChains/Haiku.cpp
@@ -118,7 +118,7 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// AddRunTimeLibs).
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
- addFortranRuntimeLibs(ToolChain, CmdArgs);
+ addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
}
CmdArgs.push_back("-lgcc");
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 4966d102c51f1ac..8a4a174c90ea855 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -131,7 +131,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (C.getDriver().IsFlangMode()) {
addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
- addFortranRuntimeLibs(TC, CmdArgs);
+ addFortranRuntimeLibs(TC, Args, CmdArgs);
// Inform the MSVC linker that we're generating a console application, i.e.
// one with `main` as the "user-defined" entry point. The `main` function is
diff --git a/clang/lib/Driver/ToolChains/MinGW.cpp b/clang/lib/Driver/ToolChains/MinGW.cpp
index 39d767795445dbe..5d7f8675daf8d28 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -249,7 +249,7 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
if (C.getDriver().IsFlangMode()) {
addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
- addFortranRuntimeLibs(TC, CmdArgs);
+ addFortranRuntimeLibs(TC, Args, CmdArgs);
}
// TODO: Add profile stuff here
diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 90b195a007caa78..240bf5764b9cce2 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -325,7 +325,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// AddRunTimeLibs).
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
- addFortranRuntimeLibs(ToolChain, CmdArgs);
+ addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
CmdArgs.push_back("-lm");
}
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 133b5ff13a4c7e2..21b9004ef2d52d1 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -237,7 +237,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// AddRunTimeLibs).
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
- addFortranRuntimeLibs(ToolChain, CmdArgs);
+ addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
if (Profiling)
CmdArgs.push_back("-lm_p");
else
diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp
index 32b6cbe647da6ad..485730da7df1f8c 100644
--- a/clang/lib/Driver/ToolChains/Solaris.cpp
+++ b/clang/lib/Driver/ToolChains/Solaris.cpp
@@ -226,7 +226,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// these dependencies need to be listed before the C runtime below.
if (D.IsFlangMode()) {
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
- addFortranRuntimeLibs(getToolChain(), CmdArgs);
+ addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
CmdArgs.push_back("-lm");
}
if (Args.hasArg(options::OPT_fstack_protector) ||
>From df8747e74c618bd680fad48ffabeb5f549dba4bd Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Tue, 28 Nov 2023 09:48:55 +0100
Subject: [PATCH 13/13] Only add --whole-archive if it is not yet active on the
link line
---
clang/lib/Driver/ToolChains/CommonArgs.cpp | 34 +++++++++++++++-------
1 file changed, 23 insertions(+), 11 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 4cac3210c6b0aea..2d4ef4c63dfb3bd 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -982,18 +982,30 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
// These are handled earlier on Windows by telling the frontend driver to add
// the correct libraries to link against as dependents in the object file.
if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
- // --whole-archive needs to be part of the link line to make sure
- // that the main() function from Fortran_main.a is pulled in by
- // the linker.
- //
- // We are using this --whole-archive/--no-whole-archive bracket w/o
- // any further checks, because -Wl,--whole-archive at the flang
- // driver's link line will not sucessfully complete, unless the user
- // correctly specified -Wl,--whole-archive/-Wl,--no-whole-archive
- // (e.g., -Wl,--whole-archive -ldummy -Wl,--no-whole-archive).
- CmdArgs.push_back("--whole-archive");
+ // The --whole-archive option needs to be part of the link line to
+ // make sure that the main() function from Fortran_main.a is pulled
+ // in by the linker. Determine if --whole-archive is active when
+ // flang will try to link Fortran_main.a. If it is, don't add the
+ // --whole-archive flag to the link line. If it's not, add a proper
+ // --whole-archive/--no-whole-archive bracket to the link line.
+ bool WholeArchiveActive = false;
+ for (auto &&Arg : Args)
+ if (Arg && Arg->getSpelling().str() == "-Wl,")
+ for (auto &&ArgValue : Arg->getValues())
+ if (ArgValue) {
+ if (ArgValue && !strncmp(ArgValue, "--whole-archive",
+ sizeof("--whole-archive")))
+ WholeArchiveActive = true;
+ if (ArgValue && !strncmp(ArgValue, "--no-whole-archive",
+ sizeof("--no-whole-archive")))
+ WholeArchiveActive = false;
+ }
+
+ if (!WholeArchiveActive)
+ CmdArgs.push_back("--whole-archive");
CmdArgs.push_back("-lFortran_main");
- CmdArgs.push_back("--no-whole-archive");
+ if (!WholeArchiveActive)
+ CmdArgs.push_back("--no-whole-archive");
// Perform regular linkage of the remaining runtime libraries.
CmdArgs.push_back("-lFortranRuntime");
More information about the cfe-commits
mailing list