From adce3fd99ba2ad3f3b03136473c29256513213fb Mon Sep 17 00:00:00 2001
From: Nathan Reller <Nathan@GingerCybersecurity.com>
Date: Thu, 11 Jan 2024 15:26:16 +0000
Subject: [PATCH] Enable Static Builds for FreeBSD

Enable crt-static for FreeBSD to enable statically compiled binaries.
---
 compiler/rustc_target/src/spec/base/freebsd.rs | 1 +
 library/unwind/src/lib.rs                      | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/compiler/rustc_target/src/spec/base/freebsd.rs b/compiler/rustc_target/src/spec/base/freebsd.rs
index 8c141aaaec34a..80b3da8a752d2 100644
--- a/compiler/rustc_target/src/spec/base/freebsd.rs
+++ b/compiler/rustc_target/src/spec/base/freebsd.rs
@@ -6,6 +6,7 @@ pub fn opts() -> TargetOptions {
         dynamic_linking: true,
         families: cvs!["unix"],
         has_rpath: true,
+        crt_static_respected: true,
         position_independent_executables: true,
         relro_level: RelroLevel::Full,
         abi_return_struct_as_int: true,
diff --git a/library/unwind/src/lib.rs b/library/unwind/src/lib.rs
index eeee98f754e09..f5988a4df1364 100644
--- a/library/unwind/src/lib.rs
+++ b/library/unwind/src/lib.rs
@@ -121,10 +121,16 @@ extern "C" {}
 #[link(name = "unwind", kind = "static", modifiers = "-bundle")]
 extern "C" {}
 
-#[cfg(any(target_os = "freebsd", target_os = "netbsd"))]
+#[cfg(target_os = "netbsd")]
 #[link(name = "gcc_s")]
 extern "C" {}
 
+#[cfg(target_os = "freebsd")]
+#[link(name = "gcc", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
+#[link(name = "gcc_eh", kind = "static", modifiers = "-bundle", cfg(target_feature = "crt-static"))]
+#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
+extern "C" {}
+
 #[cfg(all(target_os = "openbsd", target_arch = "sparc64"))]
 #[link(name = "gcc")]
 extern "C" {}