about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMads Marquart <mads@marquart.dk>2025-03-24 21:55:14 +0100
committerMads Marquart <mads@marquart.dk>2025-03-24 23:22:08 +0100
commitf9770e742ad3853d2a472e422b00cdbe8578faae (patch)
treea593e2e3b7b5fa9b18506543e06ddc104fb03a8a
parent4510e86a41388733675465a8647d4235f3bf2023 (diff)
downloadrust-f9770e742ad3853d2a472e422b00cdbe8578faae.tar.gz
rust-f9770e742ad3853d2a472e422b00cdbe8578faae.zip
Test linking and running no_std binaries
-rw-r--r--tests/ui/no_std/simple-runs.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/ui/no_std/simple-runs.rs b/tests/ui/no_std/simple-runs.rs
new file mode 100644
index 00000000000..8931ac7ed11
--- /dev/null
+++ b/tests/ui/no_std/simple-runs.rs
@@ -0,0 +1,41 @@
+//! Check that `no_std` binaries can link and run without depending on `libstd`.
+
+//@ run-pass
+//@ compile-flags: -Cpanic=abort
+//@ ignore-wasm different `main` convention
+
+#![no_std]
+#![no_main]
+
+use core::ffi::{c_char, c_int};
+use core::panic::PanicInfo;
+
+// # Linux
+//
+// Linking `libc` is required by crt1.o, otherwise the linker fails with:
+// > /usr/bin/ld: in function `_start': undefined reference to `__libc_start_main'
+//
+// # Apple
+//
+// Linking `libSystem` is required, otherwise the linker fails with:
+// > ld: dynamic executables or dylibs must link with libSystem.dylib
+//
+// With the new linker introduced in Xcode 15, the error is instead:
+// > Undefined symbols: "dyld_stub_binder", referenced from: <initial-undefines>
+//
+// This _can_ be worked around by raising the deployment target with
+// MACOSX_DEPLOYMENT_TARGET=13.0, though it's a bit hard to test that while
+// still allowing the test suite to support running with older Xcode versions.
+#[cfg_attr(all(not(target_vendor = "apple"), unix), link(name = "c"))]
+#[cfg_attr(target_vendor = "apple", link(name = "System"))]
+extern "C" {}
+
+#[panic_handler]
+fn panic_handler(_info: &PanicInfo<'_>) -> ! {
+    loop {}
+}
+
+#[no_mangle]
+extern "C" fn main(_argc: c_int, _argv: *const *const c_char) -> c_int {
+    0
+}