[clang] [flang][Driver] Let the linker fail on multiple definitions of main() (PR #73124)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 22 06:10:12 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Michael Klemm (mjklemm)
<details>
<summary>Changes</summary>
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()`.
---
Full diff: https://github.com/llvm/llvm-project/pull/73124.diff
1 Files Affected:
- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+14-1)
``````````diff
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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/73124
More information about the cfe-commits
mailing list