https://github.com/mjklemm created 
https://github.com/llvm/llvm-project/pull/73124

The flang driver was silently ignoring the `main()` function in 
`Fortran_main.a` for entry into the Fortran program unit if an external 
`main()` as supplied (e.g., via cross-language linkage with Fortran and C/C++). 
 This PR fixes this by making sure that the linker always pulls in the `main()` 
definition from `Fortran_main.a` and consequently fails due to multiple 
definitions of the same symbol if another object file also has a definition of 
`main()`.

>From ba38aec7ac04c63fd5167908fe7f91d6ac7bceed Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.kl...@amd.com>
Date: Wed, 22 Nov 2023 14:22:20 +0100
Subject: [PATCH] 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 5d2cd1959b06925..30c249d05677ce5 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1018,7 +1018,20 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, 
const ArgList &Args,
       break;
     }
   } else {
+    // --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");
   }
@@ -1029,7 +1042,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);

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to