[clang] [Toolchains][FreeBSD] Honor system libgcc (PR #181972)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 17 23:57:35 PST 2026
https://github.com/aokblast created https://github.com/llvm/llvm-project/pull/181972
On Linux, the system libgcc may act as the compiler runtime, and Clang
provides -rtlib=compiler-rt to switch implementations.
FreeBSD ships libcompiler_rt.a (LLVM’s builtins) in the base system,
with libgcc.a as a symlink to it. These libraries are linked via -L and
-lgcc.
An interesting detail is that even if the Clang resource directory
(llvm-xx/lib/clang/xx) appears before the system path in the search
order, it is still not used. This is because the linker looks for
libgcc.a rather than libclang_rt.builtins.a.
Since FreeBSD does not currently support -rtlib, honor the system libgcc
by hardcoding its path. Detect and handle cases where a custom
compiler-rt is injected via -L as an override workaround.
>From 2680016317d4b90dea5061e93c76e83015e172ba Mon Sep 17 00:00:00 2001
From: ShengYi Hung <aokblast at FreeBSD.org>
Date: Wed, 18 Feb 2026 15:04:48 +0800
Subject: [PATCH] [Toolchains][FreeBSD] Honor system libgcc
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
On Linux, the system libgcc may act as the compiler runtime, and Clang
provides -rtlib=compiler-rt to switch implementations.
FreeBSD ships libcompiler_rt.a (LLVM’s builtins) in the base system,
with libgcc.a as a symlink to it. These libraries are linked via -L and
-lgcc.
An interesting detail is that even if the Clang resource directory
(llvm-xx/lib/clang/xx) appears before the system path in the search
order, it is still not used. This is because the linker looks for
libgcc.a rather than libclang_rt.builtins.a.
Since FreeBSD does not currently support -rtlib, honor the system libgcc
by hardcoding its path. Detect and handle cases where a custom
compiler-rt is injected via -L as an override workaround.
---
clang/lib/Driver/ToolChains/FreeBSD.cpp | 17 +++++++++++++++++
clang/lib/Driver/ToolChains/FreeBSD.h | 2 ++
clang/test/Driver/freebsd.cpp | 4 ++++
3 files changed, 23 insertions(+)
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index cf6ad385d949a..0d0142bdd5968 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -511,3 +511,20 @@ SanitizerMask FreeBSD::getSupportedSanitizers() const {
}
return Res;
}
+
+std::string FreeBSD::getCompilerRT(const llvm::opt::ArgList &Args,
+ StringRef Component, FileType Type,
+ bool IsFortran) const {
+ if (Component == "builtins") {
+ std::string DetectedPath =
+ ToolChain::getCompilerRT(Args, Component, Type, IsFortran);
+ if (getVFS().exists(DetectedPath))
+ return DetectedPath;
+ SmallString<128> Path(getDriver().SysRoot);
+ llvm::sys::path::append(Path, "/usr/lib/libgcc.a");
+ if (getVFS().exists(Path))
+ return std::string(Path);
+ return DetectedPath;
+ }
+ return ToolChain::getCompilerRT(Args, Component, Type, IsFortran);
+}
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.h b/clang/lib/Driver/ToolChains/FreeBSD.h
index 7d090ba682b30..51473597a5703 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.h
+++ b/clang/lib/Driver/ToolChains/FreeBSD.h
@@ -91,6 +91,8 @@ class LLVM_LIBRARY_VISIBILITY FreeBSD : public Generic_ELF {
// Until dtrace (via CTF) and LLDB can deal with distributed debug info,
// FreeBSD defaults to standalone/full debug info.
bool GetDefaultStandaloneDebug() const override { return true; }
+ std::string getCompilerRT(const llvm::opt::ArgList &Args, StringRef Component,
+ FileType Type, bool IsFortran) const override;
protected:
Tool *buildAssembler() const override;
diff --git a/clang/test/Driver/freebsd.cpp b/clang/test/Driver/freebsd.cpp
index e2f76cd013f7f..c0619a8da39a5 100644
--- a/clang/test/Driver/freebsd.cpp
+++ b/clang/test/Driver/freebsd.cpp
@@ -9,6 +9,10 @@
// CHECK-DEFAULT: "-lc++" "-lm"
// CHECK-STDLIBCXX: "-lstdc++" "-lm"
+// RUN: %clangxx %s --print-libgcc-file-name --target=amd64-unknown-freebsd 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-BUILTIN %s
+// CHECK-BUILTIN: /usr/lib/libgcc.a
+
// RUN: %clangxx %s -### -pg --target=amd64-unknown-freebsd -stdlib=platform 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-PG-DEFAULT %s
// RUN: %clangxx %s -### -pg --target=amd64-unknown-freebsd14.0 -stdlib=platform 2>&1 \
More information about the cfe-commits
mailing list