Skip to content

[flang][AArch64] Always link compiler-rt to flang after libgcc #144710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DavidTruby
Copy link
Member

@DavidTruby DavidTruby commented Jun 18, 2025

This patch fixes an issue where the __trampoline_setup symbol is missing
with some programs compiled with flang. This symbol is present only in
compiler-rt and not in libgcc. This patch adds compiler-rt to the link
line after libgcc if libgcc is being used, so that only this symbol will
be picked from compiler-rt.

Fixes #141147

This patch fixes an issue where the __trampoline_setup symbol is missing
with some programs compiled with flang. This symbol is present only in
compiler-rt and not in libgcc. This patch adds compiler-rt to the link
line after libgcc if libgcc is being used, so that only this symbol will
be picked from compiler-rt.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Jun 18, 2025
@DavidTruby DavidTruby requested review from pawosm-arm and tblah June 18, 2025 14:21
@llvmbot
Copy link
Member

llvmbot commented Jun 18, 2025

@llvm/pr-subscribers-flang-driver
@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: David Truby (DavidTruby)

Changes

This patch fixes an issue where the __trampoline_setup symbol is missing
with some programs compiled with flang. This symbol is present only in
compiler-rt and not in libgcc. This patch adds compiler-rt to the link
line after libgcc if libgcc is being used, so that only this symbol will
be picked from compiler-rt.


Full diff: https://github.com/llvm/llvm-project/pull/144710.diff

3 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+7)
  • (added) flang/test/Driver/flang-ld-aarch64.f90 (+9)
  • (modified) llvm/CMakeLists.txt (+7)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index d5b2c5c1e199e..672b73432847d 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2252,6 +2252,13 @@ static void AddLibgcc(const ToolChain &TC, const Driver &D,
   if (LGT == LibGccType::SharedLibGcc ||
       (LGT == LibGccType::UnspecifiedLibGcc && D.CCCIsCXX()))
     CmdArgs.push_back("-lgcc");
+  // compiler-rt is needed after libgcc for flang on AArch64 for the
+  // __trampoline_setup symbol
+  if (D.IsFlangMode() && TC.getArch() == llvm::Triple::aarch64) {
+    CmdArgs.push_back("--as-needed");
+    CmdArgs.push_back(TC.getCompilerRTArgString(Args, "builtins"));
+    CmdArgs.push_back("--no-as-needed");
+  }
 }
 
 void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D,
diff --git a/flang/test/Driver/flang-ld-aarch64.f90 b/flang/test/Driver/flang-ld-aarch64.f90
new file mode 100644
index 0000000000000..61cd46cea5cd1
--- /dev/null
+++ b/flang/test/Driver/flang-ld-aarch64.f90
@@ -0,0 +1,9 @@
+! Check linker flags for AArch64 linux, since it needs both libgcc and 
+! compiler-rt, with compiler-rt second when -rtlib=libgcc.
+
+! RUN: %flang -### -rtlib=libgcc --target=aarch64-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s
+
+! CHECK-LABEL:  "{{.*}}ld{{(\.exe)?}}"
+! CHECK-SAME: "-lflang_rt.runtime" "-lm"
+! CHECK-SAME: "-lgcc" "--as-needed" "-lgcc_s" "--no-as-needed"
+! CHECK-SAME: "--as-needed" "{{.*}}{{\\|/}}libclang_rt.builtins.a" "--no-as-needed"
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 0849bec26d56a..1b91e45c202e3 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -653,6 +653,13 @@ if(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD STREQUAL "all")
   set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD ${LLVM_ALL_EXPERIMENTAL_TARGETS})
 endif()
 
+if("flang" IN_LIST LLVM_ENABLE_PROJECTS AND 
+   "AArch64" IN_LIST LLVM_TARGETS_TO_BUILD AND
+   NOT "compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES)
+  message(STATUS "Enabling Flang-RT as a dependency of Flang")
+  list(APPEND LLVM_ENABLE_RUNTIMES "compiler-rt")
+endif()
+
 set(LLVM_TARGETS_TO_BUILD
    ${LLVM_TARGETS_TO_BUILD}
    ${LLVM_EXPERIMENTAL_TARGETS_TO_BUILD})

@DavidTruby DavidTruby changed the title [flang][AArch64] Always compiler-rt to flang linking after libgcc [flang][AArch64] Always link compiler-rt to flang after libgcc Jun 18, 2025
@kiranchandramohan
Copy link
Contributor

Can you add a fixes tag for #141147 ?

@DavidTruby
Copy link
Member Author

Looks like the pre-commit CI has compiler-rt in LLVM_ENABLE_PROJECTS. I thought that wasn't supported anymore? Anyways I guess I have to add a check that it's not in ENABLE_PROJECTS as well as ENABLE_RUNTIMES

@vzakhari
Copy link
Contributor

Looks good to me modulo the CI issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Undefined reference to '__trampoline_setup' with the flang driver
5 participants