[flang] [clang] [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 08:48:06 PST 2023


================
@@ -977,14 +977,61 @@ 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.
   if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
+    // 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 NeedWholeArchive = true;
+    auto * Arg = Args.getLastArg(options::OPT_Wl_COMMA);
+    for (StringRef ArgValue : llvm::reverse(Arg->getValues())) {
+      if (ArgValue == "--whole-archive") {
+        NeedWholeArchive = false;
+        break;
+      }
----------------
mjklemm wrote:

Are you sure?  The default is to modify the link line and only not do it if --whole-archive was found.   However, there's an actual bug now, as this goes is treated wrongly:

`-Wl,--whole-archive -ldummy  -Wl,-dummy`

Now, the check does not see the --whole-archive anymore.  I need to go back to go through all the -Wl, and see which is the last one to have something to do with --whole-archive.

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


More information about the cfe-commits mailing list