Skip to content

Commit 049d61a

Browse files
authored
[flang][AArch64] Always link compiler-rt to flang after libgcc (#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 #141147
1 parent 594ebe6 commit 049d61a

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,13 @@ static void AddLibgcc(const ToolChain &TC, const Driver &D,
22522252
if (LGT == LibGccType::SharedLibGcc ||
22532253
(LGT == LibGccType::UnspecifiedLibGcc && D.CCCIsCXX()))
22542254
CmdArgs.push_back("-lgcc");
2255+
// compiler-rt is needed after libgcc for flang on AArch64 for the
2256+
// __trampoline_setup symbol
2257+
if (D.IsFlangMode() && TC.getArch() == llvm::Triple::aarch64) {
2258+
CmdArgs.push_back("--as-needed");
2259+
CmdArgs.push_back(TC.getCompilerRTArgString(Args, "builtins"));
2260+
CmdArgs.push_back("--no-as-needed");
2261+
}
22552262
}
22562263

22572264
void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! Check linker flags for AArch64 linux, since it needs both libgcc and
2+
! compiler-rt, with compiler-rt second when -rtlib=libgcc.
3+
4+
! RUN: %flang -### -rtlib=libgcc --target=aarch64-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s
5+
6+
! CHECK-LABEL: "{{.*}}ld{{(\.exe)?}}"
7+
! CHECK-SAME: "-lflang_rt.runtime" "-lm"
8+
! CHECK-SAME: "-lgcc" "--as-needed" "-lgcc_s" "--no-as-needed"
9+
! CHECK-SAME: "--as-needed" "{{.*}}{{\\|/}}libclang_rt.builtins.a" "--no-as-needed"

llvm/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ if(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD STREQUAL "all")
653653
set(LLVM_EXPERIMENTAL_TARGETS_TO_BUILD ${LLVM_ALL_EXPERIMENTAL_TARGETS})
654654
endif()
655655

656+
if("flang" IN_LIST LLVM_ENABLE_PROJECTS AND
657+
"AArch64" IN_LIST LLVM_TARGETS_TO_BUILD AND
658+
NOT ("compiler-rt" IN_LIST LLVM_ENABLE_RUNTIMES OR "compiler-rt" IN_LIST LLVM_ENABLE_PROJECTS))
659+
message(STATUS "Enabling compiler-rt as a dependency of Flang")
660+
list(APPEND LLVM_ENABLE_RUNTIMES "compiler-rt")
661+
endif()
662+
656663
set(LLVM_TARGETS_TO_BUILD
657664
${LLVM_TARGETS_TO_BUILD}
658665
${LLVM_EXPERIMENTAL_TARGETS_TO_BUILD})

0 commit comments

Comments
 (0)