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

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Driver/flang-ld-aarch64.f90
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 OR "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS))
message(STATUS "Enabling compiler-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})
Expand Down
Loading