From 82b447a0cc47c5b476fb85eaeba865c97490f93f Mon Sep 17 00:00:00 2001
From: Havard Eidnes <he@NetBSD.org>
Date: Sun, 29 Oct 2023 12:39:30 +0000
Subject: [PATCH 1/2] Add support for mipsel-unknown-netbsd, 32-bit LE mips.

---
 .../src/spec/mipsel_unknown_netbsd.rs         | 21 +++++++++++++++++++
 compiler/rustc_target/src/spec/mod.rs         |  1 +
 src/bootstrap/src/core/build_steps/llvm.rs    |  6 ++++++
 src/doc/rustc/src/platform-support.md         |  1 +
 4 files changed, 29 insertions(+)
 create mode 100644 compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs

diff --git a/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs b/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs
new file mode 100644
index 0000000000000..74b324d57a08e
--- /dev/null
+++ b/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs
@@ -0,0 +1,21 @@
+use crate::abi::Endian;
+use crate::spec::{Target, TargetOptions};
+
+pub fn target() -> Target {
+    let mut base = super::netbsd_base::opts();
+    base.max_atomic_width = Some(32);
+    base.cpu = "mips32".into();
+
+    Target {
+       llvm_target: "mipsel-unknown-netbsd".into(),
+       pointer_width: 32,
+       data_layout: "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".into(),
+       arch: "mips".into(),
+       options: TargetOptions {
+           features: "+soft-float".into(),
+           mcount: "__mcount".into(),
+           endian: Endian::Little,
+           ..base
+       },
+    }
+}
diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs
index ccf297615521a..48ca5fcf3ad0e 100644
--- a/compiler/rustc_target/src/spec/mod.rs
+++ b/compiler/rustc_target/src/spec/mod.rs
@@ -1566,6 +1566,7 @@ supported_targets! {
     ("armv7-unknown-netbsd-eabihf", armv7_unknown_netbsd_eabihf),
     ("i586-unknown-netbsd", i586_unknown_netbsd),
     ("i686-unknown-netbsd", i686_unknown_netbsd),
+    ("mipsel-unknown-netbsd", mipsel_unknown_netbsd),
     ("powerpc-unknown-netbsd", powerpc_unknown_netbsd),
     ("riscv64gc-unknown-netbsd", riscv64gc_unknown_netbsd),
     ("sparc64-unknown-netbsd", sparc64_unknown_netbsd),
diff --git a/src/bootstrap/src/core/build_steps/llvm.rs b/src/bootstrap/src/core/build_steps/llvm.rs
index 24351118a5aa1..5b45430772167 100644
--- a/src/bootstrap/src/core/build_steps/llvm.rs
+++ b/src/bootstrap/src/core/build_steps/llvm.rs
@@ -396,6 +396,12 @@ impl Step for Llvm {
             ldflags.shared.push(" -latomic");
         }
 
+        if target.starts_with("mips") && target.contains("netbsd") {
+            // LLVM wants 64-bit atomics, while mipsel is 32-bit only, so needs -latomic
+            ldflags.exe.push(" -latomic");
+            ldflags.shared.push(" -latomic");
+        }
+
         if target.contains("msvc") {
             cfg.define("LLVM_USE_CRT_DEBUG", "MT");
             cfg.define("LLVM_USE_CRT_RELEASE", "MT");
diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md
index 68ec0f348dc5b..6a979869a59a6 100644
--- a/src/doc/rustc/src/platform-support.md
+++ b/src/doc/rustc/src/platform-support.md
@@ -287,6 +287,7 @@ target | std | host | notes
 `mips64el-unknown-linux-muslabi64` | ✓ |  | MIPS64 (little endian) Linux, N64 ABI, musl libc
 `mipsel-unknown-linux-gnu` | ✓ | ✓ | MIPS (little endian) Linux (kernel 4.4, glibc 2.23)
 `mipsel-unknown-linux-musl` | ✓ |  | MIPS (little endian) Linux with musl libc
+[`mipsel-unknown-netbsd`](platform-support/netbsd.md) | ✓ | ✓ | 32-bit MIPS (LE), requires mips32 cpu support
 `mipsel-sony-psp` | * |  | MIPS (LE) Sony PlayStation Portable (PSP)
 [`mipsel-sony-psx`](platform-support/mipsel-sony-psx.md) | * |  | MIPS (LE) Sony PlayStation 1 (PSX)
 `mipsel-unknown-linux-uclibc` | ✓ |  | MIPS (LE) Linux with uClibc

From 5e6c313cafd636bf54973d64875ebeba635dc612 Mon Sep 17 00:00:00 2001
From: Havard Eidnes <he@NetBSD.org>
Date: Sun, 29 Oct 2023 13:53:24 +0000
Subject: [PATCH 2/2] mipsel_unknown_netbsd.rs: fix indentation.

---
 .../src/spec/mipsel_unknown_netbsd.rs         | 20 +++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs b/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs
index 74b324d57a08e..651358d6422c6 100644
--- a/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs
+++ b/compiler/rustc_target/src/spec/mipsel_unknown_netbsd.rs
@@ -7,15 +7,15 @@ pub fn target() -> Target {
     base.cpu = "mips32".into();
 
     Target {
-       llvm_target: "mipsel-unknown-netbsd".into(),
-       pointer_width: 32,
-       data_layout: "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".into(),
-       arch: "mips".into(),
-       options: TargetOptions {
-           features: "+soft-float".into(),
-           mcount: "__mcount".into(),
-           endian: Endian::Little,
-           ..base
-       },
+        llvm_target: "mipsel-unknown-netbsd".into(),
+        pointer_width: 32,
+        data_layout: "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64".into(),
+        arch: "mips".into(),
+        options: TargetOptions {
+            features: "+soft-float".into(),
+            mcount: "__mcount".into(),
+            endian: Endian::Little,
+            ..base
+        },
     }
 }