Skip to content

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.

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the question about Flang-RT vs compiler-rt in the llvm/CMakeLists, this looks good to me. Thanks.

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this say "Enabling compiler-rt" instead of Flang_RT if the next line enables compiler-rt?

@DavidTruby DavidTruby merged commit 049d61a into llvm:main Jun 24, 2025
7 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 24, 2025

LLVM Buildbot has detected a new failure on builder flang-aarch64-out-of-tree running on linaro-flang-aarch64-out-of-tree while building clang,flang,llvm at step 13 "test-build-flang-rt-unified-tree-check-flang-rt".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/53/builds/17185

Here is the relevant piece of the build log for the reference
Step 13 (test-build-flang-rt-unified-tree-check-flang-rt) failure: test (failure)
******************** TEST 'flang-rt :: Driver/ctofortran.f90' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
split-file /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/llvm-project/flang-rt/test/Driver/ctofortran.f90 /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp # RUN: at line 4
+ split-file /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/llvm-project/flang-rt/test/Driver/ctofortran.f90 /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp
/home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_llvm/bin/clang -I"/home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/llvm-project/flang-rt/../flang/include/flang" -c /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/cfile.c -o /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/cfile.o # RUN: at line 5
+ /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_llvm/bin/clang -I/home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/llvm-project/flang-rt/../flang/include/flang -c /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/cfile.c -o /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/cfile.o
/home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang/bin/flang -L"/home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/lib" /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/ffile.f90 /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/cfile.o -o /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/ctofortran # RUN: at line 6
+ /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang/bin/flang -L/home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/lib /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/ffile.f90 /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/cfile.o -o /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang-rt/flang-rt/test/Driver/Output/ctofortran.f90.tmp/ctofortran
/usr/bin/ld: cannot find /home/tcwg-buildbot/worker/flang-aarch64-out-of-tree/build_flang/lib/clang/21/lib/aarch64-unknown-linux-gnu/libclang_rt.builtins.a: No such file or directory
flang-21: error: linker command failed with exit code 1 (use -v to see invocation)

--

********************


DrSergei pushed a commit to DrSergei/llvm-project that referenced this pull request Jun 24, 2025
…144710)

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 llvm#141147
anthonyhatran pushed a commit to anthonyhatran/llvm-project that referenced this pull request Jun 26, 2025
…144710)

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 llvm#141147
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

7 participants